]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
Update copyright year range in all GDB files
[thirdparty/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2021 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "dwarf2/dwz.h"
41 #include "dwarf2/macro.h"
42 #include "dwarf2/die.h"
43 #include "dwarf2/stringify.h"
44 #include "bfd.h"
45 #include "elf-bfd.h"
46 #include "symtab.h"
47 #include "gdbtypes.h"
48 #include "objfiles.h"
49 #include "dwarf2.h"
50 #include "buildsym.h"
51 #include "demangle.h"
52 #include "gdb-demangle.h"
53 #include "filenames.h" /* for DOSish file names */
54 #include "language.h"
55 #include "complaints.h"
56 #include "dwarf2/expr.h"
57 #include "dwarf2/loc.h"
58 #include "cp-support.h"
59 #include "hashtab.h"
60 #include "command.h"
61 #include "gdbcmd.h"
62 #include "block.h"
63 #include "addrmap.h"
64 #include "typeprint.h"
65 #include "psympriv.h"
66 #include "c-lang.h"
67 #include "go-lang.h"
68 #include "valprint.h"
69 #include "gdbcore.h" /* for gnutarget */
70 #include "gdb/gdb-index.h"
71 #include "gdb_bfd.h"
72 #include "f-lang.h"
73 #include "source.h"
74 #include "build-id.h"
75 #include "namespace.h"
76 #include "gdbsupport/function-view.h"
77 #include "gdbsupport/gdb_optional.h"
78 #include "gdbsupport/underlying.h"
79 #include "gdbsupport/hash_enum.h"
80 #include "filename-seen-cache.h"
81 #include "producer.h"
82 #include <fcntl.h>
83 #include <algorithm>
84 #include <unordered_map>
85 #include "gdbsupport/selftest.h"
86 #include "rust-lang.h"
87 #include "gdbsupport/pathstuff.h"
88 #include "count-one-bits.h"
89 #include "debuginfod-support.h"
90
91 /* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94 static unsigned int dwarf_read_debug = 0;
95
96 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
97
98 #define dwarf_read_debug_printf(fmt, ...) \
99 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
100 ##__VA_ARGS__)
101
102 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
103
104 #define dwarf_read_debug_printf_v(fmt, ...) \
105 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
106 ##__VA_ARGS__)
107
108 /* When non-zero, dump DIEs after they are read in. */
109 static unsigned int dwarf_die_debug = 0;
110
111 /* When non-zero, dump line number entries as they are read in. */
112 unsigned int dwarf_line_debug = 0;
113
114 /* When true, cross-check physname against demangler. */
115 static bool check_physname = false;
116
117 /* When true, do not reject deprecated .gdb_index sections. */
118 static bool use_deprecated_index_sections = false;
119
120 /* This is used to store the data that is always per objfile. */
121 static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
122
123 /* These are used to store the dwarf2_per_bfd objects.
124
125 objfiles having the same BFD, which doesn't require relocations, are going to
126 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
127
128 Other objfiles are not going to share a dwarf2_per_bfd with any other
129 objfiles, so they'll have their own version kept in the _objfile_data_key
130 version. */
131 static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
132 static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
133
134 /* The "aclass" indices for various kinds of computed DWARF symbols. */
135
136 static int dwarf2_locexpr_index;
137 static int dwarf2_loclist_index;
138 static int dwarf2_locexpr_block_index;
139 static int dwarf2_loclist_block_index;
140
141 /* Size of .debug_loclists section header for 32-bit DWARF format. */
142 #define LOCLIST_HEADER_SIZE32 12
143
144 /* Size of .debug_loclists section header for 64-bit DWARF format. */
145 #define LOCLIST_HEADER_SIZE64 20
146
147 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
148 #define RNGLIST_HEADER_SIZE32 12
149
150 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
151 #define RNGLIST_HEADER_SIZE64 20
152
153 /* An index into a (C++) symbol name component in a symbol name as
154 recorded in the mapped_index's symbol table. For each C++ symbol
155 in the symbol table, we record one entry for the start of each
156 component in the symbol in a table of name components, and then
157 sort the table, in order to be able to binary search symbol names,
158 ignoring leading namespaces, both completion and regular look up.
159 For example, for symbol "A::B::C", we'll have an entry that points
160 to "A::B::C", another that points to "B::C", and another for "C".
161 Note that function symbols in GDB index have no parameter
162 information, just the function/method names. You can convert a
163 name_component to a "const char *" using the
164 'mapped_index::symbol_name_at(offset_type)' method. */
165
166 struct name_component
167 {
168 /* Offset in the symbol name where the component starts. Stored as
169 a (32-bit) offset instead of a pointer to save memory and improve
170 locality on 64-bit architectures. */
171 offset_type name_offset;
172
173 /* The symbol's index in the symbol and constant pool tables of a
174 mapped_index. */
175 offset_type idx;
176 };
177
178 /* Base class containing bits shared by both .gdb_index and
179 .debug_name indexes. */
180
181 struct mapped_index_base
182 {
183 mapped_index_base () = default;
184 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
185
186 /* The name_component table (a sorted vector). See name_component's
187 description above. */
188 std::vector<name_component> name_components;
189
190 /* How NAME_COMPONENTS is sorted. */
191 enum case_sensitivity name_components_casing;
192
193 /* Return the number of names in the symbol table. */
194 virtual size_t symbol_name_count () const = 0;
195
196 /* Get the name of the symbol at IDX in the symbol table. */
197 virtual const char *symbol_name_at
198 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
199
200 /* Return whether the name at IDX in the symbol table should be
201 ignored. */
202 virtual bool symbol_name_slot_invalid (offset_type idx) const
203 {
204 return false;
205 }
206
207 /* Build the symbol name component sorted vector, if we haven't
208 yet. */
209 void build_name_components (dwarf2_per_objfile *per_objfile);
210
211 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
212 possible matches for LN_NO_PARAMS in the name component
213 vector. */
214 std::pair<std::vector<name_component>::const_iterator,
215 std::vector<name_component>::const_iterator>
216 find_name_components_bounds (const lookup_name_info &ln_no_params,
217 enum language lang,
218 dwarf2_per_objfile *per_objfile) const;
219
220 /* Prevent deleting/destroying via a base class pointer. */
221 protected:
222 ~mapped_index_base() = default;
223 };
224
225 /* A description of the mapped index. The file format is described in
226 a comment by the code that writes the index. */
227 struct mapped_index final : public mapped_index_base
228 {
229 /* A slot/bucket in the symbol table hash. */
230 struct symbol_table_slot
231 {
232 const offset_type name;
233 const offset_type vec;
234 };
235
236 /* Index data format version. */
237 int version = 0;
238
239 /* The address table data. */
240 gdb::array_view<const gdb_byte> address_table;
241
242 /* The symbol table, implemented as a hash table. */
243 gdb::array_view<symbol_table_slot> symbol_table;
244
245 /* A pointer to the constant pool. */
246 const char *constant_pool = nullptr;
247
248 bool symbol_name_slot_invalid (offset_type idx) const override
249 {
250 const auto &bucket = this->symbol_table[idx];
251 return bucket.name == 0 && bucket.vec == 0;
252 }
253
254 /* Convenience method to get at the name of the symbol at IDX in the
255 symbol table. */
256 const char *symbol_name_at
257 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
258 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
259
260 size_t symbol_name_count () const override
261 { return this->symbol_table.size (); }
262 };
263
264 /* A description of the mapped .debug_names.
265 Uninitialized map has CU_COUNT 0. */
266 struct mapped_debug_names final : public mapped_index_base
267 {
268 bfd_endian dwarf5_byte_order;
269 bool dwarf5_is_dwarf64;
270 bool augmentation_is_gdb;
271 uint8_t offset_size;
272 uint32_t cu_count = 0;
273 uint32_t tu_count, bucket_count, name_count;
274 const gdb_byte *cu_table_reordered, *tu_table_reordered;
275 const uint32_t *bucket_table_reordered, *hash_table_reordered;
276 const gdb_byte *name_table_string_offs_reordered;
277 const gdb_byte *name_table_entry_offs_reordered;
278 const gdb_byte *entry_pool;
279
280 struct index_val
281 {
282 ULONGEST dwarf_tag;
283 struct attr
284 {
285 /* Attribute name DW_IDX_*. */
286 ULONGEST dw_idx;
287
288 /* Attribute form DW_FORM_*. */
289 ULONGEST form;
290
291 /* Value if FORM is DW_FORM_implicit_const. */
292 LONGEST implicit_const;
293 };
294 std::vector<attr> attr_vec;
295 };
296
297 std::unordered_map<ULONGEST, index_val> abbrev_map;
298
299 const char *namei_to_name
300 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
301
302 /* Implementation of the mapped_index_base virtual interface, for
303 the name_components cache. */
304
305 const char *symbol_name_at
306 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
307 { return namei_to_name (idx, per_objfile); }
308
309 size_t symbol_name_count () const override
310 { return this->name_count; }
311 };
312
313 /* See dwarf2read.h. */
314
315 dwarf2_per_objfile *
316 get_dwarf2_per_objfile (struct objfile *objfile)
317 {
318 return dwarf2_objfile_data_key.get (objfile);
319 }
320
321 /* Default names of the debugging sections. */
322
323 /* Note that if the debugging section has been compressed, it might
324 have a name like .zdebug_info. */
325
326 static const struct dwarf2_debug_sections dwarf2_elf_names =
327 {
328 { ".debug_info", ".zdebug_info" },
329 { ".debug_abbrev", ".zdebug_abbrev" },
330 { ".debug_line", ".zdebug_line" },
331 { ".debug_loc", ".zdebug_loc" },
332 { ".debug_loclists", ".zdebug_loclists" },
333 { ".debug_macinfo", ".zdebug_macinfo" },
334 { ".debug_macro", ".zdebug_macro" },
335 { ".debug_str", ".zdebug_str" },
336 { ".debug_str_offsets", ".zdebug_str_offsets" },
337 { ".debug_line_str", ".zdebug_line_str" },
338 { ".debug_ranges", ".zdebug_ranges" },
339 { ".debug_rnglists", ".zdebug_rnglists" },
340 { ".debug_types", ".zdebug_types" },
341 { ".debug_addr", ".zdebug_addr" },
342 { ".debug_frame", ".zdebug_frame" },
343 { ".eh_frame", NULL },
344 { ".gdb_index", ".zgdb_index" },
345 { ".debug_names", ".zdebug_names" },
346 { ".debug_aranges", ".zdebug_aranges" },
347 23
348 };
349
350 /* List of DWO/DWP sections. */
351
352 static const struct dwop_section_names
353 {
354 struct dwarf2_section_names abbrev_dwo;
355 struct dwarf2_section_names info_dwo;
356 struct dwarf2_section_names line_dwo;
357 struct dwarf2_section_names loc_dwo;
358 struct dwarf2_section_names loclists_dwo;
359 struct dwarf2_section_names macinfo_dwo;
360 struct dwarf2_section_names macro_dwo;
361 struct dwarf2_section_names rnglists_dwo;
362 struct dwarf2_section_names str_dwo;
363 struct dwarf2_section_names str_offsets_dwo;
364 struct dwarf2_section_names types_dwo;
365 struct dwarf2_section_names cu_index;
366 struct dwarf2_section_names tu_index;
367 }
368 dwop_section_names =
369 {
370 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
371 { ".debug_info.dwo", ".zdebug_info.dwo" },
372 { ".debug_line.dwo", ".zdebug_line.dwo" },
373 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
374 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
375 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
376 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
377 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
378 { ".debug_str.dwo", ".zdebug_str.dwo" },
379 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
380 { ".debug_types.dwo", ".zdebug_types.dwo" },
381 { ".debug_cu_index", ".zdebug_cu_index" },
382 { ".debug_tu_index", ".zdebug_tu_index" },
383 };
384
385 /* local data types */
386
387 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
388 begin with a header, which contains the following information. */
389 struct loclists_rnglists_header
390 {
391 /* A 4-byte or 12-byte length containing the length of the
392 set of entries for this compilation unit, not including the
393 length field itself. */
394 unsigned int length;
395
396 /* A 2-byte version identifier. */
397 short version;
398
399 /* A 1-byte unsigned integer containing the size in bytes of an address on
400 the target system. */
401 unsigned char addr_size;
402
403 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
404 on the target system. */
405 unsigned char segment_collector_size;
406
407 /* A 4-byte count of the number of offsets that follow the header. */
408 unsigned int offset_entry_count;
409 };
410
411 /* Type used for delaying computation of method physnames.
412 See comments for compute_delayed_physnames. */
413 struct delayed_method_info
414 {
415 /* The type to which the method is attached, i.e., its parent class. */
416 struct type *type;
417
418 /* The index of the method in the type's function fieldlists. */
419 int fnfield_index;
420
421 /* The index of the method in the fieldlist. */
422 int index;
423
424 /* The name of the DIE. */
425 const char *name;
426
427 /* The DIE associated with this method. */
428 struct die_info *die;
429 };
430
431 /* Internal state when decoding a particular compilation unit. */
432 struct dwarf2_cu
433 {
434 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
435 dwarf2_per_objfile *per_objfile);
436
437 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
438
439 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
440 Create the set of symtabs used by this TU, or if this TU is sharing
441 symtabs with another TU and the symtabs have already been created
442 then restore those symtabs in the line header.
443 We don't need the pc/line-number mapping for type units. */
444 void setup_type_unit_groups (struct die_info *die);
445
446 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
447 buildsym_compunit constructor. */
448 struct compunit_symtab *start_symtab (const char *name,
449 const char *comp_dir,
450 CORE_ADDR low_pc);
451
452 /* Reset the builder. */
453 void reset_builder () { m_builder.reset (); }
454
455 /* Return a type that is a generic pointer type, the size of which
456 matches the address size given in the compilation unit header for
457 this CU. */
458 struct type *addr_type () const;
459
460 /* Find an integer type the same size as the address size given in
461 the compilation unit header for this CU. UNSIGNED_P controls if
462 the integer is unsigned or not. */
463 struct type *addr_sized_int_type (bool unsigned_p) const;
464
465 /* The header of the compilation unit. */
466 struct comp_unit_head header {};
467
468 /* Base address of this compilation unit. */
469 gdb::optional<CORE_ADDR> base_address;
470
471 /* The language we are debugging. */
472 enum language language = language_unknown;
473 const struct language_defn *language_defn = nullptr;
474
475 const char *producer = nullptr;
476
477 private:
478 /* The symtab builder for this CU. This is only non-NULL when full
479 symbols are being read. */
480 std::unique_ptr<buildsym_compunit> m_builder;
481
482 public:
483 /* The generic symbol table building routines have separate lists for
484 file scope symbols and all all other scopes (local scopes). So
485 we need to select the right one to pass to add_symbol_to_list().
486 We do it by keeping a pointer to the correct list in list_in_scope.
487
488 FIXME: The original dwarf code just treated the file scope as the
489 first local scope, and all other local scopes as nested local
490 scopes, and worked fine. Check to see if we really need to
491 distinguish these in buildsym.c. */
492 struct pending **list_in_scope = nullptr;
493
494 /* Hash table holding all the loaded partial DIEs
495 with partial_die->offset.SECT_OFF as hash. */
496 htab_t partial_dies = nullptr;
497
498 /* Storage for things with the same lifetime as this read-in compilation
499 unit, including partial DIEs. */
500 auto_obstack comp_unit_obstack;
501
502 /* Backlink to our per_cu entry. */
503 struct dwarf2_per_cu_data *per_cu;
504
505 /* The dwarf2_per_objfile that owns this. */
506 dwarf2_per_objfile *per_objfile;
507
508 /* How many compilation units ago was this CU last referenced? */
509 int last_used = 0;
510
511 /* A hash table of DIE cu_offset for following references with
512 die_info->offset.sect_off as hash. */
513 htab_t die_hash = nullptr;
514
515 /* Full DIEs if read in. */
516 struct die_info *dies = nullptr;
517
518 /* A set of pointers to dwarf2_per_cu_data objects for compilation
519 units referenced by this one. Only set during full symbol processing;
520 partial symbol tables do not have dependencies. */
521 htab_t dependencies = nullptr;
522
523 /* Header data from the line table, during full symbol processing. */
524 struct line_header *line_header = nullptr;
525 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
526 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
527 this is the DW_TAG_compile_unit die for this CU. We'll hold on
528 to the line header as long as this DIE is being processed. See
529 process_die_scope. */
530 die_info *line_header_die_owner = nullptr;
531
532 /* A list of methods which need to have physnames computed
533 after all type information has been read. */
534 std::vector<delayed_method_info> method_list;
535
536 /* To be copied to symtab->call_site_htab. */
537 htab_t call_site_htab = nullptr;
538
539 /* Non-NULL if this CU came from a DWO file.
540 There is an invariant here that is important to remember:
541 Except for attributes copied from the top level DIE in the "main"
542 (or "stub") file in preparation for reading the DWO file
543 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
544 Either there isn't a DWO file (in which case this is NULL and the point
545 is moot), or there is and either we're not going to read it (in which
546 case this is NULL) or there is and we are reading it (in which case this
547 is non-NULL). */
548 struct dwo_unit *dwo_unit = nullptr;
549
550 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
551 Note this value comes from the Fission stub CU/TU's DIE. */
552 gdb::optional<ULONGEST> addr_base;
553
554 /* The DW_AT_rnglists_base attribute if present.
555 Note this value comes from the Fission stub CU/TU's DIE.
556 Also note that the value is zero in the non-DWO case so this value can
557 be used without needing to know whether DWO files are in use or not.
558 N.B. This does not apply to DW_AT_ranges appearing in
559 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
560 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
561 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
562 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
563 ULONGEST ranges_base = 0;
564
565 /* The DW_AT_loclists_base attribute if present. */
566 ULONGEST loclist_base = 0;
567
568 /* When reading debug info generated by older versions of rustc, we
569 have to rewrite some union types to be struct types with a
570 variant part. This rewriting must be done after the CU is fully
571 read in, because otherwise at the point of rewriting some struct
572 type might not have been fully processed. So, we keep a list of
573 all such types here and process them after expansion. */
574 std::vector<struct type *> rust_unions;
575
576 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
577 files, the value is implicitly zero. For DWARF 5 version DWO files, the
578 value is often implicit and is the size of the header of
579 .debug_str_offsets section (8 or 4, depending on the address size). */
580 gdb::optional<ULONGEST> str_offsets_base;
581
582 /* Mark used when releasing cached dies. */
583 bool mark : 1;
584
585 /* This CU references .debug_loc. See the symtab->locations_valid field.
586 This test is imperfect as there may exist optimized debug code not using
587 any location list and still facing inlining issues if handled as
588 unoptimized code. For a future better test see GCC PR other/32998. */
589 bool has_loclist : 1;
590
591 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
592 if all the producer_is_* fields are valid. This information is cached
593 because profiling CU expansion showed excessive time spent in
594 producer_is_gxx_lt_4_6. */
595 bool checked_producer : 1;
596 bool producer_is_gxx_lt_4_6 : 1;
597 bool producer_is_gcc_lt_4_3 : 1;
598 bool producer_is_icc : 1;
599 bool producer_is_icc_lt_14 : 1;
600 bool producer_is_codewarrior : 1;
601
602 /* When true, the file that we're processing is known to have
603 debugging info for C++ namespaces. GCC 3.3.x did not produce
604 this information, but later versions do. */
605
606 bool processing_has_namespace_info : 1;
607
608 struct partial_die_info *find_partial_die (sect_offset sect_off);
609
610 /* If this CU was inherited by another CU (via specification,
611 abstract_origin, etc), this is the ancestor CU. */
612 dwarf2_cu *ancestor;
613
614 /* Get the buildsym_compunit for this CU. */
615 buildsym_compunit *get_builder ()
616 {
617 /* If this CU has a builder associated with it, use that. */
618 if (m_builder != nullptr)
619 return m_builder.get ();
620
621 /* Otherwise, search ancestors for a valid builder. */
622 if (ancestor != nullptr)
623 return ancestor->get_builder ();
624
625 return nullptr;
626 }
627 };
628
629 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
630 This includes type_unit_group and quick_file_names. */
631
632 struct stmt_list_hash
633 {
634 /* The DWO unit this table is from or NULL if there is none. */
635 struct dwo_unit *dwo_unit;
636
637 /* Offset in .debug_line or .debug_line.dwo. */
638 sect_offset line_sect_off;
639 };
640
641 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
642 an object of this type. This contains elements of type unit groups
643 that can be shared across objfiles. The non-shareable parts are in
644 type_unit_group_unshareable. */
645
646 struct type_unit_group
647 {
648 /* dwarf2read.c's main "handle" on a TU symtab.
649 To simplify things we create an artificial CU that "includes" all the
650 type units using this stmt_list so that the rest of the code still has
651 a "per_cu" handle on the symtab. */
652 struct dwarf2_per_cu_data per_cu;
653
654 /* The TUs that share this DW_AT_stmt_list entry.
655 This is added to while parsing type units to build partial symtabs,
656 and is deleted afterwards and not used again. */
657 std::vector<signatured_type *> *tus;
658
659 /* The data used to construct the hash key. */
660 struct stmt_list_hash hash;
661 };
662
663 /* These sections are what may appear in a (real or virtual) DWO file. */
664
665 struct dwo_sections
666 {
667 struct dwarf2_section_info abbrev;
668 struct dwarf2_section_info line;
669 struct dwarf2_section_info loc;
670 struct dwarf2_section_info loclists;
671 struct dwarf2_section_info macinfo;
672 struct dwarf2_section_info macro;
673 struct dwarf2_section_info rnglists;
674 struct dwarf2_section_info str;
675 struct dwarf2_section_info str_offsets;
676 /* In the case of a virtual DWO file, these two are unused. */
677 struct dwarf2_section_info info;
678 std::vector<dwarf2_section_info> types;
679 };
680
681 /* CUs/TUs in DWP/DWO files. */
682
683 struct dwo_unit
684 {
685 /* Backlink to the containing struct dwo_file. */
686 struct dwo_file *dwo_file;
687
688 /* The "id" that distinguishes this CU/TU.
689 .debug_info calls this "dwo_id", .debug_types calls this "signature".
690 Since signatures came first, we stick with it for consistency. */
691 ULONGEST signature;
692
693 /* The section this CU/TU lives in, in the DWO file. */
694 struct dwarf2_section_info *section;
695
696 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
697 sect_offset sect_off;
698 unsigned int length;
699
700 /* For types, offset in the type's DIE of the type defined by this TU. */
701 cu_offset type_offset_in_tu;
702 };
703
704 /* include/dwarf2.h defines the DWP section codes.
705 It defines a max value but it doesn't define a min value, which we
706 use for error checking, so provide one. */
707
708 enum dwp_v2_section_ids
709 {
710 DW_SECT_MIN = 1
711 };
712
713 /* Data for one DWO file.
714
715 This includes virtual DWO files (a virtual DWO file is a DWO file as it
716 appears in a DWP file). DWP files don't really have DWO files per se -
717 comdat folding of types "loses" the DWO file they came from, and from
718 a high level view DWP files appear to contain a mass of random types.
719 However, to maintain consistency with the non-DWP case we pretend DWP
720 files contain virtual DWO files, and we assign each TU with one virtual
721 DWO file (generally based on the line and abbrev section offsets -
722 a heuristic that seems to work in practice). */
723
724 struct dwo_file
725 {
726 dwo_file () = default;
727 DISABLE_COPY_AND_ASSIGN (dwo_file);
728
729 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
730 For virtual DWO files the name is constructed from the section offsets
731 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
732 from related CU+TUs. */
733 const char *dwo_name = nullptr;
734
735 /* The DW_AT_comp_dir attribute. */
736 const char *comp_dir = nullptr;
737
738 /* The bfd, when the file is open. Otherwise this is NULL.
739 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
740 gdb_bfd_ref_ptr dbfd;
741
742 /* The sections that make up this DWO file.
743 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
744 sections (for lack of a better name). */
745 struct dwo_sections sections {};
746
747 /* The CUs in the file.
748 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
749 an extension to handle LLVM's Link Time Optimization output (where
750 multiple source files may be compiled into a single object/dwo pair). */
751 htab_up cus;
752
753 /* Table of TUs in the file.
754 Each element is a struct dwo_unit. */
755 htab_up tus;
756 };
757
758 /* These sections are what may appear in a DWP file. */
759
760 struct dwp_sections
761 {
762 /* These are used by all DWP versions (1, 2 and 5). */
763 struct dwarf2_section_info str;
764 struct dwarf2_section_info cu_index;
765 struct dwarf2_section_info tu_index;
766
767 /* These are only used by DWP version 2 and version 5 files.
768 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
769 sections are referenced by section number, and are not recorded here.
770 In DWP version 2 or 5 there is at most one copy of all these sections,
771 each section being (effectively) comprised of the concatenation of all of
772 the individual sections that exist in the version 1 format.
773 To keep the code simple we treat each of these concatenated pieces as a
774 section itself (a virtual section?). */
775 struct dwarf2_section_info abbrev;
776 struct dwarf2_section_info info;
777 struct dwarf2_section_info line;
778 struct dwarf2_section_info loc;
779 struct dwarf2_section_info loclists;
780 struct dwarf2_section_info macinfo;
781 struct dwarf2_section_info macro;
782 struct dwarf2_section_info rnglists;
783 struct dwarf2_section_info str_offsets;
784 struct dwarf2_section_info types;
785 };
786
787 /* These sections are what may appear in a virtual DWO file in DWP version 1.
788 A virtual DWO file is a DWO file as it appears in a DWP file. */
789
790 struct virtual_v1_dwo_sections
791 {
792 struct dwarf2_section_info abbrev;
793 struct dwarf2_section_info line;
794 struct dwarf2_section_info loc;
795 struct dwarf2_section_info macinfo;
796 struct dwarf2_section_info macro;
797 struct dwarf2_section_info str_offsets;
798 /* Each DWP hash table entry records one CU or one TU.
799 That is recorded here, and copied to dwo_unit.section. */
800 struct dwarf2_section_info info_or_types;
801 };
802
803 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
804 In version 2, the sections of the DWO files are concatenated together
805 and stored in one section of that name. Thus each ELF section contains
806 several "virtual" sections. */
807
808 struct virtual_v2_or_v5_dwo_sections
809 {
810 bfd_size_type abbrev_offset;
811 bfd_size_type abbrev_size;
812
813 bfd_size_type line_offset;
814 bfd_size_type line_size;
815
816 bfd_size_type loc_offset;
817 bfd_size_type loc_size;
818
819 bfd_size_type loclists_offset;
820 bfd_size_type loclists_size;
821
822 bfd_size_type macinfo_offset;
823 bfd_size_type macinfo_size;
824
825 bfd_size_type macro_offset;
826 bfd_size_type macro_size;
827
828 bfd_size_type rnglists_offset;
829 bfd_size_type rnglists_size;
830
831 bfd_size_type str_offsets_offset;
832 bfd_size_type str_offsets_size;
833
834 /* Each DWP hash table entry records one CU or one TU.
835 That is recorded here, and copied to dwo_unit.section. */
836 bfd_size_type info_or_types_offset;
837 bfd_size_type info_or_types_size;
838 };
839
840 /* Contents of DWP hash tables. */
841
842 struct dwp_hash_table
843 {
844 uint32_t version, nr_columns;
845 uint32_t nr_units, nr_slots;
846 const gdb_byte *hash_table, *unit_table;
847 union
848 {
849 struct
850 {
851 const gdb_byte *indices;
852 } v1;
853 struct
854 {
855 /* This is indexed by column number and gives the id of the section
856 in that column. */
857 #define MAX_NR_V2_DWO_SECTIONS \
858 (1 /* .debug_info or .debug_types */ \
859 + 1 /* .debug_abbrev */ \
860 + 1 /* .debug_line */ \
861 + 1 /* .debug_loc */ \
862 + 1 /* .debug_str_offsets */ \
863 + 1 /* .debug_macro or .debug_macinfo */)
864 int section_ids[MAX_NR_V2_DWO_SECTIONS];
865 const gdb_byte *offsets;
866 const gdb_byte *sizes;
867 } v2;
868 struct
869 {
870 /* This is indexed by column number and gives the id of the section
871 in that column. */
872 #define MAX_NR_V5_DWO_SECTIONS \
873 (1 /* .debug_info */ \
874 + 1 /* .debug_abbrev */ \
875 + 1 /* .debug_line */ \
876 + 1 /* .debug_loclists */ \
877 + 1 /* .debug_str_offsets */ \
878 + 1 /* .debug_macro */ \
879 + 1 /* .debug_rnglists */)
880 int section_ids[MAX_NR_V5_DWO_SECTIONS];
881 const gdb_byte *offsets;
882 const gdb_byte *sizes;
883 } v5;
884 } section_pool;
885 };
886
887 /* Data for one DWP file. */
888
889 struct dwp_file
890 {
891 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
892 : name (name_),
893 dbfd (std::move (abfd))
894 {
895 }
896
897 /* Name of the file. */
898 const char *name;
899
900 /* File format version. */
901 int version = 0;
902
903 /* The bfd. */
904 gdb_bfd_ref_ptr dbfd;
905
906 /* Section info for this file. */
907 struct dwp_sections sections {};
908
909 /* Table of CUs in the file. */
910 const struct dwp_hash_table *cus = nullptr;
911
912 /* Table of TUs in the file. */
913 const struct dwp_hash_table *tus = nullptr;
914
915 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
916 htab_up loaded_cus;
917 htab_up loaded_tus;
918
919 /* Table to map ELF section numbers to their sections.
920 This is only needed for the DWP V1 file format. */
921 unsigned int num_sections = 0;
922 asection **elf_sections = nullptr;
923 };
924
925 /* Struct used to pass misc. parameters to read_die_and_children, et
926 al. which are used for both .debug_info and .debug_types dies.
927 All parameters here are unchanging for the life of the call. This
928 struct exists to abstract away the constant parameters of die reading. */
929
930 struct die_reader_specs
931 {
932 /* The bfd of die_section. */
933 bfd* abfd;
934
935 /* The CU of the DIE we are parsing. */
936 struct dwarf2_cu *cu;
937
938 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
939 struct dwo_file *dwo_file;
940
941 /* The section the die comes from.
942 This is either .debug_info or .debug_types, or the .dwo variants. */
943 struct dwarf2_section_info *die_section;
944
945 /* die_section->buffer. */
946 const gdb_byte *buffer;
947
948 /* The end of the buffer. */
949 const gdb_byte *buffer_end;
950
951 /* The abbreviation table to use when reading the DIEs. */
952 struct abbrev_table *abbrev_table;
953 };
954
955 /* A subclass of die_reader_specs that holds storage and has complex
956 constructor and destructor behavior. */
957
958 class cutu_reader : public die_reader_specs
959 {
960 public:
961
962 cutu_reader (dwarf2_per_cu_data *this_cu,
963 dwarf2_per_objfile *per_objfile,
964 struct abbrev_table *abbrev_table,
965 dwarf2_cu *existing_cu,
966 bool skip_partial);
967
968 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
969 dwarf2_per_objfile *per_objfile,
970 struct dwarf2_cu *parent_cu = nullptr,
971 struct dwo_file *dwo_file = nullptr);
972
973 DISABLE_COPY_AND_ASSIGN (cutu_reader);
974
975 const gdb_byte *info_ptr = nullptr;
976 struct die_info *comp_unit_die = nullptr;
977 bool dummy_p = false;
978
979 /* Release the new CU, putting it on the chain. This cannot be done
980 for dummy CUs. */
981 void keep ();
982
983 private:
984 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
985 dwarf2_per_objfile *per_objfile,
986 dwarf2_cu *existing_cu);
987
988 struct dwarf2_per_cu_data *m_this_cu;
989 std::unique_ptr<dwarf2_cu> m_new_cu;
990
991 /* The ordinary abbreviation table. */
992 abbrev_table_up m_abbrev_table_holder;
993
994 /* The DWO abbreviation table. */
995 abbrev_table_up m_dwo_abbrev_table;
996 };
997
998 /* When we construct a partial symbol table entry we only
999 need this much information. */
1000 struct partial_die_info : public allocate_on_obstack
1001 {
1002 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1003
1004 /* Disable assign but still keep copy ctor, which is needed
1005 load_partial_dies. */
1006 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1007
1008 /* Adjust the partial die before generating a symbol for it. This
1009 function may set the is_external flag or change the DIE's
1010 name. */
1011 void fixup (struct dwarf2_cu *cu);
1012
1013 /* Read a minimal amount of information into the minimal die
1014 structure. */
1015 const gdb_byte *read (const struct die_reader_specs *reader,
1016 const struct abbrev_info &abbrev,
1017 const gdb_byte *info_ptr);
1018
1019 /* Compute the name of this partial DIE. This memoizes the
1020 result, so it is safe to call multiple times. */
1021 const char *name (dwarf2_cu *cu);
1022
1023 /* Offset of this DIE. */
1024 const sect_offset sect_off;
1025
1026 /* DWARF-2 tag for this DIE. */
1027 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1028
1029 /* Assorted flags describing the data found in this DIE. */
1030 const unsigned int has_children : 1;
1031
1032 unsigned int is_external : 1;
1033 unsigned int is_declaration : 1;
1034 unsigned int has_type : 1;
1035 unsigned int has_specification : 1;
1036 unsigned int has_pc_info : 1;
1037 unsigned int may_be_inlined : 1;
1038
1039 /* This DIE has been marked DW_AT_main_subprogram. */
1040 unsigned int main_subprogram : 1;
1041
1042 /* Flag set if the SCOPE field of this structure has been
1043 computed. */
1044 unsigned int scope_set : 1;
1045
1046 /* Flag set if the DIE has a byte_size attribute. */
1047 unsigned int has_byte_size : 1;
1048
1049 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1050 unsigned int has_const_value : 1;
1051
1052 /* Flag set if any of the DIE's children are template arguments. */
1053 unsigned int has_template_arguments : 1;
1054
1055 /* Flag set if fixup has been called on this die. */
1056 unsigned int fixup_called : 1;
1057
1058 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1059 unsigned int is_dwz : 1;
1060
1061 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1062 unsigned int spec_is_dwz : 1;
1063
1064 unsigned int canonical_name : 1;
1065
1066 /* The name of this DIE. Normally the value of DW_AT_name, but
1067 sometimes a default name for unnamed DIEs. */
1068 const char *raw_name = nullptr;
1069
1070 /* The linkage name, if present. */
1071 const char *linkage_name = nullptr;
1072
1073 /* The scope to prepend to our children. This is generally
1074 allocated on the comp_unit_obstack, so will disappear
1075 when this compilation unit leaves the cache. */
1076 const char *scope = nullptr;
1077
1078 /* Some data associated with the partial DIE. The tag determines
1079 which field is live. */
1080 union
1081 {
1082 /* The location description associated with this DIE, if any. */
1083 struct dwarf_block *locdesc;
1084 /* The offset of an import, for DW_TAG_imported_unit. */
1085 sect_offset sect_off;
1086 } d {};
1087
1088 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1089 CORE_ADDR lowpc = 0;
1090 CORE_ADDR highpc = 0;
1091
1092 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1093 DW_AT_sibling, if any. */
1094 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1095 could return DW_AT_sibling values to its caller load_partial_dies. */
1096 const gdb_byte *sibling = nullptr;
1097
1098 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1099 DW_AT_specification (or DW_AT_abstract_origin or
1100 DW_AT_extension). */
1101 sect_offset spec_offset {};
1102
1103 /* Pointers to this DIE's parent, first child, and next sibling,
1104 if any. */
1105 struct partial_die_info *die_parent = nullptr;
1106 struct partial_die_info *die_child = nullptr;
1107 struct partial_die_info *die_sibling = nullptr;
1108
1109 friend struct partial_die_info *
1110 dwarf2_cu::find_partial_die (sect_offset sect_off);
1111
1112 private:
1113 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1114 partial_die_info (sect_offset sect_off)
1115 : partial_die_info (sect_off, DW_TAG_padding, 0)
1116 {
1117 }
1118
1119 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1120 int has_children_)
1121 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1122 {
1123 is_external = 0;
1124 is_declaration = 0;
1125 has_type = 0;
1126 has_specification = 0;
1127 has_pc_info = 0;
1128 may_be_inlined = 0;
1129 main_subprogram = 0;
1130 scope_set = 0;
1131 has_byte_size = 0;
1132 has_const_value = 0;
1133 has_template_arguments = 0;
1134 fixup_called = 0;
1135 is_dwz = 0;
1136 spec_is_dwz = 0;
1137 canonical_name = 0;
1138 }
1139 };
1140
1141 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1142 but this would require a corresponding change in unpack_field_as_long
1143 and friends. */
1144 static int bits_per_byte = 8;
1145
1146 struct variant_part_builder;
1147
1148 /* When reading a variant, we track a bit more information about the
1149 field, and store it in an object of this type. */
1150
1151 struct variant_field
1152 {
1153 int first_field = -1;
1154 int last_field = -1;
1155
1156 /* A variant can contain other variant parts. */
1157 std::vector<variant_part_builder> variant_parts;
1158
1159 /* If we see a DW_TAG_variant, then this will be set if this is the
1160 default branch. */
1161 bool default_branch = false;
1162 /* If we see a DW_AT_discr_value, then this will be the discriminant
1163 value. */
1164 ULONGEST discriminant_value = 0;
1165 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1166 data. */
1167 struct dwarf_block *discr_list_data = nullptr;
1168 };
1169
1170 /* This represents a DW_TAG_variant_part. */
1171
1172 struct variant_part_builder
1173 {
1174 /* The offset of the discriminant field. */
1175 sect_offset discriminant_offset {};
1176
1177 /* Variants that are direct children of this variant part. */
1178 std::vector<variant_field> variants;
1179
1180 /* True if we're currently reading a variant. */
1181 bool processing_variant = false;
1182 };
1183
1184 struct nextfield
1185 {
1186 int accessibility = 0;
1187 int virtuality = 0;
1188 /* Variant parts need to find the discriminant, which is a DIE
1189 reference. We track the section offset of each field to make
1190 this link. */
1191 sect_offset offset;
1192 struct field field {};
1193 };
1194
1195 struct fnfieldlist
1196 {
1197 const char *name = nullptr;
1198 std::vector<struct fn_field> fnfields;
1199 };
1200
1201 /* The routines that read and process dies for a C struct or C++ class
1202 pass lists of data member fields and lists of member function fields
1203 in an instance of a field_info structure, as defined below. */
1204 struct field_info
1205 {
1206 /* List of data member and baseclasses fields. */
1207 std::vector<struct nextfield> fields;
1208 std::vector<struct nextfield> baseclasses;
1209
1210 /* Set if the accessibility of one of the fields is not public. */
1211 bool non_public_fields = false;
1212
1213 /* Member function fieldlist array, contains name of possibly overloaded
1214 member function, number of overloaded member functions and a pointer
1215 to the head of the member function field chain. */
1216 std::vector<struct fnfieldlist> fnfieldlists;
1217
1218 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1219 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1220 std::vector<struct decl_field> typedef_field_list;
1221
1222 /* Nested types defined by this class and the number of elements in this
1223 list. */
1224 std::vector<struct decl_field> nested_types_list;
1225
1226 /* If non-null, this is the variant part we are currently
1227 reading. */
1228 variant_part_builder *current_variant_part = nullptr;
1229 /* This holds all the top-level variant parts attached to the type
1230 we're reading. */
1231 std::vector<variant_part_builder> variant_parts;
1232
1233 /* Return the total number of fields (including baseclasses). */
1234 int nfields () const
1235 {
1236 return fields.size () + baseclasses.size ();
1237 }
1238 };
1239
1240 /* Loaded secondary compilation units are kept in memory until they
1241 have not been referenced for the processing of this many
1242 compilation units. Set this to zero to disable caching. Cache
1243 sizes of up to at least twenty will improve startup time for
1244 typical inter-CU-reference binaries, at an obvious memory cost. */
1245 static int dwarf_max_cache_age = 5;
1246 static void
1247 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1248 struct cmd_list_element *c, const char *value)
1249 {
1250 fprintf_filtered (file, _("The upper bound on the age of cached "
1251 "DWARF compilation units is %s.\n"),
1252 value);
1253 }
1254 \f
1255 /* local function prototypes */
1256
1257 static void dwarf2_find_base_address (struct die_info *die,
1258 struct dwarf2_cu *cu);
1259
1260 static dwarf2_psymtab *create_partial_symtab
1261 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1262 const char *name);
1263
1264 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1265 const gdb_byte *info_ptr,
1266 struct die_info *type_unit_die);
1267
1268 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
1269
1270 static void scan_partial_symbols (struct partial_die_info *,
1271 CORE_ADDR *, CORE_ADDR *,
1272 int, struct dwarf2_cu *);
1273
1274 static void add_partial_symbol (struct partial_die_info *,
1275 struct dwarf2_cu *);
1276
1277 static void add_partial_namespace (struct partial_die_info *pdi,
1278 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1279 int set_addrmap, struct dwarf2_cu *cu);
1280
1281 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1282 CORE_ADDR *highpc, int set_addrmap,
1283 struct dwarf2_cu *cu);
1284
1285 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1286 struct dwarf2_cu *cu);
1287
1288 static void add_partial_subprogram (struct partial_die_info *pdi,
1289 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1290 int need_pc, struct dwarf2_cu *cu);
1291
1292 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1293
1294 static struct partial_die_info *load_partial_dies
1295 (const struct die_reader_specs *, const gdb_byte *, int);
1296
1297 /* A pair of partial_die_info and compilation unit. */
1298 struct cu_partial_die_info
1299 {
1300 /* The compilation unit of the partial_die_info. */
1301 struct dwarf2_cu *cu;
1302 /* A partial_die_info. */
1303 struct partial_die_info *pdi;
1304
1305 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1306 : cu (cu),
1307 pdi (pdi)
1308 { /* Nothing. */ }
1309
1310 private:
1311 cu_partial_die_info () = delete;
1312 };
1313
1314 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1315 struct dwarf2_cu *);
1316
1317 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1318 struct attribute *, struct attr_abbrev *,
1319 const gdb_byte *);
1320
1321 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1322 struct attribute *attr, dwarf_tag tag);
1323
1324 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1325
1326 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1327 dwarf2_section_info *, sect_offset);
1328
1329 static const char *read_indirect_string
1330 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
1331 const struct comp_unit_head *, unsigned int *);
1332
1333 static const char *read_indirect_string_at_offset
1334 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
1335
1336 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1337 const gdb_byte *,
1338 unsigned int *);
1339
1340 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1341 ULONGEST str_index);
1342
1343 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1344 ULONGEST str_index);
1345
1346 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1347
1348 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1349 struct dwarf2_cu *);
1350
1351 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1352 struct dwarf2_cu *cu);
1353
1354 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1355
1356 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1357 struct dwarf2_cu *cu);
1358
1359 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1360
1361 static struct die_info *die_specification (struct die_info *die,
1362 struct dwarf2_cu **);
1363
1364 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1365 struct dwarf2_cu *cu);
1366
1367 static void dwarf_decode_lines (struct line_header *, const char *,
1368 struct dwarf2_cu *, dwarf2_psymtab *,
1369 CORE_ADDR, int decode_mapping);
1370
1371 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1372 const char *);
1373
1374 static struct symbol *new_symbol (struct die_info *, struct type *,
1375 struct dwarf2_cu *, struct symbol * = NULL);
1376
1377 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1378 struct dwarf2_cu *);
1379
1380 static void dwarf2_const_value_attr (const struct attribute *attr,
1381 struct type *type,
1382 const char *name,
1383 struct obstack *obstack,
1384 struct dwarf2_cu *cu, LONGEST *value,
1385 const gdb_byte **bytes,
1386 struct dwarf2_locexpr_baton **baton);
1387
1388 static struct type *read_subrange_index_type (struct die_info *die,
1389 struct dwarf2_cu *cu);
1390
1391 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1392
1393 static int need_gnat_info (struct dwarf2_cu *);
1394
1395 static struct type *die_descriptive_type (struct die_info *,
1396 struct dwarf2_cu *);
1397
1398 static void set_descriptive_type (struct type *, struct die_info *,
1399 struct dwarf2_cu *);
1400
1401 static struct type *die_containing_type (struct die_info *,
1402 struct dwarf2_cu *);
1403
1404 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1405 struct dwarf2_cu *);
1406
1407 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1408
1409 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1410
1411 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1412
1413 static char *typename_concat (struct obstack *obs, const char *prefix,
1414 const char *suffix, int physname,
1415 struct dwarf2_cu *cu);
1416
1417 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1418
1419 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1420
1421 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1422
1423 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1424
1425 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1426
1427 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1428
1429 /* Return the .debug_loclists section to use for cu. */
1430 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1431
1432 /* Return the .debug_rnglists section to use for cu. */
1433 static struct dwarf2_section_info *cu_debug_rnglists_section
1434 (struct dwarf2_cu *cu, dwarf_tag tag);
1435
1436 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1437 values. Keep the items ordered with increasing constraints compliance. */
1438 enum pc_bounds_kind
1439 {
1440 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1441 PC_BOUNDS_NOT_PRESENT,
1442
1443 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1444 were present but they do not form a valid range of PC addresses. */
1445 PC_BOUNDS_INVALID,
1446
1447 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1448 PC_BOUNDS_RANGES,
1449
1450 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1451 PC_BOUNDS_HIGH_LOW,
1452 };
1453
1454 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1455 CORE_ADDR *, CORE_ADDR *,
1456 struct dwarf2_cu *,
1457 dwarf2_psymtab *);
1458
1459 static void get_scope_pc_bounds (struct die_info *,
1460 CORE_ADDR *, CORE_ADDR *,
1461 struct dwarf2_cu *);
1462
1463 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1464 CORE_ADDR, struct dwarf2_cu *);
1465
1466 static void dwarf2_add_field (struct field_info *, struct die_info *,
1467 struct dwarf2_cu *);
1468
1469 static void dwarf2_attach_fields_to_type (struct field_info *,
1470 struct type *, struct dwarf2_cu *);
1471
1472 static void dwarf2_add_member_fn (struct field_info *,
1473 struct die_info *, struct type *,
1474 struct dwarf2_cu *);
1475
1476 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1477 struct type *,
1478 struct dwarf2_cu *);
1479
1480 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1481
1482 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1483
1484 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1485
1486 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1487
1488 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1489
1490 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1491
1492 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1493
1494 static struct type *read_module_type (struct die_info *die,
1495 struct dwarf2_cu *cu);
1496
1497 static const char *namespace_name (struct die_info *die,
1498 int *is_anonymous, struct dwarf2_cu *);
1499
1500 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1501
1502 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1503 bool * = nullptr);
1504
1505 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1506 struct dwarf2_cu *);
1507
1508 static struct die_info *read_die_and_siblings_1
1509 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1510 struct die_info *);
1511
1512 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1513 const gdb_byte *info_ptr,
1514 const gdb_byte **new_info_ptr,
1515 struct die_info *parent);
1516
1517 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1518 struct die_info **, const gdb_byte *,
1519 int);
1520
1521 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1522 struct die_info **, const gdb_byte *);
1523
1524 static void process_die (struct die_info *, struct dwarf2_cu *);
1525
1526 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1527 struct objfile *);
1528
1529 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1530
1531 static const char *dwarf2_full_name (const char *name,
1532 struct die_info *die,
1533 struct dwarf2_cu *cu);
1534
1535 static const char *dwarf2_physname (const char *name, struct die_info *die,
1536 struct dwarf2_cu *cu);
1537
1538 static struct die_info *dwarf2_extension (struct die_info *die,
1539 struct dwarf2_cu **);
1540
1541 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1542
1543 static void dump_die_for_error (struct die_info *);
1544
1545 static void dump_die_1 (struct ui_file *, int level, int max_level,
1546 struct die_info *);
1547
1548 /*static*/ void dump_die (struct die_info *, int max_level);
1549
1550 static void store_in_ref_table (struct die_info *,
1551 struct dwarf2_cu *);
1552
1553 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1554 const struct attribute *,
1555 struct dwarf2_cu **);
1556
1557 static struct die_info *follow_die_ref (struct die_info *,
1558 const struct attribute *,
1559 struct dwarf2_cu **);
1560
1561 static struct die_info *follow_die_sig (struct die_info *,
1562 const struct attribute *,
1563 struct dwarf2_cu **);
1564
1565 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1566 struct dwarf2_cu *);
1567
1568 static struct type *get_DW_AT_signature_type (struct die_info *,
1569 const struct attribute *,
1570 struct dwarf2_cu *);
1571
1572 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1573 dwarf2_per_objfile *per_objfile);
1574
1575 static void read_signatured_type (signatured_type *sig_type,
1576 dwarf2_per_objfile *per_objfile);
1577
1578 static int attr_to_dynamic_prop (const struct attribute *attr,
1579 struct die_info *die, struct dwarf2_cu *cu,
1580 struct dynamic_prop *prop, struct type *type);
1581
1582 /* memory allocation interface */
1583
1584 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1585
1586 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1587
1588 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1589
1590 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1591 struct dwarf2_loclist_baton *baton,
1592 const struct attribute *attr);
1593
1594 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1595 struct symbol *sym,
1596 struct dwarf2_cu *cu,
1597 int is_block);
1598
1599 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1600 const gdb_byte *info_ptr,
1601 struct abbrev_info *abbrev);
1602
1603 static hashval_t partial_die_hash (const void *item);
1604
1605 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1606
1607 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1608 (sect_offset sect_off, unsigned int offset_in_dwz,
1609 dwarf2_per_objfile *per_objfile);
1610
1611 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1612 struct die_info *comp_unit_die,
1613 enum language pretend_language);
1614
1615 static struct type *set_die_type (struct die_info *, struct type *,
1616 struct dwarf2_cu *, bool = false);
1617
1618 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1619
1620 static int create_all_type_units (dwarf2_per_objfile *per_objfile);
1621
1622 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1623 dwarf2_per_objfile *per_objfile,
1624 dwarf2_cu *existing_cu,
1625 bool skip_partial,
1626 enum language pretend_language);
1627
1628 static void process_full_comp_unit (dwarf2_cu *cu,
1629 enum language pretend_language);
1630
1631 static void process_full_type_unit (dwarf2_cu *cu,
1632 enum language pretend_language);
1633
1634 static void dwarf2_add_dependence (struct dwarf2_cu *,
1635 struct dwarf2_per_cu_data *);
1636
1637 static void dwarf2_mark (struct dwarf2_cu *);
1638
1639 static struct type *get_die_type_at_offset (sect_offset,
1640 dwarf2_per_cu_data *per_cu,
1641 dwarf2_per_objfile *per_objfile);
1642
1643 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1644
1645 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1646 dwarf2_per_objfile *per_objfile,
1647 enum language pretend_language);
1648
1649 static void process_queue (dwarf2_per_objfile *per_objfile);
1650
1651 /* Class, the destructor of which frees all allocated queue entries. This
1652 will only have work to do if an error was thrown while processing the
1653 dwarf. If no error was thrown then the queue entries should have all
1654 been processed, and freed, as we went along. */
1655
1656 class dwarf2_queue_guard
1657 {
1658 public:
1659 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1660 : m_per_objfile (per_objfile)
1661 {
1662 }
1663
1664 /* Free any entries remaining on the queue. There should only be
1665 entries left if we hit an error while processing the dwarf. */
1666 ~dwarf2_queue_guard ()
1667 {
1668 /* Ensure that no memory is allocated by the queue. */
1669 std::queue<dwarf2_queue_item> empty;
1670 std::swap (m_per_objfile->per_bfd->queue, empty);
1671 }
1672
1673 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1674
1675 private:
1676 dwarf2_per_objfile *m_per_objfile;
1677 };
1678
1679 dwarf2_queue_item::~dwarf2_queue_item ()
1680 {
1681 /* Anything still marked queued is likely to be in an
1682 inconsistent state, so discard it. */
1683 if (per_cu->queued)
1684 {
1685 per_objfile->remove_cu (per_cu);
1686 per_cu->queued = 0;
1687 }
1688 }
1689
1690 /* The return type of find_file_and_directory. Note, the enclosed
1691 string pointers are only valid while this object is valid. */
1692
1693 struct file_and_directory
1694 {
1695 /* The filename. This is never NULL. */
1696 const char *name;
1697
1698 /* The compilation directory. NULL if not known. If we needed to
1699 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1700 points directly to the DW_AT_comp_dir string attribute owned by
1701 the obstack that owns the DIE. */
1702 const char *comp_dir;
1703
1704 /* If we needed to build a new string for comp_dir, this is what
1705 owns the storage. */
1706 std::string comp_dir_storage;
1707 };
1708
1709 static file_and_directory find_file_and_directory (struct die_info *die,
1710 struct dwarf2_cu *cu);
1711
1712 static htab_up allocate_signatured_type_table ();
1713
1714 static htab_up allocate_dwo_unit_table ();
1715
1716 static struct dwo_unit *lookup_dwo_unit_in_dwp
1717 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1718 const char *comp_dir, ULONGEST signature, int is_debug_types);
1719
1720 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1721
1722 static struct dwo_unit *lookup_dwo_comp_unit
1723 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1724 ULONGEST signature);
1725
1726 static struct dwo_unit *lookup_dwo_type_unit
1727 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1728
1729 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1730
1731 /* A unique pointer to a dwo_file. */
1732
1733 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1734
1735 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1736
1737 static void check_producer (struct dwarf2_cu *cu);
1738
1739 static void free_line_header_voidp (void *arg);
1740 \f
1741 /* Various complaints about symbol reading that don't abort the process. */
1742
1743 static void
1744 dwarf2_debug_line_missing_file_complaint (void)
1745 {
1746 complaint (_(".debug_line section has line data without a file"));
1747 }
1748
1749 static void
1750 dwarf2_debug_line_missing_end_sequence_complaint (void)
1751 {
1752 complaint (_(".debug_line section has line "
1753 "program sequence without an end"));
1754 }
1755
1756 static void
1757 dwarf2_complex_location_expr_complaint (void)
1758 {
1759 complaint (_("location expression too complex"));
1760 }
1761
1762 static void
1763 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1764 int arg3)
1765 {
1766 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1767 arg1, arg2, arg3);
1768 }
1769
1770 static void
1771 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1772 {
1773 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1774 arg1, arg2);
1775 }
1776
1777 /* Hash function for line_header_hash. */
1778
1779 static hashval_t
1780 line_header_hash (const struct line_header *ofs)
1781 {
1782 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1783 }
1784
1785 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1786
1787 static hashval_t
1788 line_header_hash_voidp (const void *item)
1789 {
1790 const struct line_header *ofs = (const struct line_header *) item;
1791
1792 return line_header_hash (ofs);
1793 }
1794
1795 /* Equality function for line_header_hash. */
1796
1797 static int
1798 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1799 {
1800 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1801 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1802
1803 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1804 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1805 }
1806
1807 \f
1808
1809 /* See declaration. */
1810
1811 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1812 bool can_copy_)
1813 : obfd (obfd),
1814 can_copy (can_copy_)
1815 {
1816 if (names == NULL)
1817 names = &dwarf2_elf_names;
1818
1819 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1820 locate_sections (obfd, sec, *names);
1821 }
1822
1823 dwarf2_per_bfd::~dwarf2_per_bfd ()
1824 {
1825 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1826 per_cu->imported_symtabs_free ();
1827
1828 for (signatured_type *sig_type : all_type_units)
1829 sig_type->per_cu.imported_symtabs_free ();
1830
1831 /* Everything else should be on this->obstack. */
1832 }
1833
1834 /* See read.h. */
1835
1836 void
1837 dwarf2_per_objfile::remove_all_cus ()
1838 {
1839 for (auto pair : m_dwarf2_cus)
1840 delete pair.second;
1841
1842 m_dwarf2_cus.clear ();
1843 }
1844
1845 /* A helper class that calls free_cached_comp_units on
1846 destruction. */
1847
1848 class free_cached_comp_units
1849 {
1850 public:
1851
1852 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1853 : m_per_objfile (per_objfile)
1854 {
1855 }
1856
1857 ~free_cached_comp_units ()
1858 {
1859 m_per_objfile->remove_all_cus ();
1860 }
1861
1862 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1863
1864 private:
1865
1866 dwarf2_per_objfile *m_per_objfile;
1867 };
1868
1869 /* See read.h. */
1870
1871 bool
1872 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1873 {
1874 gdb_assert (per_cu->index < this->m_symtabs.size ());
1875
1876 return this->m_symtabs[per_cu->index] != nullptr;
1877 }
1878
1879 /* See read.h. */
1880
1881 compunit_symtab *
1882 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1883 {
1884 gdb_assert (per_cu->index < this->m_symtabs.size ());
1885
1886 return this->m_symtabs[per_cu->index];
1887 }
1888
1889 /* See read.h. */
1890
1891 void
1892 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1893 compunit_symtab *symtab)
1894 {
1895 gdb_assert (per_cu->index < this->m_symtabs.size ());
1896 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1897
1898 this->m_symtabs[per_cu->index] = symtab;
1899 }
1900
1901 /* Try to locate the sections we need for DWARF 2 debugging
1902 information and return true if we have enough to do something.
1903 NAMES points to the dwarf2 section names, or is NULL if the standard
1904 ELF names are used. CAN_COPY is true for formats where symbol
1905 interposition is possible and so symbol values must follow copy
1906 relocation rules. */
1907
1908 int
1909 dwarf2_has_info (struct objfile *objfile,
1910 const struct dwarf2_debug_sections *names,
1911 bool can_copy)
1912 {
1913 if (objfile->flags & OBJF_READNEVER)
1914 return 0;
1915
1916 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1917
1918 if (per_objfile == NULL)
1919 {
1920 dwarf2_per_bfd *per_bfd;
1921
1922 /* We can share a "dwarf2_per_bfd" with other objfiles if the BFD
1923 doesn't require relocations and if there aren't partial symbols
1924 from some other reader. */
1925 if (!objfile_has_partial_symbols (objfile)
1926 && !gdb_bfd_requires_relocations (objfile->obfd))
1927 {
1928 /* See if one has been created for this BFD yet. */
1929 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1930
1931 if (per_bfd == nullptr)
1932 {
1933 /* No, create it now. */
1934 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1935 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1936 }
1937 }
1938 else
1939 {
1940 /* No sharing possible, create one specifically for this objfile. */
1941 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1942 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1943 }
1944
1945 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1946 }
1947
1948 return (!per_objfile->per_bfd->info.is_virtual
1949 && per_objfile->per_bfd->info.s.section != NULL
1950 && !per_objfile->per_bfd->abbrev.is_virtual
1951 && per_objfile->per_bfd->abbrev.s.section != NULL);
1952 }
1953
1954 /* When loading sections, we look either for uncompressed section or for
1955 compressed section names. */
1956
1957 static int
1958 section_is_p (const char *section_name,
1959 const struct dwarf2_section_names *names)
1960 {
1961 if (names->normal != NULL
1962 && strcmp (section_name, names->normal) == 0)
1963 return 1;
1964 if (names->compressed != NULL
1965 && strcmp (section_name, names->compressed) == 0)
1966 return 1;
1967 return 0;
1968 }
1969
1970 /* See declaration. */
1971
1972 void
1973 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1974 const dwarf2_debug_sections &names)
1975 {
1976 flagword aflag = bfd_section_flags (sectp);
1977
1978 if ((aflag & SEC_HAS_CONTENTS) == 0)
1979 {
1980 }
1981 else if (elf_section_data (sectp)->this_hdr.sh_size
1982 > bfd_get_file_size (abfd))
1983 {
1984 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1985 warning (_("Discarding section %s which has a section size (%s"
1986 ") larger than the file size [in module %s]"),
1987 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1988 bfd_get_filename (abfd));
1989 }
1990 else if (section_is_p (sectp->name, &names.info))
1991 {
1992 this->info.s.section = sectp;
1993 this->info.size = bfd_section_size (sectp);
1994 }
1995 else if (section_is_p (sectp->name, &names.abbrev))
1996 {
1997 this->abbrev.s.section = sectp;
1998 this->abbrev.size = bfd_section_size (sectp);
1999 }
2000 else if (section_is_p (sectp->name, &names.line))
2001 {
2002 this->line.s.section = sectp;
2003 this->line.size = bfd_section_size (sectp);
2004 }
2005 else if (section_is_p (sectp->name, &names.loc))
2006 {
2007 this->loc.s.section = sectp;
2008 this->loc.size = bfd_section_size (sectp);
2009 }
2010 else if (section_is_p (sectp->name, &names.loclists))
2011 {
2012 this->loclists.s.section = sectp;
2013 this->loclists.size = bfd_section_size (sectp);
2014 }
2015 else if (section_is_p (sectp->name, &names.macinfo))
2016 {
2017 this->macinfo.s.section = sectp;
2018 this->macinfo.size = bfd_section_size (sectp);
2019 }
2020 else if (section_is_p (sectp->name, &names.macro))
2021 {
2022 this->macro.s.section = sectp;
2023 this->macro.size = bfd_section_size (sectp);
2024 }
2025 else if (section_is_p (sectp->name, &names.str))
2026 {
2027 this->str.s.section = sectp;
2028 this->str.size = bfd_section_size (sectp);
2029 }
2030 else if (section_is_p (sectp->name, &names.str_offsets))
2031 {
2032 this->str_offsets.s.section = sectp;
2033 this->str_offsets.size = bfd_section_size (sectp);
2034 }
2035 else if (section_is_p (sectp->name, &names.line_str))
2036 {
2037 this->line_str.s.section = sectp;
2038 this->line_str.size = bfd_section_size (sectp);
2039 }
2040 else if (section_is_p (sectp->name, &names.addr))
2041 {
2042 this->addr.s.section = sectp;
2043 this->addr.size = bfd_section_size (sectp);
2044 }
2045 else if (section_is_p (sectp->name, &names.frame))
2046 {
2047 this->frame.s.section = sectp;
2048 this->frame.size = bfd_section_size (sectp);
2049 }
2050 else if (section_is_p (sectp->name, &names.eh_frame))
2051 {
2052 this->eh_frame.s.section = sectp;
2053 this->eh_frame.size = bfd_section_size (sectp);
2054 }
2055 else if (section_is_p (sectp->name, &names.ranges))
2056 {
2057 this->ranges.s.section = sectp;
2058 this->ranges.size = bfd_section_size (sectp);
2059 }
2060 else if (section_is_p (sectp->name, &names.rnglists))
2061 {
2062 this->rnglists.s.section = sectp;
2063 this->rnglists.size = bfd_section_size (sectp);
2064 }
2065 else if (section_is_p (sectp->name, &names.types))
2066 {
2067 struct dwarf2_section_info type_section;
2068
2069 memset (&type_section, 0, sizeof (type_section));
2070 type_section.s.section = sectp;
2071 type_section.size = bfd_section_size (sectp);
2072
2073 this->types.push_back (type_section);
2074 }
2075 else if (section_is_p (sectp->name, &names.gdb_index))
2076 {
2077 this->gdb_index.s.section = sectp;
2078 this->gdb_index.size = bfd_section_size (sectp);
2079 }
2080 else if (section_is_p (sectp->name, &names.debug_names))
2081 {
2082 this->debug_names.s.section = sectp;
2083 this->debug_names.size = bfd_section_size (sectp);
2084 }
2085 else if (section_is_p (sectp->name, &names.debug_aranges))
2086 {
2087 this->debug_aranges.s.section = sectp;
2088 this->debug_aranges.size = bfd_section_size (sectp);
2089 }
2090
2091 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2092 && bfd_section_vma (sectp) == 0)
2093 this->has_section_at_zero = true;
2094 }
2095
2096 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2097 SECTION_NAME. */
2098
2099 void
2100 dwarf2_get_section_info (struct objfile *objfile,
2101 enum dwarf2_section_enum sect,
2102 asection **sectp, const gdb_byte **bufp,
2103 bfd_size_type *sizep)
2104 {
2105 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2106 struct dwarf2_section_info *info;
2107
2108 /* We may see an objfile without any DWARF, in which case we just
2109 return nothing. */
2110 if (per_objfile == NULL)
2111 {
2112 *sectp = NULL;
2113 *bufp = NULL;
2114 *sizep = 0;
2115 return;
2116 }
2117 switch (sect)
2118 {
2119 case DWARF2_DEBUG_FRAME:
2120 info = &per_objfile->per_bfd->frame;
2121 break;
2122 case DWARF2_EH_FRAME:
2123 info = &per_objfile->per_bfd->eh_frame;
2124 break;
2125 default:
2126 gdb_assert_not_reached ("unexpected section");
2127 }
2128
2129 info->read (objfile);
2130
2131 *sectp = info->get_bfd_section ();
2132 *bufp = info->buffer;
2133 *sizep = info->size;
2134 }
2135
2136 /* A helper function to find the sections for a .dwz file. */
2137
2138 static void
2139 locate_dwz_sections (bfd *abfd, asection *sectp, dwz_file *dwz_file)
2140 {
2141 /* Note that we only support the standard ELF names, because .dwz
2142 is ELF-only (at the time of writing). */
2143 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2144 {
2145 dwz_file->abbrev.s.section = sectp;
2146 dwz_file->abbrev.size = bfd_section_size (sectp);
2147 }
2148 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2149 {
2150 dwz_file->info.s.section = sectp;
2151 dwz_file->info.size = bfd_section_size (sectp);
2152 }
2153 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2154 {
2155 dwz_file->str.s.section = sectp;
2156 dwz_file->str.size = bfd_section_size (sectp);
2157 }
2158 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2159 {
2160 dwz_file->line.s.section = sectp;
2161 dwz_file->line.size = bfd_section_size (sectp);
2162 }
2163 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2164 {
2165 dwz_file->macro.s.section = sectp;
2166 dwz_file->macro.size = bfd_section_size (sectp);
2167 }
2168 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2169 {
2170 dwz_file->gdb_index.s.section = sectp;
2171 dwz_file->gdb_index.size = bfd_section_size (sectp);
2172 }
2173 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2174 {
2175 dwz_file->debug_names.s.section = sectp;
2176 dwz_file->debug_names.size = bfd_section_size (sectp);
2177 }
2178 }
2179
2180 /* Attempt to find a .dwz file (whose full path is represented by
2181 FILENAME) in all of the specified debug file directories provided.
2182
2183 Return the equivalent gdb_bfd_ref_ptr of the .dwz file found, or
2184 nullptr if it could not find anything. */
2185
2186 static gdb_bfd_ref_ptr
2187 dwz_search_other_debugdirs (std::string &filename, bfd_byte *buildid,
2188 size_t buildid_len)
2189 {
2190 /* Let's assume that the path represented by FILENAME has the
2191 "/.dwz/" subpath in it. This is what (most) GNU/Linux
2192 distributions do, anyway. */
2193 size_t dwz_pos = filename.find ("/.dwz/");
2194
2195 if (dwz_pos == std::string::npos)
2196 return nullptr;
2197
2198 /* This is an obvious assertion, but it's here more to educate
2199 future readers of this code that FILENAME at DWZ_POS *must*
2200 contain a directory separator. */
2201 gdb_assert (IS_DIR_SEPARATOR (filename[dwz_pos]));
2202
2203 gdb_bfd_ref_ptr dwz_bfd;
2204 std::vector<gdb::unique_xmalloc_ptr<char>> debugdir_vec
2205 = dirnames_to_char_ptr_vec (debug_file_directory);
2206
2207 for (const gdb::unique_xmalloc_ptr<char> &debugdir : debugdir_vec)
2208 {
2209 /* The idea is to iterate over the
2210 debug file directories provided by the user and
2211 replace the hard-coded path in the "filename" by each
2212 debug-file-directory.
2213
2214 For example, suppose that filename is:
2215
2216 /usr/lib/debug/.dwz/foo.dwz
2217
2218 And suppose that we have "$HOME/bar" as the
2219 debug-file-directory. We would then adjust filename
2220 to look like:
2221
2222 $HOME/bar/.dwz/foo.dwz
2223
2224 which would hopefully allow us to find the alt debug
2225 file. */
2226 std::string ddir = debugdir.get ();
2227
2228 if (ddir.empty ())
2229 continue;
2230
2231 /* Make sure the current debug-file-directory ends with a
2232 directory separator. This is needed because, if FILENAME
2233 contains something like "/usr/lib/abcde/.dwz/foo.dwz" and
2234 DDIR is "/usr/lib/abc", then could wrongfully skip it
2235 below. */
2236 if (!IS_DIR_SEPARATOR (ddir.back ()))
2237 ddir += SLASH_STRING;
2238
2239 /* Check whether the beginning of FILENAME is DDIR. If it is,
2240 then we are dealing with a file which we already attempted to
2241 open before, so we just skip it and continue processing the
2242 remaining debug file directories. */
2243 if (filename.size () > ddir.size ()
2244 && filename.compare (0, ddir.size (), ddir) == 0)
2245 continue;
2246
2247 /* Replace FILENAME's default debug-file-directory with
2248 DDIR. */
2249 std::string new_filename = ddir + &filename[dwz_pos + 1];
2250
2251 dwz_bfd = gdb_bfd_open (new_filename.c_str (), gnutarget);
2252
2253 if (dwz_bfd == nullptr)
2254 continue;
2255
2256 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2257 {
2258 dwz_bfd.reset (nullptr);
2259 continue;
2260 }
2261
2262 /* Found it. */
2263 break;
2264 }
2265
2266 return dwz_bfd;
2267 }
2268
2269 /* See dwarf2read.h. */
2270
2271 struct dwz_file *
2272 dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
2273 {
2274 bfd_size_type buildid_len_arg;
2275 size_t buildid_len;
2276 bfd_byte *buildid;
2277
2278 if (per_bfd->dwz_file != NULL)
2279 return per_bfd->dwz_file.get ();
2280
2281 bfd_set_error (bfd_error_no_error);
2282 gdb::unique_xmalloc_ptr<char> data
2283 (bfd_get_alt_debug_link_info (per_bfd->obfd,
2284 &buildid_len_arg, &buildid));
2285 if (data == NULL)
2286 {
2287 if (bfd_get_error () == bfd_error_no_error)
2288 return NULL;
2289 error (_("could not read '.gnu_debugaltlink' section: %s"),
2290 bfd_errmsg (bfd_get_error ()));
2291 }
2292
2293 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2294
2295 buildid_len = (size_t) buildid_len_arg;
2296
2297 std::string filename = data.get ();
2298
2299 if (!IS_ABSOLUTE_PATH (filename.c_str ()))
2300 {
2301 gdb::unique_xmalloc_ptr<char> abs
2302 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
2303
2304 filename = ldirname (abs.get ()) + SLASH_STRING + filename;
2305 }
2306
2307 /* First try the file name given in the section. If that doesn't
2308 work, try to use the build-id instead. */
2309 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename.c_str (), gnutarget));
2310 if (dwz_bfd != NULL)
2311 {
2312 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2313 dwz_bfd.reset (nullptr);
2314 }
2315
2316 if (dwz_bfd == NULL)
2317 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2318
2319 if (dwz_bfd == nullptr)
2320 {
2321 /* If the user has provided us with different
2322 debug file directories, we can try them in order. */
2323 dwz_bfd = dwz_search_other_debugdirs (filename, buildid, buildid_len);
2324 }
2325
2326 if (dwz_bfd == nullptr)
2327 {
2328 gdb::unique_xmalloc_ptr<char> alt_filename;
2329 const char *origname = bfd_get_filename (per_bfd->obfd);
2330
2331 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2332 buildid_len,
2333 origname,
2334 &alt_filename));
2335
2336 if (fd.get () >= 0)
2337 {
2338 /* File successfully retrieved from server. */
2339 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
2340
2341 if (dwz_bfd == nullptr)
2342 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2343 alt_filename.get ());
2344 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2345 dwz_bfd.reset (nullptr);
2346 }
2347 }
2348
2349 if (dwz_bfd == NULL)
2350 error (_("could not find '.gnu_debugaltlink' file for %s"),
2351 bfd_get_filename (per_bfd->obfd));
2352
2353 std::unique_ptr<struct dwz_file> result
2354 (new struct dwz_file (std::move (dwz_bfd)));
2355
2356 for (asection *sec : gdb_bfd_sections (result->dwz_bfd))
2357 locate_dwz_sections (result->dwz_bfd.get (), sec, result.get ());
2358
2359 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2360 per_bfd->dwz_file = std::move (result);
2361 return per_bfd->dwz_file.get ();
2362 }
2363 \f
2364 /* DWARF quick_symbols_functions support. */
2365
2366 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2367 unique line tables, so we maintain a separate table of all .debug_line
2368 derived entries to support the sharing.
2369 All the quick functions need is the list of file names. We discard the
2370 line_header when we're done and don't need to record it here. */
2371 struct quick_file_names
2372 {
2373 /* The data used to construct the hash key. */
2374 struct stmt_list_hash hash;
2375
2376 /* The number of entries in file_names, real_names. */
2377 unsigned int num_file_names;
2378
2379 /* The file names from the line table, after being run through
2380 file_full_name. */
2381 const char **file_names;
2382
2383 /* The file names from the line table after being run through
2384 gdb_realpath. These are computed lazily. */
2385 const char **real_names;
2386 };
2387
2388 /* When using the index (and thus not using psymtabs), each CU has an
2389 object of this type. This is used to hold information needed by
2390 the various "quick" methods. */
2391 struct dwarf2_per_cu_quick_data
2392 {
2393 /* The file table. This can be NULL if there was no file table
2394 or it's currently not read in.
2395 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2396 struct quick_file_names *file_names;
2397
2398 /* A temporary mark bit used when iterating over all CUs in
2399 expand_symtabs_matching. */
2400 unsigned int mark : 1;
2401
2402 /* True if we've tried to read the file table and found there isn't one.
2403 There will be no point in trying to read it again next time. */
2404 unsigned int no_file_data : 1;
2405 };
2406
2407 /* Utility hash function for a stmt_list_hash. */
2408
2409 static hashval_t
2410 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2411 {
2412 hashval_t v = 0;
2413
2414 if (stmt_list_hash->dwo_unit != NULL)
2415 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2416 v += to_underlying (stmt_list_hash->line_sect_off);
2417 return v;
2418 }
2419
2420 /* Utility equality function for a stmt_list_hash. */
2421
2422 static int
2423 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2424 const struct stmt_list_hash *rhs)
2425 {
2426 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2427 return 0;
2428 if (lhs->dwo_unit != NULL
2429 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2430 return 0;
2431
2432 return lhs->line_sect_off == rhs->line_sect_off;
2433 }
2434
2435 /* Hash function for a quick_file_names. */
2436
2437 static hashval_t
2438 hash_file_name_entry (const void *e)
2439 {
2440 const struct quick_file_names *file_data
2441 = (const struct quick_file_names *) e;
2442
2443 return hash_stmt_list_entry (&file_data->hash);
2444 }
2445
2446 /* Equality function for a quick_file_names. */
2447
2448 static int
2449 eq_file_name_entry (const void *a, const void *b)
2450 {
2451 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2452 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2453
2454 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2455 }
2456
2457 /* Delete function for a quick_file_names. */
2458
2459 static void
2460 delete_file_name_entry (void *e)
2461 {
2462 struct quick_file_names *file_data = (struct quick_file_names *) e;
2463 int i;
2464
2465 for (i = 0; i < file_data->num_file_names; ++i)
2466 {
2467 xfree ((void*) file_data->file_names[i]);
2468 if (file_data->real_names)
2469 xfree ((void*) file_data->real_names[i]);
2470 }
2471
2472 /* The space for the struct itself lives on the obstack, so we don't
2473 free it here. */
2474 }
2475
2476 /* Create a quick_file_names hash table. */
2477
2478 static htab_up
2479 create_quick_file_names_table (unsigned int nr_initial_entries)
2480 {
2481 return htab_up (htab_create_alloc (nr_initial_entries,
2482 hash_file_name_entry, eq_file_name_entry,
2483 delete_file_name_entry, xcalloc, xfree));
2484 }
2485
2486 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2487 function is unrelated to symtabs, symtab would have to be created afterwards.
2488 You should call age_cached_comp_units after processing the CU. */
2489
2490 static dwarf2_cu *
2491 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2492 bool skip_partial)
2493 {
2494 if (per_cu->is_debug_types)
2495 load_full_type_unit (per_cu, per_objfile);
2496 else
2497 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2498 skip_partial, language_minimal);
2499
2500 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2501 if (cu == nullptr)
2502 return nullptr; /* Dummy CU. */
2503
2504 dwarf2_find_base_address (cu->dies, cu);
2505
2506 return cu;
2507 }
2508
2509 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2510
2511 static void
2512 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2513 dwarf2_per_objfile *per_objfile, bool skip_partial)
2514 {
2515 /* Skip type_unit_groups, reading the type units they contain
2516 is handled elsewhere. */
2517 if (per_cu->type_unit_group_p ())
2518 return;
2519
2520 /* The destructor of dwarf2_queue_guard frees any entries left on
2521 the queue. After this point we're guaranteed to leave this function
2522 with the dwarf queue empty. */
2523 dwarf2_queue_guard q_guard (per_objfile);
2524
2525 if (!per_objfile->symtab_set_p (per_cu))
2526 {
2527 queue_comp_unit (per_cu, per_objfile, language_minimal);
2528 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2529
2530 /* If we just loaded a CU from a DWO, and we're working with an index
2531 that may badly handle TUs, load all the TUs in that DWO as well.
2532 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2533 if (!per_cu->is_debug_types
2534 && cu != NULL
2535 && cu->dwo_unit != NULL
2536 && per_objfile->per_bfd->index_table != NULL
2537 && per_objfile->per_bfd->index_table->version <= 7
2538 /* DWP files aren't supported yet. */
2539 && get_dwp_file (per_objfile) == NULL)
2540 queue_and_load_all_dwo_tus (cu);
2541 }
2542
2543 process_queue (per_objfile);
2544
2545 /* Age the cache, releasing compilation units that have not
2546 been used recently. */
2547 per_objfile->age_comp_units ();
2548 }
2549
2550 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2551 the per-objfile for which this symtab is instantiated.
2552
2553 Returns the resulting symbol table. */
2554
2555 static struct compunit_symtab *
2556 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2557 dwarf2_per_objfile *per_objfile,
2558 bool skip_partial)
2559 {
2560 gdb_assert (per_objfile->per_bfd->using_index);
2561
2562 if (!per_objfile->symtab_set_p (per_cu))
2563 {
2564 free_cached_comp_units freer (per_objfile);
2565 scoped_restore decrementer = increment_reading_symtab ();
2566 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2567 process_cu_includes (per_objfile);
2568 }
2569
2570 return per_objfile->get_symtab (per_cu);
2571 }
2572
2573 /* See declaration. */
2574
2575 dwarf2_per_cu_data *
2576 dwarf2_per_bfd::get_cutu (int index)
2577 {
2578 if (index >= this->all_comp_units.size ())
2579 {
2580 index -= this->all_comp_units.size ();
2581 gdb_assert (index < this->all_type_units.size ());
2582 return &this->all_type_units[index]->per_cu;
2583 }
2584
2585 return this->all_comp_units[index];
2586 }
2587
2588 /* See declaration. */
2589
2590 dwarf2_per_cu_data *
2591 dwarf2_per_bfd::get_cu (int index)
2592 {
2593 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2594
2595 return this->all_comp_units[index];
2596 }
2597
2598 /* See declaration. */
2599
2600 signatured_type *
2601 dwarf2_per_bfd::get_tu (int index)
2602 {
2603 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2604
2605 return this->all_type_units[index];
2606 }
2607
2608 /* See read.h. */
2609
2610 dwarf2_per_cu_data *
2611 dwarf2_per_bfd::allocate_per_cu ()
2612 {
2613 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2614 result->per_bfd = this;
2615 result->index = m_num_psymtabs++;
2616 return result;
2617 }
2618
2619 /* See read.h. */
2620
2621 signatured_type *
2622 dwarf2_per_bfd::allocate_signatured_type ()
2623 {
2624 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2625 result->per_cu.per_bfd = this;
2626 result->per_cu.index = m_num_psymtabs++;
2627 return result;
2628 }
2629
2630 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2631 obstack, and constructed with the specified field values. */
2632
2633 static dwarf2_per_cu_data *
2634 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2635 struct dwarf2_section_info *section,
2636 int is_dwz,
2637 sect_offset sect_off, ULONGEST length)
2638 {
2639 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
2640 the_cu->sect_off = sect_off;
2641 the_cu->length = length;
2642 the_cu->section = section;
2643 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2644 struct dwarf2_per_cu_quick_data);
2645 the_cu->is_dwz = is_dwz;
2646 return the_cu;
2647 }
2648
2649 /* A helper for create_cus_from_index that handles a given list of
2650 CUs. */
2651
2652 static void
2653 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2654 const gdb_byte *cu_list, offset_type n_elements,
2655 struct dwarf2_section_info *section,
2656 int is_dwz)
2657 {
2658 for (offset_type i = 0; i < n_elements; i += 2)
2659 {
2660 gdb_static_assert (sizeof (ULONGEST) >= 8);
2661
2662 sect_offset sect_off
2663 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2664 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2665 cu_list += 2 * 8;
2666
2667 dwarf2_per_cu_data *per_cu
2668 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2669 length);
2670 per_bfd->all_comp_units.push_back (per_cu);
2671 }
2672 }
2673
2674 /* Read the CU list from the mapped index, and use it to create all
2675 the CU objects for PER_BFD. */
2676
2677 static void
2678 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2679 const gdb_byte *cu_list, offset_type cu_list_elements,
2680 const gdb_byte *dwz_list, offset_type dwz_elements)
2681 {
2682 gdb_assert (per_bfd->all_comp_units.empty ());
2683 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2684
2685 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2686 &per_bfd->info, 0);
2687
2688 if (dwz_elements == 0)
2689 return;
2690
2691 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2692 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2693 &dwz->info, 1);
2694 }
2695
2696 /* Create the signatured type hash table from the index. */
2697
2698 static void
2699 create_signatured_type_table_from_index
2700 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2701 const gdb_byte *bytes, offset_type elements)
2702 {
2703 gdb_assert (per_bfd->all_type_units.empty ());
2704 per_bfd->all_type_units.reserve (elements / 3);
2705
2706 htab_up sig_types_hash = allocate_signatured_type_table ();
2707
2708 for (offset_type i = 0; i < elements; i += 3)
2709 {
2710 struct signatured_type *sig_type;
2711 ULONGEST signature;
2712 void **slot;
2713 cu_offset type_offset_in_tu;
2714
2715 gdb_static_assert (sizeof (ULONGEST) >= 8);
2716 sect_offset sect_off
2717 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2718 type_offset_in_tu
2719 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2720 BFD_ENDIAN_LITTLE);
2721 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2722 bytes += 3 * 8;
2723
2724 sig_type = per_bfd->allocate_signatured_type ();
2725 sig_type->signature = signature;
2726 sig_type->type_offset_in_tu = type_offset_in_tu;
2727 sig_type->per_cu.is_debug_types = 1;
2728 sig_type->per_cu.section = section;
2729 sig_type->per_cu.sect_off = sect_off;
2730 sig_type->per_cu.v.quick
2731 = OBSTACK_ZALLOC (&per_bfd->obstack,
2732 struct dwarf2_per_cu_quick_data);
2733
2734 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2735 *slot = sig_type;
2736
2737 per_bfd->all_type_units.push_back (sig_type);
2738 }
2739
2740 per_bfd->signatured_types = std::move (sig_types_hash);
2741 }
2742
2743 /* Create the signatured type hash table from .debug_names. */
2744
2745 static void
2746 create_signatured_type_table_from_debug_names
2747 (dwarf2_per_objfile *per_objfile,
2748 const mapped_debug_names &map,
2749 struct dwarf2_section_info *section,
2750 struct dwarf2_section_info *abbrev_section)
2751 {
2752 struct objfile *objfile = per_objfile->objfile;
2753
2754 section->read (objfile);
2755 abbrev_section->read (objfile);
2756
2757 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2758 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2759
2760 htab_up sig_types_hash = allocate_signatured_type_table ();
2761
2762 for (uint32_t i = 0; i < map.tu_count; ++i)
2763 {
2764 struct signatured_type *sig_type;
2765 void **slot;
2766
2767 sect_offset sect_off
2768 = (sect_offset) (extract_unsigned_integer
2769 (map.tu_table_reordered + i * map.offset_size,
2770 map.offset_size,
2771 map.dwarf5_byte_order));
2772
2773 comp_unit_head cu_header;
2774 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2775 abbrev_section,
2776 section->buffer + to_underlying (sect_off),
2777 rcuh_kind::TYPE);
2778
2779 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
2780 sig_type->signature = cu_header.signature;
2781 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2782 sig_type->per_cu.is_debug_types = 1;
2783 sig_type->per_cu.section = section;
2784 sig_type->per_cu.sect_off = sect_off;
2785 sig_type->per_cu.v.quick
2786 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2787 struct dwarf2_per_cu_quick_data);
2788
2789 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2790 *slot = sig_type;
2791
2792 per_objfile->per_bfd->all_type_units.push_back (sig_type);
2793 }
2794
2795 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2796 }
2797
2798 /* Read the address map data from the mapped index, and use it to
2799 populate the objfile's psymtabs_addrmap. */
2800
2801 static void
2802 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2803 struct mapped_index *index)
2804 {
2805 struct objfile *objfile = per_objfile->objfile;
2806 struct gdbarch *gdbarch = objfile->arch ();
2807 const gdb_byte *iter, *end;
2808 struct addrmap *mutable_map;
2809 CORE_ADDR baseaddr;
2810
2811 auto_obstack temp_obstack;
2812
2813 mutable_map = addrmap_create_mutable (&temp_obstack);
2814
2815 iter = index->address_table.data ();
2816 end = iter + index->address_table.size ();
2817
2818 baseaddr = objfile->text_section_offset ();
2819
2820 while (iter < end)
2821 {
2822 ULONGEST hi, lo, cu_index;
2823 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2824 iter += 8;
2825 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2826 iter += 8;
2827 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2828 iter += 4;
2829
2830 if (lo > hi)
2831 {
2832 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2833 hex_string (lo), hex_string (hi));
2834 continue;
2835 }
2836
2837 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
2838 {
2839 complaint (_(".gdb_index address table has invalid CU number %u"),
2840 (unsigned) cu_index);
2841 continue;
2842 }
2843
2844 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2845 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2846 addrmap_set_empty (mutable_map, lo, hi - 1,
2847 per_objfile->per_bfd->get_cu (cu_index));
2848 }
2849
2850 objfile->partial_symtabs->psymtabs_addrmap
2851 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2852 }
2853
2854 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2855 populate the objfile's psymtabs_addrmap. */
2856
2857 static void
2858 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2859 struct dwarf2_section_info *section)
2860 {
2861 struct objfile *objfile = per_objfile->objfile;
2862 bfd *abfd = objfile->obfd;
2863 struct gdbarch *gdbarch = objfile->arch ();
2864 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2865
2866 auto_obstack temp_obstack;
2867 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2868
2869 std::unordered_map<sect_offset,
2870 dwarf2_per_cu_data *,
2871 gdb::hash_enum<sect_offset>>
2872 debug_info_offset_to_per_cu;
2873 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
2874 {
2875 const auto insertpair
2876 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2877 if (!insertpair.second)
2878 {
2879 warning (_("Section .debug_aranges in %s has duplicate "
2880 "debug_info_offset %s, ignoring .debug_aranges."),
2881 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2882 return;
2883 }
2884 }
2885
2886 section->read (objfile);
2887
2888 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2889
2890 const gdb_byte *addr = section->buffer;
2891
2892 while (addr < section->buffer + section->size)
2893 {
2894 const gdb_byte *const entry_addr = addr;
2895 unsigned int bytes_read;
2896
2897 const LONGEST entry_length = read_initial_length (abfd, addr,
2898 &bytes_read);
2899 addr += bytes_read;
2900
2901 const gdb_byte *const entry_end = addr + entry_length;
2902 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2903 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2904 if (addr + entry_length > section->buffer + section->size)
2905 {
2906 warning (_("Section .debug_aranges in %s entry at offset %s "
2907 "length %s exceeds section length %s, "
2908 "ignoring .debug_aranges."),
2909 objfile_name (objfile),
2910 plongest (entry_addr - section->buffer),
2911 plongest (bytes_read + entry_length),
2912 pulongest (section->size));
2913 return;
2914 }
2915
2916 /* The version number. */
2917 const uint16_t version = read_2_bytes (abfd, addr);
2918 addr += 2;
2919 if (version != 2)
2920 {
2921 warning (_("Section .debug_aranges in %s entry at offset %s "
2922 "has unsupported version %d, ignoring .debug_aranges."),
2923 objfile_name (objfile),
2924 plongest (entry_addr - section->buffer), version);
2925 return;
2926 }
2927
2928 const uint64_t debug_info_offset
2929 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2930 addr += offset_size;
2931 const auto per_cu_it
2932 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2933 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2934 {
2935 warning (_("Section .debug_aranges in %s entry at offset %s "
2936 "debug_info_offset %s does not exists, "
2937 "ignoring .debug_aranges."),
2938 objfile_name (objfile),
2939 plongest (entry_addr - section->buffer),
2940 pulongest (debug_info_offset));
2941 return;
2942 }
2943 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2944
2945 const uint8_t address_size = *addr++;
2946 if (address_size < 1 || address_size > 8)
2947 {
2948 warning (_("Section .debug_aranges in %s entry at offset %s "
2949 "address_size %u is invalid, ignoring .debug_aranges."),
2950 objfile_name (objfile),
2951 plongest (entry_addr - section->buffer), address_size);
2952 return;
2953 }
2954
2955 const uint8_t segment_selector_size = *addr++;
2956 if (segment_selector_size != 0)
2957 {
2958 warning (_("Section .debug_aranges in %s entry at offset %s "
2959 "segment_selector_size %u is not supported, "
2960 "ignoring .debug_aranges."),
2961 objfile_name (objfile),
2962 plongest (entry_addr - section->buffer),
2963 segment_selector_size);
2964 return;
2965 }
2966
2967 /* Must pad to an alignment boundary that is twice the address
2968 size. It is undocumented by the DWARF standard but GCC does
2969 use it. */
2970 for (size_t padding = ((-(addr - section->buffer))
2971 & (2 * address_size - 1));
2972 padding > 0; padding--)
2973 if (*addr++ != 0)
2974 {
2975 warning (_("Section .debug_aranges in %s entry at offset %s "
2976 "padding is not zero, ignoring .debug_aranges."),
2977 objfile_name (objfile),
2978 plongest (entry_addr - section->buffer));
2979 return;
2980 }
2981
2982 for (;;)
2983 {
2984 if (addr + 2 * address_size > entry_end)
2985 {
2986 warning (_("Section .debug_aranges in %s entry at offset %s "
2987 "address list is not properly terminated, "
2988 "ignoring .debug_aranges."),
2989 objfile_name (objfile),
2990 plongest (entry_addr - section->buffer));
2991 return;
2992 }
2993 ULONGEST start = extract_unsigned_integer (addr, address_size,
2994 dwarf5_byte_order);
2995 addr += address_size;
2996 ULONGEST length = extract_unsigned_integer (addr, address_size,
2997 dwarf5_byte_order);
2998 addr += address_size;
2999 if (start == 0 && length == 0)
3000 break;
3001 if (start == 0 && !per_objfile->per_bfd->has_section_at_zero)
3002 {
3003 /* Symbol was eliminated due to a COMDAT group. */
3004 continue;
3005 }
3006 ULONGEST end = start + length;
3007 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3008 - baseaddr);
3009 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3010 - baseaddr);
3011 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3012 }
3013 }
3014
3015 objfile->partial_symtabs->psymtabs_addrmap
3016 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3017 }
3018
3019 /* Find a slot in the mapped index INDEX for the object named NAME.
3020 If NAME is found, set *VEC_OUT to point to the CU vector in the
3021 constant pool and return true. If NAME cannot be found, return
3022 false. */
3023
3024 static bool
3025 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3026 offset_type **vec_out)
3027 {
3028 offset_type hash;
3029 offset_type slot, step;
3030 int (*cmp) (const char *, const char *);
3031
3032 gdb::unique_xmalloc_ptr<char> without_params;
3033 if (current_language->la_language == language_cplus
3034 || current_language->la_language == language_fortran
3035 || current_language->la_language == language_d)
3036 {
3037 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3038 not contain any. */
3039
3040 if (strchr (name, '(') != NULL)
3041 {
3042 without_params = cp_remove_params (name);
3043
3044 if (without_params != NULL)
3045 name = without_params.get ();
3046 }
3047 }
3048
3049 /* Index version 4 did not support case insensitive searches. But the
3050 indices for case insensitive languages are built in lowercase, therefore
3051 simulate our NAME being searched is also lowercased. */
3052 hash = mapped_index_string_hash ((index->version == 4
3053 && case_sensitivity == case_sensitive_off
3054 ? 5 : index->version),
3055 name);
3056
3057 slot = hash & (index->symbol_table.size () - 1);
3058 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3059 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3060
3061 for (;;)
3062 {
3063 const char *str;
3064
3065 const auto &bucket = index->symbol_table[slot];
3066 if (bucket.name == 0 && bucket.vec == 0)
3067 return false;
3068
3069 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3070 if (!cmp (name, str))
3071 {
3072 *vec_out = (offset_type *) (index->constant_pool
3073 + MAYBE_SWAP (bucket.vec));
3074 return true;
3075 }
3076
3077 slot = (slot + step) & (index->symbol_table.size () - 1);
3078 }
3079 }
3080
3081 /* A helper function that reads the .gdb_index from BUFFER and fills
3082 in MAP. FILENAME is the name of the file containing the data;
3083 it is used for error reporting. DEPRECATED_OK is true if it is
3084 ok to use deprecated sections.
3085
3086 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3087 out parameters that are filled in with information about the CU and
3088 TU lists in the section.
3089
3090 Returns true if all went well, false otherwise. */
3091
3092 static bool
3093 read_gdb_index_from_buffer (const char *filename,
3094 bool deprecated_ok,
3095 gdb::array_view<const gdb_byte> buffer,
3096 struct mapped_index *map,
3097 const gdb_byte **cu_list,
3098 offset_type *cu_list_elements,
3099 const gdb_byte **types_list,
3100 offset_type *types_list_elements)
3101 {
3102 const gdb_byte *addr = &buffer[0];
3103
3104 /* Version check. */
3105 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3106 /* Versions earlier than 3 emitted every copy of a psymbol. This
3107 causes the index to behave very poorly for certain requests. Version 3
3108 contained incomplete addrmap. So, it seems better to just ignore such
3109 indices. */
3110 if (version < 4)
3111 {
3112 static int warning_printed = 0;
3113 if (!warning_printed)
3114 {
3115 warning (_("Skipping obsolete .gdb_index section in %s."),
3116 filename);
3117 warning_printed = 1;
3118 }
3119 return 0;
3120 }
3121 /* Index version 4 uses a different hash function than index version
3122 5 and later.
3123
3124 Versions earlier than 6 did not emit psymbols for inlined
3125 functions. Using these files will cause GDB not to be able to
3126 set breakpoints on inlined functions by name, so we ignore these
3127 indices unless the user has done
3128 "set use-deprecated-index-sections on". */
3129 if (version < 6 && !deprecated_ok)
3130 {
3131 static int warning_printed = 0;
3132 if (!warning_printed)
3133 {
3134 warning (_("\
3135 Skipping deprecated .gdb_index section in %s.\n\
3136 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3137 to use the section anyway."),
3138 filename);
3139 warning_printed = 1;
3140 }
3141 return 0;
3142 }
3143 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3144 of the TU (for symbols coming from TUs),
3145 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3146 Plus gold-generated indices can have duplicate entries for global symbols,
3147 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3148 These are just performance bugs, and we can't distinguish gdb-generated
3149 indices from gold-generated ones, so issue no warning here. */
3150
3151 /* Indexes with higher version than the one supported by GDB may be no
3152 longer backward compatible. */
3153 if (version > 8)
3154 return 0;
3155
3156 map->version = version;
3157
3158 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3159
3160 int i = 0;
3161 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3162 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3163 / 8);
3164 ++i;
3165
3166 *types_list = addr + MAYBE_SWAP (metadata[i]);
3167 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3168 - MAYBE_SWAP (metadata[i]))
3169 / 8);
3170 ++i;
3171
3172 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3173 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3174 map->address_table
3175 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3176 ++i;
3177
3178 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3179 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3180 map->symbol_table
3181 = gdb::array_view<mapped_index::symbol_table_slot>
3182 ((mapped_index::symbol_table_slot *) symbol_table,
3183 (mapped_index::symbol_table_slot *) symbol_table_end);
3184
3185 ++i;
3186 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3187
3188 return 1;
3189 }
3190
3191 /* Callback types for dwarf2_read_gdb_index. */
3192
3193 typedef gdb::function_view
3194 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3195 get_gdb_index_contents_ftype;
3196 typedef gdb::function_view
3197 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3198 get_gdb_index_contents_dwz_ftype;
3199
3200 /* Read .gdb_index. If everything went ok, initialize the "quick"
3201 elements of all the CUs and return 1. Otherwise, return 0. */
3202
3203 static int
3204 dwarf2_read_gdb_index
3205 (dwarf2_per_objfile *per_objfile,
3206 get_gdb_index_contents_ftype get_gdb_index_contents,
3207 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3208 {
3209 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3210 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3211 struct dwz_file *dwz;
3212 struct objfile *objfile = per_objfile->objfile;
3213 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3214
3215 gdb::array_view<const gdb_byte> main_index_contents
3216 = get_gdb_index_contents (objfile, per_bfd);
3217
3218 if (main_index_contents.empty ())
3219 return 0;
3220
3221 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3222 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3223 use_deprecated_index_sections,
3224 main_index_contents, map.get (), &cu_list,
3225 &cu_list_elements, &types_list,
3226 &types_list_elements))
3227 return 0;
3228
3229 /* Don't use the index if it's empty. */
3230 if (map->symbol_table.empty ())
3231 return 0;
3232
3233 /* If there is a .dwz file, read it so we can get its CU list as
3234 well. */
3235 dwz = dwarf2_get_dwz_file (per_bfd);
3236 if (dwz != NULL)
3237 {
3238 struct mapped_index dwz_map;
3239 const gdb_byte *dwz_types_ignore;
3240 offset_type dwz_types_elements_ignore;
3241
3242 gdb::array_view<const gdb_byte> dwz_index_content
3243 = get_gdb_index_contents_dwz (objfile, dwz);
3244
3245 if (dwz_index_content.empty ())
3246 return 0;
3247
3248 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3249 1, dwz_index_content, &dwz_map,
3250 &dwz_list, &dwz_list_elements,
3251 &dwz_types_ignore,
3252 &dwz_types_elements_ignore))
3253 {
3254 warning (_("could not read '.gdb_index' section from %s; skipping"),
3255 bfd_get_filename (dwz->dwz_bfd.get ()));
3256 return 0;
3257 }
3258 }
3259
3260 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3261 dwz_list_elements);
3262
3263 if (types_list_elements)
3264 {
3265 /* We can only handle a single .debug_types when we have an
3266 index. */
3267 if (per_bfd->types.size () != 1)
3268 return 0;
3269
3270 dwarf2_section_info *section = &per_bfd->types[0];
3271
3272 create_signatured_type_table_from_index (per_bfd, section, types_list,
3273 types_list_elements);
3274 }
3275
3276 create_addrmap_from_index (per_objfile, map.get ());
3277
3278 per_bfd->index_table = std::move (map);
3279 per_bfd->using_index = 1;
3280 per_bfd->quick_file_names_table =
3281 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3282
3283 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
3284 objfiles using the same BFD. */
3285 gdb_assert (per_bfd->partial_symtabs == nullptr);
3286 per_bfd->partial_symtabs = objfile->partial_symtabs;
3287
3288 return 1;
3289 }
3290
3291 /* die_reader_func for dw2_get_file_names. */
3292
3293 static void
3294 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3295 const gdb_byte *info_ptr,
3296 struct die_info *comp_unit_die)
3297 {
3298 struct dwarf2_cu *cu = reader->cu;
3299 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3300 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3301 struct dwarf2_per_cu_data *lh_cu;
3302 struct attribute *attr;
3303 void **slot;
3304 struct quick_file_names *qfn;
3305
3306 gdb_assert (! this_cu->is_debug_types);
3307
3308 /* Our callers never want to match partial units -- instead they
3309 will match the enclosing full CU. */
3310 if (comp_unit_die->tag == DW_TAG_partial_unit)
3311 {
3312 this_cu->v.quick->no_file_data = 1;
3313 return;
3314 }
3315
3316 lh_cu = this_cu;
3317 slot = NULL;
3318
3319 line_header_up lh;
3320 sect_offset line_offset {};
3321
3322 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3323 if (attr != nullptr && attr->form_is_unsigned ())
3324 {
3325 struct quick_file_names find_entry;
3326
3327 line_offset = (sect_offset) attr->as_unsigned ();
3328
3329 /* We may have already read in this line header (TU line header sharing).
3330 If we have we're done. */
3331 find_entry.hash.dwo_unit = cu->dwo_unit;
3332 find_entry.hash.line_sect_off = line_offset;
3333 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
3334 &find_entry, INSERT);
3335 if (*slot != NULL)
3336 {
3337 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3338 return;
3339 }
3340
3341 lh = dwarf_decode_line_header (line_offset, cu);
3342 }
3343 if (lh == NULL)
3344 {
3345 lh_cu->v.quick->no_file_data = 1;
3346 return;
3347 }
3348
3349 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3350 qfn->hash.dwo_unit = cu->dwo_unit;
3351 qfn->hash.line_sect_off = line_offset;
3352 gdb_assert (slot != NULL);
3353 *slot = qfn;
3354
3355 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3356
3357 int offset = 0;
3358 if (strcmp (fnd.name, "<unknown>") != 0)
3359 ++offset;
3360
3361 qfn->num_file_names = offset + lh->file_names_size ();
3362 qfn->file_names =
3363 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3364 qfn->num_file_names);
3365 if (offset != 0)
3366 qfn->file_names[0] = xstrdup (fnd.name);
3367 for (int i = 0; i < lh->file_names_size (); ++i)
3368 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3369 fnd.comp_dir).release ();
3370 qfn->real_names = NULL;
3371
3372 lh_cu->v.quick->file_names = qfn;
3373 }
3374
3375 /* A helper for the "quick" functions which attempts to read the line
3376 table for THIS_CU. */
3377
3378 static struct quick_file_names *
3379 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3380 dwarf2_per_objfile *per_objfile)
3381 {
3382 /* This should never be called for TUs. */
3383 gdb_assert (! this_cu->is_debug_types);
3384 /* Nor type unit groups. */
3385 gdb_assert (! this_cu->type_unit_group_p ());
3386
3387 if (this_cu->v.quick->file_names != NULL)
3388 return this_cu->v.quick->file_names;
3389 /* If we know there is no line data, no point in looking again. */
3390 if (this_cu->v.quick->no_file_data)
3391 return NULL;
3392
3393 cutu_reader reader (this_cu, per_objfile);
3394 if (!reader.dummy_p)
3395 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3396
3397 if (this_cu->v.quick->no_file_data)
3398 return NULL;
3399 return this_cu->v.quick->file_names;
3400 }
3401
3402 /* A helper for the "quick" functions which computes and caches the
3403 real path for a given file name from the line table. */
3404
3405 static const char *
3406 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3407 struct quick_file_names *qfn, int index)
3408 {
3409 if (qfn->real_names == NULL)
3410 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3411 qfn->num_file_names, const char *);
3412
3413 if (qfn->real_names[index] == NULL)
3414 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3415
3416 return qfn->real_names[index];
3417 }
3418
3419 static struct symtab *
3420 dw2_find_last_source_symtab (struct objfile *objfile)
3421 {
3422 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3423 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3424 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3425
3426 if (cust == NULL)
3427 return NULL;
3428
3429 return compunit_primary_filetab (cust);
3430 }
3431
3432 /* Traversal function for dw2_forget_cached_source_info. */
3433
3434 static int
3435 dw2_free_cached_file_names (void **slot, void *info)
3436 {
3437 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3438
3439 if (file_data->real_names)
3440 {
3441 int i;
3442
3443 for (i = 0; i < file_data->num_file_names; ++i)
3444 {
3445 xfree ((void*) file_data->real_names[i]);
3446 file_data->real_names[i] = NULL;
3447 }
3448 }
3449
3450 return 1;
3451 }
3452
3453 static void
3454 dw2_forget_cached_source_info (struct objfile *objfile)
3455 {
3456 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3457
3458 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
3459 dw2_free_cached_file_names, NULL);
3460 }
3461
3462 /* Helper function for dw2_map_symtabs_matching_filename that expands
3463 the symtabs and calls the iterator. */
3464
3465 static int
3466 dw2_map_expand_apply (struct objfile *objfile,
3467 struct dwarf2_per_cu_data *per_cu,
3468 const char *name, const char *real_path,
3469 gdb::function_view<bool (symtab *)> callback)
3470 {
3471 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3472
3473 /* Don't visit already-expanded CUs. */
3474 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3475 if (per_objfile->symtab_set_p (per_cu))
3476 return 0;
3477
3478 /* This may expand more than one symtab, and we want to iterate over
3479 all of them. */
3480 dw2_instantiate_symtab (per_cu, per_objfile, false);
3481
3482 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3483 last_made, callback);
3484 }
3485
3486 /* Implementation of the map_symtabs_matching_filename method. */
3487
3488 static bool
3489 dw2_map_symtabs_matching_filename
3490 (struct objfile *objfile, const char *name, const char *real_path,
3491 gdb::function_view<bool (symtab *)> callback)
3492 {
3493 const char *name_basename = lbasename (name);
3494 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3495
3496 /* The rule is CUs specify all the files, including those used by
3497 any TU, so there's no need to scan TUs here. */
3498
3499 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3500 {
3501 /* We only need to look at symtabs not already expanded. */
3502 if (per_objfile->symtab_set_p (per_cu))
3503 continue;
3504
3505 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3506 if (file_data == NULL)
3507 continue;
3508
3509 for (int j = 0; j < file_data->num_file_names; ++j)
3510 {
3511 const char *this_name = file_data->file_names[j];
3512 const char *this_real_name;
3513
3514 if (compare_filenames_for_search (this_name, name))
3515 {
3516 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3517 callback))
3518 return true;
3519 continue;
3520 }
3521
3522 /* Before we invoke realpath, which can get expensive when many
3523 files are involved, do a quick comparison of the basenames. */
3524 if (! basenames_may_differ
3525 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3526 continue;
3527
3528 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
3529 if (compare_filenames_for_search (this_real_name, name))
3530 {
3531 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3532 callback))
3533 return true;
3534 continue;
3535 }
3536
3537 if (real_path != NULL)
3538 {
3539 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3540 gdb_assert (IS_ABSOLUTE_PATH (name));
3541 if (this_real_name != NULL
3542 && FILENAME_CMP (real_path, this_real_name) == 0)
3543 {
3544 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3545 callback))
3546 return true;
3547 continue;
3548 }
3549 }
3550 }
3551 }
3552
3553 return false;
3554 }
3555
3556 /* Struct used to manage iterating over all CUs looking for a symbol. */
3557
3558 struct dw2_symtab_iterator
3559 {
3560 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3561 dwarf2_per_objfile *per_objfile;
3562 /* If set, only look for symbols that match that block. Valid values are
3563 GLOBAL_BLOCK and STATIC_BLOCK. */
3564 gdb::optional<block_enum> block_index;
3565 /* The kind of symbol we're looking for. */
3566 domain_enum domain;
3567 /* The list of CUs from the index entry of the symbol,
3568 or NULL if not found. */
3569 offset_type *vec;
3570 /* The next element in VEC to look at. */
3571 int next;
3572 /* The number of elements in VEC, or zero if there is no match. */
3573 int length;
3574 /* Have we seen a global version of the symbol?
3575 If so we can ignore all further global instances.
3576 This is to work around gold/15646, inefficient gold-generated
3577 indices. */
3578 int global_seen;
3579 };
3580
3581 /* Initialize the index symtab iterator ITER, common part. */
3582
3583 static void
3584 dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3585 dwarf2_per_objfile *per_objfile,
3586 gdb::optional<block_enum> block_index,
3587 domain_enum domain)
3588 {
3589 iter->per_objfile = per_objfile;
3590 iter->block_index = block_index;
3591 iter->domain = domain;
3592 iter->next = 0;
3593 iter->global_seen = 0;
3594 iter->vec = NULL;
3595 iter->length = 0;
3596 }
3597
3598 /* Initialize the index symtab iterator ITER, const char *NAME variant. */
3599
3600 static void
3601 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3602 dwarf2_per_objfile *per_objfile,
3603 gdb::optional<block_enum> block_index,
3604 domain_enum domain,
3605 const char *name)
3606 {
3607 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3608
3609 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3610 /* index is NULL if OBJF_READNOW. */
3611 if (index == NULL)
3612 return;
3613
3614 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3615 iter->length = MAYBE_SWAP (*iter->vec);
3616 }
3617
3618 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3619
3620 static void
3621 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3622 dwarf2_per_objfile *per_objfile,
3623 gdb::optional<block_enum> block_index,
3624 domain_enum domain, offset_type namei)
3625 {
3626 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3627
3628 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3629 /* index is NULL if OBJF_READNOW. */
3630 if (index == NULL)
3631 return;
3632
3633 gdb_assert (!index->symbol_name_slot_invalid (namei));
3634 const auto &bucket = index->symbol_table[namei];
3635
3636 iter->vec = (offset_type *) (index->constant_pool
3637 + MAYBE_SWAP (bucket.vec));
3638 iter->length = MAYBE_SWAP (*iter->vec);
3639 }
3640
3641 /* Return the next matching CU or NULL if there are no more. */
3642
3643 static struct dwarf2_per_cu_data *
3644 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3645 {
3646 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3647
3648 for ( ; iter->next < iter->length; ++iter->next)
3649 {
3650 offset_type cu_index_and_attrs =
3651 MAYBE_SWAP (iter->vec[iter->next + 1]);
3652 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3653 gdb_index_symbol_kind symbol_kind =
3654 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3655 /* Only check the symbol attributes if they're present.
3656 Indices prior to version 7 don't record them,
3657 and indices >= 7 may elide them for certain symbols
3658 (gold does this). */
3659 int attrs_valid =
3660 (per_objfile->per_bfd->index_table->version >= 7
3661 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3662
3663 /* Don't crash on bad data. */
3664 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3665 + per_objfile->per_bfd->all_type_units.size ()))
3666 {
3667 complaint (_(".gdb_index entry has bad CU index"
3668 " [in module %s]"), objfile_name (per_objfile->objfile));
3669 continue;
3670 }
3671
3672 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3673
3674 /* Skip if already read in. */
3675 if (per_objfile->symtab_set_p (per_cu))
3676 continue;
3677
3678 /* Check static vs global. */
3679 if (attrs_valid)
3680 {
3681 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3682
3683 if (iter->block_index.has_value ())
3684 {
3685 bool want_static = *iter->block_index == STATIC_BLOCK;
3686
3687 if (is_static != want_static)
3688 continue;
3689 }
3690
3691 /* Work around gold/15646. */
3692 if (!is_static
3693 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3694 {
3695 if (iter->global_seen)
3696 continue;
3697
3698 iter->global_seen = 1;
3699 }
3700 }
3701
3702 /* Only check the symbol's kind if it has one. */
3703 if (attrs_valid)
3704 {
3705 switch (iter->domain)
3706 {
3707 case VAR_DOMAIN:
3708 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3709 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3710 /* Some types are also in VAR_DOMAIN. */
3711 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3712 continue;
3713 break;
3714 case STRUCT_DOMAIN:
3715 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3716 continue;
3717 break;
3718 case LABEL_DOMAIN:
3719 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3720 continue;
3721 break;
3722 case MODULE_DOMAIN:
3723 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3724 continue;
3725 break;
3726 default:
3727 break;
3728 }
3729 }
3730
3731 ++iter->next;
3732 return per_cu;
3733 }
3734
3735 return NULL;
3736 }
3737
3738 static struct compunit_symtab *
3739 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3740 const char *name, domain_enum domain)
3741 {
3742 struct compunit_symtab *stab_best = NULL;
3743 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3744
3745 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3746
3747 struct dw2_symtab_iterator iter;
3748 struct dwarf2_per_cu_data *per_cu;
3749
3750 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
3751
3752 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3753 {
3754 struct symbol *sym, *with_opaque = NULL;
3755 struct compunit_symtab *stab
3756 = dw2_instantiate_symtab (per_cu, per_objfile, false);
3757 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3758 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3759
3760 sym = block_find_symbol (block, name, domain,
3761 block_find_non_opaque_type_preferred,
3762 &with_opaque);
3763
3764 /* Some caution must be observed with overloaded functions
3765 and methods, since the index will not contain any overload
3766 information (but NAME might contain it). */
3767
3768 if (sym != NULL
3769 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3770 return stab;
3771 if (with_opaque != NULL
3772 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3773 stab_best = stab;
3774
3775 /* Keep looking through other CUs. */
3776 }
3777
3778 return stab_best;
3779 }
3780
3781 static void
3782 dw2_print_stats (struct objfile *objfile)
3783 {
3784 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3785 int total = (per_objfile->per_bfd->all_comp_units.size ()
3786 + per_objfile->per_bfd->all_type_units.size ());
3787 int count = 0;
3788
3789 for (int i = 0; i < total; ++i)
3790 {
3791 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3792
3793 if (!per_objfile->symtab_set_p (per_cu))
3794 ++count;
3795 }
3796 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3797 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3798 }
3799
3800 /* This dumps minimal information about the index.
3801 It is called via "mt print objfiles".
3802 One use is to verify .gdb_index has been loaded by the
3803 gdb.dwarf2/gdb-index.exp testcase. */
3804
3805 static void
3806 dw2_dump (struct objfile *objfile)
3807 {
3808 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3809
3810 gdb_assert (per_objfile->per_bfd->using_index);
3811 printf_filtered (".gdb_index:");
3812 if (per_objfile->per_bfd->index_table != NULL)
3813 {
3814 printf_filtered (" version %d\n",
3815 per_objfile->per_bfd->index_table->version);
3816 }
3817 else
3818 printf_filtered (" faked for \"readnow\"\n");
3819 printf_filtered ("\n");
3820 }
3821
3822 static void
3823 dw2_expand_symtabs_for_function (struct objfile *objfile,
3824 const char *func_name)
3825 {
3826 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3827
3828 struct dw2_symtab_iterator iter;
3829 struct dwarf2_per_cu_data *per_cu;
3830
3831 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
3832
3833 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3834 dw2_instantiate_symtab (per_cu, per_objfile, false);
3835
3836 }
3837
3838 static void
3839 dw2_expand_all_symtabs (struct objfile *objfile)
3840 {
3841 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3842 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3843 + per_objfile->per_bfd->all_type_units.size ());
3844
3845 for (int i = 0; i < total_units; ++i)
3846 {
3847 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3848
3849 /* We don't want to directly expand a partial CU, because if we
3850 read it with the wrong language, then assertion failures can
3851 be triggered later on. See PR symtab/23010. So, tell
3852 dw2_instantiate_symtab to skip partial CUs -- any important
3853 partial CU will be read via DW_TAG_imported_unit anyway. */
3854 dw2_instantiate_symtab (per_cu, per_objfile, true);
3855 }
3856 }
3857
3858 static void
3859 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3860 const char *fullname)
3861 {
3862 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3863
3864 /* We don't need to consider type units here.
3865 This is only called for examining code, e.g. expand_line_sal.
3866 There can be an order of magnitude (or more) more type units
3867 than comp units, and we avoid them if we can. */
3868
3869 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3870 {
3871 /* We only need to look at symtabs not already expanded. */
3872 if (per_objfile->symtab_set_p (per_cu))
3873 continue;
3874
3875 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3876 if (file_data == NULL)
3877 continue;
3878
3879 for (int j = 0; j < file_data->num_file_names; ++j)
3880 {
3881 const char *this_fullname = file_data->file_names[j];
3882
3883 if (filename_cmp (this_fullname, fullname) == 0)
3884 {
3885 dw2_instantiate_symtab (per_cu, per_objfile, false);
3886 break;
3887 }
3888 }
3889 }
3890 }
3891
3892 static void
3893 dw2_expand_symtabs_matching_symbol
3894 (mapped_index_base &index,
3895 const lookup_name_info &lookup_name_in,
3896 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3897 enum search_domain kind,
3898 gdb::function_view<bool (offset_type)> match_callback,
3899 dwarf2_per_objfile *per_objfile);
3900
3901 static void
3902 dw2_expand_symtabs_matching_one
3903 (dwarf2_per_cu_data *per_cu,
3904 dwarf2_per_objfile *per_objfile,
3905 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3906 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3907
3908 static void
3909 dw2_map_matching_symbols
3910 (struct objfile *objfile,
3911 const lookup_name_info &name, domain_enum domain,
3912 int global,
3913 gdb::function_view<symbol_found_callback_ftype> callback,
3914 symbol_compare_ftype *ordered_compare)
3915 {
3916 /* Used for Ada. */
3917 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3918
3919 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3920
3921 if (per_objfile->per_bfd->index_table != nullptr)
3922 {
3923 mapped_index &index = *per_objfile->per_bfd->index_table;
3924
3925 const char *match_name = name.ada ().lookup_name ().c_str ();
3926 auto matcher = [&] (const char *symname)
3927 {
3928 if (ordered_compare == nullptr)
3929 return true;
3930 return ordered_compare (symname, match_name) == 0;
3931 };
3932
3933 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3934 [&] (offset_type namei)
3935 {
3936 struct dw2_symtab_iterator iter;
3937 struct dwarf2_per_cu_data *per_cu;
3938
3939 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3940 namei);
3941 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3942 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3943 nullptr);
3944 return true;
3945 }, per_objfile);
3946 }
3947 else
3948 {
3949 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3950 proceed assuming all symtabs have been read in. */
3951 }
3952
3953 for (compunit_symtab *cust : objfile->compunits ())
3954 {
3955 const struct block *block;
3956
3957 if (cust == NULL)
3958 continue;
3959 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3960 if (!iterate_over_symbols_terminated (block, name,
3961 domain, callback))
3962 return;
3963 }
3964 }
3965
3966 /* Starting from a search name, return the string that finds the upper
3967 bound of all strings that start with SEARCH_NAME in a sorted name
3968 list. Returns the empty string to indicate that the upper bound is
3969 the end of the list. */
3970
3971 static std::string
3972 make_sort_after_prefix_name (const char *search_name)
3973 {
3974 /* When looking to complete "func", we find the upper bound of all
3975 symbols that start with "func" by looking for where we'd insert
3976 the closest string that would follow "func" in lexicographical
3977 order. Usually, that's "func"-with-last-character-incremented,
3978 i.e. "fund". Mind non-ASCII characters, though. Usually those
3979 will be UTF-8 multi-byte sequences, but we can't be certain.
3980 Especially mind the 0xff character, which is a valid character in
3981 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3982 rule out compilers allowing it in identifiers. Note that
3983 conveniently, strcmp/strcasecmp are specified to compare
3984 characters interpreted as unsigned char. So what we do is treat
3985 the whole string as a base 256 number composed of a sequence of
3986 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3987 to 0, and carries 1 to the following more-significant position.
3988 If the very first character in SEARCH_NAME ends up incremented
3989 and carries/overflows, then the upper bound is the end of the
3990 list. The string after the empty string is also the empty
3991 string.
3992
3993 Some examples of this operation:
3994
3995 SEARCH_NAME => "+1" RESULT
3996
3997 "abc" => "abd"
3998 "ab\xff" => "ac"
3999 "\xff" "a" "\xff" => "\xff" "b"
4000 "\xff" => ""
4001 "\xff\xff" => ""
4002 "" => ""
4003
4004 Then, with these symbols for example:
4005
4006 func
4007 func1
4008 fund
4009
4010 completing "func" looks for symbols between "func" and
4011 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4012 which finds "func" and "func1", but not "fund".
4013
4014 And with:
4015
4016 funcÿ (Latin1 'ÿ' [0xff])
4017 funcÿ1
4018 fund
4019
4020 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4021 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4022
4023 And with:
4024
4025 ÿÿ (Latin1 'ÿ' [0xff])
4026 ÿÿ1
4027
4028 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4029 the end of the list.
4030 */
4031 std::string after = search_name;
4032 while (!after.empty () && (unsigned char) after.back () == 0xff)
4033 after.pop_back ();
4034 if (!after.empty ())
4035 after.back () = (unsigned char) after.back () + 1;
4036 return after;
4037 }
4038
4039 /* See declaration. */
4040
4041 std::pair<std::vector<name_component>::const_iterator,
4042 std::vector<name_component>::const_iterator>
4043 mapped_index_base::find_name_components_bounds
4044 (const lookup_name_info &lookup_name_without_params, language lang,
4045 dwarf2_per_objfile *per_objfile) const
4046 {
4047 auto *name_cmp
4048 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4049
4050 const char *lang_name
4051 = lookup_name_without_params.language_lookup_name (lang);
4052
4053 /* Comparison function object for lower_bound that matches against a
4054 given symbol name. */
4055 auto lookup_compare_lower = [&] (const name_component &elem,
4056 const char *name)
4057 {
4058 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
4059 const char *elem_name = elem_qualified + elem.name_offset;
4060 return name_cmp (elem_name, name) < 0;
4061 };
4062
4063 /* Comparison function object for upper_bound that matches against a
4064 given symbol name. */
4065 auto lookup_compare_upper = [&] (const char *name,
4066 const name_component &elem)
4067 {
4068 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
4069 const char *elem_name = elem_qualified + elem.name_offset;
4070 return name_cmp (name, elem_name) < 0;
4071 };
4072
4073 auto begin = this->name_components.begin ();
4074 auto end = this->name_components.end ();
4075
4076 /* Find the lower bound. */
4077 auto lower = [&] ()
4078 {
4079 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
4080 return begin;
4081 else
4082 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4083 } ();
4084
4085 /* Find the upper bound. */
4086 auto upper = [&] ()
4087 {
4088 if (lookup_name_without_params.completion_mode ())
4089 {
4090 /* In completion mode, we want UPPER to point past all
4091 symbols names that have the same prefix. I.e., with
4092 these symbols, and completing "func":
4093
4094 function << lower bound
4095 function1
4096 other_function << upper bound
4097
4098 We find the upper bound by looking for the insertion
4099 point of "func"-with-last-character-incremented,
4100 i.e. "fund". */
4101 std::string after = make_sort_after_prefix_name (lang_name);
4102 if (after.empty ())
4103 return end;
4104 return std::lower_bound (lower, end, after.c_str (),
4105 lookup_compare_lower);
4106 }
4107 else
4108 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4109 } ();
4110
4111 return {lower, upper};
4112 }
4113
4114 /* See declaration. */
4115
4116 void
4117 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
4118 {
4119 if (!this->name_components.empty ())
4120 return;
4121
4122 this->name_components_casing = case_sensitivity;
4123 auto *name_cmp
4124 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4125
4126 /* The code below only knows how to break apart components of C++
4127 symbol names (and other languages that use '::' as
4128 namespace/module separator) and Ada symbol names. */
4129 auto count = this->symbol_name_count ();
4130 for (offset_type idx = 0; idx < count; idx++)
4131 {
4132 if (this->symbol_name_slot_invalid (idx))
4133 continue;
4134
4135 const char *name = this->symbol_name_at (idx, per_objfile);
4136
4137 /* Add each name component to the name component table. */
4138 unsigned int previous_len = 0;
4139
4140 if (strstr (name, "::") != nullptr)
4141 {
4142 for (unsigned int current_len = cp_find_first_component (name);
4143 name[current_len] != '\0';
4144 current_len += cp_find_first_component (name + current_len))
4145 {
4146 gdb_assert (name[current_len] == ':');
4147 this->name_components.push_back ({previous_len, idx});
4148 /* Skip the '::'. */
4149 current_len += 2;
4150 previous_len = current_len;
4151 }
4152 }
4153 else
4154 {
4155 /* Handle the Ada encoded (aka mangled) form here. */
4156 for (const char *iter = strstr (name, "__");
4157 iter != nullptr;
4158 iter = strstr (iter, "__"))
4159 {
4160 this->name_components.push_back ({previous_len, idx});
4161 iter += 2;
4162 previous_len = iter - name;
4163 }
4164 }
4165
4166 this->name_components.push_back ({previous_len, idx});
4167 }
4168
4169 /* Sort name_components elements by name. */
4170 auto name_comp_compare = [&] (const name_component &left,
4171 const name_component &right)
4172 {
4173 const char *left_qualified
4174 = this->symbol_name_at (left.idx, per_objfile);
4175 const char *right_qualified
4176 = this->symbol_name_at (right.idx, per_objfile);
4177
4178 const char *left_name = left_qualified + left.name_offset;
4179 const char *right_name = right_qualified + right.name_offset;
4180
4181 return name_cmp (left_name, right_name) < 0;
4182 };
4183
4184 std::sort (this->name_components.begin (),
4185 this->name_components.end (),
4186 name_comp_compare);
4187 }
4188
4189 /* Helper for dw2_expand_symtabs_matching that works with a
4190 mapped_index_base instead of the containing objfile. This is split
4191 to a separate function in order to be able to unit test the
4192 name_components matching using a mock mapped_index_base. For each
4193 symbol name that matches, calls MATCH_CALLBACK, passing it the
4194 symbol's index in the mapped_index_base symbol table. */
4195
4196 static void
4197 dw2_expand_symtabs_matching_symbol
4198 (mapped_index_base &index,
4199 const lookup_name_info &lookup_name_in,
4200 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4201 enum search_domain kind,
4202 gdb::function_view<bool (offset_type)> match_callback,
4203 dwarf2_per_objfile *per_objfile)
4204 {
4205 lookup_name_info lookup_name_without_params
4206 = lookup_name_in.make_ignore_params ();
4207
4208 /* Build the symbol name component sorted vector, if we haven't
4209 yet. */
4210 index.build_name_components (per_objfile);
4211
4212 /* The same symbol may appear more than once in the range though.
4213 E.g., if we're looking for symbols that complete "w", and we have
4214 a symbol named "w1::w2", we'll find the two name components for
4215 that same symbol in the range. To be sure we only call the
4216 callback once per symbol, we first collect the symbol name
4217 indexes that matched in a temporary vector and ignore
4218 duplicates. */
4219 std::vector<offset_type> matches;
4220
4221 struct name_and_matcher
4222 {
4223 symbol_name_matcher_ftype *matcher;
4224 const char *name;
4225
4226 bool operator== (const name_and_matcher &other) const
4227 {
4228 return matcher == other.matcher && strcmp (name, other.name) == 0;
4229 }
4230 };
4231
4232 /* A vector holding all the different symbol name matchers, for all
4233 languages. */
4234 std::vector<name_and_matcher> matchers;
4235
4236 for (int i = 0; i < nr_languages; i++)
4237 {
4238 enum language lang_e = (enum language) i;
4239
4240 const language_defn *lang = language_def (lang_e);
4241 symbol_name_matcher_ftype *name_matcher
4242 = lang->get_symbol_name_matcher (lookup_name_without_params);
4243
4244 name_and_matcher key {
4245 name_matcher,
4246 lookup_name_without_params.language_lookup_name (lang_e)
4247 };
4248
4249 /* Don't insert the same comparison routine more than once.
4250 Note that we do this linear walk. This is not a problem in
4251 practice because the number of supported languages is
4252 low. */
4253 if (std::find (matchers.begin (), matchers.end (), key)
4254 != matchers.end ())
4255 continue;
4256 matchers.push_back (std::move (key));
4257
4258 auto bounds
4259 = index.find_name_components_bounds (lookup_name_without_params,
4260 lang_e, per_objfile);
4261
4262 /* Now for each symbol name in range, check to see if we have a name
4263 match, and if so, call the MATCH_CALLBACK callback. */
4264
4265 for (; bounds.first != bounds.second; ++bounds.first)
4266 {
4267 const char *qualified
4268 = index.symbol_name_at (bounds.first->idx, per_objfile);
4269
4270 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4271 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4272 continue;
4273
4274 matches.push_back (bounds.first->idx);
4275 }
4276 }
4277
4278 std::sort (matches.begin (), matches.end ());
4279
4280 /* Finally call the callback, once per match. */
4281 ULONGEST prev = -1;
4282 for (offset_type idx : matches)
4283 {
4284 if (prev != idx)
4285 {
4286 if (!match_callback (idx))
4287 break;
4288 prev = idx;
4289 }
4290 }
4291
4292 /* Above we use a type wider than idx's for 'prev', since 0 and
4293 (offset_type)-1 are both possible values. */
4294 static_assert (sizeof (prev) > sizeof (offset_type), "");
4295 }
4296
4297 #if GDB_SELF_TEST
4298
4299 namespace selftests { namespace dw2_expand_symtabs_matching {
4300
4301 /* A mock .gdb_index/.debug_names-like name index table, enough to
4302 exercise dw2_expand_symtabs_matching_symbol, which works with the
4303 mapped_index_base interface. Builds an index from the symbol list
4304 passed as parameter to the constructor. */
4305 class mock_mapped_index : public mapped_index_base
4306 {
4307 public:
4308 mock_mapped_index (gdb::array_view<const char *> symbols)
4309 : m_symbol_table (symbols)
4310 {}
4311
4312 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4313
4314 /* Return the number of names in the symbol table. */
4315 size_t symbol_name_count () const override
4316 {
4317 return m_symbol_table.size ();
4318 }
4319
4320 /* Get the name of the symbol at IDX in the symbol table. */
4321 const char *symbol_name_at
4322 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
4323 {
4324 return m_symbol_table[idx];
4325 }
4326
4327 private:
4328 gdb::array_view<const char *> m_symbol_table;
4329 };
4330
4331 /* Convenience function that converts a NULL pointer to a "<null>"
4332 string, to pass to print routines. */
4333
4334 static const char *
4335 string_or_null (const char *str)
4336 {
4337 return str != NULL ? str : "<null>";
4338 }
4339
4340 /* Check if a lookup_name_info built from
4341 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4342 index. EXPECTED_LIST is the list of expected matches, in expected
4343 matching order. If no match expected, then an empty list is
4344 specified. Returns true on success. On failure prints a warning
4345 indicating the file:line that failed, and returns false. */
4346
4347 static bool
4348 check_match (const char *file, int line,
4349 mock_mapped_index &mock_index,
4350 const char *name, symbol_name_match_type match_type,
4351 bool completion_mode,
4352 std::initializer_list<const char *> expected_list,
4353 dwarf2_per_objfile *per_objfile)
4354 {
4355 lookup_name_info lookup_name (name, match_type, completion_mode);
4356
4357 bool matched = true;
4358
4359 auto mismatch = [&] (const char *expected_str,
4360 const char *got)
4361 {
4362 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4363 "expected=\"%s\", got=\"%s\"\n"),
4364 file, line,
4365 (match_type == symbol_name_match_type::FULL
4366 ? "FULL" : "WILD"),
4367 name, string_or_null (expected_str), string_or_null (got));
4368 matched = false;
4369 };
4370
4371 auto expected_it = expected_list.begin ();
4372 auto expected_end = expected_list.end ();
4373
4374 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4375 NULL, ALL_DOMAIN,
4376 [&] (offset_type idx)
4377 {
4378 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
4379 const char *expected_str
4380 = expected_it == expected_end ? NULL : *expected_it++;
4381
4382 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4383 mismatch (expected_str, matched_name);
4384 return true;
4385 }, per_objfile);
4386
4387 const char *expected_str
4388 = expected_it == expected_end ? NULL : *expected_it++;
4389 if (expected_str != NULL)
4390 mismatch (expected_str, NULL);
4391
4392 return matched;
4393 }
4394
4395 /* The symbols added to the mock mapped_index for testing (in
4396 canonical form). */
4397 static const char *test_symbols[] = {
4398 "function",
4399 "std::bar",
4400 "std::zfunction",
4401 "std::zfunction2",
4402 "w1::w2",
4403 "ns::foo<char*>",
4404 "ns::foo<int>",
4405 "ns::foo<long>",
4406 "ns2::tmpl<int>::foo2",
4407 "(anonymous namespace)::A::B::C",
4408
4409 /* These are used to check that the increment-last-char in the
4410 matching algorithm for completion doesn't match "t1_fund" when
4411 completing "t1_func". */
4412 "t1_func",
4413 "t1_func1",
4414 "t1_fund",
4415 "t1_fund1",
4416
4417 /* A UTF-8 name with multi-byte sequences to make sure that
4418 cp-name-parser understands this as a single identifier ("função"
4419 is "function" in PT). */
4420 u8"u8função",
4421
4422 /* \377 (0xff) is Latin1 'ÿ'. */
4423 "yfunc\377",
4424
4425 /* \377 (0xff) is Latin1 'ÿ'. */
4426 "\377",
4427 "\377\377123",
4428
4429 /* A name with all sorts of complications. Starts with "z" to make
4430 it easier for the completion tests below. */
4431 #define Z_SYM_NAME \
4432 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4433 "::tuple<(anonymous namespace)::ui*, " \
4434 "std::default_delete<(anonymous namespace)::ui>, void>"
4435
4436 Z_SYM_NAME
4437 };
4438
4439 /* Returns true if the mapped_index_base::find_name_component_bounds
4440 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4441 in completion mode. */
4442
4443 static bool
4444 check_find_bounds_finds (mapped_index_base &index,
4445 const char *search_name,
4446 gdb::array_view<const char *> expected_syms,
4447 dwarf2_per_objfile *per_objfile)
4448 {
4449 lookup_name_info lookup_name (search_name,
4450 symbol_name_match_type::FULL, true);
4451
4452 auto bounds = index.find_name_components_bounds (lookup_name,
4453 language_cplus,
4454 per_objfile);
4455
4456 size_t distance = std::distance (bounds.first, bounds.second);
4457 if (distance != expected_syms.size ())
4458 return false;
4459
4460 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4461 {
4462 auto nc_elem = bounds.first + exp_elem;
4463 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
4464 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4465 return false;
4466 }
4467
4468 return true;
4469 }
4470
4471 /* Test the lower-level mapped_index::find_name_component_bounds
4472 method. */
4473
4474 static void
4475 test_mapped_index_find_name_component_bounds ()
4476 {
4477 mock_mapped_index mock_index (test_symbols);
4478
4479 mock_index.build_name_components (NULL /* per_objfile */);
4480
4481 /* Test the lower-level mapped_index::find_name_component_bounds
4482 method in completion mode. */
4483 {
4484 static const char *expected_syms[] = {
4485 "t1_func",
4486 "t1_func1",
4487 };
4488
4489 SELF_CHECK (check_find_bounds_finds
4490 (mock_index, "t1_func", expected_syms,
4491 NULL /* per_objfile */));
4492 }
4493
4494 /* Check that the increment-last-char in the name matching algorithm
4495 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4496 {
4497 static const char *expected_syms1[] = {
4498 "\377",
4499 "\377\377123",
4500 };
4501 SELF_CHECK (check_find_bounds_finds
4502 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
4503
4504 static const char *expected_syms2[] = {
4505 "\377\377123",
4506 };
4507 SELF_CHECK (check_find_bounds_finds
4508 (mock_index, "\377\377", expected_syms2,
4509 NULL /* per_objfile */));
4510 }
4511 }
4512
4513 /* Test dw2_expand_symtabs_matching_symbol. */
4514
4515 static void
4516 test_dw2_expand_symtabs_matching_symbol ()
4517 {
4518 mock_mapped_index mock_index (test_symbols);
4519
4520 /* We let all tests run until the end even if some fails, for debug
4521 convenience. */
4522 bool any_mismatch = false;
4523
4524 /* Create the expected symbols list (an initializer_list). Needed
4525 because lists have commas, and we need to pass them to CHECK,
4526 which is a macro. */
4527 #define EXPECT(...) { __VA_ARGS__ }
4528
4529 /* Wrapper for check_match that passes down the current
4530 __FILE__/__LINE__. */
4531 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4532 any_mismatch |= !check_match (__FILE__, __LINE__, \
4533 mock_index, \
4534 NAME, MATCH_TYPE, COMPLETION_MODE, \
4535 EXPECTED_LIST, NULL)
4536
4537 /* Identity checks. */
4538 for (const char *sym : test_symbols)
4539 {
4540 /* Should be able to match all existing symbols. */
4541 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4542 EXPECT (sym));
4543
4544 /* Should be able to match all existing symbols with
4545 parameters. */
4546 std::string with_params = std::string (sym) + "(int)";
4547 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4548 EXPECT (sym));
4549
4550 /* Should be able to match all existing symbols with
4551 parameters and qualifiers. */
4552 with_params = std::string (sym) + " ( int ) const";
4553 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4554 EXPECT (sym));
4555
4556 /* This should really find sym, but cp-name-parser.y doesn't
4557 know about lvalue/rvalue qualifiers yet. */
4558 with_params = std::string (sym) + " ( int ) &&";
4559 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4560 {});
4561 }
4562
4563 /* Check that the name matching algorithm for completion doesn't get
4564 confused with Latin1 'ÿ' / 0xff. */
4565 {
4566 static const char str[] = "\377";
4567 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4568 EXPECT ("\377", "\377\377123"));
4569 }
4570
4571 /* Check that the increment-last-char in the matching algorithm for
4572 completion doesn't match "t1_fund" when completing "t1_func". */
4573 {
4574 static const char str[] = "t1_func";
4575 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4576 EXPECT ("t1_func", "t1_func1"));
4577 }
4578
4579 /* Check that completion mode works at each prefix of the expected
4580 symbol name. */
4581 {
4582 static const char str[] = "function(int)";
4583 size_t len = strlen (str);
4584 std::string lookup;
4585
4586 for (size_t i = 1; i < len; i++)
4587 {
4588 lookup.assign (str, i);
4589 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4590 EXPECT ("function"));
4591 }
4592 }
4593
4594 /* While "w" is a prefix of both components, the match function
4595 should still only be called once. */
4596 {
4597 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4598 EXPECT ("w1::w2"));
4599 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4600 EXPECT ("w1::w2"));
4601 }
4602
4603 /* Same, with a "complicated" symbol. */
4604 {
4605 static const char str[] = Z_SYM_NAME;
4606 size_t len = strlen (str);
4607 std::string lookup;
4608
4609 for (size_t i = 1; i < len; i++)
4610 {
4611 lookup.assign (str, i);
4612 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4613 EXPECT (Z_SYM_NAME));
4614 }
4615 }
4616
4617 /* In FULL mode, an incomplete symbol doesn't match. */
4618 {
4619 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4620 {});
4621 }
4622
4623 /* A complete symbol with parameters matches any overload, since the
4624 index has no overload info. */
4625 {
4626 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4627 EXPECT ("std::zfunction", "std::zfunction2"));
4628 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4629 EXPECT ("std::zfunction", "std::zfunction2"));
4630 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4631 EXPECT ("std::zfunction", "std::zfunction2"));
4632 }
4633
4634 /* Check that whitespace is ignored appropriately. A symbol with a
4635 template argument list. */
4636 {
4637 static const char expected[] = "ns::foo<int>";
4638 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4639 EXPECT (expected));
4640 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4641 EXPECT (expected));
4642 }
4643
4644 /* Check that whitespace is ignored appropriately. A symbol with a
4645 template argument list that includes a pointer. */
4646 {
4647 static const char expected[] = "ns::foo<char*>";
4648 /* Try both completion and non-completion modes. */
4649 static const bool completion_mode[2] = {false, true};
4650 for (size_t i = 0; i < 2; i++)
4651 {
4652 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4653 completion_mode[i], EXPECT (expected));
4654 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4655 completion_mode[i], EXPECT (expected));
4656
4657 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4658 completion_mode[i], EXPECT (expected));
4659 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4660 completion_mode[i], EXPECT (expected));
4661 }
4662 }
4663
4664 {
4665 /* Check method qualifiers are ignored. */
4666 static const char expected[] = "ns::foo<char*>";
4667 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4668 symbol_name_match_type::FULL, true, EXPECT (expected));
4669 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4670 symbol_name_match_type::FULL, true, EXPECT (expected));
4671 CHECK_MATCH ("foo < char * > ( int ) const",
4672 symbol_name_match_type::WILD, true, EXPECT (expected));
4673 CHECK_MATCH ("foo < char * > ( int ) &&",
4674 symbol_name_match_type::WILD, true, EXPECT (expected));
4675 }
4676
4677 /* Test lookup names that don't match anything. */
4678 {
4679 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4680 {});
4681
4682 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4683 {});
4684 }
4685
4686 /* Some wild matching tests, exercising "(anonymous namespace)",
4687 which should not be confused with a parameter list. */
4688 {
4689 static const char *syms[] = {
4690 "A::B::C",
4691 "B::C",
4692 "C",
4693 "A :: B :: C ( int )",
4694 "B :: C ( int )",
4695 "C ( int )",
4696 };
4697
4698 for (const char *s : syms)
4699 {
4700 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4701 EXPECT ("(anonymous namespace)::A::B::C"));
4702 }
4703 }
4704
4705 {
4706 static const char expected[] = "ns2::tmpl<int>::foo2";
4707 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4708 EXPECT (expected));
4709 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4710 EXPECT (expected));
4711 }
4712
4713 SELF_CHECK (!any_mismatch);
4714
4715 #undef EXPECT
4716 #undef CHECK_MATCH
4717 }
4718
4719 static void
4720 run_test ()
4721 {
4722 test_mapped_index_find_name_component_bounds ();
4723 test_dw2_expand_symtabs_matching_symbol ();
4724 }
4725
4726 }} // namespace selftests::dw2_expand_symtabs_matching
4727
4728 #endif /* GDB_SELF_TEST */
4729
4730 /* If FILE_MATCHER is NULL or if PER_CU has
4731 dwarf2_per_cu_quick_data::MARK set (see
4732 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4733 EXPANSION_NOTIFY on it. */
4734
4735 static void
4736 dw2_expand_symtabs_matching_one
4737 (dwarf2_per_cu_data *per_cu,
4738 dwarf2_per_objfile *per_objfile,
4739 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4740 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4741 {
4742 if (file_matcher == NULL || per_cu->v.quick->mark)
4743 {
4744 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4745
4746 compunit_symtab *symtab
4747 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4748 gdb_assert (symtab != nullptr);
4749
4750 if (expansion_notify != NULL && symtab_was_null)
4751 expansion_notify (symtab);
4752 }
4753 }
4754
4755 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4756 matched, to expand corresponding CUs that were marked. IDX is the
4757 index of the symbol name that matched. */
4758
4759 static void
4760 dw2_expand_marked_cus
4761 (dwarf2_per_objfile *per_objfile, offset_type idx,
4762 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4763 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4764 search_domain kind)
4765 {
4766 offset_type *vec, vec_len, vec_idx;
4767 bool global_seen = false;
4768 mapped_index &index = *per_objfile->per_bfd->index_table;
4769
4770 vec = (offset_type *) (index.constant_pool
4771 + MAYBE_SWAP (index.symbol_table[idx].vec));
4772 vec_len = MAYBE_SWAP (vec[0]);
4773 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4774 {
4775 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4776 /* This value is only valid for index versions >= 7. */
4777 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4778 gdb_index_symbol_kind symbol_kind =
4779 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4780 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4781 /* Only check the symbol attributes if they're present.
4782 Indices prior to version 7 don't record them,
4783 and indices >= 7 may elide them for certain symbols
4784 (gold does this). */
4785 int attrs_valid =
4786 (index.version >= 7
4787 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4788
4789 /* Work around gold/15646. */
4790 if (attrs_valid
4791 && !is_static
4792 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4793 {
4794 if (global_seen)
4795 continue;
4796
4797 global_seen = true;
4798 }
4799
4800 /* Only check the symbol's kind if it has one. */
4801 if (attrs_valid)
4802 {
4803 switch (kind)
4804 {
4805 case VARIABLES_DOMAIN:
4806 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4807 continue;
4808 break;
4809 case FUNCTIONS_DOMAIN:
4810 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4811 continue;
4812 break;
4813 case TYPES_DOMAIN:
4814 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4815 continue;
4816 break;
4817 case MODULES_DOMAIN:
4818 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4819 continue;
4820 break;
4821 default:
4822 break;
4823 }
4824 }
4825
4826 /* Don't crash on bad data. */
4827 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4828 + per_objfile->per_bfd->all_type_units.size ()))
4829 {
4830 complaint (_(".gdb_index entry has bad CU index"
4831 " [in module %s]"), objfile_name (per_objfile->objfile));
4832 continue;
4833 }
4834
4835 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4836 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4837 expansion_notify);
4838 }
4839 }
4840
4841 /* If FILE_MATCHER is non-NULL, set all the
4842 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4843 that match FILE_MATCHER. */
4844
4845 static void
4846 dw_expand_symtabs_matching_file_matcher
4847 (dwarf2_per_objfile *per_objfile,
4848 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4849 {
4850 if (file_matcher == NULL)
4851 return;
4852
4853 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4854 htab_eq_pointer,
4855 NULL, xcalloc, xfree));
4856 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4857 htab_eq_pointer,
4858 NULL, xcalloc, xfree));
4859
4860 /* The rule is CUs specify all the files, including those used by
4861 any TU, so there's no need to scan TUs here. */
4862
4863 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4864 {
4865 QUIT;
4866
4867 per_cu->v.quick->mark = 0;
4868
4869 /* We only need to look at symtabs not already expanded. */
4870 if (per_objfile->symtab_set_p (per_cu))
4871 continue;
4872
4873 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4874 if (file_data == NULL)
4875 continue;
4876
4877 if (htab_find (visited_not_found.get (), file_data) != NULL)
4878 continue;
4879 else if (htab_find (visited_found.get (), file_data) != NULL)
4880 {
4881 per_cu->v.quick->mark = 1;
4882 continue;
4883 }
4884
4885 for (int j = 0; j < file_data->num_file_names; ++j)
4886 {
4887 const char *this_real_name;
4888
4889 if (file_matcher (file_data->file_names[j], false))
4890 {
4891 per_cu->v.quick->mark = 1;
4892 break;
4893 }
4894
4895 /* Before we invoke realpath, which can get expensive when many
4896 files are involved, do a quick comparison of the basenames. */
4897 if (!basenames_may_differ
4898 && !file_matcher (lbasename (file_data->file_names[j]),
4899 true))
4900 continue;
4901
4902 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4903 if (file_matcher (this_real_name, false))
4904 {
4905 per_cu->v.quick->mark = 1;
4906 break;
4907 }
4908 }
4909
4910 void **slot = htab_find_slot (per_cu->v.quick->mark
4911 ? visited_found.get ()
4912 : visited_not_found.get (),
4913 file_data, INSERT);
4914 *slot = file_data;
4915 }
4916 }
4917
4918 static void
4919 dw2_expand_symtabs_matching
4920 (struct objfile *objfile,
4921 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4922 const lookup_name_info *lookup_name,
4923 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4924 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4925 enum search_domain kind)
4926 {
4927 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4928
4929 /* index_table is NULL if OBJF_READNOW. */
4930 if (!per_objfile->per_bfd->index_table)
4931 return;
4932
4933 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4934
4935 if (symbol_matcher == NULL && lookup_name == NULL)
4936 {
4937 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4938 {
4939 QUIT;
4940
4941 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4942 file_matcher, expansion_notify);
4943 }
4944 return;
4945 }
4946
4947 mapped_index &index = *per_objfile->per_bfd->index_table;
4948
4949 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4950 symbol_matcher,
4951 kind, [&] (offset_type idx)
4952 {
4953 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4954 kind);
4955 return true;
4956 }, per_objfile);
4957 }
4958
4959 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4960 symtab. */
4961
4962 static struct compunit_symtab *
4963 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4964 CORE_ADDR pc)
4965 {
4966 int i;
4967
4968 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4969 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4970 return cust;
4971
4972 if (cust->includes == NULL)
4973 return NULL;
4974
4975 for (i = 0; cust->includes[i]; ++i)
4976 {
4977 struct compunit_symtab *s = cust->includes[i];
4978
4979 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4980 if (s != NULL)
4981 return s;
4982 }
4983
4984 return NULL;
4985 }
4986
4987 static struct compunit_symtab *
4988 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4989 struct bound_minimal_symbol msymbol,
4990 CORE_ADDR pc,
4991 struct obj_section *section,
4992 int warn_if_readin)
4993 {
4994 struct dwarf2_per_cu_data *data;
4995 struct compunit_symtab *result;
4996
4997 if (!objfile->partial_symtabs->psymtabs_addrmap)
4998 return NULL;
4999
5000 CORE_ADDR baseaddr = objfile->text_section_offset ();
5001 data = (struct dwarf2_per_cu_data *) addrmap_find
5002 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
5003 if (!data)
5004 return NULL;
5005
5006 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5007 if (warn_if_readin && per_objfile->symtab_set_p (data))
5008 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5009 paddress (objfile->arch (), pc));
5010
5011 result = recursively_find_pc_sect_compunit_symtab
5012 (dw2_instantiate_symtab (data, per_objfile, false), pc);
5013
5014 gdb_assert (result != NULL);
5015 return result;
5016 }
5017
5018 static void
5019 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5020 void *data, int need_fullname)
5021 {
5022 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5023
5024 if (!per_objfile->per_bfd->filenames_cache)
5025 {
5026 per_objfile->per_bfd->filenames_cache.emplace ();
5027
5028 htab_up visited (htab_create_alloc (10,
5029 htab_hash_pointer, htab_eq_pointer,
5030 NULL, xcalloc, xfree));
5031
5032 /* The rule is CUs specify all the files, including those used
5033 by any TU, so there's no need to scan TUs here. We can
5034 ignore file names coming from already-expanded CUs. */
5035
5036 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5037 {
5038 if (per_objfile->symtab_set_p (per_cu))
5039 {
5040 void **slot = htab_find_slot (visited.get (),
5041 per_cu->v.quick->file_names,
5042 INSERT);
5043
5044 *slot = per_cu->v.quick->file_names;
5045 }
5046 }
5047
5048 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5049 {
5050 /* We only need to look at symtabs not already expanded. */
5051 if (per_objfile->symtab_set_p (per_cu))
5052 continue;
5053
5054 quick_file_names *file_data
5055 = dw2_get_file_names (per_cu, per_objfile);
5056 if (file_data == NULL)
5057 continue;
5058
5059 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5060 if (*slot)
5061 {
5062 /* Already visited. */
5063 continue;
5064 }
5065 *slot = file_data;
5066
5067 for (int j = 0; j < file_data->num_file_names; ++j)
5068 {
5069 const char *filename = file_data->file_names[j];
5070 per_objfile->per_bfd->filenames_cache->seen (filename);
5071 }
5072 }
5073 }
5074
5075 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
5076 {
5077 gdb::unique_xmalloc_ptr<char> this_real_name;
5078
5079 if (need_fullname)
5080 this_real_name = gdb_realpath (filename);
5081 (*fun) (filename, this_real_name.get (), data);
5082 });
5083 }
5084
5085 static int
5086 dw2_has_symbols (struct objfile *objfile)
5087 {
5088 return 1;
5089 }
5090
5091 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5092 {
5093 dw2_has_symbols,
5094 dw2_find_last_source_symtab,
5095 dw2_forget_cached_source_info,
5096 dw2_map_symtabs_matching_filename,
5097 dw2_lookup_symbol,
5098 NULL,
5099 dw2_print_stats,
5100 dw2_dump,
5101 dw2_expand_symtabs_for_function,
5102 dw2_expand_all_symtabs,
5103 dw2_expand_symtabs_with_fullname,
5104 dw2_map_matching_symbols,
5105 dw2_expand_symtabs_matching,
5106 dw2_find_pc_sect_compunit_symtab,
5107 NULL,
5108 dw2_map_symbol_filenames
5109 };
5110
5111 /* DWARF-5 debug_names reader. */
5112
5113 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5114 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5115
5116 /* A helper function that reads the .debug_names section in SECTION
5117 and fills in MAP. FILENAME is the name of the file containing the
5118 section; it is used for error reporting.
5119
5120 Returns true if all went well, false otherwise. */
5121
5122 static bool
5123 read_debug_names_from_section (struct objfile *objfile,
5124 const char *filename,
5125 struct dwarf2_section_info *section,
5126 mapped_debug_names &map)
5127 {
5128 if (section->empty ())
5129 return false;
5130
5131 /* Older elfutils strip versions could keep the section in the main
5132 executable while splitting it for the separate debug info file. */
5133 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5134 return false;
5135
5136 section->read (objfile);
5137
5138 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
5139
5140 const gdb_byte *addr = section->buffer;
5141
5142 bfd *const abfd = section->get_bfd_owner ();
5143
5144 unsigned int bytes_read;
5145 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5146 addr += bytes_read;
5147
5148 map.dwarf5_is_dwarf64 = bytes_read != 4;
5149 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5150 if (bytes_read + length != section->size)
5151 {
5152 /* There may be multiple per-CU indices. */
5153 warning (_("Section .debug_names in %s length %s does not match "
5154 "section length %s, ignoring .debug_names."),
5155 filename, plongest (bytes_read + length),
5156 pulongest (section->size));
5157 return false;
5158 }
5159
5160 /* The version number. */
5161 uint16_t version = read_2_bytes (abfd, addr);
5162 addr += 2;
5163 if (version != 5)
5164 {
5165 warning (_("Section .debug_names in %s has unsupported version %d, "
5166 "ignoring .debug_names."),
5167 filename, version);
5168 return false;
5169 }
5170
5171 /* Padding. */
5172 uint16_t padding = read_2_bytes (abfd, addr);
5173 addr += 2;
5174 if (padding != 0)
5175 {
5176 warning (_("Section .debug_names in %s has unsupported padding %d, "
5177 "ignoring .debug_names."),
5178 filename, padding);
5179 return false;
5180 }
5181
5182 /* comp_unit_count - The number of CUs in the CU list. */
5183 map.cu_count = read_4_bytes (abfd, addr);
5184 addr += 4;
5185
5186 /* local_type_unit_count - The number of TUs in the local TU
5187 list. */
5188 map.tu_count = read_4_bytes (abfd, addr);
5189 addr += 4;
5190
5191 /* foreign_type_unit_count - The number of TUs in the foreign TU
5192 list. */
5193 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5194 addr += 4;
5195 if (foreign_tu_count != 0)
5196 {
5197 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5198 "ignoring .debug_names."),
5199 filename, static_cast<unsigned long> (foreign_tu_count));
5200 return false;
5201 }
5202
5203 /* bucket_count - The number of hash buckets in the hash lookup
5204 table. */
5205 map.bucket_count = read_4_bytes (abfd, addr);
5206 addr += 4;
5207
5208 /* name_count - The number of unique names in the index. */
5209 map.name_count = read_4_bytes (abfd, addr);
5210 addr += 4;
5211
5212 /* abbrev_table_size - The size in bytes of the abbreviations
5213 table. */
5214 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5215 addr += 4;
5216
5217 /* augmentation_string_size - The size in bytes of the augmentation
5218 string. This value is rounded up to a multiple of 4. */
5219 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5220 addr += 4;
5221 map.augmentation_is_gdb = ((augmentation_string_size
5222 == sizeof (dwarf5_augmentation))
5223 && memcmp (addr, dwarf5_augmentation,
5224 sizeof (dwarf5_augmentation)) == 0);
5225 augmentation_string_size += (-augmentation_string_size) & 3;
5226 addr += augmentation_string_size;
5227
5228 /* List of CUs */
5229 map.cu_table_reordered = addr;
5230 addr += map.cu_count * map.offset_size;
5231
5232 /* List of Local TUs */
5233 map.tu_table_reordered = addr;
5234 addr += map.tu_count * map.offset_size;
5235
5236 /* Hash Lookup Table */
5237 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5238 addr += map.bucket_count * 4;
5239 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5240 addr += map.name_count * 4;
5241
5242 /* Name Table */
5243 map.name_table_string_offs_reordered = addr;
5244 addr += map.name_count * map.offset_size;
5245 map.name_table_entry_offs_reordered = addr;
5246 addr += map.name_count * map.offset_size;
5247
5248 const gdb_byte *abbrev_table_start = addr;
5249 for (;;)
5250 {
5251 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5252 addr += bytes_read;
5253 if (index_num == 0)
5254 break;
5255
5256 const auto insertpair
5257 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5258 if (!insertpair.second)
5259 {
5260 warning (_("Section .debug_names in %s has duplicate index %s, "
5261 "ignoring .debug_names."),
5262 filename, pulongest (index_num));
5263 return false;
5264 }
5265 mapped_debug_names::index_val &indexval = insertpair.first->second;
5266 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5267 addr += bytes_read;
5268
5269 for (;;)
5270 {
5271 mapped_debug_names::index_val::attr attr;
5272 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5273 addr += bytes_read;
5274 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5275 addr += bytes_read;
5276 if (attr.form == DW_FORM_implicit_const)
5277 {
5278 attr.implicit_const = read_signed_leb128 (abfd, addr,
5279 &bytes_read);
5280 addr += bytes_read;
5281 }
5282 if (attr.dw_idx == 0 && attr.form == 0)
5283 break;
5284 indexval.attr_vec.push_back (std::move (attr));
5285 }
5286 }
5287 if (addr != abbrev_table_start + abbrev_table_size)
5288 {
5289 warning (_("Section .debug_names in %s has abbreviation_table "
5290 "of size %s vs. written as %u, ignoring .debug_names."),
5291 filename, plongest (addr - abbrev_table_start),
5292 abbrev_table_size);
5293 return false;
5294 }
5295 map.entry_pool = addr;
5296
5297 return true;
5298 }
5299
5300 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5301 list. */
5302
5303 static void
5304 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
5305 const mapped_debug_names &map,
5306 dwarf2_section_info &section,
5307 bool is_dwz)
5308 {
5309 if (!map.augmentation_is_gdb)
5310 {
5311 for (uint32_t i = 0; i < map.cu_count; ++i)
5312 {
5313 sect_offset sect_off
5314 = (sect_offset) (extract_unsigned_integer
5315 (map.cu_table_reordered + i * map.offset_size,
5316 map.offset_size,
5317 map.dwarf5_byte_order));
5318 /* We don't know the length of the CU, because the CU list in a
5319 .debug_names index can be incomplete, so we can't use the start of
5320 the next CU as end of this CU. We create the CUs here with length 0,
5321 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5322 dwarf2_per_cu_data *per_cu
5323 = create_cu_from_index_list (per_bfd, &section, is_dwz, sect_off, 0);
5324 per_bfd->all_comp_units.push_back (per_cu);
5325 }
5326 }
5327
5328 sect_offset sect_off_prev;
5329 for (uint32_t i = 0; i <= map.cu_count; ++i)
5330 {
5331 sect_offset sect_off_next;
5332 if (i < map.cu_count)
5333 {
5334 sect_off_next
5335 = (sect_offset) (extract_unsigned_integer
5336 (map.cu_table_reordered + i * map.offset_size,
5337 map.offset_size,
5338 map.dwarf5_byte_order));
5339 }
5340 else
5341 sect_off_next = (sect_offset) section.size;
5342 if (i >= 1)
5343 {
5344 const ULONGEST length = sect_off_next - sect_off_prev;
5345 dwarf2_per_cu_data *per_cu
5346 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5347 sect_off_prev, length);
5348 per_bfd->all_comp_units.push_back (per_cu);
5349 }
5350 sect_off_prev = sect_off_next;
5351 }
5352 }
5353
5354 /* Read the CU list from the mapped index, and use it to create all
5355 the CU objects for this dwarf2_per_objfile. */
5356
5357 static void
5358 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
5359 const mapped_debug_names &map,
5360 const mapped_debug_names &dwz_map)
5361 {
5362 gdb_assert (per_bfd->all_comp_units.empty ());
5363 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5364
5365 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
5366 false /* is_dwz */);
5367
5368 if (dwz_map.cu_count == 0)
5369 return;
5370
5371 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5372 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
5373 true /* is_dwz */);
5374 }
5375
5376 /* Read .debug_names. If everything went ok, initialize the "quick"
5377 elements of all the CUs and return true. Otherwise, return false. */
5378
5379 static bool
5380 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
5381 {
5382 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5383 mapped_debug_names dwz_map;
5384 struct objfile *objfile = per_objfile->objfile;
5385 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5386
5387 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5388 &per_objfile->per_bfd->debug_names, *map))
5389 return false;
5390
5391 /* Don't use the index if it's empty. */
5392 if (map->name_count == 0)
5393 return false;
5394
5395 /* If there is a .dwz file, read it so we can get its CU list as
5396 well. */
5397 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5398 if (dwz != NULL)
5399 {
5400 if (!read_debug_names_from_section (objfile,
5401 bfd_get_filename (dwz->dwz_bfd.get ()),
5402 &dwz->debug_names, dwz_map))
5403 {
5404 warning (_("could not read '.debug_names' section from %s; skipping"),
5405 bfd_get_filename (dwz->dwz_bfd.get ()));
5406 return false;
5407 }
5408 }
5409
5410 create_cus_from_debug_names (per_bfd, *map, dwz_map);
5411
5412 if (map->tu_count != 0)
5413 {
5414 /* We can only handle a single .debug_types when we have an
5415 index. */
5416 if (per_bfd->types.size () != 1)
5417 return false;
5418
5419 dwarf2_section_info *section = &per_bfd->types[0];
5420
5421 create_signatured_type_table_from_debug_names
5422 (per_objfile, *map, section, &per_bfd->abbrev);
5423 }
5424
5425 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
5426
5427 per_bfd->debug_names_table = std::move (map);
5428 per_bfd->using_index = 1;
5429 per_bfd->quick_file_names_table =
5430 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
5431
5432 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
5433 objfiles using the same BFD. */
5434 gdb_assert (per_bfd->partial_symtabs == nullptr);
5435 per_bfd->partial_symtabs = objfile->partial_symtabs;
5436
5437 return true;
5438 }
5439
5440 /* Type used to manage iterating over all CUs looking for a symbol for
5441 .debug_names. */
5442
5443 class dw2_debug_names_iterator
5444 {
5445 public:
5446 dw2_debug_names_iterator (const mapped_debug_names &map,
5447 gdb::optional<block_enum> block_index,
5448 domain_enum domain,
5449 const char *name, dwarf2_per_objfile *per_objfile)
5450 : m_map (map), m_block_index (block_index), m_domain (domain),
5451 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5452 m_per_objfile (per_objfile)
5453 {}
5454
5455 dw2_debug_names_iterator (const mapped_debug_names &map,
5456 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
5457 : m_map (map),
5458 m_search (search),
5459 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5460 m_per_objfile (per_objfile)
5461 {}
5462
5463 dw2_debug_names_iterator (const mapped_debug_names &map,
5464 block_enum block_index, domain_enum domain,
5465 uint32_t namei, dwarf2_per_objfile *per_objfile)
5466 : m_map (map), m_block_index (block_index), m_domain (domain),
5467 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5468 m_per_objfile (per_objfile)
5469 {}
5470
5471 /* Return the next matching CU or NULL if there are no more. */
5472 dwarf2_per_cu_data *next ();
5473
5474 private:
5475 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5476 const char *name,
5477 dwarf2_per_objfile *per_objfile);
5478 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5479 uint32_t namei,
5480 dwarf2_per_objfile *per_objfile);
5481
5482 /* The internalized form of .debug_names. */
5483 const mapped_debug_names &m_map;
5484
5485 /* If set, only look for symbols that match that block. Valid values are
5486 GLOBAL_BLOCK and STATIC_BLOCK. */
5487 const gdb::optional<block_enum> m_block_index;
5488
5489 /* The kind of symbol we're looking for. */
5490 const domain_enum m_domain = UNDEF_DOMAIN;
5491 const search_domain m_search = ALL_DOMAIN;
5492
5493 /* The list of CUs from the index entry of the symbol, or NULL if
5494 not found. */
5495 const gdb_byte *m_addr;
5496
5497 dwarf2_per_objfile *m_per_objfile;
5498 };
5499
5500 const char *
5501 mapped_debug_names::namei_to_name
5502 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
5503 {
5504 const ULONGEST namei_string_offs
5505 = extract_unsigned_integer ((name_table_string_offs_reordered
5506 + namei * offset_size),
5507 offset_size,
5508 dwarf5_byte_order);
5509 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
5510 }
5511
5512 /* Find a slot in .debug_names for the object named NAME. If NAME is
5513 found, return pointer to its pool data. If NAME cannot be found,
5514 return NULL. */
5515
5516 const gdb_byte *
5517 dw2_debug_names_iterator::find_vec_in_debug_names
5518 (const mapped_debug_names &map, const char *name,
5519 dwarf2_per_objfile *per_objfile)
5520 {
5521 int (*cmp) (const char *, const char *);
5522
5523 gdb::unique_xmalloc_ptr<char> without_params;
5524 if (current_language->la_language == language_cplus
5525 || current_language->la_language == language_fortran
5526 || current_language->la_language == language_d)
5527 {
5528 /* NAME is already canonical. Drop any qualifiers as
5529 .debug_names does not contain any. */
5530
5531 if (strchr (name, '(') != NULL)
5532 {
5533 without_params = cp_remove_params (name);
5534 if (without_params != NULL)
5535 name = without_params.get ();
5536 }
5537 }
5538
5539 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5540
5541 const uint32_t full_hash = dwarf5_djb_hash (name);
5542 uint32_t namei
5543 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5544 (map.bucket_table_reordered
5545 + (full_hash % map.bucket_count)), 4,
5546 map.dwarf5_byte_order);
5547 if (namei == 0)
5548 return NULL;
5549 --namei;
5550 if (namei >= map.name_count)
5551 {
5552 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5553 "[in module %s]"),
5554 namei, map.name_count,
5555 objfile_name (per_objfile->objfile));
5556 return NULL;
5557 }
5558
5559 for (;;)
5560 {
5561 const uint32_t namei_full_hash
5562 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5563 (map.hash_table_reordered + namei), 4,
5564 map.dwarf5_byte_order);
5565 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5566 return NULL;
5567
5568 if (full_hash == namei_full_hash)
5569 {
5570 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5571
5572 #if 0 /* An expensive sanity check. */
5573 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5574 {
5575 complaint (_("Wrong .debug_names hash for string at index %u "
5576 "[in module %s]"),
5577 namei, objfile_name (dwarf2_per_objfile->objfile));
5578 return NULL;
5579 }
5580 #endif
5581
5582 if (cmp (namei_string, name) == 0)
5583 {
5584 const ULONGEST namei_entry_offs
5585 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5586 + namei * map.offset_size),
5587 map.offset_size, map.dwarf5_byte_order);
5588 return map.entry_pool + namei_entry_offs;
5589 }
5590 }
5591
5592 ++namei;
5593 if (namei >= map.name_count)
5594 return NULL;
5595 }
5596 }
5597
5598 const gdb_byte *
5599 dw2_debug_names_iterator::find_vec_in_debug_names
5600 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5601 {
5602 if (namei >= map.name_count)
5603 {
5604 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5605 "[in module %s]"),
5606 namei, map.name_count,
5607 objfile_name (per_objfile->objfile));
5608 return NULL;
5609 }
5610
5611 const ULONGEST namei_entry_offs
5612 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5613 + namei * map.offset_size),
5614 map.offset_size, map.dwarf5_byte_order);
5615 return map.entry_pool + namei_entry_offs;
5616 }
5617
5618 /* See dw2_debug_names_iterator. */
5619
5620 dwarf2_per_cu_data *
5621 dw2_debug_names_iterator::next ()
5622 {
5623 if (m_addr == NULL)
5624 return NULL;
5625
5626 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5627 struct objfile *objfile = m_per_objfile->objfile;
5628 bfd *const abfd = objfile->obfd;
5629
5630 again:
5631
5632 unsigned int bytes_read;
5633 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5634 m_addr += bytes_read;
5635 if (abbrev == 0)
5636 return NULL;
5637
5638 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5639 if (indexval_it == m_map.abbrev_map.cend ())
5640 {
5641 complaint (_("Wrong .debug_names undefined abbrev code %s "
5642 "[in module %s]"),
5643 pulongest (abbrev), objfile_name (objfile));
5644 return NULL;
5645 }
5646 const mapped_debug_names::index_val &indexval = indexval_it->second;
5647 enum class symbol_linkage {
5648 unknown,
5649 static_,
5650 extern_,
5651 } symbol_linkage_ = symbol_linkage::unknown;
5652 dwarf2_per_cu_data *per_cu = NULL;
5653 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5654 {
5655 ULONGEST ull;
5656 switch (attr.form)
5657 {
5658 case DW_FORM_implicit_const:
5659 ull = attr.implicit_const;
5660 break;
5661 case DW_FORM_flag_present:
5662 ull = 1;
5663 break;
5664 case DW_FORM_udata:
5665 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5666 m_addr += bytes_read;
5667 break;
5668 case DW_FORM_ref4:
5669 ull = read_4_bytes (abfd, m_addr);
5670 m_addr += 4;
5671 break;
5672 case DW_FORM_ref8:
5673 ull = read_8_bytes (abfd, m_addr);
5674 m_addr += 8;
5675 break;
5676 case DW_FORM_ref_sig8:
5677 ull = read_8_bytes (abfd, m_addr);
5678 m_addr += 8;
5679 break;
5680 default:
5681 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5682 dwarf_form_name (attr.form),
5683 objfile_name (objfile));
5684 return NULL;
5685 }
5686 switch (attr.dw_idx)
5687 {
5688 case DW_IDX_compile_unit:
5689 /* Don't crash on bad data. */
5690 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
5691 {
5692 complaint (_(".debug_names entry has bad CU index %s"
5693 " [in module %s]"),
5694 pulongest (ull),
5695 objfile_name (objfile));
5696 continue;
5697 }
5698 per_cu = per_bfd->get_cutu (ull);
5699 break;
5700 case DW_IDX_type_unit:
5701 /* Don't crash on bad data. */
5702 if (ull >= per_bfd->all_type_units.size ())
5703 {
5704 complaint (_(".debug_names entry has bad TU index %s"
5705 " [in module %s]"),
5706 pulongest (ull),
5707 objfile_name (objfile));
5708 continue;
5709 }
5710 per_cu = &per_bfd->get_tu (ull)->per_cu;
5711 break;
5712 case DW_IDX_die_offset:
5713 /* In a per-CU index (as opposed to a per-module index), index
5714 entries without CU attribute implicitly refer to the single CU. */
5715 if (per_cu == NULL)
5716 per_cu = per_bfd->get_cu (0);
5717 break;
5718 case DW_IDX_GNU_internal:
5719 if (!m_map.augmentation_is_gdb)
5720 break;
5721 symbol_linkage_ = symbol_linkage::static_;
5722 break;
5723 case DW_IDX_GNU_external:
5724 if (!m_map.augmentation_is_gdb)
5725 break;
5726 symbol_linkage_ = symbol_linkage::extern_;
5727 break;
5728 }
5729 }
5730
5731 /* Skip if already read in. */
5732 if (m_per_objfile->symtab_set_p (per_cu))
5733 goto again;
5734
5735 /* Check static vs global. */
5736 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5737 {
5738 const bool want_static = *m_block_index == STATIC_BLOCK;
5739 const bool symbol_is_static =
5740 symbol_linkage_ == symbol_linkage::static_;
5741 if (want_static != symbol_is_static)
5742 goto again;
5743 }
5744
5745 /* Match dw2_symtab_iter_next, symbol_kind
5746 and debug_names::psymbol_tag. */
5747 switch (m_domain)
5748 {
5749 case VAR_DOMAIN:
5750 switch (indexval.dwarf_tag)
5751 {
5752 case DW_TAG_variable:
5753 case DW_TAG_subprogram:
5754 /* Some types are also in VAR_DOMAIN. */
5755 case DW_TAG_typedef:
5756 case DW_TAG_structure_type:
5757 break;
5758 default:
5759 goto again;
5760 }
5761 break;
5762 case STRUCT_DOMAIN:
5763 switch (indexval.dwarf_tag)
5764 {
5765 case DW_TAG_typedef:
5766 case DW_TAG_structure_type:
5767 break;
5768 default:
5769 goto again;
5770 }
5771 break;
5772 case LABEL_DOMAIN:
5773 switch (indexval.dwarf_tag)
5774 {
5775 case 0:
5776 case DW_TAG_variable:
5777 break;
5778 default:
5779 goto again;
5780 }
5781 break;
5782 case MODULE_DOMAIN:
5783 switch (indexval.dwarf_tag)
5784 {
5785 case DW_TAG_module:
5786 break;
5787 default:
5788 goto again;
5789 }
5790 break;
5791 default:
5792 break;
5793 }
5794
5795 /* Match dw2_expand_symtabs_matching, symbol_kind and
5796 debug_names::psymbol_tag. */
5797 switch (m_search)
5798 {
5799 case VARIABLES_DOMAIN:
5800 switch (indexval.dwarf_tag)
5801 {
5802 case DW_TAG_variable:
5803 break;
5804 default:
5805 goto again;
5806 }
5807 break;
5808 case FUNCTIONS_DOMAIN:
5809 switch (indexval.dwarf_tag)
5810 {
5811 case DW_TAG_subprogram:
5812 break;
5813 default:
5814 goto again;
5815 }
5816 break;
5817 case TYPES_DOMAIN:
5818 switch (indexval.dwarf_tag)
5819 {
5820 case DW_TAG_typedef:
5821 case DW_TAG_structure_type:
5822 break;
5823 default:
5824 goto again;
5825 }
5826 break;
5827 case MODULES_DOMAIN:
5828 switch (indexval.dwarf_tag)
5829 {
5830 case DW_TAG_module:
5831 break;
5832 default:
5833 goto again;
5834 }
5835 default:
5836 break;
5837 }
5838
5839 return per_cu;
5840 }
5841
5842 static struct compunit_symtab *
5843 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5844 const char *name, domain_enum domain)
5845 {
5846 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5847
5848 const auto &mapp = per_objfile->per_bfd->debug_names_table;
5849 if (!mapp)
5850 {
5851 /* index is NULL if OBJF_READNOW. */
5852 return NULL;
5853 }
5854 const auto &map = *mapp;
5855
5856 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
5857
5858 struct compunit_symtab *stab_best = NULL;
5859 struct dwarf2_per_cu_data *per_cu;
5860 while ((per_cu = iter.next ()) != NULL)
5861 {
5862 struct symbol *sym, *with_opaque = NULL;
5863 compunit_symtab *stab
5864 = dw2_instantiate_symtab (per_cu, per_objfile, false);
5865 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5866 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5867
5868 sym = block_find_symbol (block, name, domain,
5869 block_find_non_opaque_type_preferred,
5870 &with_opaque);
5871
5872 /* Some caution must be observed with overloaded functions and
5873 methods, since the index will not contain any overload
5874 information (but NAME might contain it). */
5875
5876 if (sym != NULL
5877 && strcmp_iw (sym->search_name (), name) == 0)
5878 return stab;
5879 if (with_opaque != NULL
5880 && strcmp_iw (with_opaque->search_name (), name) == 0)
5881 stab_best = stab;
5882
5883 /* Keep looking through other CUs. */
5884 }
5885
5886 return stab_best;
5887 }
5888
5889 /* This dumps minimal information about .debug_names. It is called
5890 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5891 uses this to verify that .debug_names has been loaded. */
5892
5893 static void
5894 dw2_debug_names_dump (struct objfile *objfile)
5895 {
5896 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5897
5898 gdb_assert (per_objfile->per_bfd->using_index);
5899 printf_filtered (".debug_names:");
5900 if (per_objfile->per_bfd->debug_names_table)
5901 printf_filtered (" exists\n");
5902 else
5903 printf_filtered (" faked for \"readnow\"\n");
5904 printf_filtered ("\n");
5905 }
5906
5907 static void
5908 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5909 const char *func_name)
5910 {
5911 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5912
5913 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5914 if (per_objfile->per_bfd->debug_names_table)
5915 {
5916 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5917
5918 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
5919 per_objfile);
5920
5921 struct dwarf2_per_cu_data *per_cu;
5922 while ((per_cu = iter.next ()) != NULL)
5923 dw2_instantiate_symtab (per_cu, per_objfile, false);
5924 }
5925 }
5926
5927 static void
5928 dw2_debug_names_map_matching_symbols
5929 (struct objfile *objfile,
5930 const lookup_name_info &name, domain_enum domain,
5931 int global,
5932 gdb::function_view<symbol_found_callback_ftype> callback,
5933 symbol_compare_ftype *ordered_compare)
5934 {
5935 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5936
5937 /* debug_names_table is NULL if OBJF_READNOW. */
5938 if (!per_objfile->per_bfd->debug_names_table)
5939 return;
5940
5941 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5942 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5943
5944 const char *match_name = name.ada ().lookup_name ().c_str ();
5945 auto matcher = [&] (const char *symname)
5946 {
5947 if (ordered_compare == nullptr)
5948 return true;
5949 return ordered_compare (symname, match_name) == 0;
5950 };
5951
5952 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5953 [&] (offset_type namei)
5954 {
5955 /* The name was matched, now expand corresponding CUs that were
5956 marked. */
5957 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
5958 per_objfile);
5959
5960 struct dwarf2_per_cu_data *per_cu;
5961 while ((per_cu = iter.next ()) != NULL)
5962 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5963 nullptr);
5964 return true;
5965 }, per_objfile);
5966
5967 /* It's a shame we couldn't do this inside the
5968 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5969 that have already been expanded. Instead, this loop matches what
5970 the psymtab code does. */
5971 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5972 {
5973 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
5974 if (symtab != nullptr)
5975 {
5976 const struct block *block
5977 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5978 if (!iterate_over_symbols_terminated (block, name,
5979 domain, callback))
5980 break;
5981 }
5982 }
5983 }
5984
5985 static void
5986 dw2_debug_names_expand_symtabs_matching
5987 (struct objfile *objfile,
5988 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5989 const lookup_name_info *lookup_name,
5990 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5991 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5992 enum search_domain kind)
5993 {
5994 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5995
5996 /* debug_names_table is NULL if OBJF_READNOW. */
5997 if (!per_objfile->per_bfd->debug_names_table)
5998 return;
5999
6000 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
6001
6002 if (symbol_matcher == NULL && lookup_name == NULL)
6003 {
6004 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
6005 {
6006 QUIT;
6007
6008 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
6009 expansion_notify);
6010 }
6011 return;
6012 }
6013
6014 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
6015
6016 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
6017 symbol_matcher,
6018 kind, [&] (offset_type namei)
6019 {
6020 /* The name was matched, now expand corresponding CUs that were
6021 marked. */
6022 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
6023
6024 struct dwarf2_per_cu_data *per_cu;
6025 while ((per_cu = iter.next ()) != NULL)
6026 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
6027 expansion_notify);
6028 return true;
6029 }, per_objfile);
6030 }
6031
6032 const struct quick_symbol_functions dwarf2_debug_names_functions =
6033 {
6034 dw2_has_symbols,
6035 dw2_find_last_source_symtab,
6036 dw2_forget_cached_source_info,
6037 dw2_map_symtabs_matching_filename,
6038 dw2_debug_names_lookup_symbol,
6039 NULL,
6040 dw2_print_stats,
6041 dw2_debug_names_dump,
6042 dw2_debug_names_expand_symtabs_for_function,
6043 dw2_expand_all_symtabs,
6044 dw2_expand_symtabs_with_fullname,
6045 dw2_debug_names_map_matching_symbols,
6046 dw2_debug_names_expand_symtabs_matching,
6047 dw2_find_pc_sect_compunit_symtab,
6048 NULL,
6049 dw2_map_symbol_filenames
6050 };
6051
6052 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6053 to either a dwarf2_per_bfd or dwz_file object. */
6054
6055 template <typename T>
6056 static gdb::array_view<const gdb_byte>
6057 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6058 {
6059 dwarf2_section_info *section = &section_owner->gdb_index;
6060
6061 if (section->empty ())
6062 return {};
6063
6064 /* Older elfutils strip versions could keep the section in the main
6065 executable while splitting it for the separate debug info file. */
6066 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
6067 return {};
6068
6069 section->read (obj);
6070
6071 /* dwarf2_section_info::size is a bfd_size_type, while
6072 gdb::array_view works with size_t. On 32-bit hosts, with
6073 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6074 is 32-bit. So we need an explicit narrowing conversion here.
6075 This is fine, because it's impossible to allocate or mmap an
6076 array/buffer larger than what size_t can represent. */
6077 return gdb::make_array_view (section->buffer, section->size);
6078 }
6079
6080 /* Lookup the index cache for the contents of the index associated to
6081 DWARF2_OBJ. */
6082
6083 static gdb::array_view<const gdb_byte>
6084 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
6085 {
6086 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6087 if (build_id == nullptr)
6088 return {};
6089
6090 return global_index_cache.lookup_gdb_index (build_id,
6091 &dwarf2_per_bfd->index_cache_res);
6092 }
6093
6094 /* Same as the above, but for DWZ. */
6095
6096 static gdb::array_view<const gdb_byte>
6097 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6098 {
6099 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6100 if (build_id == nullptr)
6101 return {};
6102
6103 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6104 }
6105
6106 /* See symfile.h. */
6107
6108 bool
6109 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6110 {
6111 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6112 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6113
6114 /* If we're about to read full symbols, don't bother with the
6115 indices. In this case we also don't care if some other debug
6116 format is making psymtabs, because they are all about to be
6117 expanded anyway. */
6118 if ((objfile->flags & OBJF_READNOW))
6119 {
6120 /* When using READNOW, the using_index flag (set below) indicates that
6121 PER_BFD was already initialized, when we loaded some other objfile. */
6122 if (per_bfd->using_index)
6123 {
6124 *index_kind = dw_index_kind::GDB_INDEX;
6125 per_objfile->resize_symtabs ();
6126 return true;
6127 }
6128
6129 per_bfd->using_index = 1;
6130 create_all_comp_units (per_objfile);
6131 create_all_type_units (per_objfile);
6132 per_bfd->quick_file_names_table
6133 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
6134 per_objfile->resize_symtabs ();
6135
6136 for (int i = 0; i < (per_bfd->all_comp_units.size ()
6137 + per_bfd->all_type_units.size ()); ++i)
6138 {
6139 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
6140
6141 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6142 struct dwarf2_per_cu_quick_data);
6143 }
6144
6145 /* Return 1 so that gdb sees the "quick" functions. However,
6146 these functions will be no-ops because we will have expanded
6147 all symtabs. */
6148 *index_kind = dw_index_kind::GDB_INDEX;
6149 return true;
6150 }
6151
6152 /* Was a debug names index already read when we processed an objfile sharing
6153 PER_BFD? */
6154 if (per_bfd->debug_names_table != nullptr)
6155 {
6156 *index_kind = dw_index_kind::DEBUG_NAMES;
6157 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
6158 per_objfile->resize_symtabs ();
6159 return true;
6160 }
6161
6162 /* Was a GDB index already read when we processed an objfile sharing
6163 PER_BFD? */
6164 if (per_bfd->index_table != nullptr)
6165 {
6166 *index_kind = dw_index_kind::GDB_INDEX;
6167 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
6168 per_objfile->resize_symtabs ();
6169 return true;
6170 }
6171
6172 /* There might already be partial symtabs built for this BFD. This happens
6173 when loading the same binary twice with the index-cache enabled. If so,
6174 don't try to read an index. The objfile / per_objfile initialization will
6175 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
6176 code path. */
6177 if (per_bfd->partial_symtabs != nullptr)
6178 return false;
6179
6180 if (dwarf2_read_debug_names (per_objfile))
6181 {
6182 *index_kind = dw_index_kind::DEBUG_NAMES;
6183 per_objfile->resize_symtabs ();
6184 return true;
6185 }
6186
6187 if (dwarf2_read_gdb_index (per_objfile,
6188 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
6189 get_gdb_index_contents_from_section<dwz_file>))
6190 {
6191 *index_kind = dw_index_kind::GDB_INDEX;
6192 per_objfile->resize_symtabs ();
6193 return true;
6194 }
6195
6196 /* ... otherwise, try to find the index in the index cache. */
6197 if (dwarf2_read_gdb_index (per_objfile,
6198 get_gdb_index_contents_from_cache,
6199 get_gdb_index_contents_from_cache_dwz))
6200 {
6201 global_index_cache.hit ();
6202 *index_kind = dw_index_kind::GDB_INDEX;
6203 per_objfile->resize_symtabs ();
6204 return true;
6205 }
6206
6207 global_index_cache.miss ();
6208 return false;
6209 }
6210
6211 \f
6212
6213 /* Build a partial symbol table. */
6214
6215 void
6216 dwarf2_build_psymtabs (struct objfile *objfile)
6217 {
6218 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6219 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6220
6221 if (per_bfd->partial_symtabs != nullptr)
6222 {
6223 /* Partial symbols were already read, so now we can simply
6224 attach them. */
6225 objfile->partial_symtabs = per_bfd->partial_symtabs;
6226 per_objfile->resize_symtabs ();
6227 return;
6228 }
6229
6230 try
6231 {
6232 /* This isn't really ideal: all the data we allocate on the
6233 objfile's obstack is still uselessly kept around. However,
6234 freeing it seems unsafe. */
6235 psymtab_discarder psymtabs (objfile);
6236 dwarf2_build_psymtabs_hard (per_objfile);
6237 psymtabs.keep ();
6238
6239 per_objfile->resize_symtabs ();
6240
6241 /* (maybe) store an index in the cache. */
6242 global_index_cache.store (per_objfile);
6243 }
6244 catch (const gdb_exception_error &except)
6245 {
6246 exception_print (gdb_stderr, except);
6247 }
6248
6249 /* Finish by setting the local reference to partial symtabs, so that
6250 we don't try to read them again if reading another objfile with the same
6251 BFD. If we can't in fact share, this won't make a difference anyway as
6252 the dwarf2_per_bfd object won't be shared. */
6253 per_bfd->partial_symtabs = objfile->partial_symtabs;
6254 }
6255
6256 /* Find the base address of the compilation unit for range lists and
6257 location lists. It will normally be specified by DW_AT_low_pc.
6258 In DWARF-3 draft 4, the base address could be overridden by
6259 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6260 compilation units with discontinuous ranges. */
6261
6262 static void
6263 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6264 {
6265 struct attribute *attr;
6266
6267 cu->base_address.reset ();
6268
6269 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6270 if (attr != nullptr)
6271 cu->base_address = attr->as_address ();
6272 else
6273 {
6274 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6275 if (attr != nullptr)
6276 cu->base_address = attr->as_address ();
6277 }
6278 }
6279
6280 /* Helper function that returns the proper abbrev section for
6281 THIS_CU. */
6282
6283 static struct dwarf2_section_info *
6284 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6285 {
6286 struct dwarf2_section_info *abbrev;
6287 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6288
6289 if (this_cu->is_dwz)
6290 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
6291 else
6292 abbrev = &per_bfd->abbrev;
6293
6294 return abbrev;
6295 }
6296
6297 /* Fetch the abbreviation table offset from a comp or type unit header. */
6298
6299 static sect_offset
6300 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
6301 struct dwarf2_section_info *section,
6302 sect_offset sect_off)
6303 {
6304 bfd *abfd = section->get_bfd_owner ();
6305 const gdb_byte *info_ptr;
6306 unsigned int initial_length_size, offset_size;
6307 uint16_t version;
6308
6309 section->read (per_objfile->objfile);
6310 info_ptr = section->buffer + to_underlying (sect_off);
6311 read_initial_length (abfd, info_ptr, &initial_length_size);
6312 offset_size = initial_length_size == 4 ? 4 : 8;
6313 info_ptr += initial_length_size;
6314
6315 version = read_2_bytes (abfd, info_ptr);
6316 info_ptr += 2;
6317 if (version >= 5)
6318 {
6319 /* Skip unit type and address size. */
6320 info_ptr += 2;
6321 }
6322
6323 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6324 }
6325
6326 /* A partial symtab that is used only for include files. */
6327 struct dwarf2_include_psymtab : public partial_symtab
6328 {
6329 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6330 : partial_symtab (filename, objfile)
6331 {
6332 }
6333
6334 void read_symtab (struct objfile *objfile) override
6335 {
6336 /* It's an include file, no symbols to read for it.
6337 Everything is in the includer symtab. */
6338
6339 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6340 expansion of the includer psymtab. We use the dependencies[0] field to
6341 model the includer. But if we go the regular route of calling
6342 expand_psymtab here, and having expand_psymtab call expand_dependencies
6343 to expand the includer, we'll only use expand_psymtab on the includer
6344 (making it a non-toplevel psymtab), while if we expand the includer via
6345 another path, we'll use read_symtab (making it a toplevel psymtab).
6346 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6347 psymtab, and trigger read_symtab on the includer here directly. */
6348 includer ()->read_symtab (objfile);
6349 }
6350
6351 void expand_psymtab (struct objfile *objfile) override
6352 {
6353 /* This is not called by read_symtab, and should not be called by any
6354 expand_dependencies. */
6355 gdb_assert (false);
6356 }
6357
6358 bool readin_p (struct objfile *objfile) const override
6359 {
6360 return includer ()->readin_p (objfile);
6361 }
6362
6363 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6364 {
6365 return nullptr;
6366 }
6367
6368 private:
6369 partial_symtab *includer () const
6370 {
6371 /* An include psymtab has exactly one dependency: the psymtab that
6372 includes it. */
6373 gdb_assert (this->number_of_dependencies == 1);
6374 return this->dependencies[0];
6375 }
6376 };
6377
6378 /* Allocate a new partial symtab for file named NAME and mark this new
6379 partial symtab as being an include of PST. */
6380
6381 static void
6382 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6383 struct objfile *objfile)
6384 {
6385 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6386
6387 if (!IS_ABSOLUTE_PATH (subpst->filename))
6388 subpst->dirname = pst->dirname;
6389
6390 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6391 subpst->dependencies[0] = pst;
6392 subpst->number_of_dependencies = 1;
6393 }
6394
6395 /* Read the Line Number Program data and extract the list of files
6396 included by the source file represented by PST. Build an include
6397 partial symtab for each of these included files. */
6398
6399 static void
6400 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6401 struct die_info *die,
6402 dwarf2_psymtab *pst)
6403 {
6404 line_header_up lh;
6405 struct attribute *attr;
6406
6407 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6408 if (attr != nullptr && attr->form_is_unsigned ())
6409 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
6410 if (lh == NULL)
6411 return; /* No linetable, so no includes. */
6412
6413 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6414 that we pass in the raw text_low here; that is ok because we're
6415 only decoding the line table to make include partial symtabs, and
6416 so the addresses aren't really used. */
6417 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6418 pst->raw_text_low (), 1);
6419 }
6420
6421 static hashval_t
6422 hash_signatured_type (const void *item)
6423 {
6424 const struct signatured_type *sig_type
6425 = (const struct signatured_type *) item;
6426
6427 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6428 return sig_type->signature;
6429 }
6430
6431 static int
6432 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6433 {
6434 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6435 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6436
6437 return lhs->signature == rhs->signature;
6438 }
6439
6440 /* Allocate a hash table for signatured types. */
6441
6442 static htab_up
6443 allocate_signatured_type_table ()
6444 {
6445 return htab_up (htab_create_alloc (41,
6446 hash_signatured_type,
6447 eq_signatured_type,
6448 NULL, xcalloc, xfree));
6449 }
6450
6451 /* A helper function to add a signatured type CU to a table. */
6452
6453 static int
6454 add_signatured_type_cu_to_table (void **slot, void *datum)
6455 {
6456 struct signatured_type *sigt = (struct signatured_type *) *slot;
6457 std::vector<signatured_type *> *all_type_units
6458 = (std::vector<signatured_type *> *) datum;
6459
6460 all_type_units->push_back (sigt);
6461
6462 return 1;
6463 }
6464
6465 /* A helper for create_debug_types_hash_table. Read types from SECTION
6466 and fill them into TYPES_HTAB. It will process only type units,
6467 therefore DW_UT_type. */
6468
6469 static void
6470 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
6471 struct dwo_file *dwo_file,
6472 dwarf2_section_info *section, htab_up &types_htab,
6473 rcuh_kind section_kind)
6474 {
6475 struct objfile *objfile = per_objfile->objfile;
6476 struct dwarf2_section_info *abbrev_section;
6477 bfd *abfd;
6478 const gdb_byte *info_ptr, *end_ptr;
6479
6480 abbrev_section = (dwo_file != NULL
6481 ? &dwo_file->sections.abbrev
6482 : &per_objfile->per_bfd->abbrev);
6483
6484 dwarf_read_debug_printf ("Reading %s for %s:",
6485 section->get_name (),
6486 abbrev_section->get_file_name ());
6487
6488 section->read (objfile);
6489 info_ptr = section->buffer;
6490
6491 if (info_ptr == NULL)
6492 return;
6493
6494 /* We can't set abfd until now because the section may be empty or
6495 not present, in which case the bfd is unknown. */
6496 abfd = section->get_bfd_owner ();
6497
6498 /* We don't use cutu_reader here because we don't need to read
6499 any dies: the signature is in the header. */
6500
6501 end_ptr = info_ptr + section->size;
6502 while (info_ptr < end_ptr)
6503 {
6504 struct signatured_type *sig_type;
6505 struct dwo_unit *dwo_tu;
6506 void **slot;
6507 const gdb_byte *ptr = info_ptr;
6508 struct comp_unit_head header;
6509 unsigned int length;
6510
6511 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6512
6513 /* Initialize it due to a false compiler warning. */
6514 header.signature = -1;
6515 header.type_cu_offset_in_tu = (cu_offset) -1;
6516
6517 /* We need to read the type's signature in order to build the hash
6518 table, but we don't need anything else just yet. */
6519
6520 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
6521 abbrev_section, ptr, section_kind);
6522
6523 length = header.get_length ();
6524
6525 /* Skip dummy type units. */
6526 if (ptr >= info_ptr + length
6527 || peek_abbrev_code (abfd, ptr) == 0
6528 || (header.unit_type != DW_UT_type
6529 && header.unit_type != DW_UT_split_type))
6530 {
6531 info_ptr += length;
6532 continue;
6533 }
6534
6535 if (types_htab == NULL)
6536 {
6537 if (dwo_file)
6538 types_htab = allocate_dwo_unit_table ();
6539 else
6540 types_htab = allocate_signatured_type_table ();
6541 }
6542
6543 if (dwo_file)
6544 {
6545 sig_type = NULL;
6546 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
6547 dwo_tu->dwo_file = dwo_file;
6548 dwo_tu->signature = header.signature;
6549 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6550 dwo_tu->section = section;
6551 dwo_tu->sect_off = sect_off;
6552 dwo_tu->length = length;
6553 }
6554 else
6555 {
6556 /* N.B.: type_offset is not usable if this type uses a DWO file.
6557 The real type_offset is in the DWO file. */
6558 dwo_tu = NULL;
6559 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6560 sig_type->signature = header.signature;
6561 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6562 sig_type->per_cu.is_debug_types = 1;
6563 sig_type->per_cu.section = section;
6564 sig_type->per_cu.sect_off = sect_off;
6565 sig_type->per_cu.length = length;
6566 }
6567
6568 slot = htab_find_slot (types_htab.get (),
6569 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6570 INSERT);
6571 gdb_assert (slot != NULL);
6572 if (*slot != NULL)
6573 {
6574 sect_offset dup_sect_off;
6575
6576 if (dwo_file)
6577 {
6578 const struct dwo_unit *dup_tu
6579 = (const struct dwo_unit *) *slot;
6580
6581 dup_sect_off = dup_tu->sect_off;
6582 }
6583 else
6584 {
6585 const struct signatured_type *dup_tu
6586 = (const struct signatured_type *) *slot;
6587
6588 dup_sect_off = dup_tu->per_cu.sect_off;
6589 }
6590
6591 complaint (_("debug type entry at offset %s is duplicate to"
6592 " the entry at offset %s, signature %s"),
6593 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6594 hex_string (header.signature));
6595 }
6596 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6597
6598 dwarf_read_debug_printf_v (" offset %s, signature %s",
6599 sect_offset_str (sect_off),
6600 hex_string (header.signature));
6601
6602 info_ptr += length;
6603 }
6604 }
6605
6606 /* Create the hash table of all entries in the .debug_types
6607 (or .debug_types.dwo) section(s).
6608 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6609 otherwise it is NULL.
6610
6611 The result is a pointer to the hash table or NULL if there are no types.
6612
6613 Note: This function processes DWO files only, not DWP files. */
6614
6615 static void
6616 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
6617 struct dwo_file *dwo_file,
6618 gdb::array_view<dwarf2_section_info> type_sections,
6619 htab_up &types_htab)
6620 {
6621 for (dwarf2_section_info &section : type_sections)
6622 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6623 rcuh_kind::TYPE);
6624 }
6625
6626 /* Create the hash table of all entries in the .debug_types section,
6627 and initialize all_type_units.
6628 The result is zero if there is an error (e.g. missing .debug_types section),
6629 otherwise non-zero. */
6630
6631 static int
6632 create_all_type_units (dwarf2_per_objfile *per_objfile)
6633 {
6634 htab_up types_htab;
6635
6636 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6637 types_htab, rcuh_kind::COMPILE);
6638 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6639 types_htab);
6640 if (types_htab == NULL)
6641 {
6642 per_objfile->per_bfd->signatured_types = NULL;
6643 return 0;
6644 }
6645
6646 per_objfile->per_bfd->signatured_types = std::move (types_htab);
6647
6648 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6649 per_objfile->per_bfd->all_type_units.reserve
6650 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
6651
6652 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
6653 add_signatured_type_cu_to_table,
6654 &per_objfile->per_bfd->all_type_units);
6655
6656 return 1;
6657 }
6658
6659 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6660 If SLOT is non-NULL, it is the entry to use in the hash table.
6661 Otherwise we find one. */
6662
6663 static struct signatured_type *
6664 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6665 {
6666 if (per_objfile->per_bfd->all_type_units.size ()
6667 == per_objfile->per_bfd->all_type_units.capacity ())
6668 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6669
6670 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6671
6672 per_objfile->resize_symtabs ();
6673
6674 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6675 sig_type->signature = sig;
6676 sig_type->per_cu.is_debug_types = 1;
6677 if (per_objfile->per_bfd->using_index)
6678 {
6679 sig_type->per_cu.v.quick =
6680 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6681 struct dwarf2_per_cu_quick_data);
6682 }
6683
6684 if (slot == NULL)
6685 {
6686 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6687 sig_type, INSERT);
6688 }
6689 gdb_assert (*slot == NULL);
6690 *slot = sig_type;
6691 /* The rest of sig_type must be filled in by the caller. */
6692 return sig_type;
6693 }
6694
6695 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6696 Fill in SIG_ENTRY with DWO_ENTRY. */
6697
6698 static void
6699 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6700 struct signatured_type *sig_entry,
6701 struct dwo_unit *dwo_entry)
6702 {
6703 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6704
6705 /* Make sure we're not clobbering something we don't expect to. */
6706 gdb_assert (! sig_entry->per_cu.queued);
6707 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
6708 if (per_bfd->using_index)
6709 {
6710 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6711 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6712 }
6713 else
6714 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6715 gdb_assert (sig_entry->signature == dwo_entry->signature);
6716 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6717 gdb_assert (sig_entry->type_unit_group == NULL);
6718 gdb_assert (sig_entry->dwo_unit == NULL);
6719
6720 sig_entry->per_cu.section = dwo_entry->section;
6721 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6722 sig_entry->per_cu.length = dwo_entry->length;
6723 sig_entry->per_cu.reading_dwo_directly = 1;
6724 sig_entry->per_cu.per_bfd = per_bfd;
6725 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6726 sig_entry->dwo_unit = dwo_entry;
6727 }
6728
6729 /* Subroutine of lookup_signatured_type.
6730 If we haven't read the TU yet, create the signatured_type data structure
6731 for a TU to be read in directly from a DWO file, bypassing the stub.
6732 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6733 using .gdb_index, then when reading a CU we want to stay in the DWO file
6734 containing that CU. Otherwise we could end up reading several other DWO
6735 files (due to comdat folding) to process the transitive closure of all the
6736 mentioned TUs, and that can be slow. The current DWO file will have every
6737 type signature that it needs.
6738 We only do this for .gdb_index because in the psymtab case we already have
6739 to read all the DWOs to build the type unit groups. */
6740
6741 static struct signatured_type *
6742 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6743 {
6744 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6745 struct dwo_file *dwo_file;
6746 struct dwo_unit find_dwo_entry, *dwo_entry;
6747 struct signatured_type find_sig_entry, *sig_entry;
6748 void **slot;
6749
6750 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6751
6752 /* If TU skeletons have been removed then we may not have read in any
6753 TUs yet. */
6754 if (per_objfile->per_bfd->signatured_types == NULL)
6755 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6756
6757 /* We only ever need to read in one copy of a signatured type.
6758 Use the global signatured_types array to do our own comdat-folding
6759 of types. If this is the first time we're reading this TU, and
6760 the TU has an entry in .gdb_index, replace the recorded data from
6761 .gdb_index with this TU. */
6762
6763 find_sig_entry.signature = sig;
6764 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6765 &find_sig_entry, INSERT);
6766 sig_entry = (struct signatured_type *) *slot;
6767
6768 /* We can get here with the TU already read, *or* in the process of being
6769 read. Don't reassign the global entry to point to this DWO if that's
6770 the case. Also note that if the TU is already being read, it may not
6771 have come from a DWO, the program may be a mix of Fission-compiled
6772 code and non-Fission-compiled code. */
6773
6774 /* Have we already tried to read this TU?
6775 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6776 needn't exist in the global table yet). */
6777 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6778 return sig_entry;
6779
6780 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6781 dwo_unit of the TU itself. */
6782 dwo_file = cu->dwo_unit->dwo_file;
6783
6784 /* Ok, this is the first time we're reading this TU. */
6785 if (dwo_file->tus == NULL)
6786 return NULL;
6787 find_dwo_entry.signature = sig;
6788 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6789 &find_dwo_entry);
6790 if (dwo_entry == NULL)
6791 return NULL;
6792
6793 /* If the global table doesn't have an entry for this TU, add one. */
6794 if (sig_entry == NULL)
6795 sig_entry = add_type_unit (per_objfile, sig, slot);
6796
6797 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6798 sig_entry->per_cu.tu_read = 1;
6799 return sig_entry;
6800 }
6801
6802 /* Subroutine of lookup_signatured_type.
6803 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6804 then try the DWP file. If the TU stub (skeleton) has been removed then
6805 it won't be in .gdb_index. */
6806
6807 static struct signatured_type *
6808 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6809 {
6810 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6811 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6812 struct dwo_unit *dwo_entry;
6813 struct signatured_type find_sig_entry, *sig_entry;
6814 void **slot;
6815
6816 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6817 gdb_assert (dwp_file != NULL);
6818
6819 /* If TU skeletons have been removed then we may not have read in any
6820 TUs yet. */
6821 if (per_objfile->per_bfd->signatured_types == NULL)
6822 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6823
6824 find_sig_entry.signature = sig;
6825 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6826 &find_sig_entry, INSERT);
6827 sig_entry = (struct signatured_type *) *slot;
6828
6829 /* Have we already tried to read this TU?
6830 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6831 needn't exist in the global table yet). */
6832 if (sig_entry != NULL)
6833 return sig_entry;
6834
6835 if (dwp_file->tus == NULL)
6836 return NULL;
6837 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6838 1 /* is_debug_types */);
6839 if (dwo_entry == NULL)
6840 return NULL;
6841
6842 sig_entry = add_type_unit (per_objfile, sig, slot);
6843 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6844
6845 return sig_entry;
6846 }
6847
6848 /* Lookup a signature based type for DW_FORM_ref_sig8.
6849 Returns NULL if signature SIG is not present in the table.
6850 It is up to the caller to complain about this. */
6851
6852 static struct signatured_type *
6853 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6854 {
6855 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6856
6857 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6858 {
6859 /* We're in a DWO/DWP file, and we're using .gdb_index.
6860 These cases require special processing. */
6861 if (get_dwp_file (per_objfile) == NULL)
6862 return lookup_dwo_signatured_type (cu, sig);
6863 else
6864 return lookup_dwp_signatured_type (cu, sig);
6865 }
6866 else
6867 {
6868 struct signatured_type find_entry, *entry;
6869
6870 if (per_objfile->per_bfd->signatured_types == NULL)
6871 return NULL;
6872 find_entry.signature = sig;
6873 entry = ((struct signatured_type *)
6874 htab_find (per_objfile->per_bfd->signatured_types.get (),
6875 &find_entry));
6876 return entry;
6877 }
6878 }
6879
6880 /* Low level DIE reading support. */
6881
6882 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6883
6884 static void
6885 init_cu_die_reader (struct die_reader_specs *reader,
6886 struct dwarf2_cu *cu,
6887 struct dwarf2_section_info *section,
6888 struct dwo_file *dwo_file,
6889 struct abbrev_table *abbrev_table)
6890 {
6891 gdb_assert (section->readin && section->buffer != NULL);
6892 reader->abfd = section->get_bfd_owner ();
6893 reader->cu = cu;
6894 reader->dwo_file = dwo_file;
6895 reader->die_section = section;
6896 reader->buffer = section->buffer;
6897 reader->buffer_end = section->buffer + section->size;
6898 reader->abbrev_table = abbrev_table;
6899 }
6900
6901 /* Subroutine of cutu_reader to simplify it.
6902 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6903 There's just a lot of work to do, and cutu_reader is big enough
6904 already.
6905
6906 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6907 from it to the DIE in the DWO. If NULL we are skipping the stub.
6908 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6909 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6910 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6911 STUB_COMP_DIR may be non-NULL.
6912 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6913 are filled in with the info of the DIE from the DWO file.
6914 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6915 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6916 kept around for at least as long as *RESULT_READER.
6917
6918 The result is non-zero if a valid (non-dummy) DIE was found. */
6919
6920 static int
6921 read_cutu_die_from_dwo (dwarf2_cu *cu,
6922 struct dwo_unit *dwo_unit,
6923 struct die_info *stub_comp_unit_die,
6924 const char *stub_comp_dir,
6925 struct die_reader_specs *result_reader,
6926 const gdb_byte **result_info_ptr,
6927 struct die_info **result_comp_unit_die,
6928 abbrev_table_up *result_dwo_abbrev_table)
6929 {
6930 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6931 dwarf2_per_cu_data *per_cu = cu->per_cu;
6932 struct objfile *objfile = per_objfile->objfile;
6933 bfd *abfd;
6934 const gdb_byte *begin_info_ptr, *info_ptr;
6935 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6936 int i,num_extra_attrs;
6937 struct dwarf2_section_info *dwo_abbrev_section;
6938 struct die_info *comp_unit_die;
6939
6940 /* At most one of these may be provided. */
6941 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6942
6943 /* These attributes aren't processed until later:
6944 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6945 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6946 referenced later. However, these attributes are found in the stub
6947 which we won't have later. In order to not impose this complication
6948 on the rest of the code, we read them here and copy them to the
6949 DWO CU/TU die. */
6950
6951 stmt_list = NULL;
6952 low_pc = NULL;
6953 high_pc = NULL;
6954 ranges = NULL;
6955 comp_dir = NULL;
6956
6957 if (stub_comp_unit_die != NULL)
6958 {
6959 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6960 DWO file. */
6961 if (!per_cu->is_debug_types)
6962 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6963 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6964 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6965 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6966 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6967
6968 cu->addr_base = stub_comp_unit_die->addr_base ();
6969
6970 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6971 here (if needed). We need the value before we can process
6972 DW_AT_ranges. */
6973 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6974 }
6975 else if (stub_comp_dir != NULL)
6976 {
6977 /* Reconstruct the comp_dir attribute to simplify the code below. */
6978 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
6979 comp_dir->name = DW_AT_comp_dir;
6980 comp_dir->form = DW_FORM_string;
6981 comp_dir->set_string_noncanonical (stub_comp_dir);
6982 }
6983
6984 /* Set up for reading the DWO CU/TU. */
6985 cu->dwo_unit = dwo_unit;
6986 dwarf2_section_info *section = dwo_unit->section;
6987 section->read (objfile);
6988 abfd = section->get_bfd_owner ();
6989 begin_info_ptr = info_ptr = (section->buffer
6990 + to_underlying (dwo_unit->sect_off));
6991 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6992
6993 if (per_cu->is_debug_types)
6994 {
6995 signatured_type *sig_type = (struct signatured_type *) per_cu;
6996
6997 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6998 section, dwo_abbrev_section,
6999 info_ptr, rcuh_kind::TYPE);
7000 /* This is not an assert because it can be caused by bad debug info. */
7001 if (sig_type->signature != cu->header.signature)
7002 {
7003 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7004 " TU at offset %s [in module %s]"),
7005 hex_string (sig_type->signature),
7006 hex_string (cu->header.signature),
7007 sect_offset_str (dwo_unit->sect_off),
7008 bfd_get_filename (abfd));
7009 }
7010 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7011 /* For DWOs coming from DWP files, we don't know the CU length
7012 nor the type's offset in the TU until now. */
7013 dwo_unit->length = cu->header.get_length ();
7014 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7015
7016 /* Establish the type offset that can be used to lookup the type.
7017 For DWO files, we don't know it until now. */
7018 sig_type->type_offset_in_section
7019 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7020 }
7021 else
7022 {
7023 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7024 section, dwo_abbrev_section,
7025 info_ptr, rcuh_kind::COMPILE);
7026 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7027 /* For DWOs coming from DWP files, we don't know the CU length
7028 until now. */
7029 dwo_unit->length = cu->header.get_length ();
7030 }
7031
7032 dwo_abbrev_section->read (objfile);
7033 *result_dwo_abbrev_table
7034 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
7035 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7036 result_dwo_abbrev_table->get ());
7037
7038 /* Read in the die, but leave space to copy over the attributes
7039 from the stub. This has the benefit of simplifying the rest of
7040 the code - all the work to maintain the illusion of a single
7041 DW_TAG_{compile,type}_unit DIE is done here. */
7042 num_extra_attrs = ((stmt_list != NULL)
7043 + (low_pc != NULL)
7044 + (high_pc != NULL)
7045 + (ranges != NULL)
7046 + (comp_dir != NULL));
7047 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7048 num_extra_attrs);
7049
7050 /* Copy over the attributes from the stub to the DIE we just read in. */
7051 comp_unit_die = *result_comp_unit_die;
7052 i = comp_unit_die->num_attrs;
7053 if (stmt_list != NULL)
7054 comp_unit_die->attrs[i++] = *stmt_list;
7055 if (low_pc != NULL)
7056 comp_unit_die->attrs[i++] = *low_pc;
7057 if (high_pc != NULL)
7058 comp_unit_die->attrs[i++] = *high_pc;
7059 if (ranges != NULL)
7060 comp_unit_die->attrs[i++] = *ranges;
7061 if (comp_dir != NULL)
7062 comp_unit_die->attrs[i++] = *comp_dir;
7063 comp_unit_die->num_attrs += num_extra_attrs;
7064
7065 if (dwarf_die_debug)
7066 {
7067 fprintf_unfiltered (gdb_stdlog,
7068 "Read die from %s@0x%x of %s:\n",
7069 section->get_name (),
7070 (unsigned) (begin_info_ptr - section->buffer),
7071 bfd_get_filename (abfd));
7072 dump_die (comp_unit_die, dwarf_die_debug);
7073 }
7074
7075 /* Skip dummy compilation units. */
7076 if (info_ptr >= begin_info_ptr + dwo_unit->length
7077 || peek_abbrev_code (abfd, info_ptr) == 0)
7078 return 0;
7079
7080 *result_info_ptr = info_ptr;
7081 return 1;
7082 }
7083
7084 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7085 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7086 signature is part of the header. */
7087 static gdb::optional<ULONGEST>
7088 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7089 {
7090 if (cu->header.version >= 5)
7091 return cu->header.signature;
7092 struct attribute *attr;
7093 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7094 if (attr == nullptr || !attr->form_is_unsigned ())
7095 return gdb::optional<ULONGEST> ();
7096 return attr->as_unsigned ();
7097 }
7098
7099 /* Subroutine of cutu_reader to simplify it.
7100 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7101 Returns NULL if the specified DWO unit cannot be found. */
7102
7103 static struct dwo_unit *
7104 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
7105 {
7106 dwarf2_per_cu_data *per_cu = cu->per_cu;
7107 struct dwo_unit *dwo_unit;
7108 const char *comp_dir;
7109
7110 gdb_assert (cu != NULL);
7111
7112 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7113 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7114 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7115
7116 if (per_cu->is_debug_types)
7117 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
7118 else
7119 {
7120 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7121
7122 if (!signature.has_value ())
7123 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7124 " [in module %s]"),
7125 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
7126
7127 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
7128 }
7129
7130 return dwo_unit;
7131 }
7132
7133 /* Subroutine of cutu_reader to simplify it.
7134 See it for a description of the parameters.
7135 Read a TU directly from a DWO file, bypassing the stub. */
7136
7137 void
7138 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7139 dwarf2_per_objfile *per_objfile,
7140 dwarf2_cu *existing_cu)
7141 {
7142 struct signatured_type *sig_type;
7143
7144 /* Verify we can do the following downcast, and that we have the
7145 data we need. */
7146 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7147 sig_type = (struct signatured_type *) this_cu;
7148 gdb_assert (sig_type->dwo_unit != NULL);
7149
7150 dwarf2_cu *cu;
7151
7152 if (existing_cu != nullptr)
7153 {
7154 cu = existing_cu;
7155 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
7156 /* There's no need to do the rereading_dwo_cu handling that
7157 cutu_reader does since we don't read the stub. */
7158 }
7159 else
7160 {
7161 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7162 in per_objfile yet. */
7163 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7164 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7165 cu = m_new_cu.get ();
7166 }
7167
7168 /* A future optimization, if needed, would be to use an existing
7169 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7170 could share abbrev tables. */
7171
7172 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
7173 NULL /* stub_comp_unit_die */,
7174 sig_type->dwo_unit->dwo_file->comp_dir,
7175 this, &info_ptr,
7176 &comp_unit_die,
7177 &m_dwo_abbrev_table) == 0)
7178 {
7179 /* Dummy die. */
7180 dummy_p = true;
7181 }
7182 }
7183
7184 /* Initialize a CU (or TU) and read its DIEs.
7185 If the CU defers to a DWO file, read the DWO file as well.
7186
7187 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7188 Otherwise the table specified in the comp unit header is read in and used.
7189 This is an optimization for when we already have the abbrev table.
7190
7191 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7192 allocated. */
7193
7194 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7195 dwarf2_per_objfile *per_objfile,
7196 struct abbrev_table *abbrev_table,
7197 dwarf2_cu *existing_cu,
7198 bool skip_partial)
7199 : die_reader_specs {},
7200 m_this_cu (this_cu)
7201 {
7202 struct objfile *objfile = per_objfile->objfile;
7203 struct dwarf2_section_info *section = this_cu->section;
7204 bfd *abfd = section->get_bfd_owner ();
7205 const gdb_byte *begin_info_ptr;
7206 struct signatured_type *sig_type = NULL;
7207 struct dwarf2_section_info *abbrev_section;
7208 /* Non-zero if CU currently points to a DWO file and we need to
7209 reread it. When this happens we need to reread the skeleton die
7210 before we can reread the DWO file (this only applies to CUs, not TUs). */
7211 int rereading_dwo_cu = 0;
7212
7213 if (dwarf_die_debug)
7214 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7215 this_cu->is_debug_types ? "type" : "comp",
7216 sect_offset_str (this_cu->sect_off));
7217
7218 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7219 file (instead of going through the stub), short-circuit all of this. */
7220 if (this_cu->reading_dwo_directly)
7221 {
7222 /* Narrow down the scope of possibilities to have to understand. */
7223 gdb_assert (this_cu->is_debug_types);
7224 gdb_assert (abbrev_table == NULL);
7225 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
7226 return;
7227 }
7228
7229 /* This is cheap if the section is already read in. */
7230 section->read (objfile);
7231
7232 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7233
7234 abbrev_section = get_abbrev_section_for_cu (this_cu);
7235
7236 dwarf2_cu *cu;
7237
7238 if (existing_cu != nullptr)
7239 {
7240 cu = existing_cu;
7241 /* If this CU is from a DWO file we need to start over, we need to
7242 refetch the attributes from the skeleton CU.
7243 This could be optimized by retrieving those attributes from when we
7244 were here the first time: the previous comp_unit_die was stored in
7245 comp_unit_obstack. But there's no data yet that we need this
7246 optimization. */
7247 if (cu->dwo_unit != NULL)
7248 rereading_dwo_cu = 1;
7249 }
7250 else
7251 {
7252 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7253 in per_objfile yet. */
7254 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7255 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7256 cu = m_new_cu.get ();
7257 }
7258
7259 /* Get the header. */
7260 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7261 {
7262 /* We already have the header, there's no need to read it in again. */
7263 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7264 }
7265 else
7266 {
7267 if (this_cu->is_debug_types)
7268 {
7269 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7270 section, abbrev_section,
7271 info_ptr, rcuh_kind::TYPE);
7272
7273 /* Since per_cu is the first member of struct signatured_type,
7274 we can go from a pointer to one to a pointer to the other. */
7275 sig_type = (struct signatured_type *) this_cu;
7276 gdb_assert (sig_type->signature == cu->header.signature);
7277 gdb_assert (sig_type->type_offset_in_tu
7278 == cu->header.type_cu_offset_in_tu);
7279 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7280
7281 /* LENGTH has not been set yet for type units if we're
7282 using .gdb_index. */
7283 this_cu->length = cu->header.get_length ();
7284
7285 /* Establish the type offset that can be used to lookup the type. */
7286 sig_type->type_offset_in_section =
7287 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7288
7289 this_cu->dwarf_version = cu->header.version;
7290 }
7291 else
7292 {
7293 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7294 section, abbrev_section,
7295 info_ptr,
7296 rcuh_kind::COMPILE);
7297
7298 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7299 if (this_cu->length == 0)
7300 this_cu->length = cu->header.get_length ();
7301 else
7302 gdb_assert (this_cu->length == cu->header.get_length ());
7303 this_cu->dwarf_version = cu->header.version;
7304 }
7305 }
7306
7307 /* Skip dummy compilation units. */
7308 if (info_ptr >= begin_info_ptr + this_cu->length
7309 || peek_abbrev_code (abfd, info_ptr) == 0)
7310 {
7311 dummy_p = true;
7312 return;
7313 }
7314
7315 /* If we don't have them yet, read the abbrevs for this compilation unit.
7316 And if we need to read them now, make sure they're freed when we're
7317 done. */
7318 if (abbrev_table != NULL)
7319 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7320 else
7321 {
7322 abbrev_section->read (objfile);
7323 m_abbrev_table_holder
7324 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
7325 abbrev_table = m_abbrev_table_holder.get ();
7326 }
7327
7328 /* Read the top level CU/TU die. */
7329 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7330 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7331
7332 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7333 {
7334 dummy_p = true;
7335 return;
7336 }
7337
7338 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7339 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7340 table from the DWO file and pass the ownership over to us. It will be
7341 referenced from READER, so we must make sure to free it after we're done
7342 with READER.
7343
7344 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7345 DWO CU, that this test will fail (the attribute will not be present). */
7346 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7347 if (dwo_name != nullptr)
7348 {
7349 struct dwo_unit *dwo_unit;
7350 struct die_info *dwo_comp_unit_die;
7351
7352 if (comp_unit_die->has_children)
7353 {
7354 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7355 " has children (offset %s) [in module %s]"),
7356 sect_offset_str (this_cu->sect_off),
7357 bfd_get_filename (abfd));
7358 }
7359 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7360 if (dwo_unit != NULL)
7361 {
7362 if (read_cutu_die_from_dwo (cu, dwo_unit,
7363 comp_unit_die, NULL,
7364 this, &info_ptr,
7365 &dwo_comp_unit_die,
7366 &m_dwo_abbrev_table) == 0)
7367 {
7368 /* Dummy die. */
7369 dummy_p = true;
7370 return;
7371 }
7372 comp_unit_die = dwo_comp_unit_die;
7373 }
7374 else
7375 {
7376 /* Yikes, we couldn't find the rest of the DIE, we only have
7377 the stub. A complaint has already been logged. There's
7378 not much more we can do except pass on the stub DIE to
7379 die_reader_func. We don't want to throw an error on bad
7380 debug info. */
7381 }
7382 }
7383 }
7384
7385 void
7386 cutu_reader::keep ()
7387 {
7388 /* Done, clean up. */
7389 gdb_assert (!dummy_p);
7390 if (m_new_cu != NULL)
7391 {
7392 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7393 now. */
7394 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7395 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
7396 }
7397 }
7398
7399 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7400 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7401 assumed to have already done the lookup to find the DWO file).
7402
7403 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7404 THIS_CU->is_debug_types, but nothing else.
7405
7406 We fill in THIS_CU->length.
7407
7408 THIS_CU->cu is always freed when done.
7409 This is done in order to not leave THIS_CU->cu in a state where we have
7410 to care whether it refers to the "main" CU or the DWO CU.
7411
7412 When parent_cu is passed, it is used to provide a default value for
7413 str_offsets_base and addr_base from the parent. */
7414
7415 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7416 dwarf2_per_objfile *per_objfile,
7417 struct dwarf2_cu *parent_cu,
7418 struct dwo_file *dwo_file)
7419 : die_reader_specs {},
7420 m_this_cu (this_cu)
7421 {
7422 struct objfile *objfile = per_objfile->objfile;
7423 struct dwarf2_section_info *section = this_cu->section;
7424 bfd *abfd = section->get_bfd_owner ();
7425 struct dwarf2_section_info *abbrev_section;
7426 const gdb_byte *begin_info_ptr, *info_ptr;
7427
7428 if (dwarf_die_debug)
7429 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7430 this_cu->is_debug_types ? "type" : "comp",
7431 sect_offset_str (this_cu->sect_off));
7432
7433 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7434
7435 abbrev_section = (dwo_file != NULL
7436 ? &dwo_file->sections.abbrev
7437 : get_abbrev_section_for_cu (this_cu));
7438
7439 /* This is cheap if the section is already read in. */
7440 section->read (objfile);
7441
7442 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7443
7444 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7445 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7446 section, abbrev_section, info_ptr,
7447 (this_cu->is_debug_types
7448 ? rcuh_kind::TYPE
7449 : rcuh_kind::COMPILE));
7450
7451 if (parent_cu != nullptr)
7452 {
7453 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7454 m_new_cu->addr_base = parent_cu->addr_base;
7455 }
7456 this_cu->length = m_new_cu->header.get_length ();
7457
7458 /* Skip dummy compilation units. */
7459 if (info_ptr >= begin_info_ptr + this_cu->length
7460 || peek_abbrev_code (abfd, info_ptr) == 0)
7461 {
7462 dummy_p = true;
7463 return;
7464 }
7465
7466 abbrev_section->read (objfile);
7467 m_abbrev_table_holder
7468 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
7469
7470 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7471 m_abbrev_table_holder.get ());
7472 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7473 }
7474
7475 \f
7476 /* Type Unit Groups.
7477
7478 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7479 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7480 so that all types coming from the same compilation (.o file) are grouped
7481 together. A future step could be to put the types in the same symtab as
7482 the CU the types ultimately came from. */
7483
7484 static hashval_t
7485 hash_type_unit_group (const void *item)
7486 {
7487 const struct type_unit_group *tu_group
7488 = (const struct type_unit_group *) item;
7489
7490 return hash_stmt_list_entry (&tu_group->hash);
7491 }
7492
7493 static int
7494 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7495 {
7496 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7497 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7498
7499 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7500 }
7501
7502 /* Allocate a hash table for type unit groups. */
7503
7504 static htab_up
7505 allocate_type_unit_groups_table ()
7506 {
7507 return htab_up (htab_create_alloc (3,
7508 hash_type_unit_group,
7509 eq_type_unit_group,
7510 NULL, xcalloc, xfree));
7511 }
7512
7513 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7514 partial symtabs. We combine several TUs per psymtab to not let the size
7515 of any one psymtab grow too big. */
7516 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7517 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7518
7519 /* Helper routine for get_type_unit_group.
7520 Create the type_unit_group object used to hold one or more TUs. */
7521
7522 static struct type_unit_group *
7523 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7524 {
7525 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7526 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7527 struct dwarf2_per_cu_data *per_cu;
7528 struct type_unit_group *tu_group;
7529
7530 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
7531 per_cu = &tu_group->per_cu;
7532 per_cu->per_bfd = per_bfd;
7533
7534 if (per_bfd->using_index)
7535 {
7536 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7537 struct dwarf2_per_cu_quick_data);
7538 }
7539 else
7540 {
7541 unsigned int line_offset = to_underlying (line_offset_struct);
7542 dwarf2_psymtab *pst;
7543 std::string name;
7544
7545 /* Give the symtab a useful name for debug purposes. */
7546 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7547 name = string_printf ("<type_units_%d>",
7548 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7549 else
7550 name = string_printf ("<type_units_at_0x%x>", line_offset);
7551
7552 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
7553 pst->anonymous = true;
7554 }
7555
7556 tu_group->hash.dwo_unit = cu->dwo_unit;
7557 tu_group->hash.line_sect_off = line_offset_struct;
7558
7559 return tu_group;
7560 }
7561
7562 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7563 STMT_LIST is a DW_AT_stmt_list attribute. */
7564
7565 static struct type_unit_group *
7566 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7567 {
7568 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7569 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7570 struct type_unit_group *tu_group;
7571 void **slot;
7572 unsigned int line_offset;
7573 struct type_unit_group type_unit_group_for_lookup;
7574
7575 if (per_objfile->per_bfd->type_unit_groups == NULL)
7576 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7577
7578 /* Do we need to create a new group, or can we use an existing one? */
7579
7580 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
7581 {
7582 line_offset = stmt_list->as_unsigned ();
7583 ++tu_stats->nr_symtab_sharers;
7584 }
7585 else
7586 {
7587 /* Ugh, no stmt_list. Rare, but we have to handle it.
7588 We can do various things here like create one group per TU or
7589 spread them over multiple groups to split up the expansion work.
7590 To avoid worst case scenarios (too many groups or too large groups)
7591 we, umm, group them in bunches. */
7592 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7593 | (tu_stats->nr_stmt_less_type_units
7594 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7595 ++tu_stats->nr_stmt_less_type_units;
7596 }
7597
7598 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7599 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7600 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
7601 &type_unit_group_for_lookup, INSERT);
7602 if (*slot != NULL)
7603 {
7604 tu_group = (struct type_unit_group *) *slot;
7605 gdb_assert (tu_group != NULL);
7606 }
7607 else
7608 {
7609 sect_offset line_offset_struct = (sect_offset) line_offset;
7610 tu_group = create_type_unit_group (cu, line_offset_struct);
7611 *slot = tu_group;
7612 ++tu_stats->nr_symtabs;
7613 }
7614
7615 return tu_group;
7616 }
7617 \f
7618 /* Partial symbol tables. */
7619
7620 /* Create a psymtab named NAME and assign it to PER_CU.
7621
7622 The caller must fill in the following details:
7623 dirname, textlow, texthigh. */
7624
7625 static dwarf2_psymtab *
7626 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7627 dwarf2_per_objfile *per_objfile,
7628 const char *name)
7629 {
7630 struct objfile *objfile = per_objfile->objfile;
7631 dwarf2_psymtab *pst;
7632
7633 pst = new dwarf2_psymtab (name, objfile, per_cu);
7634
7635 pst->psymtabs_addrmap_supported = true;
7636
7637 /* This is the glue that links PST into GDB's symbol API. */
7638 per_cu->v.psymtab = pst;
7639
7640 return pst;
7641 }
7642
7643 /* DIE reader function for process_psymtab_comp_unit. */
7644
7645 static void
7646 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7647 const gdb_byte *info_ptr,
7648 struct die_info *comp_unit_die,
7649 enum language pretend_language)
7650 {
7651 struct dwarf2_cu *cu = reader->cu;
7652 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7653 struct objfile *objfile = per_objfile->objfile;
7654 struct gdbarch *gdbarch = objfile->arch ();
7655 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7656 CORE_ADDR baseaddr;
7657 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7658 dwarf2_psymtab *pst;
7659 enum pc_bounds_kind cu_bounds_kind;
7660 const char *filename;
7661
7662 gdb_assert (! per_cu->is_debug_types);
7663
7664 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7665
7666 /* Allocate a new partial symbol table structure. */
7667 gdb::unique_xmalloc_ptr<char> debug_filename;
7668 static const char artificial[] = "<artificial>";
7669 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7670 if (filename == NULL)
7671 filename = "";
7672 else if (strcmp (filename, artificial) == 0)
7673 {
7674 debug_filename.reset (concat (artificial, "@",
7675 sect_offset_str (per_cu->sect_off),
7676 (char *) NULL));
7677 filename = debug_filename.get ();
7678 }
7679
7680 pst = create_partial_symtab (per_cu, per_objfile, filename);
7681
7682 /* This must be done before calling dwarf2_build_include_psymtabs. */
7683 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7684
7685 baseaddr = objfile->text_section_offset ();
7686
7687 dwarf2_find_base_address (comp_unit_die, cu);
7688
7689 /* Possibly set the default values of LOWPC and HIGHPC from
7690 `DW_AT_ranges'. */
7691 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7692 &best_highpc, cu, pst);
7693 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7694 {
7695 CORE_ADDR low
7696 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7697 - baseaddr);
7698 CORE_ADDR high
7699 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7700 - baseaddr - 1);
7701 /* Store the contiguous range if it is not empty; it can be
7702 empty for CUs with no code. */
7703 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7704 low, high, pst);
7705 }
7706
7707 /* Check if comp unit has_children.
7708 If so, read the rest of the partial symbols from this comp unit.
7709 If not, there's no more debug_info for this comp unit. */
7710 if (comp_unit_die->has_children)
7711 {
7712 struct partial_die_info *first_die;
7713 CORE_ADDR lowpc, highpc;
7714
7715 lowpc = ((CORE_ADDR) -1);
7716 highpc = ((CORE_ADDR) 0);
7717
7718 first_die = load_partial_dies (reader, info_ptr, 1);
7719
7720 scan_partial_symbols (first_die, &lowpc, &highpc,
7721 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7722
7723 /* If we didn't find a lowpc, set it to highpc to avoid
7724 complaints from `maint check'. */
7725 if (lowpc == ((CORE_ADDR) -1))
7726 lowpc = highpc;
7727
7728 /* If the compilation unit didn't have an explicit address range,
7729 then use the information extracted from its child dies. */
7730 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7731 {
7732 best_lowpc = lowpc;
7733 best_highpc = highpc;
7734 }
7735 }
7736 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7737 best_lowpc + baseaddr)
7738 - baseaddr);
7739 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7740 best_highpc + baseaddr)
7741 - baseaddr);
7742
7743 pst->end ();
7744
7745 if (!cu->per_cu->imported_symtabs_empty ())
7746 {
7747 int i;
7748 int len = cu->per_cu->imported_symtabs_size ();
7749
7750 /* Fill in 'dependencies' here; we fill in 'users' in a
7751 post-pass. */
7752 pst->number_of_dependencies = len;
7753 pst->dependencies
7754 = objfile->partial_symtabs->allocate_dependencies (len);
7755 for (i = 0; i < len; ++i)
7756 {
7757 pst->dependencies[i]
7758 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7759 }
7760
7761 cu->per_cu->imported_symtabs_free ();
7762 }
7763
7764 /* Get the list of files included in the current compilation unit,
7765 and build a psymtab for each of them. */
7766 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7767
7768 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7769 ", %d global, %d static syms",
7770 per_cu->is_debug_types ? "type" : "comp",
7771 sect_offset_str (per_cu->sect_off),
7772 paddress (gdbarch, pst->text_low (objfile)),
7773 paddress (gdbarch, pst->text_high (objfile)),
7774 (int) pst->global_psymbols.size (),
7775 (int) pst->static_psymbols.size ());
7776 }
7777
7778 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7779 Process compilation unit THIS_CU for a psymtab. */
7780
7781 static void
7782 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7783 dwarf2_per_objfile *per_objfile,
7784 bool want_partial_unit,
7785 enum language pretend_language)
7786 {
7787 /* If this compilation unit was already read in, free the
7788 cached copy in order to read it in again. This is
7789 necessary because we skipped some symbols when we first
7790 read in the compilation unit (see load_partial_dies).
7791 This problem could be avoided, but the benefit is unclear. */
7792 per_objfile->remove_cu (this_cu);
7793
7794 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7795
7796 switch (reader.comp_unit_die->tag)
7797 {
7798 case DW_TAG_compile_unit:
7799 this_cu->unit_type = DW_UT_compile;
7800 break;
7801 case DW_TAG_partial_unit:
7802 this_cu->unit_type = DW_UT_partial;
7803 break;
7804 default:
7805 abort ();
7806 }
7807
7808 if (reader.dummy_p)
7809 {
7810 /* Nothing. */
7811 }
7812 else if (this_cu->is_debug_types)
7813 build_type_psymtabs_reader (&reader, reader.info_ptr,
7814 reader.comp_unit_die);
7815 else if (want_partial_unit
7816 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7817 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7818 reader.comp_unit_die,
7819 pretend_language);
7820
7821 this_cu->lang = reader.cu->language;
7822
7823 /* Age out any secondary CUs. */
7824 per_objfile->age_comp_units ();
7825 }
7826
7827 /* Reader function for build_type_psymtabs. */
7828
7829 static void
7830 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7831 const gdb_byte *info_ptr,
7832 struct die_info *type_unit_die)
7833 {
7834 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7835 struct dwarf2_cu *cu = reader->cu;
7836 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7837 struct signatured_type *sig_type;
7838 struct type_unit_group *tu_group;
7839 struct attribute *attr;
7840 struct partial_die_info *first_die;
7841 CORE_ADDR lowpc, highpc;
7842 dwarf2_psymtab *pst;
7843
7844 gdb_assert (per_cu->is_debug_types);
7845 sig_type = (struct signatured_type *) per_cu;
7846
7847 if (! type_unit_die->has_children)
7848 return;
7849
7850 attr = type_unit_die->attr (DW_AT_stmt_list);
7851 tu_group = get_type_unit_group (cu, attr);
7852
7853 if (tu_group->tus == nullptr)
7854 tu_group->tus = new std::vector<signatured_type *>;
7855 tu_group->tus->push_back (sig_type);
7856
7857 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7858 pst = create_partial_symtab (per_cu, per_objfile, "");
7859 pst->anonymous = true;
7860
7861 first_die = load_partial_dies (reader, info_ptr, 1);
7862
7863 lowpc = (CORE_ADDR) -1;
7864 highpc = (CORE_ADDR) 0;
7865 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7866
7867 pst->end ();
7868 }
7869
7870 /* Struct used to sort TUs by their abbreviation table offset. */
7871
7872 struct tu_abbrev_offset
7873 {
7874 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7875 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7876 {}
7877
7878 signatured_type *sig_type;
7879 sect_offset abbrev_offset;
7880 };
7881
7882 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7883
7884 static bool
7885 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7886 const struct tu_abbrev_offset &b)
7887 {
7888 return a.abbrev_offset < b.abbrev_offset;
7889 }
7890
7891 /* Efficiently read all the type units.
7892 This does the bulk of the work for build_type_psymtabs.
7893
7894 The efficiency is because we sort TUs by the abbrev table they use and
7895 only read each abbrev table once. In one program there are 200K TUs
7896 sharing 8K abbrev tables.
7897
7898 The main purpose of this function is to support building the
7899 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7900 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7901 can collapse the search space by grouping them by stmt_list.
7902 The savings can be significant, in the same program from above the 200K TUs
7903 share 8K stmt_list tables.
7904
7905 FUNC is expected to call get_type_unit_group, which will create the
7906 struct type_unit_group if necessary and add it to
7907 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7908
7909 static void
7910 build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
7911 {
7912 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7913 abbrev_table_up abbrev_table;
7914 sect_offset abbrev_offset;
7915
7916 /* It's up to the caller to not call us multiple times. */
7917 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7918
7919 if (per_objfile->per_bfd->all_type_units.empty ())
7920 return;
7921
7922 /* TUs typically share abbrev tables, and there can be way more TUs than
7923 abbrev tables. Sort by abbrev table to reduce the number of times we
7924 read each abbrev table in.
7925 Alternatives are to punt or to maintain a cache of abbrev tables.
7926 This is simpler and efficient enough for now.
7927
7928 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7929 symtab to use). Typically TUs with the same abbrev offset have the same
7930 stmt_list value too so in practice this should work well.
7931
7932 The basic algorithm here is:
7933
7934 sort TUs by abbrev table
7935 for each TU with same abbrev table:
7936 read abbrev table if first user
7937 read TU top level DIE
7938 [IWBN if DWO skeletons had DW_AT_stmt_list]
7939 call FUNC */
7940
7941 dwarf_read_debug_printf ("Building type unit groups ...");
7942
7943 /* Sort in a separate table to maintain the order of all_type_units
7944 for .gdb_index: TU indices directly index all_type_units. */
7945 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7946 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
7947
7948 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
7949 sorted_by_abbrev.emplace_back
7950 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
7951 sig_type->per_cu.sect_off));
7952
7953 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7954 sort_tu_by_abbrev_offset);
7955
7956 abbrev_offset = (sect_offset) ~(unsigned) 0;
7957
7958 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7959 {
7960 /* Switch to the next abbrev table if necessary. */
7961 if (abbrev_table == NULL
7962 || tu.abbrev_offset != abbrev_offset)
7963 {
7964 abbrev_offset = tu.abbrev_offset;
7965 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
7966 abbrev_table =
7967 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
7968 ++tu_stats->nr_uniq_abbrev_tables;
7969 }
7970
7971 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
7972 abbrev_table.get (), nullptr, false);
7973 if (!reader.dummy_p)
7974 build_type_psymtabs_reader (&reader, reader.info_ptr,
7975 reader.comp_unit_die);
7976 }
7977 }
7978
7979 /* Print collected type unit statistics. */
7980
7981 static void
7982 print_tu_stats (dwarf2_per_objfile *per_objfile)
7983 {
7984 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7985
7986 dwarf_read_debug_printf ("Type unit statistics:");
7987 dwarf_read_debug_printf (" %zu TUs",
7988 per_objfile->per_bfd->all_type_units.size ());
7989 dwarf_read_debug_printf (" %d uniq abbrev tables",
7990 tu_stats->nr_uniq_abbrev_tables);
7991 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7992 tu_stats->nr_symtabs);
7993 dwarf_read_debug_printf (" %d symtab sharers",
7994 tu_stats->nr_symtab_sharers);
7995 dwarf_read_debug_printf (" %d type units without a stmt_list",
7996 tu_stats->nr_stmt_less_type_units);
7997 dwarf_read_debug_printf (" %d all_type_units reallocs",
7998 tu_stats->nr_all_type_units_reallocs);
7999 }
8000
8001 /* Traversal function for build_type_psymtabs. */
8002
8003 static int
8004 build_type_psymtab_dependencies (void **slot, void *info)
8005 {
8006 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
8007 struct objfile *objfile = per_objfile->objfile;
8008 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8009 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8010 dwarf2_psymtab *pst = per_cu->v.psymtab;
8011 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
8012 int i;
8013
8014 gdb_assert (len > 0);
8015 gdb_assert (per_cu->type_unit_group_p ());
8016
8017 pst->number_of_dependencies = len;
8018 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8019 for (i = 0; i < len; ++i)
8020 {
8021 struct signatured_type *iter = tu_group->tus->at (i);
8022 gdb_assert (iter->per_cu.is_debug_types);
8023 pst->dependencies[i] = iter->per_cu.v.psymtab;
8024 iter->type_unit_group = tu_group;
8025 }
8026
8027 delete tu_group->tus;
8028 tu_group->tus = nullptr;
8029
8030 return 1;
8031 }
8032
8033 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8034 Build partial symbol tables for the .debug_types comp-units. */
8035
8036 static void
8037 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
8038 {
8039 if (! create_all_type_units (per_objfile))
8040 return;
8041
8042 build_type_psymtabs_1 (per_objfile);
8043 }
8044
8045 /* Traversal function for process_skeletonless_type_unit.
8046 Read a TU in a DWO file and build partial symbols for it. */
8047
8048 static int
8049 process_skeletonless_type_unit (void **slot, void *info)
8050 {
8051 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8052 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
8053 struct signatured_type find_entry, *entry;
8054
8055 /* If this TU doesn't exist in the global table, add it and read it in. */
8056
8057 if (per_objfile->per_bfd->signatured_types == NULL)
8058 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
8059
8060 find_entry.signature = dwo_unit->signature;
8061 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
8062 &find_entry, INSERT);
8063 /* If we've already seen this type there's nothing to do. What's happening
8064 is we're doing our own version of comdat-folding here. */
8065 if (*slot != NULL)
8066 return 1;
8067
8068 /* This does the job that create_all_type_units would have done for
8069 this TU. */
8070 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
8071 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
8072 *slot = entry;
8073
8074 /* This does the job that build_type_psymtabs_1 would have done. */
8075 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
8076 if (!reader.dummy_p)
8077 build_type_psymtabs_reader (&reader, reader.info_ptr,
8078 reader.comp_unit_die);
8079
8080 return 1;
8081 }
8082
8083 /* Traversal function for process_skeletonless_type_units. */
8084
8085 static int
8086 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8087 {
8088 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8089
8090 if (dwo_file->tus != NULL)
8091 htab_traverse_noresize (dwo_file->tus.get (),
8092 process_skeletonless_type_unit, info);
8093
8094 return 1;
8095 }
8096
8097 /* Scan all TUs of DWO files, verifying we've processed them.
8098 This is needed in case a TU was emitted without its skeleton.
8099 Note: This can't be done until we know what all the DWO files are. */
8100
8101 static void
8102 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
8103 {
8104 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8105 if (get_dwp_file (per_objfile) == NULL
8106 && per_objfile->per_bfd->dwo_files != NULL)
8107 {
8108 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
8109 process_dwo_file_for_skeletonless_type_units,
8110 per_objfile);
8111 }
8112 }
8113
8114 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8115
8116 static void
8117 set_partial_user (dwarf2_per_objfile *per_objfile)
8118 {
8119 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8120 {
8121 dwarf2_psymtab *pst = per_cu->v.psymtab;
8122
8123 if (pst == NULL)
8124 continue;
8125
8126 for (int j = 0; j < pst->number_of_dependencies; ++j)
8127 {
8128 /* Set the 'user' field only if it is not already set. */
8129 if (pst->dependencies[j]->user == NULL)
8130 pst->dependencies[j]->user = pst;
8131 }
8132 }
8133 }
8134
8135 /* Build the partial symbol table by doing a quick pass through the
8136 .debug_info and .debug_abbrev sections. */
8137
8138 static void
8139 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
8140 {
8141 struct objfile *objfile = per_objfile->objfile;
8142
8143 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
8144 objfile_name (objfile));
8145
8146 scoped_restore restore_reading_psyms
8147 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
8148 true);
8149
8150 per_objfile->per_bfd->info.read (objfile);
8151
8152 /* Any cached compilation units will be linked by the per-objfile
8153 read_in_chain. Make sure to free them when we're done. */
8154 free_cached_comp_units freer (per_objfile);
8155
8156 build_type_psymtabs (per_objfile);
8157
8158 create_all_comp_units (per_objfile);
8159
8160 /* Create a temporary address map on a temporary obstack. We later
8161 copy this to the final obstack. */
8162 auto_obstack temp_obstack;
8163
8164 scoped_restore save_psymtabs_addrmap
8165 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8166 addrmap_create_mutable (&temp_obstack));
8167
8168 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8169 {
8170 if (per_cu->v.psymtab != NULL)
8171 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8172 continue;
8173 process_psymtab_comp_unit (per_cu, per_objfile, false,
8174 language_minimal);
8175 }
8176
8177 /* This has to wait until we read the CUs, we need the list of DWOs. */
8178 process_skeletonless_type_units (per_objfile);
8179
8180 /* Now that all TUs have been processed we can fill in the dependencies. */
8181 if (per_objfile->per_bfd->type_unit_groups != NULL)
8182 {
8183 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8184 build_type_psymtab_dependencies, per_objfile);
8185 }
8186
8187 if (dwarf_read_debug > 0)
8188 print_tu_stats (per_objfile);
8189
8190 set_partial_user (per_objfile);
8191
8192 objfile->partial_symtabs->psymtabs_addrmap
8193 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8194 objfile->partial_symtabs->obstack ());
8195 /* At this point we want to keep the address map. */
8196 save_psymtabs_addrmap.release ();
8197
8198 dwarf_read_debug_printf ("Done building psymtabs of %s",
8199 objfile_name (objfile));
8200 }
8201
8202 /* Load the partial DIEs for a secondary CU into memory.
8203 This is also used when rereading a primary CU with load_all_dies. */
8204
8205 static void
8206 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8207 dwarf2_per_objfile *per_objfile,
8208 dwarf2_cu *existing_cu)
8209 {
8210 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
8211
8212 if (!reader.dummy_p)
8213 {
8214 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8215 language_minimal);
8216
8217 /* Check if comp unit has_children.
8218 If so, read the rest of the partial symbols from this comp unit.
8219 If not, there's no more debug_info for this comp unit. */
8220 if (reader.comp_unit_die->has_children)
8221 load_partial_dies (&reader, reader.info_ptr, 0);
8222
8223 reader.keep ();
8224 }
8225 }
8226
8227 static void
8228 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
8229 struct dwarf2_section_info *section,
8230 struct dwarf2_section_info *abbrev_section,
8231 unsigned int is_dwz)
8232 {
8233 const gdb_byte *info_ptr;
8234 struct objfile *objfile = per_objfile->objfile;
8235
8236 dwarf_read_debug_printf ("Reading %s for %s",
8237 section->get_name (),
8238 section->get_file_name ());
8239
8240 section->read (objfile);
8241
8242 info_ptr = section->buffer;
8243
8244 while (info_ptr < section->buffer + section->size)
8245 {
8246 struct dwarf2_per_cu_data *this_cu;
8247
8248 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8249
8250 comp_unit_head cu_header;
8251 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
8252 abbrev_section, info_ptr,
8253 rcuh_kind::COMPILE);
8254
8255 /* Save the compilation unit for later lookup. */
8256 if (cu_header.unit_type != DW_UT_type)
8257 this_cu = per_objfile->per_bfd->allocate_per_cu ();
8258 else
8259 {
8260 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
8261 sig_type->signature = cu_header.signature;
8262 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8263 this_cu = &sig_type->per_cu;
8264 }
8265 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8266 this_cu->sect_off = sect_off;
8267 this_cu->length = cu_header.length + cu_header.initial_length_size;
8268 this_cu->is_dwz = is_dwz;
8269 this_cu->section = section;
8270
8271 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8272
8273 info_ptr = info_ptr + this_cu->length;
8274 }
8275 }
8276
8277 /* Create a list of all compilation units in OBJFILE.
8278 This is only done for -readnow and building partial symtabs. */
8279
8280 static void
8281 create_all_comp_units (dwarf2_per_objfile *per_objfile)
8282 {
8283 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8284 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8285 &per_objfile->per_bfd->abbrev, 0);
8286
8287 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8288 if (dwz != NULL)
8289 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
8290 }
8291
8292 /* Process all loaded DIEs for compilation unit CU, starting at
8293 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8294 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8295 DW_AT_ranges). See the comments of add_partial_subprogram on how
8296 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8297
8298 static void
8299 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8300 CORE_ADDR *highpc, int set_addrmap,
8301 struct dwarf2_cu *cu)
8302 {
8303 struct partial_die_info *pdi;
8304
8305 /* Now, march along the PDI's, descending into ones which have
8306 interesting children but skipping the children of the other ones,
8307 until we reach the end of the compilation unit. */
8308
8309 pdi = first_die;
8310
8311 while (pdi != NULL)
8312 {
8313 pdi->fixup (cu);
8314
8315 /* Anonymous namespaces or modules have no name but have interesting
8316 children, so we need to look at them. Ditto for anonymous
8317 enums. */
8318
8319 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
8320 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8321 || pdi->tag == DW_TAG_imported_unit
8322 || pdi->tag == DW_TAG_inlined_subroutine)
8323 {
8324 switch (pdi->tag)
8325 {
8326 case DW_TAG_subprogram:
8327 case DW_TAG_inlined_subroutine:
8328 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8329 if (cu->language == language_cplus)
8330 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8331 set_addrmap, cu);
8332 break;
8333 case DW_TAG_constant:
8334 case DW_TAG_variable:
8335 case DW_TAG_typedef:
8336 case DW_TAG_union_type:
8337 if (!pdi->is_declaration
8338 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8339 {
8340 add_partial_symbol (pdi, cu);
8341 }
8342 break;
8343 case DW_TAG_class_type:
8344 case DW_TAG_interface_type:
8345 case DW_TAG_structure_type:
8346 if (!pdi->is_declaration)
8347 {
8348 add_partial_symbol (pdi, cu);
8349 }
8350 if ((cu->language == language_rust
8351 || cu->language == language_cplus) && pdi->has_children)
8352 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8353 set_addrmap, cu);
8354 break;
8355 case DW_TAG_enumeration_type:
8356 if (!pdi->is_declaration)
8357 add_partial_enumeration (pdi, cu);
8358 break;
8359 case DW_TAG_base_type:
8360 case DW_TAG_subrange_type:
8361 /* File scope base type definitions are added to the partial
8362 symbol table. */
8363 add_partial_symbol (pdi, cu);
8364 break;
8365 case DW_TAG_namespace:
8366 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8367 break;
8368 case DW_TAG_module:
8369 if (!pdi->is_declaration)
8370 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8371 break;
8372 case DW_TAG_imported_unit:
8373 {
8374 struct dwarf2_per_cu_data *per_cu;
8375
8376 /* For now we don't handle imported units in type units. */
8377 if (cu->per_cu->is_debug_types)
8378 {
8379 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8380 " supported in type units [in module %s]"),
8381 objfile_name (cu->per_objfile->objfile));
8382 }
8383
8384 per_cu = dwarf2_find_containing_comp_unit
8385 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8386
8387 /* Go read the partial unit, if needed. */
8388 if (per_cu->v.psymtab == NULL)
8389 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8390 cu->language);
8391
8392 cu->per_cu->imported_symtabs_push (per_cu);
8393 }
8394 break;
8395 case DW_TAG_imported_declaration:
8396 add_partial_symbol (pdi, cu);
8397 break;
8398 default:
8399 break;
8400 }
8401 }
8402
8403 /* If the die has a sibling, skip to the sibling. */
8404
8405 pdi = pdi->die_sibling;
8406 }
8407 }
8408
8409 /* Functions used to compute the fully scoped name of a partial DIE.
8410
8411 Normally, this is simple. For C++, the parent DIE's fully scoped
8412 name is concatenated with "::" and the partial DIE's name.
8413 Enumerators are an exception; they use the scope of their parent
8414 enumeration type, i.e. the name of the enumeration type is not
8415 prepended to the enumerator.
8416
8417 There are two complexities. One is DW_AT_specification; in this
8418 case "parent" means the parent of the target of the specification,
8419 instead of the direct parent of the DIE. The other is compilers
8420 which do not emit DW_TAG_namespace; in this case we try to guess
8421 the fully qualified name of structure types from their members'
8422 linkage names. This must be done using the DIE's children rather
8423 than the children of any DW_AT_specification target. We only need
8424 to do this for structures at the top level, i.e. if the target of
8425 any DW_AT_specification (if any; otherwise the DIE itself) does not
8426 have a parent. */
8427
8428 /* Compute the scope prefix associated with PDI's parent, in
8429 compilation unit CU. The result will be allocated on CU's
8430 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8431 field. NULL is returned if no prefix is necessary. */
8432 static const char *
8433 partial_die_parent_scope (struct partial_die_info *pdi,
8434 struct dwarf2_cu *cu)
8435 {
8436 const char *grandparent_scope;
8437 struct partial_die_info *parent, *real_pdi;
8438
8439 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8440 then this means the parent of the specification DIE. */
8441
8442 real_pdi = pdi;
8443 while (real_pdi->has_specification)
8444 {
8445 auto res = find_partial_die (real_pdi->spec_offset,
8446 real_pdi->spec_is_dwz, cu);
8447 real_pdi = res.pdi;
8448 cu = res.cu;
8449 }
8450
8451 parent = real_pdi->die_parent;
8452 if (parent == NULL)
8453 return NULL;
8454
8455 if (parent->scope_set)
8456 return parent->scope;
8457
8458 parent->fixup (cu);
8459
8460 grandparent_scope = partial_die_parent_scope (parent, cu);
8461
8462 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8463 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8464 Work around this problem here. */
8465 if (cu->language == language_cplus
8466 && parent->tag == DW_TAG_namespace
8467 && strcmp (parent->name (cu), "::") == 0
8468 && grandparent_scope == NULL)
8469 {
8470 parent->scope = NULL;
8471 parent->scope_set = 1;
8472 return NULL;
8473 }
8474
8475 /* Nested subroutines in Fortran get a prefix. */
8476 if (pdi->tag == DW_TAG_enumerator)
8477 /* Enumerators should not get the name of the enumeration as a prefix. */
8478 parent->scope = grandparent_scope;
8479 else if (parent->tag == DW_TAG_namespace
8480 || parent->tag == DW_TAG_module
8481 || parent->tag == DW_TAG_structure_type
8482 || parent->tag == DW_TAG_class_type
8483 || parent->tag == DW_TAG_interface_type
8484 || parent->tag == DW_TAG_union_type
8485 || parent->tag == DW_TAG_enumeration_type
8486 || (cu->language == language_fortran
8487 && parent->tag == DW_TAG_subprogram
8488 && pdi->tag == DW_TAG_subprogram))
8489 {
8490 if (grandparent_scope == NULL)
8491 parent->scope = parent->name (cu);
8492 else
8493 parent->scope = typename_concat (&cu->comp_unit_obstack,
8494 grandparent_scope,
8495 parent->name (cu), 0, cu);
8496 }
8497 else
8498 {
8499 /* FIXME drow/2004-04-01: What should we be doing with
8500 function-local names? For partial symbols, we should probably be
8501 ignoring them. */
8502 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8503 dwarf_tag_name (parent->tag),
8504 sect_offset_str (pdi->sect_off));
8505 parent->scope = grandparent_scope;
8506 }
8507
8508 parent->scope_set = 1;
8509 return parent->scope;
8510 }
8511
8512 /* Return the fully scoped name associated with PDI, from compilation unit
8513 CU. The result will be allocated with malloc. */
8514
8515 static gdb::unique_xmalloc_ptr<char>
8516 partial_die_full_name (struct partial_die_info *pdi,
8517 struct dwarf2_cu *cu)
8518 {
8519 const char *parent_scope;
8520
8521 /* If this is a template instantiation, we can not work out the
8522 template arguments from partial DIEs. So, unfortunately, we have
8523 to go through the full DIEs. At least any work we do building
8524 types here will be reused if full symbols are loaded later. */
8525 if (pdi->has_template_arguments)
8526 {
8527 pdi->fixup (cu);
8528
8529 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
8530 {
8531 struct die_info *die;
8532 struct attribute attr;
8533 struct dwarf2_cu *ref_cu = cu;
8534
8535 /* DW_FORM_ref_addr is using section offset. */
8536 attr.name = (enum dwarf_attribute) 0;
8537 attr.form = DW_FORM_ref_addr;
8538 attr.u.unsnd = to_underlying (pdi->sect_off);
8539 die = follow_die_ref (NULL, &attr, &ref_cu);
8540
8541 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8542 }
8543 }
8544
8545 parent_scope = partial_die_parent_scope (pdi, cu);
8546 if (parent_scope == NULL)
8547 return NULL;
8548 else
8549 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8550 pdi->name (cu),
8551 0, cu));
8552 }
8553
8554 static void
8555 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8556 {
8557 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8558 struct objfile *objfile = per_objfile->objfile;
8559 struct gdbarch *gdbarch = objfile->arch ();
8560 CORE_ADDR addr = 0;
8561 const char *actual_name = NULL;
8562 CORE_ADDR baseaddr;
8563
8564 baseaddr = objfile->text_section_offset ();
8565
8566 gdb::unique_xmalloc_ptr<char> built_actual_name
8567 = partial_die_full_name (pdi, cu);
8568 if (built_actual_name != NULL)
8569 actual_name = built_actual_name.get ();
8570
8571 if (actual_name == NULL)
8572 actual_name = pdi->name (cu);
8573
8574 partial_symbol psymbol;
8575 memset (&psymbol, 0, sizeof (psymbol));
8576 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8577 psymbol.ginfo.section = -1;
8578
8579 /* The code below indicates that the psymbol should be installed by
8580 setting this. */
8581 gdb::optional<psymbol_placement> where;
8582
8583 switch (pdi->tag)
8584 {
8585 case DW_TAG_inlined_subroutine:
8586 case DW_TAG_subprogram:
8587 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8588 - baseaddr);
8589 if (pdi->is_external
8590 || cu->language == language_ada
8591 || (cu->language == language_fortran
8592 && pdi->die_parent != NULL
8593 && pdi->die_parent->tag == DW_TAG_subprogram))
8594 {
8595 /* Normally, only "external" DIEs are part of the global scope.
8596 But in Ada and Fortran, we want to be able to access nested
8597 procedures globally. So all Ada and Fortran subprograms are
8598 stored in the global scope. */
8599 where = psymbol_placement::GLOBAL;
8600 }
8601 else
8602 where = psymbol_placement::STATIC;
8603
8604 psymbol.domain = VAR_DOMAIN;
8605 psymbol.aclass = LOC_BLOCK;
8606 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8607 psymbol.ginfo.value.address = addr;
8608
8609 if (pdi->main_subprogram && actual_name != NULL)
8610 set_objfile_main_name (objfile, actual_name, cu->language);
8611 break;
8612 case DW_TAG_constant:
8613 psymbol.domain = VAR_DOMAIN;
8614 psymbol.aclass = LOC_STATIC;
8615 where = (pdi->is_external
8616 ? psymbol_placement::GLOBAL
8617 : psymbol_placement::STATIC);
8618 break;
8619 case DW_TAG_variable:
8620 if (pdi->d.locdesc)
8621 addr = decode_locdesc (pdi->d.locdesc, cu);
8622
8623 if (pdi->d.locdesc
8624 && addr == 0
8625 && !per_objfile->per_bfd->has_section_at_zero)
8626 {
8627 /* A global or static variable may also have been stripped
8628 out by the linker if unused, in which case its address
8629 will be nullified; do not add such variables into partial
8630 symbol table then. */
8631 }
8632 else if (pdi->is_external)
8633 {
8634 /* Global Variable.
8635 Don't enter into the minimal symbol tables as there is
8636 a minimal symbol table entry from the ELF symbols already.
8637 Enter into partial symbol table if it has a location
8638 descriptor or a type.
8639 If the location descriptor is missing, new_symbol will create
8640 a LOC_UNRESOLVED symbol, the address of the variable will then
8641 be determined from the minimal symbol table whenever the variable
8642 is referenced.
8643 The address for the partial symbol table entry is not
8644 used by GDB, but it comes in handy for debugging partial symbol
8645 table building. */
8646
8647 if (pdi->d.locdesc || pdi->has_type)
8648 {
8649 psymbol.domain = VAR_DOMAIN;
8650 psymbol.aclass = LOC_STATIC;
8651 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8652 psymbol.ginfo.value.address = addr;
8653 where = psymbol_placement::GLOBAL;
8654 }
8655 }
8656 else
8657 {
8658 int has_loc = pdi->d.locdesc != NULL;
8659
8660 /* Static Variable. Skip symbols whose value we cannot know (those
8661 without location descriptors or constant values). */
8662 if (!has_loc && !pdi->has_const_value)
8663 return;
8664
8665 psymbol.domain = VAR_DOMAIN;
8666 psymbol.aclass = LOC_STATIC;
8667 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8668 if (has_loc)
8669 psymbol.ginfo.value.address = addr;
8670 where = psymbol_placement::STATIC;
8671 }
8672 break;
8673 case DW_TAG_array_type:
8674 case DW_TAG_typedef:
8675 case DW_TAG_base_type:
8676 case DW_TAG_subrange_type:
8677 psymbol.domain = VAR_DOMAIN;
8678 psymbol.aclass = LOC_TYPEDEF;
8679 where = psymbol_placement::STATIC;
8680 break;
8681 case DW_TAG_imported_declaration:
8682 case DW_TAG_namespace:
8683 psymbol.domain = VAR_DOMAIN;
8684 psymbol.aclass = LOC_TYPEDEF;
8685 where = psymbol_placement::GLOBAL;
8686 break;
8687 case DW_TAG_module:
8688 /* With Fortran 77 there might be a "BLOCK DATA" module
8689 available without any name. If so, we skip the module as it
8690 doesn't bring any value. */
8691 if (actual_name != nullptr)
8692 {
8693 psymbol.domain = MODULE_DOMAIN;
8694 psymbol.aclass = LOC_TYPEDEF;
8695 where = psymbol_placement::GLOBAL;
8696 }
8697 break;
8698 case DW_TAG_class_type:
8699 case DW_TAG_interface_type:
8700 case DW_TAG_structure_type:
8701 case DW_TAG_union_type:
8702 case DW_TAG_enumeration_type:
8703 /* Skip external references. The DWARF standard says in the section
8704 about "Structure, Union, and Class Type Entries": "An incomplete
8705 structure, union or class type is represented by a structure,
8706 union or class entry that does not have a byte size attribute
8707 and that has a DW_AT_declaration attribute." */
8708 if (!pdi->has_byte_size && pdi->is_declaration)
8709 return;
8710
8711 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8712 static vs. global. */
8713 psymbol.domain = STRUCT_DOMAIN;
8714 psymbol.aclass = LOC_TYPEDEF;
8715 where = (cu->language == language_cplus
8716 ? psymbol_placement::GLOBAL
8717 : psymbol_placement::STATIC);
8718 break;
8719 case DW_TAG_enumerator:
8720 psymbol.domain = VAR_DOMAIN;
8721 psymbol.aclass = LOC_CONST;
8722 where = (cu->language == language_cplus
8723 ? psymbol_placement::GLOBAL
8724 : psymbol_placement::STATIC);
8725 break;
8726 default:
8727 break;
8728 }
8729
8730 if (where.has_value ())
8731 {
8732 if (built_actual_name != nullptr)
8733 actual_name = objfile->intern (actual_name);
8734 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8735 psymbol.ginfo.set_linkage_name (actual_name);
8736 else
8737 {
8738 psymbol.ginfo.set_demangled_name (actual_name,
8739 &objfile->objfile_obstack);
8740 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8741 }
8742 cu->per_cu->v.psymtab->add_psymbol (psymbol, *where, objfile);
8743 }
8744 }
8745
8746 /* Read a partial die corresponding to a namespace; also, add a symbol
8747 corresponding to that namespace to the symbol table. NAMESPACE is
8748 the name of the enclosing namespace. */
8749
8750 static void
8751 add_partial_namespace (struct partial_die_info *pdi,
8752 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8753 int set_addrmap, struct dwarf2_cu *cu)
8754 {
8755 /* Add a symbol for the namespace. */
8756
8757 add_partial_symbol (pdi, cu);
8758
8759 /* Now scan partial symbols in that namespace. */
8760
8761 if (pdi->has_children)
8762 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8763 }
8764
8765 /* Read a partial die corresponding to a Fortran module. */
8766
8767 static void
8768 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8769 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8770 {
8771 /* Add a symbol for the namespace. */
8772
8773 add_partial_symbol (pdi, cu);
8774
8775 /* Now scan partial symbols in that module. */
8776
8777 if (pdi->has_children)
8778 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8779 }
8780
8781 /* Read a partial die corresponding to a subprogram or an inlined
8782 subprogram and create a partial symbol for that subprogram.
8783 When the CU language allows it, this routine also defines a partial
8784 symbol for each nested subprogram that this subprogram contains.
8785 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8786 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8787
8788 PDI may also be a lexical block, in which case we simply search
8789 recursively for subprograms defined inside that lexical block.
8790 Again, this is only performed when the CU language allows this
8791 type of definitions. */
8792
8793 static void
8794 add_partial_subprogram (struct partial_die_info *pdi,
8795 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8796 int set_addrmap, struct dwarf2_cu *cu)
8797 {
8798 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8799 {
8800 if (pdi->has_pc_info)
8801 {
8802 if (pdi->lowpc < *lowpc)
8803 *lowpc = pdi->lowpc;
8804 if (pdi->highpc > *highpc)
8805 *highpc = pdi->highpc;
8806 if (set_addrmap)
8807 {
8808 struct objfile *objfile = cu->per_objfile->objfile;
8809 struct gdbarch *gdbarch = objfile->arch ();
8810 CORE_ADDR baseaddr;
8811 CORE_ADDR this_highpc;
8812 CORE_ADDR this_lowpc;
8813
8814 baseaddr = objfile->text_section_offset ();
8815 this_lowpc
8816 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8817 pdi->lowpc + baseaddr)
8818 - baseaddr);
8819 this_highpc
8820 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8821 pdi->highpc + baseaddr)
8822 - baseaddr);
8823 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8824 this_lowpc, this_highpc - 1,
8825 cu->per_cu->v.psymtab);
8826 }
8827 }
8828
8829 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8830 {
8831 if (!pdi->is_declaration)
8832 /* Ignore subprogram DIEs that do not have a name, they are
8833 illegal. Do not emit a complaint at this point, we will
8834 do so when we convert this psymtab into a symtab. */
8835 if (pdi->name (cu))
8836 add_partial_symbol (pdi, cu);
8837 }
8838 }
8839
8840 if (! pdi->has_children)
8841 return;
8842
8843 if (cu->language == language_ada || cu->language == language_fortran)
8844 {
8845 pdi = pdi->die_child;
8846 while (pdi != NULL)
8847 {
8848 pdi->fixup (cu);
8849 if (pdi->tag == DW_TAG_subprogram
8850 || pdi->tag == DW_TAG_inlined_subroutine
8851 || pdi->tag == DW_TAG_lexical_block)
8852 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8853 pdi = pdi->die_sibling;
8854 }
8855 }
8856 }
8857
8858 /* Read a partial die corresponding to an enumeration type. */
8859
8860 static void
8861 add_partial_enumeration (struct partial_die_info *enum_pdi,
8862 struct dwarf2_cu *cu)
8863 {
8864 struct partial_die_info *pdi;
8865
8866 if (enum_pdi->name (cu) != NULL)
8867 add_partial_symbol (enum_pdi, cu);
8868
8869 pdi = enum_pdi->die_child;
8870 while (pdi)
8871 {
8872 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8873 complaint (_("malformed enumerator DIE ignored"));
8874 else
8875 add_partial_symbol (pdi, cu);
8876 pdi = pdi->die_sibling;
8877 }
8878 }
8879
8880 /* Return the initial uleb128 in the die at INFO_PTR. */
8881
8882 static unsigned int
8883 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8884 {
8885 unsigned int bytes_read;
8886
8887 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8888 }
8889
8890 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8891 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8892
8893 Return the corresponding abbrev, or NULL if the number is zero (indicating
8894 an empty DIE). In either case *BYTES_READ will be set to the length of
8895 the initial number. */
8896
8897 static struct abbrev_info *
8898 peek_die_abbrev (const die_reader_specs &reader,
8899 const gdb_byte *info_ptr, unsigned int *bytes_read)
8900 {
8901 dwarf2_cu *cu = reader.cu;
8902 bfd *abfd = cu->per_objfile->objfile->obfd;
8903 unsigned int abbrev_number
8904 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8905
8906 if (abbrev_number == 0)
8907 return NULL;
8908
8909 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8910 if (!abbrev)
8911 {
8912 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8913 " at offset %s [in module %s]"),
8914 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8915 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8916 }
8917
8918 return abbrev;
8919 }
8920
8921 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8922 Returns a pointer to the end of a series of DIEs, terminated by an empty
8923 DIE. Any children of the skipped DIEs will also be skipped. */
8924
8925 static const gdb_byte *
8926 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8927 {
8928 while (1)
8929 {
8930 unsigned int bytes_read;
8931 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8932
8933 if (abbrev == NULL)
8934 return info_ptr + bytes_read;
8935 else
8936 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8937 }
8938 }
8939
8940 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8941 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8942 abbrev corresponding to that skipped uleb128 should be passed in
8943 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8944 children. */
8945
8946 static const gdb_byte *
8947 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8948 struct abbrev_info *abbrev)
8949 {
8950 unsigned int bytes_read;
8951 struct attribute attr;
8952 bfd *abfd = reader->abfd;
8953 struct dwarf2_cu *cu = reader->cu;
8954 const gdb_byte *buffer = reader->buffer;
8955 const gdb_byte *buffer_end = reader->buffer_end;
8956 unsigned int form, i;
8957
8958 for (i = 0; i < abbrev->num_attrs; i++)
8959 {
8960 /* The only abbrev we care about is DW_AT_sibling. */
8961 if (abbrev->attrs[i].name == DW_AT_sibling)
8962 {
8963 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8964 if (attr.form == DW_FORM_ref_addr)
8965 complaint (_("ignoring absolute DW_AT_sibling"));
8966 else
8967 {
8968 sect_offset off = attr.get_ref_die_offset ();
8969 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8970
8971 if (sibling_ptr < info_ptr)
8972 complaint (_("DW_AT_sibling points backwards"));
8973 else if (sibling_ptr > reader->buffer_end)
8974 reader->die_section->overflow_complaint ();
8975 else
8976 return sibling_ptr;
8977 }
8978 }
8979
8980 /* If it isn't DW_AT_sibling, skip this attribute. */
8981 form = abbrev->attrs[i].form;
8982 skip_attribute:
8983 switch (form)
8984 {
8985 case DW_FORM_ref_addr:
8986 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8987 and later it is offset sized. */
8988 if (cu->header.version == 2)
8989 info_ptr += cu->header.addr_size;
8990 else
8991 info_ptr += cu->header.offset_size;
8992 break;
8993 case DW_FORM_GNU_ref_alt:
8994 info_ptr += cu->header.offset_size;
8995 break;
8996 case DW_FORM_addr:
8997 info_ptr += cu->header.addr_size;
8998 break;
8999 case DW_FORM_data1:
9000 case DW_FORM_ref1:
9001 case DW_FORM_flag:
9002 case DW_FORM_strx1:
9003 info_ptr += 1;
9004 break;
9005 case DW_FORM_flag_present:
9006 case DW_FORM_implicit_const:
9007 break;
9008 case DW_FORM_data2:
9009 case DW_FORM_ref2:
9010 case DW_FORM_strx2:
9011 info_ptr += 2;
9012 break;
9013 case DW_FORM_strx3:
9014 info_ptr += 3;
9015 break;
9016 case DW_FORM_data4:
9017 case DW_FORM_ref4:
9018 case DW_FORM_strx4:
9019 info_ptr += 4;
9020 break;
9021 case DW_FORM_data8:
9022 case DW_FORM_ref8:
9023 case DW_FORM_ref_sig8:
9024 info_ptr += 8;
9025 break;
9026 case DW_FORM_data16:
9027 info_ptr += 16;
9028 break;
9029 case DW_FORM_string:
9030 read_direct_string (abfd, info_ptr, &bytes_read);
9031 info_ptr += bytes_read;
9032 break;
9033 case DW_FORM_sec_offset:
9034 case DW_FORM_strp:
9035 case DW_FORM_GNU_strp_alt:
9036 info_ptr += cu->header.offset_size;
9037 break;
9038 case DW_FORM_exprloc:
9039 case DW_FORM_block:
9040 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9041 info_ptr += bytes_read;
9042 break;
9043 case DW_FORM_block1:
9044 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9045 break;
9046 case DW_FORM_block2:
9047 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9048 break;
9049 case DW_FORM_block4:
9050 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9051 break;
9052 case DW_FORM_addrx:
9053 case DW_FORM_strx:
9054 case DW_FORM_sdata:
9055 case DW_FORM_udata:
9056 case DW_FORM_ref_udata:
9057 case DW_FORM_GNU_addr_index:
9058 case DW_FORM_GNU_str_index:
9059 case DW_FORM_rnglistx:
9060 case DW_FORM_loclistx:
9061 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9062 break;
9063 case DW_FORM_indirect:
9064 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9065 info_ptr += bytes_read;
9066 /* We need to continue parsing from here, so just go back to
9067 the top. */
9068 goto skip_attribute;
9069
9070 default:
9071 error (_("Dwarf Error: Cannot handle %s "
9072 "in DWARF reader [in module %s]"),
9073 dwarf_form_name (form),
9074 bfd_get_filename (abfd));
9075 }
9076 }
9077
9078 if (abbrev->has_children)
9079 return skip_children (reader, info_ptr);
9080 else
9081 return info_ptr;
9082 }
9083
9084 /* Locate ORIG_PDI's sibling.
9085 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9086
9087 static const gdb_byte *
9088 locate_pdi_sibling (const struct die_reader_specs *reader,
9089 struct partial_die_info *orig_pdi,
9090 const gdb_byte *info_ptr)
9091 {
9092 /* Do we know the sibling already? */
9093
9094 if (orig_pdi->sibling)
9095 return orig_pdi->sibling;
9096
9097 /* Are there any children to deal with? */
9098
9099 if (!orig_pdi->has_children)
9100 return info_ptr;
9101
9102 /* Skip the children the long way. */
9103
9104 return skip_children (reader, info_ptr);
9105 }
9106
9107 /* Expand this partial symbol table into a full symbol table. SELF is
9108 not NULL. */
9109
9110 void
9111 dwarf2_psymtab::read_symtab (struct objfile *objfile)
9112 {
9113 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9114
9115 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
9116
9117 /* If this psymtab is constructed from a debug-only objfile, the
9118 has_section_at_zero flag will not necessarily be correct. We
9119 can get the correct value for this flag by looking at the data
9120 associated with the (presumably stripped) associated objfile. */
9121 if (objfile->separate_debug_objfile_backlink)
9122 {
9123 dwarf2_per_objfile *per_objfile_backlink
9124 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9125
9126 per_objfile->per_bfd->has_section_at_zero
9127 = per_objfile_backlink->per_bfd->has_section_at_zero;
9128 }
9129
9130 expand_psymtab (objfile);
9131
9132 process_cu_includes (per_objfile);
9133 }
9134 \f
9135 /* Reading in full CUs. */
9136
9137 /* Add PER_CU to the queue. */
9138
9139 static void
9140 queue_comp_unit (dwarf2_per_cu_data *per_cu,
9141 dwarf2_per_objfile *per_objfile,
9142 enum language pretend_language)
9143 {
9144 per_cu->queued = 1;
9145 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
9146 }
9147
9148 /* If PER_CU is not yet queued, add it to the queue.
9149 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9150 dependency.
9151 The result is non-zero if PER_CU was queued, otherwise the result is zero
9152 meaning either PER_CU is already queued or it is already loaded.
9153
9154 N.B. There is an invariant here that if a CU is queued then it is loaded.
9155 The caller is required to load PER_CU if we return non-zero. */
9156
9157 static int
9158 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9159 dwarf2_per_cu_data *per_cu,
9160 dwarf2_per_objfile *per_objfile,
9161 enum language pretend_language)
9162 {
9163 /* We may arrive here during partial symbol reading, if we need full
9164 DIEs to process an unusual case (e.g. template arguments). Do
9165 not queue PER_CU, just tell our caller to load its DIEs. */
9166 if (per_cu->per_bfd->reading_partial_symbols)
9167 {
9168 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9169
9170 if (cu == NULL || cu->dies == NULL)
9171 return 1;
9172 return 0;
9173 }
9174
9175 /* Mark the dependence relation so that we don't flush PER_CU
9176 too early. */
9177 if (dependent_cu != NULL)
9178 dwarf2_add_dependence (dependent_cu, per_cu);
9179
9180 /* If it's already on the queue, we have nothing to do. */
9181 if (per_cu->queued)
9182 return 0;
9183
9184 /* If the compilation unit is already loaded, just mark it as
9185 used. */
9186 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9187 if (cu != nullptr)
9188 {
9189 cu->last_used = 0;
9190 return 0;
9191 }
9192
9193 /* Add it to the queue. */
9194 queue_comp_unit (per_cu, per_objfile, pretend_language);
9195
9196 return 1;
9197 }
9198
9199 /* Process the queue. */
9200
9201 static void
9202 process_queue (dwarf2_per_objfile *per_objfile)
9203 {
9204 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
9205 objfile_name (per_objfile->objfile));
9206
9207 /* The queue starts out with one item, but following a DIE reference
9208 may load a new CU, adding it to the end of the queue. */
9209 while (!per_objfile->per_bfd->queue.empty ())
9210 {
9211 dwarf2_queue_item &item = per_objfile->per_bfd->queue.front ();
9212 dwarf2_per_cu_data *per_cu = item.per_cu;
9213
9214 if (!per_objfile->symtab_set_p (per_cu))
9215 {
9216 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9217
9218 /* Skip dummy CUs. */
9219 if (cu != nullptr)
9220 {
9221 unsigned int debug_print_threshold;
9222 char buf[100];
9223
9224 if (per_cu->is_debug_types)
9225 {
9226 struct signatured_type *sig_type =
9227 (struct signatured_type *) per_cu;
9228
9229 sprintf (buf, "TU %s at offset %s",
9230 hex_string (sig_type->signature),
9231 sect_offset_str (per_cu->sect_off));
9232 /* There can be 100s of TUs.
9233 Only print them in verbose mode. */
9234 debug_print_threshold = 2;
9235 }
9236 else
9237 {
9238 sprintf (buf, "CU at offset %s",
9239 sect_offset_str (per_cu->sect_off));
9240 debug_print_threshold = 1;
9241 }
9242
9243 if (dwarf_read_debug >= debug_print_threshold)
9244 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
9245
9246 if (per_cu->is_debug_types)
9247 process_full_type_unit (cu, item.pretend_language);
9248 else
9249 process_full_comp_unit (cu, item.pretend_language);
9250
9251 if (dwarf_read_debug >= debug_print_threshold)
9252 dwarf_read_debug_printf ("Done expanding %s", buf);
9253 }
9254 }
9255
9256 per_cu->queued = 0;
9257 per_objfile->per_bfd->queue.pop ();
9258 }
9259
9260 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
9261 objfile_name (per_objfile->objfile));
9262 }
9263
9264 /* Read in full symbols for PST, and anything it depends on. */
9265
9266 void
9267 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9268 {
9269 gdb_assert (!readin_p (objfile));
9270
9271 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9272 free_cached_comp_units freer (per_objfile);
9273 expand_dependencies (objfile);
9274
9275 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9276 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9277 }
9278
9279 /* See psympriv.h. */
9280
9281 bool
9282 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9283 {
9284 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9285 return per_objfile->symtab_set_p (per_cu_data);
9286 }
9287
9288 /* See psympriv.h. */
9289
9290 compunit_symtab *
9291 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9292 {
9293 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9294 return per_objfile->get_symtab (per_cu_data);
9295 }
9296
9297 /* Trivial hash function for die_info: the hash value of a DIE
9298 is its offset in .debug_info for this objfile. */
9299
9300 static hashval_t
9301 die_hash (const void *item)
9302 {
9303 const struct die_info *die = (const struct die_info *) item;
9304
9305 return to_underlying (die->sect_off);
9306 }
9307
9308 /* Trivial comparison function for die_info structures: two DIEs
9309 are equal if they have the same offset. */
9310
9311 static int
9312 die_eq (const void *item_lhs, const void *item_rhs)
9313 {
9314 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9315 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9316
9317 return die_lhs->sect_off == die_rhs->sect_off;
9318 }
9319
9320 /* Load the DIEs associated with PER_CU into memory.
9321
9322 In some cases, the caller, while reading partial symbols, will need to load
9323 the full symbols for the CU for some reason. It will already have a
9324 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
9325 rather than creating a new one. */
9326
9327 static void
9328 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9329 dwarf2_per_objfile *per_objfile,
9330 dwarf2_cu *existing_cu,
9331 bool skip_partial,
9332 enum language pretend_language)
9333 {
9334 gdb_assert (! this_cu->is_debug_types);
9335
9336 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
9337 if (reader.dummy_p)
9338 return;
9339
9340 struct dwarf2_cu *cu = reader.cu;
9341 const gdb_byte *info_ptr = reader.info_ptr;
9342
9343 gdb_assert (cu->die_hash == NULL);
9344 cu->die_hash =
9345 htab_create_alloc_ex (cu->header.length / 12,
9346 die_hash,
9347 die_eq,
9348 NULL,
9349 &cu->comp_unit_obstack,
9350 hashtab_obstack_allocate,
9351 dummy_obstack_deallocate);
9352
9353 if (reader.comp_unit_die->has_children)
9354 reader.comp_unit_die->child
9355 = read_die_and_siblings (&reader, reader.info_ptr,
9356 &info_ptr, reader.comp_unit_die);
9357 cu->dies = reader.comp_unit_die;
9358 /* comp_unit_die is not stored in die_hash, no need. */
9359
9360 /* We try not to read any attributes in this function, because not
9361 all CUs needed for references have been loaded yet, and symbol
9362 table processing isn't initialized. But we have to set the CU language,
9363 or we won't be able to build types correctly.
9364 Similarly, if we do not read the producer, we can not apply
9365 producer-specific interpretation. */
9366 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9367
9368 reader.keep ();
9369 }
9370
9371 /* Add a DIE to the delayed physname list. */
9372
9373 static void
9374 add_to_method_list (struct type *type, int fnfield_index, int index,
9375 const char *name, struct die_info *die,
9376 struct dwarf2_cu *cu)
9377 {
9378 struct delayed_method_info mi;
9379 mi.type = type;
9380 mi.fnfield_index = fnfield_index;
9381 mi.index = index;
9382 mi.name = name;
9383 mi.die = die;
9384 cu->method_list.push_back (mi);
9385 }
9386
9387 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9388 "const" / "volatile". If so, decrements LEN by the length of the
9389 modifier and return true. Otherwise return false. */
9390
9391 template<size_t N>
9392 static bool
9393 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9394 {
9395 size_t mod_len = sizeof (mod) - 1;
9396 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9397 {
9398 len -= mod_len;
9399 return true;
9400 }
9401 return false;
9402 }
9403
9404 /* Compute the physnames of any methods on the CU's method list.
9405
9406 The computation of method physnames is delayed in order to avoid the
9407 (bad) condition that one of the method's formal parameters is of an as yet
9408 incomplete type. */
9409
9410 static void
9411 compute_delayed_physnames (struct dwarf2_cu *cu)
9412 {
9413 /* Only C++ delays computing physnames. */
9414 if (cu->method_list.empty ())
9415 return;
9416 gdb_assert (cu->language == language_cplus);
9417
9418 for (const delayed_method_info &mi : cu->method_list)
9419 {
9420 const char *physname;
9421 struct fn_fieldlist *fn_flp
9422 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9423 physname = dwarf2_physname (mi.name, mi.die, cu);
9424 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9425 = physname ? physname : "";
9426
9427 /* Since there's no tag to indicate whether a method is a
9428 const/volatile overload, extract that information out of the
9429 demangled name. */
9430 if (physname != NULL)
9431 {
9432 size_t len = strlen (physname);
9433
9434 while (1)
9435 {
9436 if (physname[len] == ')') /* shortcut */
9437 break;
9438 else if (check_modifier (physname, len, " const"))
9439 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9440 else if (check_modifier (physname, len, " volatile"))
9441 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9442 else
9443 break;
9444 }
9445 }
9446 }
9447
9448 /* The list is no longer needed. */
9449 cu->method_list.clear ();
9450 }
9451
9452 /* Go objects should be embedded in a DW_TAG_module DIE,
9453 and it's not clear if/how imported objects will appear.
9454 To keep Go support simple until that's worked out,
9455 go back through what we've read and create something usable.
9456 We could do this while processing each DIE, and feels kinda cleaner,
9457 but that way is more invasive.
9458 This is to, for example, allow the user to type "p var" or "b main"
9459 without having to specify the package name, and allow lookups
9460 of module.object to work in contexts that use the expression
9461 parser. */
9462
9463 static void
9464 fixup_go_packaging (struct dwarf2_cu *cu)
9465 {
9466 gdb::unique_xmalloc_ptr<char> package_name;
9467 struct pending *list;
9468 int i;
9469
9470 for (list = *cu->get_builder ()->get_global_symbols ();
9471 list != NULL;
9472 list = list->next)
9473 {
9474 for (i = 0; i < list->nsyms; ++i)
9475 {
9476 struct symbol *sym = list->symbol[i];
9477
9478 if (sym->language () == language_go
9479 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9480 {
9481 gdb::unique_xmalloc_ptr<char> this_package_name
9482 (go_symbol_package_name (sym));
9483
9484 if (this_package_name == NULL)
9485 continue;
9486 if (package_name == NULL)
9487 package_name = std::move (this_package_name);
9488 else
9489 {
9490 struct objfile *objfile = cu->per_objfile->objfile;
9491 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9492 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9493 (symbol_symtab (sym) != NULL
9494 ? symtab_to_filename_for_display
9495 (symbol_symtab (sym))
9496 : objfile_name (objfile)),
9497 this_package_name.get (), package_name.get ());
9498 }
9499 }
9500 }
9501 }
9502
9503 if (package_name != NULL)
9504 {
9505 struct objfile *objfile = cu->per_objfile->objfile;
9506 const char *saved_package_name = objfile->intern (package_name.get ());
9507 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9508 saved_package_name);
9509 struct symbol *sym;
9510
9511 sym = new (&objfile->objfile_obstack) symbol;
9512 sym->set_language (language_go, &objfile->objfile_obstack);
9513 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9514 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9515 e.g., "main" finds the "main" module and not C's main(). */
9516 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9517 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9518 SYMBOL_TYPE (sym) = type;
9519
9520 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9521 }
9522 }
9523
9524 /* Allocate a fully-qualified name consisting of the two parts on the
9525 obstack. */
9526
9527 static const char *
9528 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9529 {
9530 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9531 }
9532
9533 /* A helper that allocates a variant part to attach to a Rust enum
9534 type. OBSTACK is where the results should be allocated. TYPE is
9535 the type we're processing. DISCRIMINANT_INDEX is the index of the
9536 discriminant. It must be the index of one of the fields of TYPE,
9537 or -1 to mean there is no discriminant (univariant enum).
9538 DEFAULT_INDEX is the index of the default field; or -1 if there is
9539 no default. RANGES is indexed by "effective" field number (the
9540 field index, but omitting the discriminant and default fields) and
9541 must hold the discriminant values used by the variants. Note that
9542 RANGES must have a lifetime at least as long as OBSTACK -- either
9543 already allocated on it, or static. */
9544
9545 static void
9546 alloc_rust_variant (struct obstack *obstack, struct type *type,
9547 int discriminant_index, int default_index,
9548 gdb::array_view<discriminant_range> ranges)
9549 {
9550 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9551 gdb_assert (discriminant_index == -1
9552 || (discriminant_index >= 0
9553 && discriminant_index < type->num_fields ()));
9554 gdb_assert (default_index == -1
9555 || (default_index >= 0 && default_index < type->num_fields ()));
9556
9557 /* We have one variant for each non-discriminant field. */
9558 int n_variants = type->num_fields ();
9559 if (discriminant_index != -1)
9560 --n_variants;
9561
9562 variant *variants = new (obstack) variant[n_variants];
9563 int var_idx = 0;
9564 int range_idx = 0;
9565 for (int i = 0; i < type->num_fields (); ++i)
9566 {
9567 if (i == discriminant_index)
9568 continue;
9569
9570 variants[var_idx].first_field = i;
9571 variants[var_idx].last_field = i + 1;
9572
9573 /* The default field does not need a range, but other fields do.
9574 We skipped the discriminant above. */
9575 if (i != default_index)
9576 {
9577 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9578 ++range_idx;
9579 }
9580
9581 ++var_idx;
9582 }
9583
9584 gdb_assert (range_idx == ranges.size ());
9585 gdb_assert (var_idx == n_variants);
9586
9587 variant_part *part = new (obstack) variant_part;
9588 part->discriminant_index = discriminant_index;
9589 /* If there is no discriminant, then whether it is signed is of no
9590 consequence. */
9591 part->is_unsigned
9592 = (discriminant_index == -1
9593 ? false
9594 : type->field (discriminant_index).type ()->is_unsigned ());
9595 part->variants = gdb::array_view<variant> (variants, n_variants);
9596
9597 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9598 gdb::array_view<variant_part> *prop_value
9599 = new (storage) gdb::array_view<variant_part> (part, 1);
9600
9601 struct dynamic_prop prop;
9602 prop.set_variant_parts (prop_value);
9603
9604 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9605 }
9606
9607 /* Some versions of rustc emitted enums in an unusual way.
9608
9609 Ordinary enums were emitted as unions. The first element of each
9610 structure in the union was named "RUST$ENUM$DISR". This element
9611 held the discriminant.
9612
9613 These versions of Rust also implemented the "non-zero"
9614 optimization. When the enum had two values, and one is empty and
9615 the other holds a pointer that cannot be zero, the pointer is used
9616 as the discriminant, with a zero value meaning the empty variant.
9617 Here, the union's first member is of the form
9618 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9619 where the fieldnos are the indices of the fields that should be
9620 traversed in order to find the field (which may be several fields deep)
9621 and the variantname is the name of the variant of the case when the
9622 field is zero.
9623
9624 This function recognizes whether TYPE is of one of these forms,
9625 and, if so, smashes it to be a variant type. */
9626
9627 static void
9628 quirk_rust_enum (struct type *type, struct objfile *objfile)
9629 {
9630 gdb_assert (type->code () == TYPE_CODE_UNION);
9631
9632 /* We don't need to deal with empty enums. */
9633 if (type->num_fields () == 0)
9634 return;
9635
9636 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9637 if (type->num_fields () == 1
9638 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9639 {
9640 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9641
9642 /* Decode the field name to find the offset of the
9643 discriminant. */
9644 ULONGEST bit_offset = 0;
9645 struct type *field_type = type->field (0).type ();
9646 while (name[0] >= '0' && name[0] <= '9')
9647 {
9648 char *tail;
9649 unsigned long index = strtoul (name, &tail, 10);
9650 name = tail;
9651 if (*name != '$'
9652 || index >= field_type->num_fields ()
9653 || (TYPE_FIELD_LOC_KIND (field_type, index)
9654 != FIELD_LOC_KIND_BITPOS))
9655 {
9656 complaint (_("Could not parse Rust enum encoding string \"%s\""
9657 "[in module %s]"),
9658 TYPE_FIELD_NAME (type, 0),
9659 objfile_name (objfile));
9660 return;
9661 }
9662 ++name;
9663
9664 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9665 field_type = field_type->field (index).type ();
9666 }
9667
9668 /* Smash this type to be a structure type. We have to do this
9669 because the type has already been recorded. */
9670 type->set_code (TYPE_CODE_STRUCT);
9671 type->set_num_fields (3);
9672 /* Save the field we care about. */
9673 struct field saved_field = type->field (0);
9674 type->set_fields
9675 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9676
9677 /* Put the discriminant at index 0. */
9678 type->field (0).set_type (field_type);
9679 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9680 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9681 SET_FIELD_BITPOS (type->field (0), bit_offset);
9682
9683 /* The order of fields doesn't really matter, so put the real
9684 field at index 1 and the data-less field at index 2. */
9685 type->field (1) = saved_field;
9686 TYPE_FIELD_NAME (type, 1)
9687 = rust_last_path_segment (type->field (1).type ()->name ());
9688 type->field (1).type ()->set_name
9689 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9690 TYPE_FIELD_NAME (type, 1)));
9691
9692 const char *dataless_name
9693 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9694 name);
9695 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9696 dataless_name);
9697 type->field (2).set_type (dataless_type);
9698 /* NAME points into the original discriminant name, which
9699 already has the correct lifetime. */
9700 TYPE_FIELD_NAME (type, 2) = name;
9701 SET_FIELD_BITPOS (type->field (2), 0);
9702
9703 /* Indicate that this is a variant type. */
9704 static discriminant_range ranges[1] = { { 0, 0 } };
9705 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9706 }
9707 /* A union with a single anonymous field is probably an old-style
9708 univariant enum. */
9709 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9710 {
9711 /* Smash this type to be a structure type. We have to do this
9712 because the type has already been recorded. */
9713 type->set_code (TYPE_CODE_STRUCT);
9714
9715 struct type *field_type = type->field (0).type ();
9716 const char *variant_name
9717 = rust_last_path_segment (field_type->name ());
9718 TYPE_FIELD_NAME (type, 0) = variant_name;
9719 field_type->set_name
9720 (rust_fully_qualify (&objfile->objfile_obstack,
9721 type->name (), variant_name));
9722
9723 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9724 }
9725 else
9726 {
9727 struct type *disr_type = nullptr;
9728 for (int i = 0; i < type->num_fields (); ++i)
9729 {
9730 disr_type = type->field (i).type ();
9731
9732 if (disr_type->code () != TYPE_CODE_STRUCT)
9733 {
9734 /* All fields of a true enum will be structs. */
9735 return;
9736 }
9737 else if (disr_type->num_fields () == 0)
9738 {
9739 /* Could be data-less variant, so keep going. */
9740 disr_type = nullptr;
9741 }
9742 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9743 "RUST$ENUM$DISR") != 0)
9744 {
9745 /* Not a Rust enum. */
9746 return;
9747 }
9748 else
9749 {
9750 /* Found one. */
9751 break;
9752 }
9753 }
9754
9755 /* If we got here without a discriminant, then it's probably
9756 just a union. */
9757 if (disr_type == nullptr)
9758 return;
9759
9760 /* Smash this type to be a structure type. We have to do this
9761 because the type has already been recorded. */
9762 type->set_code (TYPE_CODE_STRUCT);
9763
9764 /* Make space for the discriminant field. */
9765 struct field *disr_field = &disr_type->field (0);
9766 field *new_fields
9767 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9768 * sizeof (struct field)));
9769 memcpy (new_fields + 1, type->fields (),
9770 type->num_fields () * sizeof (struct field));
9771 type->set_fields (new_fields);
9772 type->set_num_fields (type->num_fields () + 1);
9773
9774 /* Install the discriminant at index 0 in the union. */
9775 type->field (0) = *disr_field;
9776 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9777 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9778
9779 /* We need a way to find the correct discriminant given a
9780 variant name. For convenience we build a map here. */
9781 struct type *enum_type = disr_field->type ();
9782 std::unordered_map<std::string, ULONGEST> discriminant_map;
9783 for (int i = 0; i < enum_type->num_fields (); ++i)
9784 {
9785 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9786 {
9787 const char *name
9788 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9789 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9790 }
9791 }
9792
9793 int n_fields = type->num_fields ();
9794 /* We don't need a range entry for the discriminant, but we do
9795 need one for every other field, as there is no default
9796 variant. */
9797 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9798 discriminant_range,
9799 n_fields - 1);
9800 /* Skip the discriminant here. */
9801 for (int i = 1; i < n_fields; ++i)
9802 {
9803 /* Find the final word in the name of this variant's type.
9804 That name can be used to look up the correct
9805 discriminant. */
9806 const char *variant_name
9807 = rust_last_path_segment (type->field (i).type ()->name ());
9808
9809 auto iter = discriminant_map.find (variant_name);
9810 if (iter != discriminant_map.end ())
9811 {
9812 ranges[i - 1].low = iter->second;
9813 ranges[i - 1].high = iter->second;
9814 }
9815
9816 /* In Rust, each element should have the size of the
9817 enclosing enum. */
9818 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9819
9820 /* Remove the discriminant field, if it exists. */
9821 struct type *sub_type = type->field (i).type ();
9822 if (sub_type->num_fields () > 0)
9823 {
9824 sub_type->set_num_fields (sub_type->num_fields () - 1);
9825 sub_type->set_fields (sub_type->fields () + 1);
9826 }
9827 TYPE_FIELD_NAME (type, i) = variant_name;
9828 sub_type->set_name
9829 (rust_fully_qualify (&objfile->objfile_obstack,
9830 type->name (), variant_name));
9831 }
9832
9833 /* Indicate that this is a variant type. */
9834 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9835 gdb::array_view<discriminant_range> (ranges,
9836 n_fields - 1));
9837 }
9838 }
9839
9840 /* Rewrite some Rust unions to be structures with variants parts. */
9841
9842 static void
9843 rust_union_quirks (struct dwarf2_cu *cu)
9844 {
9845 gdb_assert (cu->language == language_rust);
9846 for (type *type_ : cu->rust_unions)
9847 quirk_rust_enum (type_, cu->per_objfile->objfile);
9848 /* We don't need this any more. */
9849 cu->rust_unions.clear ();
9850 }
9851
9852 /* See read.h. */
9853
9854 type_unit_group_unshareable *
9855 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9856 {
9857 auto iter = this->m_type_units.find (tu_group);
9858 if (iter != this->m_type_units.end ())
9859 return iter->second.get ();
9860
9861 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9862 type_unit_group_unshareable *result = uniq.get ();
9863 this->m_type_units[tu_group] = std::move (uniq);
9864 return result;
9865 }
9866
9867 struct type *
9868 dwarf2_per_objfile::get_type_for_signatured_type
9869 (signatured_type *sig_type) const
9870 {
9871 auto iter = this->m_type_map.find (sig_type);
9872 if (iter == this->m_type_map.end ())
9873 return nullptr;
9874
9875 return iter->second;
9876 }
9877
9878 void dwarf2_per_objfile::set_type_for_signatured_type
9879 (signatured_type *sig_type, struct type *type)
9880 {
9881 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9882
9883 this->m_type_map[sig_type] = type;
9884 }
9885
9886 /* A helper function for computing the list of all symbol tables
9887 included by PER_CU. */
9888
9889 static void
9890 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9891 htab_t all_children, htab_t all_type_symtabs,
9892 dwarf2_per_cu_data *per_cu,
9893 dwarf2_per_objfile *per_objfile,
9894 struct compunit_symtab *immediate_parent)
9895 {
9896 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9897 if (*slot != NULL)
9898 {
9899 /* This inclusion and its children have been processed. */
9900 return;
9901 }
9902
9903 *slot = per_cu;
9904
9905 /* Only add a CU if it has a symbol table. */
9906 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9907 if (cust != NULL)
9908 {
9909 /* If this is a type unit only add its symbol table if we haven't
9910 seen it yet (type unit per_cu's can share symtabs). */
9911 if (per_cu->is_debug_types)
9912 {
9913 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9914 if (*slot == NULL)
9915 {
9916 *slot = cust;
9917 result->push_back (cust);
9918 if (cust->user == NULL)
9919 cust->user = immediate_parent;
9920 }
9921 }
9922 else
9923 {
9924 result->push_back (cust);
9925 if (cust->user == NULL)
9926 cust->user = immediate_parent;
9927 }
9928 }
9929
9930 if (!per_cu->imported_symtabs_empty ())
9931 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9932 {
9933 recursively_compute_inclusions (result, all_children,
9934 all_type_symtabs, ptr, per_objfile,
9935 cust);
9936 }
9937 }
9938
9939 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9940 PER_CU. */
9941
9942 static void
9943 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9944 dwarf2_per_objfile *per_objfile)
9945 {
9946 gdb_assert (! per_cu->is_debug_types);
9947
9948 if (!per_cu->imported_symtabs_empty ())
9949 {
9950 int len;
9951 std::vector<compunit_symtab *> result_symtabs;
9952 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9953
9954 /* If we don't have a symtab, we can just skip this case. */
9955 if (cust == NULL)
9956 return;
9957
9958 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9959 htab_eq_pointer,
9960 NULL, xcalloc, xfree));
9961 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9962 htab_eq_pointer,
9963 NULL, xcalloc, xfree));
9964
9965 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9966 {
9967 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9968 all_type_symtabs.get (), ptr,
9969 per_objfile, cust);
9970 }
9971
9972 /* Now we have a transitive closure of all the included symtabs. */
9973 len = result_symtabs.size ();
9974 cust->includes
9975 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9976 struct compunit_symtab *, len + 1);
9977 memcpy (cust->includes, result_symtabs.data (),
9978 len * sizeof (compunit_symtab *));
9979 cust->includes[len] = NULL;
9980 }
9981 }
9982
9983 /* Compute the 'includes' field for the symtabs of all the CUs we just
9984 read. */
9985
9986 static void
9987 process_cu_includes (dwarf2_per_objfile *per_objfile)
9988 {
9989 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9990 {
9991 if (! iter->is_debug_types)
9992 compute_compunit_symtab_includes (iter, per_objfile);
9993 }
9994
9995 per_objfile->per_bfd->just_read_cus.clear ();
9996 }
9997
9998 /* Generate full symbol information for CU, whose DIEs have
9999 already been loaded into memory. */
10000
10001 static void
10002 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10003 {
10004 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10005 struct objfile *objfile = per_objfile->objfile;
10006 struct gdbarch *gdbarch = objfile->arch ();
10007 CORE_ADDR lowpc, highpc;
10008 struct compunit_symtab *cust;
10009 CORE_ADDR baseaddr;
10010 struct block *static_block;
10011 CORE_ADDR addr;
10012
10013 baseaddr = objfile->text_section_offset ();
10014
10015 /* Clear the list here in case something was left over. */
10016 cu->method_list.clear ();
10017
10018 cu->language = pretend_language;
10019 cu->language_defn = language_def (cu->language);
10020
10021 dwarf2_find_base_address (cu->dies, cu);
10022
10023 /* Do line number decoding in read_file_scope () */
10024 process_die (cu->dies, cu);
10025
10026 /* For now fudge the Go package. */
10027 if (cu->language == language_go)
10028 fixup_go_packaging (cu);
10029
10030 /* Now that we have processed all the DIEs in the CU, all the types
10031 should be complete, and it should now be safe to compute all of the
10032 physnames. */
10033 compute_delayed_physnames (cu);
10034
10035 if (cu->language == language_rust)
10036 rust_union_quirks (cu);
10037
10038 /* Some compilers don't define a DW_AT_high_pc attribute for the
10039 compilation unit. If the DW_AT_high_pc is missing, synthesize
10040 it, by scanning the DIE's below the compilation unit. */
10041 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10042
10043 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10044 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10045
10046 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10047 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10048 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10049 addrmap to help ensure it has an accurate map of pc values belonging to
10050 this comp unit. */
10051 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10052
10053 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10054 SECT_OFF_TEXT (objfile),
10055 0);
10056
10057 if (cust != NULL)
10058 {
10059 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10060
10061 /* Set symtab language to language from DW_AT_language. If the
10062 compilation is from a C file generated by language preprocessors, do
10063 not set the language if it was already deduced by start_subfile. */
10064 if (!(cu->language == language_c
10065 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10066 COMPUNIT_FILETABS (cust)->language = cu->language;
10067
10068 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10069 produce DW_AT_location with location lists but it can be possibly
10070 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10071 there were bugs in prologue debug info, fixed later in GCC-4.5
10072 by "unwind info for epilogues" patch (which is not directly related).
10073
10074 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10075 needed, it would be wrong due to missing DW_AT_producer there.
10076
10077 Still one can confuse GDB by using non-standard GCC compilation
10078 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10079 */
10080 if (cu->has_loclist && gcc_4_minor >= 5)
10081 cust->locations_valid = 1;
10082
10083 if (gcc_4_minor >= 5)
10084 cust->epilogue_unwind_valid = 1;
10085
10086 cust->call_site_htab = cu->call_site_htab;
10087 }
10088
10089 per_objfile->set_symtab (cu->per_cu, cust);
10090
10091 /* Push it for inclusion processing later. */
10092 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
10093
10094 /* Not needed any more. */
10095 cu->reset_builder ();
10096 }
10097
10098 /* Generate full symbol information for type unit CU, whose DIEs have
10099 already been loaded into memory. */
10100
10101 static void
10102 process_full_type_unit (dwarf2_cu *cu,
10103 enum language pretend_language)
10104 {
10105 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10106 struct objfile *objfile = per_objfile->objfile;
10107 struct compunit_symtab *cust;
10108 struct signatured_type *sig_type;
10109
10110 gdb_assert (cu->per_cu->is_debug_types);
10111 sig_type = (struct signatured_type *) cu->per_cu;
10112
10113 /* Clear the list here in case something was left over. */
10114 cu->method_list.clear ();
10115
10116 cu->language = pretend_language;
10117 cu->language_defn = language_def (cu->language);
10118
10119 /* The symbol tables are set up in read_type_unit_scope. */
10120 process_die (cu->dies, cu);
10121
10122 /* For now fudge the Go package. */
10123 if (cu->language == language_go)
10124 fixup_go_packaging (cu);
10125
10126 /* Now that we have processed all the DIEs in the CU, all the types
10127 should be complete, and it should now be safe to compute all of the
10128 physnames. */
10129 compute_delayed_physnames (cu);
10130
10131 if (cu->language == language_rust)
10132 rust_union_quirks (cu);
10133
10134 /* TUs share symbol tables.
10135 If this is the first TU to use this symtab, complete the construction
10136 of it with end_expandable_symtab. Otherwise, complete the addition of
10137 this TU's symbols to the existing symtab. */
10138 type_unit_group_unshareable *tug_unshare =
10139 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
10140 if (tug_unshare->compunit_symtab == NULL)
10141 {
10142 buildsym_compunit *builder = cu->get_builder ();
10143 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10144 tug_unshare->compunit_symtab = cust;
10145
10146 if (cust != NULL)
10147 {
10148 /* Set symtab language to language from DW_AT_language. If the
10149 compilation is from a C file generated by language preprocessors,
10150 do not set the language if it was already deduced by
10151 start_subfile. */
10152 if (!(cu->language == language_c
10153 && COMPUNIT_FILETABS (cust)->language != language_c))
10154 COMPUNIT_FILETABS (cust)->language = cu->language;
10155 }
10156 }
10157 else
10158 {
10159 cu->get_builder ()->augment_type_symtab ();
10160 cust = tug_unshare->compunit_symtab;
10161 }
10162
10163 per_objfile->set_symtab (cu->per_cu, cust);
10164
10165 /* Not needed any more. */
10166 cu->reset_builder ();
10167 }
10168
10169 /* Process an imported unit DIE. */
10170
10171 static void
10172 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10173 {
10174 struct attribute *attr;
10175
10176 /* For now we don't handle imported units in type units. */
10177 if (cu->per_cu->is_debug_types)
10178 {
10179 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10180 " supported in type units [in module %s]"),
10181 objfile_name (cu->per_objfile->objfile));
10182 }
10183
10184 attr = dwarf2_attr (die, DW_AT_import, cu);
10185 if (attr != NULL)
10186 {
10187 sect_offset sect_off = attr->get_ref_die_offset ();
10188 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10189 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10190 dwarf2_per_cu_data *per_cu
10191 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
10192
10193 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10194 into another compilation unit, at root level. Regard this as a hint,
10195 and ignore it. */
10196 if (die->parent && die->parent->parent == NULL
10197 && per_cu->unit_type == DW_UT_compile
10198 && per_cu->lang == language_cplus)
10199 return;
10200
10201 /* If necessary, add it to the queue and load its DIEs. */
10202 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
10203 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
10204 false, cu->language);
10205
10206 cu->per_cu->imported_symtabs_push (per_cu);
10207 }
10208 }
10209
10210 /* RAII object that represents a process_die scope: i.e.,
10211 starts/finishes processing a DIE. */
10212 class process_die_scope
10213 {
10214 public:
10215 process_die_scope (die_info *die, dwarf2_cu *cu)
10216 : m_die (die), m_cu (cu)
10217 {
10218 /* We should only be processing DIEs not already in process. */
10219 gdb_assert (!m_die->in_process);
10220 m_die->in_process = true;
10221 }
10222
10223 ~process_die_scope ()
10224 {
10225 m_die->in_process = false;
10226
10227 /* If we're done processing the DIE for the CU that owns the line
10228 header, we don't need the line header anymore. */
10229 if (m_cu->line_header_die_owner == m_die)
10230 {
10231 delete m_cu->line_header;
10232 m_cu->line_header = NULL;
10233 m_cu->line_header_die_owner = NULL;
10234 }
10235 }
10236
10237 private:
10238 die_info *m_die;
10239 dwarf2_cu *m_cu;
10240 };
10241
10242 /* Process a die and its children. */
10243
10244 static void
10245 process_die (struct die_info *die, struct dwarf2_cu *cu)
10246 {
10247 process_die_scope scope (die, cu);
10248
10249 switch (die->tag)
10250 {
10251 case DW_TAG_padding:
10252 break;
10253 case DW_TAG_compile_unit:
10254 case DW_TAG_partial_unit:
10255 read_file_scope (die, cu);
10256 break;
10257 case DW_TAG_type_unit:
10258 read_type_unit_scope (die, cu);
10259 break;
10260 case DW_TAG_subprogram:
10261 /* Nested subprograms in Fortran get a prefix. */
10262 if (cu->language == language_fortran
10263 && die->parent != NULL
10264 && die->parent->tag == DW_TAG_subprogram)
10265 cu->processing_has_namespace_info = true;
10266 /* Fall through. */
10267 case DW_TAG_inlined_subroutine:
10268 read_func_scope (die, cu);
10269 break;
10270 case DW_TAG_lexical_block:
10271 case DW_TAG_try_block:
10272 case DW_TAG_catch_block:
10273 read_lexical_block_scope (die, cu);
10274 break;
10275 case DW_TAG_call_site:
10276 case DW_TAG_GNU_call_site:
10277 read_call_site_scope (die, cu);
10278 break;
10279 case DW_TAG_class_type:
10280 case DW_TAG_interface_type:
10281 case DW_TAG_structure_type:
10282 case DW_TAG_union_type:
10283 process_structure_scope (die, cu);
10284 break;
10285 case DW_TAG_enumeration_type:
10286 process_enumeration_scope (die, cu);
10287 break;
10288
10289 /* These dies have a type, but processing them does not create
10290 a symbol or recurse to process the children. Therefore we can
10291 read them on-demand through read_type_die. */
10292 case DW_TAG_subroutine_type:
10293 case DW_TAG_set_type:
10294 case DW_TAG_pointer_type:
10295 case DW_TAG_ptr_to_member_type:
10296 case DW_TAG_reference_type:
10297 case DW_TAG_rvalue_reference_type:
10298 case DW_TAG_string_type:
10299 break;
10300
10301 case DW_TAG_array_type:
10302 /* We only need to handle this case for Ada -- in other
10303 languages, it's normal for the compiler to emit a typedef
10304 instead. */
10305 if (cu->language != language_ada)
10306 break;
10307 /* FALLTHROUGH */
10308 case DW_TAG_base_type:
10309 case DW_TAG_subrange_type:
10310 case DW_TAG_typedef:
10311 /* Add a typedef symbol for the type definition, if it has a
10312 DW_AT_name. */
10313 new_symbol (die, read_type_die (die, cu), cu);
10314 break;
10315 case DW_TAG_common_block:
10316 read_common_block (die, cu);
10317 break;
10318 case DW_TAG_common_inclusion:
10319 break;
10320 case DW_TAG_namespace:
10321 cu->processing_has_namespace_info = true;
10322 read_namespace (die, cu);
10323 break;
10324 case DW_TAG_module:
10325 cu->processing_has_namespace_info = true;
10326 read_module (die, cu);
10327 break;
10328 case DW_TAG_imported_declaration:
10329 cu->processing_has_namespace_info = true;
10330 if (read_namespace_alias (die, cu))
10331 break;
10332 /* The declaration is not a global namespace alias. */
10333 /* Fall through. */
10334 case DW_TAG_imported_module:
10335 cu->processing_has_namespace_info = true;
10336 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10337 || cu->language != language_fortran))
10338 complaint (_("Tag '%s' has unexpected children"),
10339 dwarf_tag_name (die->tag));
10340 read_import_statement (die, cu);
10341 break;
10342
10343 case DW_TAG_imported_unit:
10344 process_imported_unit_die (die, cu);
10345 break;
10346
10347 case DW_TAG_variable:
10348 read_variable (die, cu);
10349 break;
10350
10351 default:
10352 new_symbol (die, NULL, cu);
10353 break;
10354 }
10355 }
10356 \f
10357 /* DWARF name computation. */
10358
10359 /* A helper function for dwarf2_compute_name which determines whether DIE
10360 needs to have the name of the scope prepended to the name listed in the
10361 die. */
10362
10363 static int
10364 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10365 {
10366 struct attribute *attr;
10367
10368 switch (die->tag)
10369 {
10370 case DW_TAG_namespace:
10371 case DW_TAG_typedef:
10372 case DW_TAG_class_type:
10373 case DW_TAG_interface_type:
10374 case DW_TAG_structure_type:
10375 case DW_TAG_union_type:
10376 case DW_TAG_enumeration_type:
10377 case DW_TAG_enumerator:
10378 case DW_TAG_subprogram:
10379 case DW_TAG_inlined_subroutine:
10380 case DW_TAG_member:
10381 case DW_TAG_imported_declaration:
10382 return 1;
10383
10384 case DW_TAG_variable:
10385 case DW_TAG_constant:
10386 /* We only need to prefix "globally" visible variables. These include
10387 any variable marked with DW_AT_external or any variable that
10388 lives in a namespace. [Variables in anonymous namespaces
10389 require prefixing, but they are not DW_AT_external.] */
10390
10391 if (dwarf2_attr (die, DW_AT_specification, cu))
10392 {
10393 struct dwarf2_cu *spec_cu = cu;
10394
10395 return die_needs_namespace (die_specification (die, &spec_cu),
10396 spec_cu);
10397 }
10398
10399 attr = dwarf2_attr (die, DW_AT_external, cu);
10400 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10401 && die->parent->tag != DW_TAG_module)
10402 return 0;
10403 /* A variable in a lexical block of some kind does not need a
10404 namespace, even though in C++ such variables may be external
10405 and have a mangled name. */
10406 if (die->parent->tag == DW_TAG_lexical_block
10407 || die->parent->tag == DW_TAG_try_block
10408 || die->parent->tag == DW_TAG_catch_block
10409 || die->parent->tag == DW_TAG_subprogram)
10410 return 0;
10411 return 1;
10412
10413 default:
10414 return 0;
10415 }
10416 }
10417
10418 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10419 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10420 defined for the given DIE. */
10421
10422 static struct attribute *
10423 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10424 {
10425 struct attribute *attr;
10426
10427 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10428 if (attr == NULL)
10429 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10430
10431 return attr;
10432 }
10433
10434 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10435 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10436 defined for the given DIE. */
10437
10438 static const char *
10439 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10440 {
10441 const char *linkage_name;
10442
10443 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10444 if (linkage_name == NULL)
10445 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10446
10447 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10448 See https://github.com/rust-lang/rust/issues/32925. */
10449 if (cu->language == language_rust && linkage_name != NULL
10450 && strchr (linkage_name, '{') != NULL)
10451 linkage_name = NULL;
10452
10453 return linkage_name;
10454 }
10455
10456 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10457 compute the physname for the object, which include a method's:
10458 - formal parameters (C++),
10459 - receiver type (Go),
10460
10461 The term "physname" is a bit confusing.
10462 For C++, for example, it is the demangled name.
10463 For Go, for example, it's the mangled name.
10464
10465 For Ada, return the DIE's linkage name rather than the fully qualified
10466 name. PHYSNAME is ignored..
10467
10468 The result is allocated on the objfile->per_bfd's obstack and
10469 canonicalized. */
10470
10471 static const char *
10472 dwarf2_compute_name (const char *name,
10473 struct die_info *die, struct dwarf2_cu *cu,
10474 int physname)
10475 {
10476 struct objfile *objfile = cu->per_objfile->objfile;
10477
10478 if (name == NULL)
10479 name = dwarf2_name (die, cu);
10480
10481 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10482 but otherwise compute it by typename_concat inside GDB.
10483 FIXME: Actually this is not really true, or at least not always true.
10484 It's all very confusing. compute_and_set_names doesn't try to demangle
10485 Fortran names because there is no mangling standard. So new_symbol
10486 will set the demangled name to the result of dwarf2_full_name, and it is
10487 the demangled name that GDB uses if it exists. */
10488 if (cu->language == language_ada
10489 || (cu->language == language_fortran && physname))
10490 {
10491 /* For Ada unit, we prefer the linkage name over the name, as
10492 the former contains the exported name, which the user expects
10493 to be able to reference. Ideally, we want the user to be able
10494 to reference this entity using either natural or linkage name,
10495 but we haven't started looking at this enhancement yet. */
10496 const char *linkage_name = dw2_linkage_name (die, cu);
10497
10498 if (linkage_name != NULL)
10499 return linkage_name;
10500 }
10501
10502 /* These are the only languages we know how to qualify names in. */
10503 if (name != NULL
10504 && (cu->language == language_cplus
10505 || cu->language == language_fortran || cu->language == language_d
10506 || cu->language == language_rust))
10507 {
10508 if (die_needs_namespace (die, cu))
10509 {
10510 const char *prefix;
10511 const char *canonical_name = NULL;
10512
10513 string_file buf;
10514
10515 prefix = determine_prefix (die, cu);
10516 if (*prefix != '\0')
10517 {
10518 gdb::unique_xmalloc_ptr<char> prefixed_name
10519 (typename_concat (NULL, prefix, name, physname, cu));
10520
10521 buf.puts (prefixed_name.get ());
10522 }
10523 else
10524 buf.puts (name);
10525
10526 /* Template parameters may be specified in the DIE's DW_AT_name, or
10527 as children with DW_TAG_template_type_param or
10528 DW_TAG_value_type_param. If the latter, add them to the name
10529 here. If the name already has template parameters, then
10530 skip this step; some versions of GCC emit both, and
10531 it is more efficient to use the pre-computed name.
10532
10533 Something to keep in mind about this process: it is very
10534 unlikely, or in some cases downright impossible, to produce
10535 something that will match the mangled name of a function.
10536 If the definition of the function has the same debug info,
10537 we should be able to match up with it anyway. But fallbacks
10538 using the minimal symbol, for instance to find a method
10539 implemented in a stripped copy of libstdc++, will not work.
10540 If we do not have debug info for the definition, we will have to
10541 match them up some other way.
10542
10543 When we do name matching there is a related problem with function
10544 templates; two instantiated function templates are allowed to
10545 differ only by their return types, which we do not add here. */
10546
10547 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10548 {
10549 struct attribute *attr;
10550 struct die_info *child;
10551 int first = 1;
10552 const language_defn *cplus_lang = language_def (cu->language);
10553
10554 die->building_fullname = 1;
10555
10556 for (child = die->child; child != NULL; child = child->sibling)
10557 {
10558 struct type *type;
10559 LONGEST value;
10560 const gdb_byte *bytes;
10561 struct dwarf2_locexpr_baton *baton;
10562 struct value *v;
10563
10564 if (child->tag != DW_TAG_template_type_param
10565 && child->tag != DW_TAG_template_value_param)
10566 continue;
10567
10568 if (first)
10569 {
10570 buf.puts ("<");
10571 first = 0;
10572 }
10573 else
10574 buf.puts (", ");
10575
10576 attr = dwarf2_attr (child, DW_AT_type, cu);
10577 if (attr == NULL)
10578 {
10579 complaint (_("template parameter missing DW_AT_type"));
10580 buf.puts ("UNKNOWN_TYPE");
10581 continue;
10582 }
10583 type = die_type (child, cu);
10584
10585 if (child->tag == DW_TAG_template_type_param)
10586 {
10587 cplus_lang->print_type (type, "", &buf, -1, 0,
10588 &type_print_raw_options);
10589 continue;
10590 }
10591
10592 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10593 if (attr == NULL)
10594 {
10595 complaint (_("template parameter missing "
10596 "DW_AT_const_value"));
10597 buf.puts ("UNKNOWN_VALUE");
10598 continue;
10599 }
10600
10601 dwarf2_const_value_attr (attr, type, name,
10602 &cu->comp_unit_obstack, cu,
10603 &value, &bytes, &baton);
10604
10605 if (type->has_no_signedness ())
10606 /* GDB prints characters as NUMBER 'CHAR'. If that's
10607 changed, this can use value_print instead. */
10608 cplus_lang->printchar (value, type, &buf);
10609 else
10610 {
10611 struct value_print_options opts;
10612
10613 if (baton != NULL)
10614 v = dwarf2_evaluate_loc_desc (type, NULL,
10615 baton->data,
10616 baton->size,
10617 baton->per_cu,
10618 baton->per_objfile);
10619 else if (bytes != NULL)
10620 {
10621 v = allocate_value (type);
10622 memcpy (value_contents_writeable (v), bytes,
10623 TYPE_LENGTH (type));
10624 }
10625 else
10626 v = value_from_longest (type, value);
10627
10628 /* Specify decimal so that we do not depend on
10629 the radix. */
10630 get_formatted_print_options (&opts, 'd');
10631 opts.raw = 1;
10632 value_print (v, &buf, &opts);
10633 release_value (v);
10634 }
10635 }
10636
10637 die->building_fullname = 0;
10638
10639 if (!first)
10640 {
10641 /* Close the argument list, with a space if necessary
10642 (nested templates). */
10643 if (!buf.empty () && buf.string ().back () == '>')
10644 buf.puts (" >");
10645 else
10646 buf.puts (">");
10647 }
10648 }
10649
10650 /* For C++ methods, append formal parameter type
10651 information, if PHYSNAME. */
10652
10653 if (physname && die->tag == DW_TAG_subprogram
10654 && cu->language == language_cplus)
10655 {
10656 struct type *type = read_type_die (die, cu);
10657
10658 c_type_print_args (type, &buf, 1, cu->language,
10659 &type_print_raw_options);
10660
10661 if (cu->language == language_cplus)
10662 {
10663 /* Assume that an artificial first parameter is
10664 "this", but do not crash if it is not. RealView
10665 marks unnamed (and thus unused) parameters as
10666 artificial; there is no way to differentiate
10667 the two cases. */
10668 if (type->num_fields () > 0
10669 && TYPE_FIELD_ARTIFICIAL (type, 0)
10670 && type->field (0).type ()->code () == TYPE_CODE_PTR
10671 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10672 buf.puts (" const");
10673 }
10674 }
10675
10676 const std::string &intermediate_name = buf.string ();
10677
10678 if (cu->language == language_cplus)
10679 canonical_name
10680 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10681 objfile);
10682
10683 /* If we only computed INTERMEDIATE_NAME, or if
10684 INTERMEDIATE_NAME is already canonical, then we need to
10685 intern it. */
10686 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10687 name = objfile->intern (intermediate_name);
10688 else
10689 name = canonical_name;
10690 }
10691 }
10692
10693 return name;
10694 }
10695
10696 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10697 If scope qualifiers are appropriate they will be added. The result
10698 will be allocated on the storage_obstack, or NULL if the DIE does
10699 not have a name. NAME may either be from a previous call to
10700 dwarf2_name or NULL.
10701
10702 The output string will be canonicalized (if C++). */
10703
10704 static const char *
10705 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10706 {
10707 return dwarf2_compute_name (name, die, cu, 0);
10708 }
10709
10710 /* Construct a physname for the given DIE in CU. NAME may either be
10711 from a previous call to dwarf2_name or NULL. The result will be
10712 allocated on the objfile_objstack or NULL if the DIE does not have a
10713 name.
10714
10715 The output string will be canonicalized (if C++). */
10716
10717 static const char *
10718 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10719 {
10720 struct objfile *objfile = cu->per_objfile->objfile;
10721 const char *retval, *mangled = NULL, *canon = NULL;
10722 int need_copy = 1;
10723
10724 /* In this case dwarf2_compute_name is just a shortcut not building anything
10725 on its own. */
10726 if (!die_needs_namespace (die, cu))
10727 return dwarf2_compute_name (name, die, cu, 1);
10728
10729 if (cu->language != language_rust)
10730 mangled = dw2_linkage_name (die, cu);
10731
10732 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10733 has computed. */
10734 gdb::unique_xmalloc_ptr<char> demangled;
10735 if (mangled != NULL)
10736 {
10737
10738 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
10739 {
10740 /* Do nothing (do not demangle the symbol name). */
10741 }
10742 else
10743 {
10744 /* Use DMGL_RET_DROP for C++ template functions to suppress
10745 their return type. It is easier for GDB users to search
10746 for such functions as `name(params)' than `long name(params)'.
10747 In such case the minimal symbol names do not match the full
10748 symbol names but for template functions there is never a need
10749 to look up their definition from their declaration so
10750 the only disadvantage remains the minimal symbol variant
10751 `long name(params)' does not have the proper inferior type. */
10752 demangled.reset (gdb_demangle (mangled,
10753 (DMGL_PARAMS | DMGL_ANSI
10754 | DMGL_RET_DROP)));
10755 }
10756 if (demangled)
10757 canon = demangled.get ();
10758 else
10759 {
10760 canon = mangled;
10761 need_copy = 0;
10762 }
10763 }
10764
10765 if (canon == NULL || check_physname)
10766 {
10767 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10768
10769 if (canon != NULL && strcmp (physname, canon) != 0)
10770 {
10771 /* It may not mean a bug in GDB. The compiler could also
10772 compute DW_AT_linkage_name incorrectly. But in such case
10773 GDB would need to be bug-to-bug compatible. */
10774
10775 complaint (_("Computed physname <%s> does not match demangled <%s> "
10776 "(from linkage <%s>) - DIE at %s [in module %s]"),
10777 physname, canon, mangled, sect_offset_str (die->sect_off),
10778 objfile_name (objfile));
10779
10780 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10781 is available here - over computed PHYSNAME. It is safer
10782 against both buggy GDB and buggy compilers. */
10783
10784 retval = canon;
10785 }
10786 else
10787 {
10788 retval = physname;
10789 need_copy = 0;
10790 }
10791 }
10792 else
10793 retval = canon;
10794
10795 if (need_copy)
10796 retval = objfile->intern (retval);
10797
10798 return retval;
10799 }
10800
10801 /* Inspect DIE in CU for a namespace alias. If one exists, record
10802 a new symbol for it.
10803
10804 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10805
10806 static int
10807 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10808 {
10809 struct attribute *attr;
10810
10811 /* If the die does not have a name, this is not a namespace
10812 alias. */
10813 attr = dwarf2_attr (die, DW_AT_name, cu);
10814 if (attr != NULL)
10815 {
10816 int num;
10817 struct die_info *d = die;
10818 struct dwarf2_cu *imported_cu = cu;
10819
10820 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10821 keep inspecting DIEs until we hit the underlying import. */
10822 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10823 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10824 {
10825 attr = dwarf2_attr (d, DW_AT_import, cu);
10826 if (attr == NULL)
10827 break;
10828
10829 d = follow_die_ref (d, attr, &imported_cu);
10830 if (d->tag != DW_TAG_imported_declaration)
10831 break;
10832 }
10833
10834 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10835 {
10836 complaint (_("DIE at %s has too many recursively imported "
10837 "declarations"), sect_offset_str (d->sect_off));
10838 return 0;
10839 }
10840
10841 if (attr != NULL)
10842 {
10843 struct type *type;
10844 sect_offset sect_off = attr->get_ref_die_offset ();
10845
10846 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10847 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10848 {
10849 /* This declaration is a global namespace alias. Add
10850 a symbol for it whose type is the aliased namespace. */
10851 new_symbol (die, type, cu);
10852 return 1;
10853 }
10854 }
10855 }
10856
10857 return 0;
10858 }
10859
10860 /* Return the using directives repository (global or local?) to use in the
10861 current context for CU.
10862
10863 For Ada, imported declarations can materialize renamings, which *may* be
10864 global. However it is impossible (for now?) in DWARF to distinguish
10865 "external" imported declarations and "static" ones. As all imported
10866 declarations seem to be static in all other languages, make them all CU-wide
10867 global only in Ada. */
10868
10869 static struct using_direct **
10870 using_directives (struct dwarf2_cu *cu)
10871 {
10872 if (cu->language == language_ada
10873 && cu->get_builder ()->outermost_context_p ())
10874 return cu->get_builder ()->get_global_using_directives ();
10875 else
10876 return cu->get_builder ()->get_local_using_directives ();
10877 }
10878
10879 /* Read the import statement specified by the given die and record it. */
10880
10881 static void
10882 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10883 {
10884 struct objfile *objfile = cu->per_objfile->objfile;
10885 struct attribute *import_attr;
10886 struct die_info *imported_die, *child_die;
10887 struct dwarf2_cu *imported_cu;
10888 const char *imported_name;
10889 const char *imported_name_prefix;
10890 const char *canonical_name;
10891 const char *import_alias;
10892 const char *imported_declaration = NULL;
10893 const char *import_prefix;
10894 std::vector<const char *> excludes;
10895
10896 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10897 if (import_attr == NULL)
10898 {
10899 complaint (_("Tag '%s' has no DW_AT_import"),
10900 dwarf_tag_name (die->tag));
10901 return;
10902 }
10903
10904 imported_cu = cu;
10905 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10906 imported_name = dwarf2_name (imported_die, imported_cu);
10907 if (imported_name == NULL)
10908 {
10909 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10910
10911 The import in the following code:
10912 namespace A
10913 {
10914 typedef int B;
10915 }
10916
10917 int main ()
10918 {
10919 using A::B;
10920 B b;
10921 return b;
10922 }
10923
10924 ...
10925 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10926 <52> DW_AT_decl_file : 1
10927 <53> DW_AT_decl_line : 6
10928 <54> DW_AT_import : <0x75>
10929 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10930 <59> DW_AT_name : B
10931 <5b> DW_AT_decl_file : 1
10932 <5c> DW_AT_decl_line : 2
10933 <5d> DW_AT_type : <0x6e>
10934 ...
10935 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10936 <76> DW_AT_byte_size : 4
10937 <77> DW_AT_encoding : 5 (signed)
10938
10939 imports the wrong die ( 0x75 instead of 0x58 ).
10940 This case will be ignored until the gcc bug is fixed. */
10941 return;
10942 }
10943
10944 /* Figure out the local name after import. */
10945 import_alias = dwarf2_name (die, cu);
10946
10947 /* Figure out where the statement is being imported to. */
10948 import_prefix = determine_prefix (die, cu);
10949
10950 /* Figure out what the scope of the imported die is and prepend it
10951 to the name of the imported die. */
10952 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10953
10954 if (imported_die->tag != DW_TAG_namespace
10955 && imported_die->tag != DW_TAG_module)
10956 {
10957 imported_declaration = imported_name;
10958 canonical_name = imported_name_prefix;
10959 }
10960 else if (strlen (imported_name_prefix) > 0)
10961 canonical_name = obconcat (&objfile->objfile_obstack,
10962 imported_name_prefix,
10963 (cu->language == language_d ? "." : "::"),
10964 imported_name, (char *) NULL);
10965 else
10966 canonical_name = imported_name;
10967
10968 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10969 for (child_die = die->child; child_die && child_die->tag;
10970 child_die = child_die->sibling)
10971 {
10972 /* DWARF-4: A Fortran use statement with a “rename list” may be
10973 represented by an imported module entry with an import attribute
10974 referring to the module and owned entries corresponding to those
10975 entities that are renamed as part of being imported. */
10976
10977 if (child_die->tag != DW_TAG_imported_declaration)
10978 {
10979 complaint (_("child DW_TAG_imported_declaration expected "
10980 "- DIE at %s [in module %s]"),
10981 sect_offset_str (child_die->sect_off),
10982 objfile_name (objfile));
10983 continue;
10984 }
10985
10986 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10987 if (import_attr == NULL)
10988 {
10989 complaint (_("Tag '%s' has no DW_AT_import"),
10990 dwarf_tag_name (child_die->tag));
10991 continue;
10992 }
10993
10994 imported_cu = cu;
10995 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10996 &imported_cu);
10997 imported_name = dwarf2_name (imported_die, imported_cu);
10998 if (imported_name == NULL)
10999 {
11000 complaint (_("child DW_TAG_imported_declaration has unknown "
11001 "imported name - DIE at %s [in module %s]"),
11002 sect_offset_str (child_die->sect_off),
11003 objfile_name (objfile));
11004 continue;
11005 }
11006
11007 excludes.push_back (imported_name);
11008
11009 process_die (child_die, cu);
11010 }
11011
11012 add_using_directive (using_directives (cu),
11013 import_prefix,
11014 canonical_name,
11015 import_alias,
11016 imported_declaration,
11017 excludes,
11018 0,
11019 &objfile->objfile_obstack);
11020 }
11021
11022 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11023 types, but gives them a size of zero. Starting with version 14,
11024 ICC is compatible with GCC. */
11025
11026 static bool
11027 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11028 {
11029 if (!cu->checked_producer)
11030 check_producer (cu);
11031
11032 return cu->producer_is_icc_lt_14;
11033 }
11034
11035 /* ICC generates a DW_AT_type for C void functions. This was observed on
11036 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11037 which says that void functions should not have a DW_AT_type. */
11038
11039 static bool
11040 producer_is_icc (struct dwarf2_cu *cu)
11041 {
11042 if (!cu->checked_producer)
11043 check_producer (cu);
11044
11045 return cu->producer_is_icc;
11046 }
11047
11048 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11049 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11050 this, it was first present in GCC release 4.3.0. */
11051
11052 static bool
11053 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11054 {
11055 if (!cu->checked_producer)
11056 check_producer (cu);
11057
11058 return cu->producer_is_gcc_lt_4_3;
11059 }
11060
11061 static file_and_directory
11062 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11063 {
11064 file_and_directory res;
11065
11066 /* Find the filename. Do not use dwarf2_name here, since the filename
11067 is not a source language identifier. */
11068 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11069 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11070
11071 if (res.comp_dir == NULL
11072 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11073 && IS_ABSOLUTE_PATH (res.name))
11074 {
11075 res.comp_dir_storage = ldirname (res.name);
11076 if (!res.comp_dir_storage.empty ())
11077 res.comp_dir = res.comp_dir_storage.c_str ();
11078 }
11079 if (res.comp_dir != NULL)
11080 {
11081 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11082 directory, get rid of it. */
11083 const char *cp = strchr (res.comp_dir, ':');
11084
11085 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11086 res.comp_dir = cp + 1;
11087 }
11088
11089 if (res.name == NULL)
11090 res.name = "<unknown>";
11091
11092 return res;
11093 }
11094
11095 /* Handle DW_AT_stmt_list for a compilation unit.
11096 DIE is the DW_TAG_compile_unit die for CU.
11097 COMP_DIR is the compilation directory. LOWPC is passed to
11098 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11099
11100 static void
11101 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11102 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11103 {
11104 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11105 struct attribute *attr;
11106 struct line_header line_header_local;
11107 hashval_t line_header_local_hash;
11108 void **slot;
11109 int decode_mapping;
11110
11111 gdb_assert (! cu->per_cu->is_debug_types);
11112
11113 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11114 if (attr == NULL || !attr->form_is_unsigned ())
11115 return;
11116
11117 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11118
11119 /* The line header hash table is only created if needed (it exists to
11120 prevent redundant reading of the line table for partial_units).
11121 If we're given a partial_unit, we'll need it. If we're given a
11122 compile_unit, then use the line header hash table if it's already
11123 created, but don't create one just yet. */
11124
11125 if (per_objfile->line_header_hash == NULL
11126 && die->tag == DW_TAG_partial_unit)
11127 {
11128 per_objfile->line_header_hash
11129 .reset (htab_create_alloc (127, line_header_hash_voidp,
11130 line_header_eq_voidp,
11131 free_line_header_voidp,
11132 xcalloc, xfree));
11133 }
11134
11135 line_header_local.sect_off = line_offset;
11136 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11137 line_header_local_hash = line_header_hash (&line_header_local);
11138 if (per_objfile->line_header_hash != NULL)
11139 {
11140 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11141 &line_header_local,
11142 line_header_local_hash, NO_INSERT);
11143
11144 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11145 is not present in *SLOT (since if there is something in *SLOT then
11146 it will be for a partial_unit). */
11147 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11148 {
11149 gdb_assert (*slot != NULL);
11150 cu->line_header = (struct line_header *) *slot;
11151 return;
11152 }
11153 }
11154
11155 /* dwarf_decode_line_header does not yet provide sufficient information.
11156 We always have to call also dwarf_decode_lines for it. */
11157 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11158 if (lh == NULL)
11159 return;
11160
11161 cu->line_header = lh.release ();
11162 cu->line_header_die_owner = die;
11163
11164 if (per_objfile->line_header_hash == NULL)
11165 slot = NULL;
11166 else
11167 {
11168 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11169 &line_header_local,
11170 line_header_local_hash, INSERT);
11171 gdb_assert (slot != NULL);
11172 }
11173 if (slot != NULL && *slot == NULL)
11174 {
11175 /* This newly decoded line number information unit will be owned
11176 by line_header_hash hash table. */
11177 *slot = cu->line_header;
11178 cu->line_header_die_owner = NULL;
11179 }
11180 else
11181 {
11182 /* We cannot free any current entry in (*slot) as that struct line_header
11183 may be already used by multiple CUs. Create only temporary decoded
11184 line_header for this CU - it may happen at most once for each line
11185 number information unit. And if we're not using line_header_hash
11186 then this is what we want as well. */
11187 gdb_assert (die->tag != DW_TAG_partial_unit);
11188 }
11189 decode_mapping = (die->tag != DW_TAG_partial_unit);
11190 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11191 decode_mapping);
11192
11193 }
11194
11195 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11196
11197 static void
11198 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11199 {
11200 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11201 struct objfile *objfile = per_objfile->objfile;
11202 struct gdbarch *gdbarch = objfile->arch ();
11203 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11204 CORE_ADDR highpc = ((CORE_ADDR) 0);
11205 struct attribute *attr;
11206 struct die_info *child_die;
11207 CORE_ADDR baseaddr;
11208
11209 prepare_one_comp_unit (cu, die, cu->language);
11210 baseaddr = objfile->text_section_offset ();
11211
11212 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11213
11214 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11215 from finish_block. */
11216 if (lowpc == ((CORE_ADDR) -1))
11217 lowpc = highpc;
11218 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11219
11220 file_and_directory fnd = find_file_and_directory (die, cu);
11221
11222 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11223 standardised yet. As a workaround for the language detection we fall
11224 back to the DW_AT_producer string. */
11225 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11226 cu->language = language_opencl;
11227
11228 /* Similar hack for Go. */
11229 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11230 set_cu_language (DW_LANG_Go, cu);
11231
11232 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11233
11234 /* Decode line number information if present. We do this before
11235 processing child DIEs, so that the line header table is available
11236 for DW_AT_decl_file. */
11237 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11238
11239 /* Process all dies in compilation unit. */
11240 if (die->child != NULL)
11241 {
11242 child_die = die->child;
11243 while (child_die && child_die->tag)
11244 {
11245 process_die (child_die, cu);
11246 child_die = child_die->sibling;
11247 }
11248 }
11249
11250 /* Decode macro information, if present. Dwarf 2 macro information
11251 refers to information in the line number info statement program
11252 header, so we can only read it if we've read the header
11253 successfully. */
11254 attr = dwarf2_attr (die, DW_AT_macros, cu);
11255 if (attr == NULL)
11256 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11257 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11258 {
11259 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11260 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11261
11262 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
11263 }
11264 else
11265 {
11266 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11267 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11268 {
11269 unsigned int macro_offset = attr->as_unsigned ();
11270
11271 dwarf_decode_macros (cu, macro_offset, 0);
11272 }
11273 }
11274 }
11275
11276 void
11277 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11278 {
11279 struct type_unit_group *tu_group;
11280 int first_time;
11281 struct attribute *attr;
11282 unsigned int i;
11283 struct signatured_type *sig_type;
11284
11285 gdb_assert (per_cu->is_debug_types);
11286 sig_type = (struct signatured_type *) per_cu;
11287
11288 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11289
11290 /* If we're using .gdb_index (includes -readnow) then
11291 per_cu->type_unit_group may not have been set up yet. */
11292 if (sig_type->type_unit_group == NULL)
11293 sig_type->type_unit_group = get_type_unit_group (this, attr);
11294 tu_group = sig_type->type_unit_group;
11295
11296 /* If we've already processed this stmt_list there's no real need to
11297 do it again, we could fake it and just recreate the part we need
11298 (file name,index -> symtab mapping). If data shows this optimization
11299 is useful we can do it then. */
11300 type_unit_group_unshareable *tug_unshare
11301 = per_objfile->get_type_unit_group_unshareable (tu_group);
11302 first_time = tug_unshare->compunit_symtab == NULL;
11303
11304 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11305 debug info. */
11306 line_header_up lh;
11307 if (attr != NULL && attr->form_is_unsigned ())
11308 {
11309 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11310 lh = dwarf_decode_line_header (line_offset, this);
11311 }
11312 if (lh == NULL)
11313 {
11314 if (first_time)
11315 start_symtab ("", NULL, 0);
11316 else
11317 {
11318 gdb_assert (tug_unshare->symtabs == NULL);
11319 gdb_assert (m_builder == nullptr);
11320 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11321 m_builder.reset (new struct buildsym_compunit
11322 (COMPUNIT_OBJFILE (cust), "",
11323 COMPUNIT_DIRNAME (cust),
11324 compunit_language (cust),
11325 0, cust));
11326 list_in_scope = get_builder ()->get_file_symbols ();
11327 }
11328 return;
11329 }
11330
11331 line_header = lh.release ();
11332 line_header_die_owner = die;
11333
11334 if (first_time)
11335 {
11336 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11337
11338 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11339 still initializing it, and our caller (a few levels up)
11340 process_full_type_unit still needs to know if this is the first
11341 time. */
11342
11343 tug_unshare->symtabs
11344 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11345 struct symtab *, line_header->file_names_size ());
11346
11347 auto &file_names = line_header->file_names ();
11348 for (i = 0; i < file_names.size (); ++i)
11349 {
11350 file_entry &fe = file_names[i];
11351 dwarf2_start_subfile (this, fe.name,
11352 fe.include_dir (line_header));
11353 buildsym_compunit *b = get_builder ();
11354 if (b->get_current_subfile ()->symtab == NULL)
11355 {
11356 /* NOTE: start_subfile will recognize when it's been
11357 passed a file it has already seen. So we can't
11358 assume there's a simple mapping from
11359 cu->line_header->file_names to subfiles, plus
11360 cu->line_header->file_names may contain dups. */
11361 b->get_current_subfile ()->symtab
11362 = allocate_symtab (cust, b->get_current_subfile ()->name);
11363 }
11364
11365 fe.symtab = b->get_current_subfile ()->symtab;
11366 tug_unshare->symtabs[i] = fe.symtab;
11367 }
11368 }
11369 else
11370 {
11371 gdb_assert (m_builder == nullptr);
11372 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11373 m_builder.reset (new struct buildsym_compunit
11374 (COMPUNIT_OBJFILE (cust), "",
11375 COMPUNIT_DIRNAME (cust),
11376 compunit_language (cust),
11377 0, cust));
11378 list_in_scope = get_builder ()->get_file_symbols ();
11379
11380 auto &file_names = line_header->file_names ();
11381 for (i = 0; i < file_names.size (); ++i)
11382 {
11383 file_entry &fe = file_names[i];
11384 fe.symtab = tug_unshare->symtabs[i];
11385 }
11386 }
11387
11388 /* The main symtab is allocated last. Type units don't have DW_AT_name
11389 so they don't have a "real" (so to speak) symtab anyway.
11390 There is later code that will assign the main symtab to all symbols
11391 that don't have one. We need to handle the case of a symbol with a
11392 missing symtab (DW_AT_decl_file) anyway. */
11393 }
11394
11395 /* Process DW_TAG_type_unit.
11396 For TUs we want to skip the first top level sibling if it's not the
11397 actual type being defined by this TU. In this case the first top
11398 level sibling is there to provide context only. */
11399
11400 static void
11401 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11402 {
11403 struct die_info *child_die;
11404
11405 prepare_one_comp_unit (cu, die, language_minimal);
11406
11407 /* Initialize (or reinitialize) the machinery for building symtabs.
11408 We do this before processing child DIEs, so that the line header table
11409 is available for DW_AT_decl_file. */
11410 cu->setup_type_unit_groups (die);
11411
11412 if (die->child != NULL)
11413 {
11414 child_die = die->child;
11415 while (child_die && child_die->tag)
11416 {
11417 process_die (child_die, cu);
11418 child_die = child_die->sibling;
11419 }
11420 }
11421 }
11422 \f
11423 /* DWO/DWP files.
11424
11425 http://gcc.gnu.org/wiki/DebugFission
11426 http://gcc.gnu.org/wiki/DebugFissionDWP
11427
11428 To simplify handling of both DWO files ("object" files with the DWARF info)
11429 and DWP files (a file with the DWOs packaged up into one file), we treat
11430 DWP files as having a collection of virtual DWO files. */
11431
11432 static hashval_t
11433 hash_dwo_file (const void *item)
11434 {
11435 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11436 hashval_t hash;
11437
11438 hash = htab_hash_string (dwo_file->dwo_name);
11439 if (dwo_file->comp_dir != NULL)
11440 hash += htab_hash_string (dwo_file->comp_dir);
11441 return hash;
11442 }
11443
11444 static int
11445 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11446 {
11447 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11448 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11449
11450 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11451 return 0;
11452 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11453 return lhs->comp_dir == rhs->comp_dir;
11454 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11455 }
11456
11457 /* Allocate a hash table for DWO files. */
11458
11459 static htab_up
11460 allocate_dwo_file_hash_table ()
11461 {
11462 auto delete_dwo_file = [] (void *item)
11463 {
11464 struct dwo_file *dwo_file = (struct dwo_file *) item;
11465
11466 delete dwo_file;
11467 };
11468
11469 return htab_up (htab_create_alloc (41,
11470 hash_dwo_file,
11471 eq_dwo_file,
11472 delete_dwo_file,
11473 xcalloc, xfree));
11474 }
11475
11476 /* Lookup DWO file DWO_NAME. */
11477
11478 static void **
11479 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
11480 const char *dwo_name,
11481 const char *comp_dir)
11482 {
11483 struct dwo_file find_entry;
11484 void **slot;
11485
11486 if (per_objfile->per_bfd->dwo_files == NULL)
11487 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11488
11489 find_entry.dwo_name = dwo_name;
11490 find_entry.comp_dir = comp_dir;
11491 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
11492 INSERT);
11493
11494 return slot;
11495 }
11496
11497 static hashval_t
11498 hash_dwo_unit (const void *item)
11499 {
11500 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11501
11502 /* This drops the top 32 bits of the id, but is ok for a hash. */
11503 return dwo_unit->signature;
11504 }
11505
11506 static int
11507 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11508 {
11509 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11510 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11511
11512 /* The signature is assumed to be unique within the DWO file.
11513 So while object file CU dwo_id's always have the value zero,
11514 that's OK, assuming each object file DWO file has only one CU,
11515 and that's the rule for now. */
11516 return lhs->signature == rhs->signature;
11517 }
11518
11519 /* Allocate a hash table for DWO CUs,TUs.
11520 There is one of these tables for each of CUs,TUs for each DWO file. */
11521
11522 static htab_up
11523 allocate_dwo_unit_table ()
11524 {
11525 /* Start out with a pretty small number.
11526 Generally DWO files contain only one CU and maybe some TUs. */
11527 return htab_up (htab_create_alloc (3,
11528 hash_dwo_unit,
11529 eq_dwo_unit,
11530 NULL, xcalloc, xfree));
11531 }
11532
11533 /* die_reader_func for create_dwo_cu. */
11534
11535 static void
11536 create_dwo_cu_reader (const struct die_reader_specs *reader,
11537 const gdb_byte *info_ptr,
11538 struct die_info *comp_unit_die,
11539 struct dwo_file *dwo_file,
11540 struct dwo_unit *dwo_unit)
11541 {
11542 struct dwarf2_cu *cu = reader->cu;
11543 sect_offset sect_off = cu->per_cu->sect_off;
11544 struct dwarf2_section_info *section = cu->per_cu->section;
11545
11546 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11547 if (!signature.has_value ())
11548 {
11549 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11550 " its dwo_id [in module %s]"),
11551 sect_offset_str (sect_off), dwo_file->dwo_name);
11552 return;
11553 }
11554
11555 dwo_unit->dwo_file = dwo_file;
11556 dwo_unit->signature = *signature;
11557 dwo_unit->section = section;
11558 dwo_unit->sect_off = sect_off;
11559 dwo_unit->length = cu->per_cu->length;
11560
11561 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11562 sect_offset_str (sect_off),
11563 hex_string (dwo_unit->signature));
11564 }
11565
11566 /* Create the dwo_units for the CUs in a DWO_FILE.
11567 Note: This function processes DWO files only, not DWP files. */
11568
11569 static void
11570 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
11571 dwarf2_cu *cu, struct dwo_file &dwo_file,
11572 dwarf2_section_info &section, htab_up &cus_htab)
11573 {
11574 struct objfile *objfile = per_objfile->objfile;
11575 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
11576 const gdb_byte *info_ptr, *end_ptr;
11577
11578 section.read (objfile);
11579 info_ptr = section.buffer;
11580
11581 if (info_ptr == NULL)
11582 return;
11583
11584 dwarf_read_debug_printf ("Reading %s for %s:",
11585 section.get_name (),
11586 section.get_file_name ());
11587
11588 end_ptr = info_ptr + section.size;
11589 while (info_ptr < end_ptr)
11590 {
11591 struct dwarf2_per_cu_data per_cu;
11592 struct dwo_unit read_unit {};
11593 struct dwo_unit *dwo_unit;
11594 void **slot;
11595 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11596
11597 memset (&per_cu, 0, sizeof (per_cu));
11598 per_cu.per_bfd = per_bfd;
11599 per_cu.is_debug_types = 0;
11600 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11601 per_cu.section = &section;
11602
11603 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11604 if (!reader.dummy_p)
11605 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11606 &dwo_file, &read_unit);
11607 info_ptr += per_cu.length;
11608
11609 // If the unit could not be parsed, skip it.
11610 if (read_unit.dwo_file == NULL)
11611 continue;
11612
11613 if (cus_htab == NULL)
11614 cus_htab = allocate_dwo_unit_table ();
11615
11616 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11617 struct dwo_unit);
11618 *dwo_unit = read_unit;
11619 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11620 gdb_assert (slot != NULL);
11621 if (*slot != NULL)
11622 {
11623 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11624 sect_offset dup_sect_off = dup_cu->sect_off;
11625
11626 complaint (_("debug cu entry at offset %s is duplicate to"
11627 " the entry at offset %s, signature %s"),
11628 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11629 hex_string (dwo_unit->signature));
11630 }
11631 *slot = (void *)dwo_unit;
11632 }
11633 }
11634
11635 /* DWP file .debug_{cu,tu}_index section format:
11636 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11637 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11638
11639 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11640 officially standard DWP format was published with DWARF v5 and is called
11641 Version 5. There are no versions 3 or 4.
11642
11643 DWP Version 1:
11644
11645 Both index sections have the same format, and serve to map a 64-bit
11646 signature to a set of section numbers. Each section begins with a header,
11647 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11648 indexes, and a pool of 32-bit section numbers. The index sections will be
11649 aligned at 8-byte boundaries in the file.
11650
11651 The index section header consists of:
11652
11653 V, 32 bit version number
11654 -, 32 bits unused
11655 N, 32 bit number of compilation units or type units in the index
11656 M, 32 bit number of slots in the hash table
11657
11658 Numbers are recorded using the byte order of the application binary.
11659
11660 The hash table begins at offset 16 in the section, and consists of an array
11661 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11662 order of the application binary). Unused slots in the hash table are 0.
11663 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11664
11665 The parallel table begins immediately after the hash table
11666 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11667 array of 32-bit indexes (using the byte order of the application binary),
11668 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11669 table contains a 32-bit index into the pool of section numbers. For unused
11670 hash table slots, the corresponding entry in the parallel table will be 0.
11671
11672 The pool of section numbers begins immediately following the hash table
11673 (at offset 16 + 12 * M from the beginning of the section). The pool of
11674 section numbers consists of an array of 32-bit words (using the byte order
11675 of the application binary). Each item in the array is indexed starting
11676 from 0. The hash table entry provides the index of the first section
11677 number in the set. Additional section numbers in the set follow, and the
11678 set is terminated by a 0 entry (section number 0 is not used in ELF).
11679
11680 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11681 section must be the first entry in the set, and the .debug_abbrev.dwo must
11682 be the second entry. Other members of the set may follow in any order.
11683
11684 ---
11685
11686 DWP Versions 2 and 5:
11687
11688 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11689 and the entries in the index tables are now offsets into these sections.
11690 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11691 section.
11692
11693 Index Section Contents:
11694 Header
11695 Hash Table of Signatures dwp_hash_table.hash_table
11696 Parallel Table of Indices dwp_hash_table.unit_table
11697 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11698 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11699
11700 The index section header consists of:
11701
11702 V, 32 bit version number
11703 L, 32 bit number of columns in the table of section offsets
11704 N, 32 bit number of compilation units or type units in the index
11705 M, 32 bit number of slots in the hash table
11706
11707 Numbers are recorded using the byte order of the application binary.
11708
11709 The hash table has the same format as version 1.
11710 The parallel table of indices has the same format as version 1,
11711 except that the entries are origin-1 indices into the table of sections
11712 offsets and the table of section sizes.
11713
11714 The table of offsets begins immediately following the parallel table
11715 (at offset 16 + 12 * M from the beginning of the section). The table is
11716 a two-dimensional array of 32-bit words (using the byte order of the
11717 application binary), with L columns and N+1 rows, in row-major order.
11718 Each row in the array is indexed starting from 0. The first row provides
11719 a key to the remaining rows: each column in this row provides an identifier
11720 for a debug section, and the offsets in the same column of subsequent rows
11721 refer to that section. The section identifiers for Version 2 are:
11722
11723 DW_SECT_INFO 1 .debug_info.dwo
11724 DW_SECT_TYPES 2 .debug_types.dwo
11725 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11726 DW_SECT_LINE 4 .debug_line.dwo
11727 DW_SECT_LOC 5 .debug_loc.dwo
11728 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11729 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11730 DW_SECT_MACRO 8 .debug_macro.dwo
11731
11732 The section identifiers for Version 5 are:
11733
11734 DW_SECT_INFO_V5 1 .debug_info.dwo
11735 DW_SECT_RESERVED_V5 2 --
11736 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11737 DW_SECT_LINE_V5 4 .debug_line.dwo
11738 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11739 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11740 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11741 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11742
11743 The offsets provided by the CU and TU index sections are the base offsets
11744 for the contributions made by each CU or TU to the corresponding section
11745 in the package file. Each CU and TU header contains an abbrev_offset
11746 field, used to find the abbreviations table for that CU or TU within the
11747 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11748 be interpreted as relative to the base offset given in the index section.
11749 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11750 should be interpreted as relative to the base offset for .debug_line.dwo,
11751 and offsets into other debug sections obtained from DWARF attributes should
11752 also be interpreted as relative to the corresponding base offset.
11753
11754 The table of sizes begins immediately following the table of offsets.
11755 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11756 with L columns and N rows, in row-major order. Each row in the array is
11757 indexed starting from 1 (row 0 is shared by the two tables).
11758
11759 ---
11760
11761 Hash table lookup is handled the same in version 1 and 2:
11762
11763 We assume that N and M will not exceed 2^32 - 1.
11764 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11765
11766 Given a 64-bit compilation unit signature or a type signature S, an entry
11767 in the hash table is located as follows:
11768
11769 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11770 the low-order k bits all set to 1.
11771
11772 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11773
11774 3) If the hash table entry at index H matches the signature, use that
11775 entry. If the hash table entry at index H is unused (all zeroes),
11776 terminate the search: the signature is not present in the table.
11777
11778 4) Let H = (H + H') modulo M. Repeat at Step 3.
11779
11780 Because M > N and H' and M are relatively prime, the search is guaranteed
11781 to stop at an unused slot or find the match. */
11782
11783 /* Create a hash table to map DWO IDs to their CU/TU entry in
11784 .debug_{info,types}.dwo in DWP_FILE.
11785 Returns NULL if there isn't one.
11786 Note: This function processes DWP files only, not DWO files. */
11787
11788 static struct dwp_hash_table *
11789 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11790 struct dwp_file *dwp_file, int is_debug_types)
11791 {
11792 struct objfile *objfile = per_objfile->objfile;
11793 bfd *dbfd = dwp_file->dbfd.get ();
11794 const gdb_byte *index_ptr, *index_end;
11795 struct dwarf2_section_info *index;
11796 uint32_t version, nr_columns, nr_units, nr_slots;
11797 struct dwp_hash_table *htab;
11798
11799 if (is_debug_types)
11800 index = &dwp_file->sections.tu_index;
11801 else
11802 index = &dwp_file->sections.cu_index;
11803
11804 if (index->empty ())
11805 return NULL;
11806 index->read (objfile);
11807
11808 index_ptr = index->buffer;
11809 index_end = index_ptr + index->size;
11810
11811 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11812 For now it's safe to just read 4 bytes (particularly as it's difficult to
11813 tell if you're dealing with Version 5 before you've read the version). */
11814 version = read_4_bytes (dbfd, index_ptr);
11815 index_ptr += 4;
11816 if (version == 2 || version == 5)
11817 nr_columns = read_4_bytes (dbfd, index_ptr);
11818 else
11819 nr_columns = 0;
11820 index_ptr += 4;
11821 nr_units = read_4_bytes (dbfd, index_ptr);
11822 index_ptr += 4;
11823 nr_slots = read_4_bytes (dbfd, index_ptr);
11824 index_ptr += 4;
11825
11826 if (version != 1 && version != 2 && version != 5)
11827 {
11828 error (_("Dwarf Error: unsupported DWP file version (%s)"
11829 " [in module %s]"),
11830 pulongest (version), dwp_file->name);
11831 }
11832 if (nr_slots != (nr_slots & -nr_slots))
11833 {
11834 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11835 " is not power of 2 [in module %s]"),
11836 pulongest (nr_slots), dwp_file->name);
11837 }
11838
11839 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11840 htab->version = version;
11841 htab->nr_columns = nr_columns;
11842 htab->nr_units = nr_units;
11843 htab->nr_slots = nr_slots;
11844 htab->hash_table = index_ptr;
11845 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11846
11847 /* Exit early if the table is empty. */
11848 if (nr_slots == 0 || nr_units == 0
11849 || (version == 2 && nr_columns == 0)
11850 || (version == 5 && nr_columns == 0))
11851 {
11852 /* All must be zero. */
11853 if (nr_slots != 0 || nr_units != 0
11854 || (version == 2 && nr_columns != 0)
11855 || (version == 5 && nr_columns != 0))
11856 {
11857 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11858 " all zero [in modules %s]"),
11859 dwp_file->name);
11860 }
11861 return htab;
11862 }
11863
11864 if (version == 1)
11865 {
11866 htab->section_pool.v1.indices =
11867 htab->unit_table + sizeof (uint32_t) * nr_slots;
11868 /* It's harder to decide whether the section is too small in v1.
11869 V1 is deprecated anyway so we punt. */
11870 }
11871 else if (version == 2)
11872 {
11873 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11874 int *ids = htab->section_pool.v2.section_ids;
11875 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11876 /* Reverse map for error checking. */
11877 int ids_seen[DW_SECT_MAX + 1];
11878 int i;
11879
11880 if (nr_columns < 2)
11881 {
11882 error (_("Dwarf Error: bad DWP hash table, too few columns"
11883 " in section table [in module %s]"),
11884 dwp_file->name);
11885 }
11886 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11887 {
11888 error (_("Dwarf Error: bad DWP hash table, too many columns"
11889 " in section table [in module %s]"),
11890 dwp_file->name);
11891 }
11892 memset (ids, 255, sizeof_ids);
11893 memset (ids_seen, 255, sizeof (ids_seen));
11894 for (i = 0; i < nr_columns; ++i)
11895 {
11896 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11897
11898 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11899 {
11900 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11901 " in section table [in module %s]"),
11902 id, dwp_file->name);
11903 }
11904 if (ids_seen[id] != -1)
11905 {
11906 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11907 " id %d in section table [in module %s]"),
11908 id, dwp_file->name);
11909 }
11910 ids_seen[id] = i;
11911 ids[i] = id;
11912 }
11913 /* Must have exactly one info or types section. */
11914 if (((ids_seen[DW_SECT_INFO] != -1)
11915 + (ids_seen[DW_SECT_TYPES] != -1))
11916 != 1)
11917 {
11918 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11919 " DWO info/types section [in module %s]"),
11920 dwp_file->name);
11921 }
11922 /* Must have an abbrev section. */
11923 if (ids_seen[DW_SECT_ABBREV] == -1)
11924 {
11925 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11926 " section [in module %s]"),
11927 dwp_file->name);
11928 }
11929 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11930 htab->section_pool.v2.sizes =
11931 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11932 * nr_units * nr_columns);
11933 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11934 * nr_units * nr_columns))
11935 > index_end)
11936 {
11937 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11938 " [in module %s]"),
11939 dwp_file->name);
11940 }
11941 }
11942 else /* version == 5 */
11943 {
11944 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11945 int *ids = htab->section_pool.v5.section_ids;
11946 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11947 /* Reverse map for error checking. */
11948 int ids_seen[DW_SECT_MAX_V5 + 1];
11949
11950 if (nr_columns < 2)
11951 {
11952 error (_("Dwarf Error: bad DWP hash table, too few columns"
11953 " in section table [in module %s]"),
11954 dwp_file->name);
11955 }
11956 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11957 {
11958 error (_("Dwarf Error: bad DWP hash table, too many columns"
11959 " in section table [in module %s]"),
11960 dwp_file->name);
11961 }
11962 memset (ids, 255, sizeof_ids);
11963 memset (ids_seen, 255, sizeof (ids_seen));
11964 for (int i = 0; i < nr_columns; ++i)
11965 {
11966 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11967
11968 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11969 {
11970 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11971 " in section table [in module %s]"),
11972 id, dwp_file->name);
11973 }
11974 if (ids_seen[id] != -1)
11975 {
11976 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11977 " id %d in section table [in module %s]"),
11978 id, dwp_file->name);
11979 }
11980 ids_seen[id] = i;
11981 ids[i] = id;
11982 }
11983 /* Must have seen an info section. */
11984 if (ids_seen[DW_SECT_INFO_V5] == -1)
11985 {
11986 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11987 " DWO info/types section [in module %s]"),
11988 dwp_file->name);
11989 }
11990 /* Must have an abbrev section. */
11991 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11992 {
11993 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11994 " section [in module %s]"),
11995 dwp_file->name);
11996 }
11997 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11998 htab->section_pool.v5.sizes
11999 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
12000 * nr_units * nr_columns);
12001 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
12002 * nr_units * nr_columns))
12003 > index_end)
12004 {
12005 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12006 " [in module %s]"),
12007 dwp_file->name);
12008 }
12009 }
12010
12011 return htab;
12012 }
12013
12014 /* Update SECTIONS with the data from SECTP.
12015
12016 This function is like the other "locate" section routines, but in
12017 this context the sections to read comes from the DWP V1 hash table,
12018 not the full ELF section table.
12019
12020 The result is non-zero for success, or zero if an error was found. */
12021
12022 static int
12023 locate_v1_virtual_dwo_sections (asection *sectp,
12024 struct virtual_v1_dwo_sections *sections)
12025 {
12026 const struct dwop_section_names *names = &dwop_section_names;
12027
12028 if (section_is_p (sectp->name, &names->abbrev_dwo))
12029 {
12030 /* There can be only one. */
12031 if (sections->abbrev.s.section != NULL)
12032 return 0;
12033 sections->abbrev.s.section = sectp;
12034 sections->abbrev.size = bfd_section_size (sectp);
12035 }
12036 else if (section_is_p (sectp->name, &names->info_dwo)
12037 || section_is_p (sectp->name, &names->types_dwo))
12038 {
12039 /* There can be only one. */
12040 if (sections->info_or_types.s.section != NULL)
12041 return 0;
12042 sections->info_or_types.s.section = sectp;
12043 sections->info_or_types.size = bfd_section_size (sectp);
12044 }
12045 else if (section_is_p (sectp->name, &names->line_dwo))
12046 {
12047 /* There can be only one. */
12048 if (sections->line.s.section != NULL)
12049 return 0;
12050 sections->line.s.section = sectp;
12051 sections->line.size = bfd_section_size (sectp);
12052 }
12053 else if (section_is_p (sectp->name, &names->loc_dwo))
12054 {
12055 /* There can be only one. */
12056 if (sections->loc.s.section != NULL)
12057 return 0;
12058 sections->loc.s.section = sectp;
12059 sections->loc.size = bfd_section_size (sectp);
12060 }
12061 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12062 {
12063 /* There can be only one. */
12064 if (sections->macinfo.s.section != NULL)
12065 return 0;
12066 sections->macinfo.s.section = sectp;
12067 sections->macinfo.size = bfd_section_size (sectp);
12068 }
12069 else if (section_is_p (sectp->name, &names->macro_dwo))
12070 {
12071 /* There can be only one. */
12072 if (sections->macro.s.section != NULL)
12073 return 0;
12074 sections->macro.s.section = sectp;
12075 sections->macro.size = bfd_section_size (sectp);
12076 }
12077 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12078 {
12079 /* There can be only one. */
12080 if (sections->str_offsets.s.section != NULL)
12081 return 0;
12082 sections->str_offsets.s.section = sectp;
12083 sections->str_offsets.size = bfd_section_size (sectp);
12084 }
12085 else
12086 {
12087 /* No other kind of section is valid. */
12088 return 0;
12089 }
12090
12091 return 1;
12092 }
12093
12094 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12095 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12096 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12097 This is for DWP version 1 files. */
12098
12099 static struct dwo_unit *
12100 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
12101 struct dwp_file *dwp_file,
12102 uint32_t unit_index,
12103 const char *comp_dir,
12104 ULONGEST signature, int is_debug_types)
12105 {
12106 const struct dwp_hash_table *dwp_htab =
12107 is_debug_types ? dwp_file->tus : dwp_file->cus;
12108 bfd *dbfd = dwp_file->dbfd.get ();
12109 const char *kind = is_debug_types ? "TU" : "CU";
12110 struct dwo_file *dwo_file;
12111 struct dwo_unit *dwo_unit;
12112 struct virtual_v1_dwo_sections sections;
12113 void **dwo_file_slot;
12114 int i;
12115
12116 gdb_assert (dwp_file->version == 1);
12117
12118 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
12119 kind, pulongest (unit_index), hex_string (signature),
12120 dwp_file->name);
12121
12122 /* Fetch the sections of this DWO unit.
12123 Put a limit on the number of sections we look for so that bad data
12124 doesn't cause us to loop forever. */
12125
12126 #define MAX_NR_V1_DWO_SECTIONS \
12127 (1 /* .debug_info or .debug_types */ \
12128 + 1 /* .debug_abbrev */ \
12129 + 1 /* .debug_line */ \
12130 + 1 /* .debug_loc */ \
12131 + 1 /* .debug_str_offsets */ \
12132 + 1 /* .debug_macro or .debug_macinfo */ \
12133 + 1 /* trailing zero */)
12134
12135 memset (&sections, 0, sizeof (sections));
12136
12137 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12138 {
12139 asection *sectp;
12140 uint32_t section_nr =
12141 read_4_bytes (dbfd,
12142 dwp_htab->section_pool.v1.indices
12143 + (unit_index + i) * sizeof (uint32_t));
12144
12145 if (section_nr == 0)
12146 break;
12147 if (section_nr >= dwp_file->num_sections)
12148 {
12149 error (_("Dwarf Error: bad DWP hash table, section number too large"
12150 " [in module %s]"),
12151 dwp_file->name);
12152 }
12153
12154 sectp = dwp_file->elf_sections[section_nr];
12155 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12156 {
12157 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12158 " [in module %s]"),
12159 dwp_file->name);
12160 }
12161 }
12162
12163 if (i < 2
12164 || sections.info_or_types.empty ()
12165 || sections.abbrev.empty ())
12166 {
12167 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12168 " [in module %s]"),
12169 dwp_file->name);
12170 }
12171 if (i == MAX_NR_V1_DWO_SECTIONS)
12172 {
12173 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12174 " [in module %s]"),
12175 dwp_file->name);
12176 }
12177
12178 /* It's easier for the rest of the code if we fake a struct dwo_file and
12179 have dwo_unit "live" in that. At least for now.
12180
12181 The DWP file can be made up of a random collection of CUs and TUs.
12182 However, for each CU + set of TUs that came from the same original DWO
12183 file, we can combine them back into a virtual DWO file to save space
12184 (fewer struct dwo_file objects to allocate). Remember that for really
12185 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12186
12187 std::string virtual_dwo_name =
12188 string_printf ("virtual-dwo/%d-%d-%d-%d",
12189 sections.abbrev.get_id (),
12190 sections.line.get_id (),
12191 sections.loc.get_id (),
12192 sections.str_offsets.get_id ());
12193 /* Can we use an existing virtual DWO file? */
12194 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12195 comp_dir);
12196 /* Create one if necessary. */
12197 if (*dwo_file_slot == NULL)
12198 {
12199 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12200 virtual_dwo_name.c_str ());
12201
12202 dwo_file = new struct dwo_file;
12203 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12204 dwo_file->comp_dir = comp_dir;
12205 dwo_file->sections.abbrev = sections.abbrev;
12206 dwo_file->sections.line = sections.line;
12207 dwo_file->sections.loc = sections.loc;
12208 dwo_file->sections.macinfo = sections.macinfo;
12209 dwo_file->sections.macro = sections.macro;
12210 dwo_file->sections.str_offsets = sections.str_offsets;
12211 /* The "str" section is global to the entire DWP file. */
12212 dwo_file->sections.str = dwp_file->sections.str;
12213 /* The info or types section is assigned below to dwo_unit,
12214 there's no need to record it in dwo_file.
12215 Also, we can't simply record type sections in dwo_file because
12216 we record a pointer into the vector in dwo_unit. As we collect more
12217 types we'll grow the vector and eventually have to reallocate space
12218 for it, invalidating all copies of pointers into the previous
12219 contents. */
12220 *dwo_file_slot = dwo_file;
12221 }
12222 else
12223 {
12224 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12225 virtual_dwo_name.c_str ());
12226
12227 dwo_file = (struct dwo_file *) *dwo_file_slot;
12228 }
12229
12230 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12231 dwo_unit->dwo_file = dwo_file;
12232 dwo_unit->signature = signature;
12233 dwo_unit->section =
12234 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12235 *dwo_unit->section = sections.info_or_types;
12236 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12237
12238 return dwo_unit;
12239 }
12240
12241 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12242 simplify them. Given a pointer to the containing section SECTION, and
12243 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12244 virtual section of just that piece. */
12245
12246 static struct dwarf2_section_info
12247 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12248 struct dwarf2_section_info *section,
12249 bfd_size_type offset, bfd_size_type size)
12250 {
12251 struct dwarf2_section_info result;
12252 asection *sectp;
12253
12254 gdb_assert (section != NULL);
12255 gdb_assert (!section->is_virtual);
12256
12257 memset (&result, 0, sizeof (result));
12258 result.s.containing_section = section;
12259 result.is_virtual = true;
12260
12261 if (size == 0)
12262 return result;
12263
12264 sectp = section->get_bfd_section ();
12265
12266 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12267 bounds of the real section. This is a pretty-rare event, so just
12268 flag an error (easier) instead of a warning and trying to cope. */
12269 if (sectp == NULL
12270 || offset + size > bfd_section_size (sectp))
12271 {
12272 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
12273 " in section %s [in module %s]"),
12274 sectp ? bfd_section_name (sectp) : "<unknown>",
12275 objfile_name (per_objfile->objfile));
12276 }
12277
12278 result.virtual_offset = offset;
12279 result.size = size;
12280 return result;
12281 }
12282
12283 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12284 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12285 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12286 This is for DWP version 2 files. */
12287
12288 static struct dwo_unit *
12289 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
12290 struct dwp_file *dwp_file,
12291 uint32_t unit_index,
12292 const char *comp_dir,
12293 ULONGEST signature, int is_debug_types)
12294 {
12295 const struct dwp_hash_table *dwp_htab =
12296 is_debug_types ? dwp_file->tus : dwp_file->cus;
12297 bfd *dbfd = dwp_file->dbfd.get ();
12298 const char *kind = is_debug_types ? "TU" : "CU";
12299 struct dwo_file *dwo_file;
12300 struct dwo_unit *dwo_unit;
12301 struct virtual_v2_or_v5_dwo_sections sections;
12302 void **dwo_file_slot;
12303 int i;
12304
12305 gdb_assert (dwp_file->version == 2);
12306
12307 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
12308 kind, pulongest (unit_index), hex_string (signature),
12309 dwp_file->name);
12310
12311 /* Fetch the section offsets of this DWO unit. */
12312
12313 memset (&sections, 0, sizeof (sections));
12314
12315 for (i = 0; i < dwp_htab->nr_columns; ++i)
12316 {
12317 uint32_t offset = read_4_bytes (dbfd,
12318 dwp_htab->section_pool.v2.offsets
12319 + (((unit_index - 1) * dwp_htab->nr_columns
12320 + i)
12321 * sizeof (uint32_t)));
12322 uint32_t size = read_4_bytes (dbfd,
12323 dwp_htab->section_pool.v2.sizes
12324 + (((unit_index - 1) * dwp_htab->nr_columns
12325 + i)
12326 * sizeof (uint32_t)));
12327
12328 switch (dwp_htab->section_pool.v2.section_ids[i])
12329 {
12330 case DW_SECT_INFO:
12331 case DW_SECT_TYPES:
12332 sections.info_or_types_offset = offset;
12333 sections.info_or_types_size = size;
12334 break;
12335 case DW_SECT_ABBREV:
12336 sections.abbrev_offset = offset;
12337 sections.abbrev_size = size;
12338 break;
12339 case DW_SECT_LINE:
12340 sections.line_offset = offset;
12341 sections.line_size = size;
12342 break;
12343 case DW_SECT_LOC:
12344 sections.loc_offset = offset;
12345 sections.loc_size = size;
12346 break;
12347 case DW_SECT_STR_OFFSETS:
12348 sections.str_offsets_offset = offset;
12349 sections.str_offsets_size = size;
12350 break;
12351 case DW_SECT_MACINFO:
12352 sections.macinfo_offset = offset;
12353 sections.macinfo_size = size;
12354 break;
12355 case DW_SECT_MACRO:
12356 sections.macro_offset = offset;
12357 sections.macro_size = size;
12358 break;
12359 }
12360 }
12361
12362 /* It's easier for the rest of the code if we fake a struct dwo_file and
12363 have dwo_unit "live" in that. At least for now.
12364
12365 The DWP file can be made up of a random collection of CUs and TUs.
12366 However, for each CU + set of TUs that came from the same original DWO
12367 file, we can combine them back into a virtual DWO file to save space
12368 (fewer struct dwo_file objects to allocate). Remember that for really
12369 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12370
12371 std::string virtual_dwo_name =
12372 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12373 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12374 (long) (sections.line_size ? sections.line_offset : 0),
12375 (long) (sections.loc_size ? sections.loc_offset : 0),
12376 (long) (sections.str_offsets_size
12377 ? sections.str_offsets_offset : 0));
12378 /* Can we use an existing virtual DWO file? */
12379 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12380 comp_dir);
12381 /* Create one if necessary. */
12382 if (*dwo_file_slot == NULL)
12383 {
12384 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12385 virtual_dwo_name.c_str ());
12386
12387 dwo_file = new struct dwo_file;
12388 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12389 dwo_file->comp_dir = comp_dir;
12390 dwo_file->sections.abbrev =
12391 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12392 sections.abbrev_offset,
12393 sections.abbrev_size);
12394 dwo_file->sections.line =
12395 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12396 sections.line_offset,
12397 sections.line_size);
12398 dwo_file->sections.loc =
12399 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12400 sections.loc_offset, sections.loc_size);
12401 dwo_file->sections.macinfo =
12402 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12403 sections.macinfo_offset,
12404 sections.macinfo_size);
12405 dwo_file->sections.macro =
12406 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12407 sections.macro_offset,
12408 sections.macro_size);
12409 dwo_file->sections.str_offsets =
12410 create_dwp_v2_or_v5_section (per_objfile,
12411 &dwp_file->sections.str_offsets,
12412 sections.str_offsets_offset,
12413 sections.str_offsets_size);
12414 /* The "str" section is global to the entire DWP file. */
12415 dwo_file->sections.str = dwp_file->sections.str;
12416 /* The info or types section is assigned below to dwo_unit,
12417 there's no need to record it in dwo_file.
12418 Also, we can't simply record type sections in dwo_file because
12419 we record a pointer into the vector in dwo_unit. As we collect more
12420 types we'll grow the vector and eventually have to reallocate space
12421 for it, invalidating all copies of pointers into the previous
12422 contents. */
12423 *dwo_file_slot = dwo_file;
12424 }
12425 else
12426 {
12427 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12428 virtual_dwo_name.c_str ());
12429
12430 dwo_file = (struct dwo_file *) *dwo_file_slot;
12431 }
12432
12433 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12434 dwo_unit->dwo_file = dwo_file;
12435 dwo_unit->signature = signature;
12436 dwo_unit->section =
12437 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12438 *dwo_unit->section = create_dwp_v2_or_v5_section
12439 (per_objfile,
12440 is_debug_types
12441 ? &dwp_file->sections.types
12442 : &dwp_file->sections.info,
12443 sections.info_or_types_offset,
12444 sections.info_or_types_size);
12445 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12446
12447 return dwo_unit;
12448 }
12449
12450 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12451 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12452 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12453 This is for DWP version 5 files. */
12454
12455 static struct dwo_unit *
12456 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12457 struct dwp_file *dwp_file,
12458 uint32_t unit_index,
12459 const char *comp_dir,
12460 ULONGEST signature, int is_debug_types)
12461 {
12462 const struct dwp_hash_table *dwp_htab
12463 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12464 bfd *dbfd = dwp_file->dbfd.get ();
12465 const char *kind = is_debug_types ? "TU" : "CU";
12466 struct dwo_file *dwo_file;
12467 struct dwo_unit *dwo_unit;
12468 struct virtual_v2_or_v5_dwo_sections sections {};
12469 void **dwo_file_slot;
12470
12471 gdb_assert (dwp_file->version == 5);
12472
12473 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12474 kind, pulongest (unit_index), hex_string (signature),
12475 dwp_file->name);
12476
12477 /* Fetch the section offsets of this DWO unit. */
12478
12479 /* memset (&sections, 0, sizeof (sections)); */
12480
12481 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12482 {
12483 uint32_t offset = read_4_bytes (dbfd,
12484 dwp_htab->section_pool.v5.offsets
12485 + (((unit_index - 1)
12486 * dwp_htab->nr_columns
12487 + i)
12488 * sizeof (uint32_t)));
12489 uint32_t size = read_4_bytes (dbfd,
12490 dwp_htab->section_pool.v5.sizes
12491 + (((unit_index - 1) * dwp_htab->nr_columns
12492 + i)
12493 * sizeof (uint32_t)));
12494
12495 switch (dwp_htab->section_pool.v5.section_ids[i])
12496 {
12497 case DW_SECT_ABBREV_V5:
12498 sections.abbrev_offset = offset;
12499 sections.abbrev_size = size;
12500 break;
12501 case DW_SECT_INFO_V5:
12502 sections.info_or_types_offset = offset;
12503 sections.info_or_types_size = size;
12504 break;
12505 case DW_SECT_LINE_V5:
12506 sections.line_offset = offset;
12507 sections.line_size = size;
12508 break;
12509 case DW_SECT_LOCLISTS_V5:
12510 sections.loclists_offset = offset;
12511 sections.loclists_size = size;
12512 break;
12513 case DW_SECT_MACRO_V5:
12514 sections.macro_offset = offset;
12515 sections.macro_size = size;
12516 break;
12517 case DW_SECT_RNGLISTS_V5:
12518 sections.rnglists_offset = offset;
12519 sections.rnglists_size = size;
12520 break;
12521 case DW_SECT_STR_OFFSETS_V5:
12522 sections.str_offsets_offset = offset;
12523 sections.str_offsets_size = size;
12524 break;
12525 case DW_SECT_RESERVED_V5:
12526 default:
12527 break;
12528 }
12529 }
12530
12531 /* It's easier for the rest of the code if we fake a struct dwo_file and
12532 have dwo_unit "live" in that. At least for now.
12533
12534 The DWP file can be made up of a random collection of CUs and TUs.
12535 However, for each CU + set of TUs that came from the same original DWO
12536 file, we can combine them back into a virtual DWO file to save space
12537 (fewer struct dwo_file objects to allocate). Remember that for really
12538 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12539
12540 std::string virtual_dwo_name =
12541 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
12542 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12543 (long) (sections.line_size ? sections.line_offset : 0),
12544 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12545 (long) (sections.str_offsets_size
12546 ? sections.str_offsets_offset : 0),
12547 (long) (sections.macro_size ? sections.macro_offset : 0),
12548 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
12549 /* Can we use an existing virtual DWO file? */
12550 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
12551 virtual_dwo_name.c_str (),
12552 comp_dir);
12553 /* Create one if necessary. */
12554 if (*dwo_file_slot == NULL)
12555 {
12556 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12557 virtual_dwo_name.c_str ());
12558
12559 dwo_file = new struct dwo_file;
12560 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12561 dwo_file->comp_dir = comp_dir;
12562 dwo_file->sections.abbrev =
12563 create_dwp_v2_or_v5_section (per_objfile,
12564 &dwp_file->sections.abbrev,
12565 sections.abbrev_offset,
12566 sections.abbrev_size);
12567 dwo_file->sections.line =
12568 create_dwp_v2_or_v5_section (per_objfile,
12569 &dwp_file->sections.line,
12570 sections.line_offset, sections.line_size);
12571 dwo_file->sections.macro =
12572 create_dwp_v2_or_v5_section (per_objfile,
12573 &dwp_file->sections.macro,
12574 sections.macro_offset,
12575 sections.macro_size);
12576 dwo_file->sections.loclists =
12577 create_dwp_v2_or_v5_section (per_objfile,
12578 &dwp_file->sections.loclists,
12579 sections.loclists_offset,
12580 sections.loclists_size);
12581 dwo_file->sections.rnglists =
12582 create_dwp_v2_or_v5_section (per_objfile,
12583 &dwp_file->sections.rnglists,
12584 sections.rnglists_offset,
12585 sections.rnglists_size);
12586 dwo_file->sections.str_offsets =
12587 create_dwp_v2_or_v5_section (per_objfile,
12588 &dwp_file->sections.str_offsets,
12589 sections.str_offsets_offset,
12590 sections.str_offsets_size);
12591 /* The "str" section is global to the entire DWP file. */
12592 dwo_file->sections.str = dwp_file->sections.str;
12593 /* The info or types section is assigned below to dwo_unit,
12594 there's no need to record it in dwo_file.
12595 Also, we can't simply record type sections in dwo_file because
12596 we record a pointer into the vector in dwo_unit. As we collect more
12597 types we'll grow the vector and eventually have to reallocate space
12598 for it, invalidating all copies of pointers into the previous
12599 contents. */
12600 *dwo_file_slot = dwo_file;
12601 }
12602 else
12603 {
12604 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12605 virtual_dwo_name.c_str ());
12606
12607 dwo_file = (struct dwo_file *) *dwo_file_slot;
12608 }
12609
12610 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12611 dwo_unit->dwo_file = dwo_file;
12612 dwo_unit->signature = signature;
12613 dwo_unit->section
12614 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12615 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12616 &dwp_file->sections.info,
12617 sections.info_or_types_offset,
12618 sections.info_or_types_size);
12619 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12620
12621 return dwo_unit;
12622 }
12623
12624 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12625 Returns NULL if the signature isn't found. */
12626
12627 static struct dwo_unit *
12628 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12629 struct dwp_file *dwp_file, const char *comp_dir,
12630 ULONGEST signature, int is_debug_types)
12631 {
12632 const struct dwp_hash_table *dwp_htab =
12633 is_debug_types ? dwp_file->tus : dwp_file->cus;
12634 bfd *dbfd = dwp_file->dbfd.get ();
12635 uint32_t mask = dwp_htab->nr_slots - 1;
12636 uint32_t hash = signature & mask;
12637 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12638 unsigned int i;
12639 void **slot;
12640 struct dwo_unit find_dwo_cu;
12641
12642 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12643 find_dwo_cu.signature = signature;
12644 slot = htab_find_slot (is_debug_types
12645 ? dwp_file->loaded_tus.get ()
12646 : dwp_file->loaded_cus.get (),
12647 &find_dwo_cu, INSERT);
12648
12649 if (*slot != NULL)
12650 return (struct dwo_unit *) *slot;
12651
12652 /* Use a for loop so that we don't loop forever on bad debug info. */
12653 for (i = 0; i < dwp_htab->nr_slots; ++i)
12654 {
12655 ULONGEST signature_in_table;
12656
12657 signature_in_table =
12658 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12659 if (signature_in_table == signature)
12660 {
12661 uint32_t unit_index =
12662 read_4_bytes (dbfd,
12663 dwp_htab->unit_table + hash * sizeof (uint32_t));
12664
12665 if (dwp_file->version == 1)
12666 {
12667 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12668 unit_index, comp_dir,
12669 signature, is_debug_types);
12670 }
12671 else if (dwp_file->version == 2)
12672 {
12673 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12674 unit_index, comp_dir,
12675 signature, is_debug_types);
12676 }
12677 else /* version == 5 */
12678 {
12679 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12680 unit_index, comp_dir,
12681 signature, is_debug_types);
12682 }
12683 return (struct dwo_unit *) *slot;
12684 }
12685 if (signature_in_table == 0)
12686 return NULL;
12687 hash = (hash + hash2) & mask;
12688 }
12689
12690 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12691 " [in module %s]"),
12692 dwp_file->name);
12693 }
12694
12695 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12696 Open the file specified by FILE_NAME and hand it off to BFD for
12697 preliminary analysis. Return a newly initialized bfd *, which
12698 includes a canonicalized copy of FILE_NAME.
12699 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12700 SEARCH_CWD is true if the current directory is to be searched.
12701 It will be searched before debug-file-directory.
12702 If successful, the file is added to the bfd include table of the
12703 objfile's bfd (see gdb_bfd_record_inclusion).
12704 If unable to find/open the file, return NULL.
12705 NOTE: This function is derived from symfile_bfd_open. */
12706
12707 static gdb_bfd_ref_ptr
12708 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12709 const char *file_name, int is_dwp, int search_cwd)
12710 {
12711 int desc;
12712 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12713 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12714 to debug_file_directory. */
12715 const char *search_path;
12716 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12717
12718 gdb::unique_xmalloc_ptr<char> search_path_holder;
12719 if (search_cwd)
12720 {
12721 if (*debug_file_directory != '\0')
12722 {
12723 search_path_holder.reset (concat (".", dirname_separator_string,
12724 debug_file_directory,
12725 (char *) NULL));
12726 search_path = search_path_holder.get ();
12727 }
12728 else
12729 search_path = ".";
12730 }
12731 else
12732 search_path = debug_file_directory;
12733
12734 openp_flags flags = OPF_RETURN_REALPATH;
12735 if (is_dwp)
12736 flags |= OPF_SEARCH_IN_PATH;
12737
12738 gdb::unique_xmalloc_ptr<char> absolute_name;
12739 desc = openp (search_path, flags, file_name,
12740 O_RDONLY | O_BINARY, &absolute_name);
12741 if (desc < 0)
12742 return NULL;
12743
12744 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12745 gnutarget, desc));
12746 if (sym_bfd == NULL)
12747 return NULL;
12748 bfd_set_cacheable (sym_bfd.get (), 1);
12749
12750 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12751 return NULL;
12752
12753 /* Success. Record the bfd as having been included by the objfile's bfd.
12754 This is important because things like demangled_names_hash lives in the
12755 objfile's per_bfd space and may have references to things like symbol
12756 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12757 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12758
12759 return sym_bfd;
12760 }
12761
12762 /* Try to open DWO file FILE_NAME.
12763 COMP_DIR is the DW_AT_comp_dir attribute.
12764 The result is the bfd handle of the file.
12765 If there is a problem finding or opening the file, return NULL.
12766 Upon success, the canonicalized path of the file is stored in the bfd,
12767 same as symfile_bfd_open. */
12768
12769 static gdb_bfd_ref_ptr
12770 open_dwo_file (dwarf2_per_objfile *per_objfile,
12771 const char *file_name, const char *comp_dir)
12772 {
12773 if (IS_ABSOLUTE_PATH (file_name))
12774 return try_open_dwop_file (per_objfile, file_name,
12775 0 /*is_dwp*/, 0 /*search_cwd*/);
12776
12777 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12778
12779 if (comp_dir != NULL)
12780 {
12781 gdb::unique_xmalloc_ptr<char> path_to_try
12782 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12783
12784 /* NOTE: If comp_dir is a relative path, this will also try the
12785 search path, which seems useful. */
12786 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12787 0 /*is_dwp*/,
12788 1 /*search_cwd*/));
12789 if (abfd != NULL)
12790 return abfd;
12791 }
12792
12793 /* That didn't work, try debug-file-directory, which, despite its name,
12794 is a list of paths. */
12795
12796 if (*debug_file_directory == '\0')
12797 return NULL;
12798
12799 return try_open_dwop_file (per_objfile, file_name,
12800 0 /*is_dwp*/, 1 /*search_cwd*/);
12801 }
12802
12803 /* This function is mapped across the sections and remembers the offset and
12804 size of each of the DWO debugging sections we are interested in. */
12805
12806 static void
12807 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12808 dwo_sections *dwo_sections)
12809 {
12810 const struct dwop_section_names *names = &dwop_section_names;
12811
12812 if (section_is_p (sectp->name, &names->abbrev_dwo))
12813 {
12814 dwo_sections->abbrev.s.section = sectp;
12815 dwo_sections->abbrev.size = bfd_section_size (sectp);
12816 }
12817 else if (section_is_p (sectp->name, &names->info_dwo))
12818 {
12819 dwo_sections->info.s.section = sectp;
12820 dwo_sections->info.size = bfd_section_size (sectp);
12821 }
12822 else if (section_is_p (sectp->name, &names->line_dwo))
12823 {
12824 dwo_sections->line.s.section = sectp;
12825 dwo_sections->line.size = bfd_section_size (sectp);
12826 }
12827 else if (section_is_p (sectp->name, &names->loc_dwo))
12828 {
12829 dwo_sections->loc.s.section = sectp;
12830 dwo_sections->loc.size = bfd_section_size (sectp);
12831 }
12832 else if (section_is_p (sectp->name, &names->loclists_dwo))
12833 {
12834 dwo_sections->loclists.s.section = sectp;
12835 dwo_sections->loclists.size = bfd_section_size (sectp);
12836 }
12837 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12838 {
12839 dwo_sections->macinfo.s.section = sectp;
12840 dwo_sections->macinfo.size = bfd_section_size (sectp);
12841 }
12842 else if (section_is_p (sectp->name, &names->macro_dwo))
12843 {
12844 dwo_sections->macro.s.section = sectp;
12845 dwo_sections->macro.size = bfd_section_size (sectp);
12846 }
12847 else if (section_is_p (sectp->name, &names->rnglists_dwo))
12848 {
12849 dwo_sections->rnglists.s.section = sectp;
12850 dwo_sections->rnglists.size = bfd_section_size (sectp);
12851 }
12852 else if (section_is_p (sectp->name, &names->str_dwo))
12853 {
12854 dwo_sections->str.s.section = sectp;
12855 dwo_sections->str.size = bfd_section_size (sectp);
12856 }
12857 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12858 {
12859 dwo_sections->str_offsets.s.section = sectp;
12860 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12861 }
12862 else if (section_is_p (sectp->name, &names->types_dwo))
12863 {
12864 struct dwarf2_section_info type_section;
12865
12866 memset (&type_section, 0, sizeof (type_section));
12867 type_section.s.section = sectp;
12868 type_section.size = bfd_section_size (sectp);
12869 dwo_sections->types.push_back (type_section);
12870 }
12871 }
12872
12873 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12874 by PER_CU. This is for the non-DWP case.
12875 The result is NULL if DWO_NAME can't be found. */
12876
12877 static struct dwo_file *
12878 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12879 const char *comp_dir)
12880 {
12881 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12882
12883 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12884 if (dbfd == NULL)
12885 {
12886 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12887
12888 return NULL;
12889 }
12890
12891 dwo_file_up dwo_file (new struct dwo_file);
12892 dwo_file->dwo_name = dwo_name;
12893 dwo_file->comp_dir = comp_dir;
12894 dwo_file->dbfd = std::move (dbfd);
12895
12896 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12897 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12898 &dwo_file->sections);
12899
12900 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12901 dwo_file->cus);
12902
12903 if (cu->per_cu->dwarf_version < 5)
12904 {
12905 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12906 dwo_file->sections.types, dwo_file->tus);
12907 }
12908 else
12909 {
12910 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12911 &dwo_file->sections.info, dwo_file->tus,
12912 rcuh_kind::TYPE);
12913 }
12914
12915 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12916
12917 return dwo_file.release ();
12918 }
12919
12920 /* This function is mapped across the sections and remembers the offset and
12921 size of each of the DWP debugging sections common to version 1 and 2 that
12922 we are interested in. */
12923
12924 static void
12925 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12926 dwp_file *dwp_file)
12927 {
12928 const struct dwop_section_names *names = &dwop_section_names;
12929 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12930
12931 /* Record the ELF section number for later lookup: this is what the
12932 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12933 gdb_assert (elf_section_nr < dwp_file->num_sections);
12934 dwp_file->elf_sections[elf_section_nr] = sectp;
12935
12936 /* Look for specific sections that we need. */
12937 if (section_is_p (sectp->name, &names->str_dwo))
12938 {
12939 dwp_file->sections.str.s.section = sectp;
12940 dwp_file->sections.str.size = bfd_section_size (sectp);
12941 }
12942 else if (section_is_p (sectp->name, &names->cu_index))
12943 {
12944 dwp_file->sections.cu_index.s.section = sectp;
12945 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12946 }
12947 else if (section_is_p (sectp->name, &names->tu_index))
12948 {
12949 dwp_file->sections.tu_index.s.section = sectp;
12950 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12951 }
12952 }
12953
12954 /* This function is mapped across the sections and remembers the offset and
12955 size of each of the DWP version 2 debugging sections that we are interested
12956 in. This is split into a separate function because we don't know if we
12957 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12958
12959 static void
12960 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12961 {
12962 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12963 const struct dwop_section_names *names = &dwop_section_names;
12964 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12965
12966 /* Record the ELF section number for later lookup: this is what the
12967 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12968 gdb_assert (elf_section_nr < dwp_file->num_sections);
12969 dwp_file->elf_sections[elf_section_nr] = sectp;
12970
12971 /* Look for specific sections that we need. */
12972 if (section_is_p (sectp->name, &names->abbrev_dwo))
12973 {
12974 dwp_file->sections.abbrev.s.section = sectp;
12975 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12976 }
12977 else if (section_is_p (sectp->name, &names->info_dwo))
12978 {
12979 dwp_file->sections.info.s.section = sectp;
12980 dwp_file->sections.info.size = bfd_section_size (sectp);
12981 }
12982 else if (section_is_p (sectp->name, &names->line_dwo))
12983 {
12984 dwp_file->sections.line.s.section = sectp;
12985 dwp_file->sections.line.size = bfd_section_size (sectp);
12986 }
12987 else if (section_is_p (sectp->name, &names->loc_dwo))
12988 {
12989 dwp_file->sections.loc.s.section = sectp;
12990 dwp_file->sections.loc.size = bfd_section_size (sectp);
12991 }
12992 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12993 {
12994 dwp_file->sections.macinfo.s.section = sectp;
12995 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12996 }
12997 else if (section_is_p (sectp->name, &names->macro_dwo))
12998 {
12999 dwp_file->sections.macro.s.section = sectp;
13000 dwp_file->sections.macro.size = bfd_section_size (sectp);
13001 }
13002 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13003 {
13004 dwp_file->sections.str_offsets.s.section = sectp;
13005 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13006 }
13007 else if (section_is_p (sectp->name, &names->types_dwo))
13008 {
13009 dwp_file->sections.types.s.section = sectp;
13010 dwp_file->sections.types.size = bfd_section_size (sectp);
13011 }
13012 }
13013
13014 /* This function is mapped across the sections and remembers the offset and
13015 size of each of the DWP version 5 debugging sections that we are interested
13016 in. This is split into a separate function because we don't know if we
13017 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
13018
13019 static void
13020 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13021 {
13022 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13023 const struct dwop_section_names *names = &dwop_section_names;
13024 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13025
13026 /* Record the ELF section number for later lookup: this is what the
13027 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13028 gdb_assert (elf_section_nr < dwp_file->num_sections);
13029 dwp_file->elf_sections[elf_section_nr] = sectp;
13030
13031 /* Look for specific sections that we need. */
13032 if (section_is_p (sectp->name, &names->abbrev_dwo))
13033 {
13034 dwp_file->sections.abbrev.s.section = sectp;
13035 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13036 }
13037 else if (section_is_p (sectp->name, &names->info_dwo))
13038 {
13039 dwp_file->sections.info.s.section = sectp;
13040 dwp_file->sections.info.size = bfd_section_size (sectp);
13041 }
13042 else if (section_is_p (sectp->name, &names->line_dwo))
13043 {
13044 dwp_file->sections.line.s.section = sectp;
13045 dwp_file->sections.line.size = bfd_section_size (sectp);
13046 }
13047 else if (section_is_p (sectp->name, &names->loclists_dwo))
13048 {
13049 dwp_file->sections.loclists.s.section = sectp;
13050 dwp_file->sections.loclists.size = bfd_section_size (sectp);
13051 }
13052 else if (section_is_p (sectp->name, &names->macro_dwo))
13053 {
13054 dwp_file->sections.macro.s.section = sectp;
13055 dwp_file->sections.macro.size = bfd_section_size (sectp);
13056 }
13057 else if (section_is_p (sectp->name, &names->rnglists_dwo))
13058 {
13059 dwp_file->sections.rnglists.s.section = sectp;
13060 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
13061 }
13062 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13063 {
13064 dwp_file->sections.str_offsets.s.section = sectp;
13065 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13066 }
13067 }
13068
13069 /* Hash function for dwp_file loaded CUs/TUs. */
13070
13071 static hashval_t
13072 hash_dwp_loaded_cutus (const void *item)
13073 {
13074 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13075
13076 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13077 return dwo_unit->signature;
13078 }
13079
13080 /* Equality function for dwp_file loaded CUs/TUs. */
13081
13082 static int
13083 eq_dwp_loaded_cutus (const void *a, const void *b)
13084 {
13085 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13086 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13087
13088 return dua->signature == dub->signature;
13089 }
13090
13091 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13092
13093 static htab_up
13094 allocate_dwp_loaded_cutus_table ()
13095 {
13096 return htab_up (htab_create_alloc (3,
13097 hash_dwp_loaded_cutus,
13098 eq_dwp_loaded_cutus,
13099 NULL, xcalloc, xfree));
13100 }
13101
13102 /* Try to open DWP file FILE_NAME.
13103 The result is the bfd handle of the file.
13104 If there is a problem finding or opening the file, return NULL.
13105 Upon success, the canonicalized path of the file is stored in the bfd,
13106 same as symfile_bfd_open. */
13107
13108 static gdb_bfd_ref_ptr
13109 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
13110 {
13111 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
13112 1 /*is_dwp*/,
13113 1 /*search_cwd*/));
13114 if (abfd != NULL)
13115 return abfd;
13116
13117 /* Work around upstream bug 15652.
13118 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13119 [Whether that's a "bug" is debatable, but it is getting in our way.]
13120 We have no real idea where the dwp file is, because gdb's realpath-ing
13121 of the executable's path may have discarded the needed info.
13122 [IWBN if the dwp file name was recorded in the executable, akin to
13123 .gnu_debuglink, but that doesn't exist yet.]
13124 Strip the directory from FILE_NAME and search again. */
13125 if (*debug_file_directory != '\0')
13126 {
13127 /* Don't implicitly search the current directory here.
13128 If the user wants to search "." to handle this case,
13129 it must be added to debug-file-directory. */
13130 return try_open_dwop_file (per_objfile, lbasename (file_name),
13131 1 /*is_dwp*/,
13132 0 /*search_cwd*/);
13133 }
13134
13135 return NULL;
13136 }
13137
13138 /* Initialize the use of the DWP file for the current objfile.
13139 By convention the name of the DWP file is ${objfile}.dwp.
13140 The result is NULL if it can't be found. */
13141
13142 static std::unique_ptr<struct dwp_file>
13143 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
13144 {
13145 struct objfile *objfile = per_objfile->objfile;
13146
13147 /* Try to find first .dwp for the binary file before any symbolic links
13148 resolving. */
13149
13150 /* If the objfile is a debug file, find the name of the real binary
13151 file and get the name of dwp file from there. */
13152 std::string dwp_name;
13153 if (objfile->separate_debug_objfile_backlink != NULL)
13154 {
13155 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13156 const char *backlink_basename = lbasename (backlink->original_name);
13157
13158 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13159 }
13160 else
13161 dwp_name = objfile->original_name;
13162
13163 dwp_name += ".dwp";
13164
13165 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
13166 if (dbfd == NULL
13167 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13168 {
13169 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13170 dwp_name = objfile_name (objfile);
13171 dwp_name += ".dwp";
13172 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
13173 }
13174
13175 if (dbfd == NULL)
13176 {
13177 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
13178
13179 return std::unique_ptr<dwp_file> ();
13180 }
13181
13182 const char *name = bfd_get_filename (dbfd.get ());
13183 std::unique_ptr<struct dwp_file> dwp_file
13184 (new struct dwp_file (name, std::move (dbfd)));
13185
13186 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13187 dwp_file->elf_sections =
13188 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
13189 dwp_file->num_sections, asection *);
13190
13191 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13192 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
13193 dwp_file.get ());
13194
13195 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
13196
13197 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
13198
13199 /* The DWP file version is stored in the hash table. Oh well. */
13200 if (dwp_file->cus && dwp_file->tus
13201 && dwp_file->cus->version != dwp_file->tus->version)
13202 {
13203 /* Technically speaking, we should try to limp along, but this is
13204 pretty bizarre. We use pulongest here because that's the established
13205 portability solution (e.g, we cannot use %u for uint32_t). */
13206 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13207 " TU version %s [in DWP file %s]"),
13208 pulongest (dwp_file->cus->version),
13209 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13210 }
13211
13212 if (dwp_file->cus)
13213 dwp_file->version = dwp_file->cus->version;
13214 else if (dwp_file->tus)
13215 dwp_file->version = dwp_file->tus->version;
13216 else
13217 dwp_file->version = 2;
13218
13219 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13220 {
13221 if (dwp_file->version == 2)
13222 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
13223 dwp_file.get ());
13224 else
13225 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
13226 dwp_file.get ());
13227 }
13228
13229 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13230 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
13231
13232 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
13233 dwarf_read_debug_printf (" %s CUs, %s TUs",
13234 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13235 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13236
13237 return dwp_file;
13238 }
13239
13240 /* Wrapper around open_and_init_dwp_file, only open it once. */
13241
13242 static struct dwp_file *
13243 get_dwp_file (dwarf2_per_objfile *per_objfile)
13244 {
13245 if (!per_objfile->per_bfd->dwp_checked)
13246 {
13247 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13248 per_objfile->per_bfd->dwp_checked = 1;
13249 }
13250 return per_objfile->per_bfd->dwp_file.get ();
13251 }
13252
13253 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13254 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13255 or in the DWP file for the objfile, referenced by THIS_UNIT.
13256 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13257 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13258
13259 This is called, for example, when wanting to read a variable with a
13260 complex location. Therefore we don't want to do file i/o for every call.
13261 Therefore we don't want to look for a DWO file on every call.
13262 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13263 then we check if we've already seen DWO_NAME, and only THEN do we check
13264 for a DWO file.
13265
13266 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13267 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13268
13269 static struct dwo_unit *
13270 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13271 ULONGEST signature, int is_debug_types)
13272 {
13273 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13274 struct objfile *objfile = per_objfile->objfile;
13275 const char *kind = is_debug_types ? "TU" : "CU";
13276 void **dwo_file_slot;
13277 struct dwo_file *dwo_file;
13278 struct dwp_file *dwp_file;
13279
13280 /* First see if there's a DWP file.
13281 If we have a DWP file but didn't find the DWO inside it, don't
13282 look for the original DWO file. It makes gdb behave differently
13283 depending on whether one is debugging in the build tree. */
13284
13285 dwp_file = get_dwp_file (per_objfile);
13286 if (dwp_file != NULL)
13287 {
13288 const struct dwp_hash_table *dwp_htab =
13289 is_debug_types ? dwp_file->tus : dwp_file->cus;
13290
13291 if (dwp_htab != NULL)
13292 {
13293 struct dwo_unit *dwo_cutu =
13294 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13295 is_debug_types);
13296
13297 if (dwo_cutu != NULL)
13298 {
13299 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
13300 kind, hex_string (signature),
13301 host_address_to_string (dwo_cutu));
13302
13303 return dwo_cutu;
13304 }
13305 }
13306 }
13307 else
13308 {
13309 /* No DWP file, look for the DWO file. */
13310
13311 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
13312 if (*dwo_file_slot == NULL)
13313 {
13314 /* Read in the file and build a table of the CUs/TUs it contains. */
13315 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
13316 }
13317 /* NOTE: This will be NULL if unable to open the file. */
13318 dwo_file = (struct dwo_file *) *dwo_file_slot;
13319
13320 if (dwo_file != NULL)
13321 {
13322 struct dwo_unit *dwo_cutu = NULL;
13323
13324 if (is_debug_types && dwo_file->tus)
13325 {
13326 struct dwo_unit find_dwo_cutu;
13327
13328 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13329 find_dwo_cutu.signature = signature;
13330 dwo_cutu
13331 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13332 &find_dwo_cutu);
13333 }
13334 else if (!is_debug_types && dwo_file->cus)
13335 {
13336 struct dwo_unit find_dwo_cutu;
13337
13338 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13339 find_dwo_cutu.signature = signature;
13340 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
13341 &find_dwo_cutu);
13342 }
13343
13344 if (dwo_cutu != NULL)
13345 {
13346 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
13347 kind, dwo_name, hex_string (signature),
13348 host_address_to_string (dwo_cutu));
13349
13350 return dwo_cutu;
13351 }
13352 }
13353 }
13354
13355 /* We didn't find it. This could mean a dwo_id mismatch, or
13356 someone deleted the DWO/DWP file, or the search path isn't set up
13357 correctly to find the file. */
13358
13359 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
13360 kind, dwo_name, hex_string (signature));
13361
13362 /* This is a warning and not a complaint because it can be caused by
13363 pilot error (e.g., user accidentally deleting the DWO). */
13364 {
13365 /* Print the name of the DWP file if we looked there, helps the user
13366 better diagnose the problem. */
13367 std::string dwp_text;
13368
13369 if (dwp_file != NULL)
13370 dwp_text = string_printf (" [in DWP file %s]",
13371 lbasename (dwp_file->name));
13372
13373 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13374 " [in module %s]"),
13375 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13376 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
13377 }
13378 return NULL;
13379 }
13380
13381 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13382 See lookup_dwo_cutu_unit for details. */
13383
13384 static struct dwo_unit *
13385 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13386 ULONGEST signature)
13387 {
13388 gdb_assert (!cu->per_cu->is_debug_types);
13389
13390 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
13391 }
13392
13393 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13394 See lookup_dwo_cutu_unit for details. */
13395
13396 static struct dwo_unit *
13397 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
13398 {
13399 gdb_assert (cu->per_cu->is_debug_types);
13400
13401 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13402
13403 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
13404 }
13405
13406 /* Traversal function for queue_and_load_all_dwo_tus. */
13407
13408 static int
13409 queue_and_load_dwo_tu (void **slot, void *info)
13410 {
13411 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13412 dwarf2_cu *cu = (dwarf2_cu *) info;
13413 ULONGEST signature = dwo_unit->signature;
13414 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
13415
13416 if (sig_type != NULL)
13417 {
13418 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13419
13420 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13421 a real dependency of PER_CU on SIG_TYPE. That is detected later
13422 while processing PER_CU. */
13423 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
13424 load_full_type_unit (sig_cu, cu->per_objfile);
13425 cu->per_cu->imported_symtabs_push (sig_cu);
13426 }
13427
13428 return 1;
13429 }
13430
13431 /* Queue all TUs contained in the DWO of CU to be read in.
13432 The DWO may have the only definition of the type, though it may not be
13433 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13434 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13435
13436 static void
13437 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
13438 {
13439 struct dwo_unit *dwo_unit;
13440 struct dwo_file *dwo_file;
13441
13442 gdb_assert (cu != nullptr);
13443 gdb_assert (!cu->per_cu->is_debug_types);
13444 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
13445
13446 dwo_unit = cu->dwo_unit;
13447 gdb_assert (dwo_unit != NULL);
13448
13449 dwo_file = dwo_unit->dwo_file;
13450 if (dwo_file->tus != NULL)
13451 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
13452 }
13453
13454 /* Read in various DIEs. */
13455
13456 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13457 Inherit only the children of the DW_AT_abstract_origin DIE not being
13458 already referenced by DW_AT_abstract_origin from the children of the
13459 current DIE. */
13460
13461 static void
13462 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13463 {
13464 struct die_info *child_die;
13465 sect_offset *offsetp;
13466 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13467 struct die_info *origin_die;
13468 /* Iterator of the ORIGIN_DIE children. */
13469 struct die_info *origin_child_die;
13470 struct attribute *attr;
13471 struct dwarf2_cu *origin_cu;
13472 struct pending **origin_previous_list_in_scope;
13473
13474 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13475 if (!attr)
13476 return;
13477
13478 /* Note that following die references may follow to a die in a
13479 different cu. */
13480
13481 origin_cu = cu;
13482 origin_die = follow_die_ref (die, attr, &origin_cu);
13483
13484 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13485 symbols in. */
13486 origin_previous_list_in_scope = origin_cu->list_in_scope;
13487 origin_cu->list_in_scope = cu->list_in_scope;
13488
13489 if (die->tag != origin_die->tag
13490 && !(die->tag == DW_TAG_inlined_subroutine
13491 && origin_die->tag == DW_TAG_subprogram))
13492 complaint (_("DIE %s and its abstract origin %s have different tags"),
13493 sect_offset_str (die->sect_off),
13494 sect_offset_str (origin_die->sect_off));
13495
13496 std::vector<sect_offset> offsets;
13497
13498 for (child_die = die->child;
13499 child_die && child_die->tag;
13500 child_die = child_die->sibling)
13501 {
13502 struct die_info *child_origin_die;
13503 struct dwarf2_cu *child_origin_cu;
13504
13505 /* We are trying to process concrete instance entries:
13506 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13507 it's not relevant to our analysis here. i.e. detecting DIEs that are
13508 present in the abstract instance but not referenced in the concrete
13509 one. */
13510 if (child_die->tag == DW_TAG_call_site
13511 || child_die->tag == DW_TAG_GNU_call_site)
13512 continue;
13513
13514 /* For each CHILD_DIE, find the corresponding child of
13515 ORIGIN_DIE. If there is more than one layer of
13516 DW_AT_abstract_origin, follow them all; there shouldn't be,
13517 but GCC versions at least through 4.4 generate this (GCC PR
13518 40573). */
13519 child_origin_die = child_die;
13520 child_origin_cu = cu;
13521 while (1)
13522 {
13523 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13524 child_origin_cu);
13525 if (attr == NULL)
13526 break;
13527 child_origin_die = follow_die_ref (child_origin_die, attr,
13528 &child_origin_cu);
13529 }
13530
13531 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13532 counterpart may exist. */
13533 if (child_origin_die != child_die)
13534 {
13535 if (child_die->tag != child_origin_die->tag
13536 && !(child_die->tag == DW_TAG_inlined_subroutine
13537 && child_origin_die->tag == DW_TAG_subprogram))
13538 complaint (_("Child DIE %s and its abstract origin %s have "
13539 "different tags"),
13540 sect_offset_str (child_die->sect_off),
13541 sect_offset_str (child_origin_die->sect_off));
13542 if (child_origin_die->parent != origin_die)
13543 complaint (_("Child DIE %s and its abstract origin %s have "
13544 "different parents"),
13545 sect_offset_str (child_die->sect_off),
13546 sect_offset_str (child_origin_die->sect_off));
13547 else
13548 offsets.push_back (child_origin_die->sect_off);
13549 }
13550 }
13551 std::sort (offsets.begin (), offsets.end ());
13552 sect_offset *offsets_end = offsets.data () + offsets.size ();
13553 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13554 if (offsetp[-1] == *offsetp)
13555 complaint (_("Multiple children of DIE %s refer "
13556 "to DIE %s as their abstract origin"),
13557 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13558
13559 offsetp = offsets.data ();
13560 origin_child_die = origin_die->child;
13561 while (origin_child_die && origin_child_die->tag)
13562 {
13563 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13564 while (offsetp < offsets_end
13565 && *offsetp < origin_child_die->sect_off)
13566 offsetp++;
13567 if (offsetp >= offsets_end
13568 || *offsetp > origin_child_die->sect_off)
13569 {
13570 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13571 Check whether we're already processing ORIGIN_CHILD_DIE.
13572 This can happen with mutually referenced abstract_origins.
13573 PR 16581. */
13574 if (!origin_child_die->in_process)
13575 process_die (origin_child_die, origin_cu);
13576 }
13577 origin_child_die = origin_child_die->sibling;
13578 }
13579 origin_cu->list_in_scope = origin_previous_list_in_scope;
13580
13581 if (cu != origin_cu)
13582 compute_delayed_physnames (origin_cu);
13583 }
13584
13585 static void
13586 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13587 {
13588 struct objfile *objfile = cu->per_objfile->objfile;
13589 struct gdbarch *gdbarch = objfile->arch ();
13590 struct context_stack *newobj;
13591 CORE_ADDR lowpc;
13592 CORE_ADDR highpc;
13593 struct die_info *child_die;
13594 struct attribute *attr, *call_line, *call_file;
13595 const char *name;
13596 CORE_ADDR baseaddr;
13597 struct block *block;
13598 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13599 std::vector<struct symbol *> template_args;
13600 struct template_symbol *templ_func = NULL;
13601
13602 if (inlined_func)
13603 {
13604 /* If we do not have call site information, we can't show the
13605 caller of this inlined function. That's too confusing, so
13606 only use the scope for local variables. */
13607 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13608 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13609 if (call_line == NULL || call_file == NULL)
13610 {
13611 read_lexical_block_scope (die, cu);
13612 return;
13613 }
13614 }
13615
13616 baseaddr = objfile->text_section_offset ();
13617
13618 name = dwarf2_name (die, cu);
13619
13620 /* Ignore functions with missing or empty names. These are actually
13621 illegal according to the DWARF standard. */
13622 if (name == NULL)
13623 {
13624 complaint (_("missing name for subprogram DIE at %s"),
13625 sect_offset_str (die->sect_off));
13626 return;
13627 }
13628
13629 /* Ignore functions with missing or invalid low and high pc attributes. */
13630 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13631 <= PC_BOUNDS_INVALID)
13632 {
13633 attr = dwarf2_attr (die, DW_AT_external, cu);
13634 if (attr == nullptr || !attr->as_boolean ())
13635 complaint (_("cannot get low and high bounds "
13636 "for subprogram DIE at %s"),
13637 sect_offset_str (die->sect_off));
13638 return;
13639 }
13640
13641 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13642 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13643
13644 /* If we have any template arguments, then we must allocate a
13645 different sort of symbol. */
13646 for (child_die = die->child; child_die; child_die = child_die->sibling)
13647 {
13648 if (child_die->tag == DW_TAG_template_type_param
13649 || child_die->tag == DW_TAG_template_value_param)
13650 {
13651 templ_func = new (&objfile->objfile_obstack) template_symbol;
13652 templ_func->subclass = SYMBOL_TEMPLATE;
13653 break;
13654 }
13655 }
13656
13657 newobj = cu->get_builder ()->push_context (0, lowpc);
13658 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13659 (struct symbol *) templ_func);
13660
13661 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13662 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13663 cu->language);
13664
13665 /* If there is a location expression for DW_AT_frame_base, record
13666 it. */
13667 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13668 if (attr != nullptr)
13669 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13670
13671 /* If there is a location for the static link, record it. */
13672 newobj->static_link = NULL;
13673 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13674 if (attr != nullptr)
13675 {
13676 newobj->static_link
13677 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13678 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13679 cu->addr_type ());
13680 }
13681
13682 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13683
13684 if (die->child != NULL)
13685 {
13686 child_die = die->child;
13687 while (child_die && child_die->tag)
13688 {
13689 if (child_die->tag == DW_TAG_template_type_param
13690 || child_die->tag == DW_TAG_template_value_param)
13691 {
13692 struct symbol *arg = new_symbol (child_die, NULL, cu);
13693
13694 if (arg != NULL)
13695 template_args.push_back (arg);
13696 }
13697 else
13698 process_die (child_die, cu);
13699 child_die = child_die->sibling;
13700 }
13701 }
13702
13703 inherit_abstract_dies (die, cu);
13704
13705 /* If we have a DW_AT_specification, we might need to import using
13706 directives from the context of the specification DIE. See the
13707 comment in determine_prefix. */
13708 if (cu->language == language_cplus
13709 && dwarf2_attr (die, DW_AT_specification, cu))
13710 {
13711 struct dwarf2_cu *spec_cu = cu;
13712 struct die_info *spec_die = die_specification (die, &spec_cu);
13713
13714 while (spec_die)
13715 {
13716 child_die = spec_die->child;
13717 while (child_die && child_die->tag)
13718 {
13719 if (child_die->tag == DW_TAG_imported_module)
13720 process_die (child_die, spec_cu);
13721 child_die = child_die->sibling;
13722 }
13723
13724 /* In some cases, GCC generates specification DIEs that
13725 themselves contain DW_AT_specification attributes. */
13726 spec_die = die_specification (spec_die, &spec_cu);
13727 }
13728 }
13729
13730 struct context_stack cstk = cu->get_builder ()->pop_context ();
13731 /* Make a block for the local symbols within. */
13732 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13733 cstk.static_link, lowpc, highpc);
13734
13735 /* For C++, set the block's scope. */
13736 if ((cu->language == language_cplus
13737 || cu->language == language_fortran
13738 || cu->language == language_d
13739 || cu->language == language_rust)
13740 && cu->processing_has_namespace_info)
13741 block_set_scope (block, determine_prefix (die, cu),
13742 &objfile->objfile_obstack);
13743
13744 /* If we have address ranges, record them. */
13745 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13746
13747 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13748
13749 /* Attach template arguments to function. */
13750 if (!template_args.empty ())
13751 {
13752 gdb_assert (templ_func != NULL);
13753
13754 templ_func->n_template_arguments = template_args.size ();
13755 templ_func->template_arguments
13756 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13757 templ_func->n_template_arguments);
13758 memcpy (templ_func->template_arguments,
13759 template_args.data (),
13760 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13761
13762 /* Make sure that the symtab is set on the new symbols. Even
13763 though they don't appear in this symtab directly, other parts
13764 of gdb assume that symbols do, and this is reasonably
13765 true. */
13766 for (symbol *sym : template_args)
13767 symbol_set_symtab (sym, symbol_symtab (templ_func));
13768 }
13769
13770 /* In C++, we can have functions nested inside functions (e.g., when
13771 a function declares a class that has methods). This means that
13772 when we finish processing a function scope, we may need to go
13773 back to building a containing block's symbol lists. */
13774 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13775 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13776
13777 /* If we've finished processing a top-level function, subsequent
13778 symbols go in the file symbol list. */
13779 if (cu->get_builder ()->outermost_context_p ())
13780 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13781 }
13782
13783 /* Process all the DIES contained within a lexical block scope. Start
13784 a new scope, process the dies, and then close the scope. */
13785
13786 static void
13787 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13788 {
13789 struct objfile *objfile = cu->per_objfile->objfile;
13790 struct gdbarch *gdbarch = objfile->arch ();
13791 CORE_ADDR lowpc, highpc;
13792 struct die_info *child_die;
13793 CORE_ADDR baseaddr;
13794
13795 baseaddr = objfile->text_section_offset ();
13796
13797 /* Ignore blocks with missing or invalid low and high pc attributes. */
13798 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13799 as multiple lexical blocks? Handling children in a sane way would
13800 be nasty. Might be easier to properly extend generic blocks to
13801 describe ranges. */
13802 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13803 {
13804 case PC_BOUNDS_NOT_PRESENT:
13805 /* DW_TAG_lexical_block has no attributes, process its children as if
13806 there was no wrapping by that DW_TAG_lexical_block.
13807 GCC does no longer produces such DWARF since GCC r224161. */
13808 for (child_die = die->child;
13809 child_die != NULL && child_die->tag;
13810 child_die = child_die->sibling)
13811 {
13812 /* We might already be processing this DIE. This can happen
13813 in an unusual circumstance -- where a subroutine A
13814 appears lexically in another subroutine B, but A actually
13815 inlines B. The recursion is broken here, rather than in
13816 inherit_abstract_dies, because it seems better to simply
13817 drop concrete children here. */
13818 if (!child_die->in_process)
13819 process_die (child_die, cu);
13820 }
13821 return;
13822 case PC_BOUNDS_INVALID:
13823 return;
13824 }
13825 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13826 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13827
13828 cu->get_builder ()->push_context (0, lowpc);
13829 if (die->child != NULL)
13830 {
13831 child_die = die->child;
13832 while (child_die && child_die->tag)
13833 {
13834 process_die (child_die, cu);
13835 child_die = child_die->sibling;
13836 }
13837 }
13838 inherit_abstract_dies (die, cu);
13839 struct context_stack cstk = cu->get_builder ()->pop_context ();
13840
13841 if (*cu->get_builder ()->get_local_symbols () != NULL
13842 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13843 {
13844 struct block *block
13845 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13846 cstk.start_addr, highpc);
13847
13848 /* Note that recording ranges after traversing children, as we
13849 do here, means that recording a parent's ranges entails
13850 walking across all its children's ranges as they appear in
13851 the address map, which is quadratic behavior.
13852
13853 It would be nicer to record the parent's ranges before
13854 traversing its children, simply overriding whatever you find
13855 there. But since we don't even decide whether to create a
13856 block until after we've traversed its children, that's hard
13857 to do. */
13858 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13859 }
13860 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13861 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13862 }
13863
13864 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13865
13866 static void
13867 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13868 {
13869 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13870 struct objfile *objfile = per_objfile->objfile;
13871 struct gdbarch *gdbarch = objfile->arch ();
13872 CORE_ADDR pc, baseaddr;
13873 struct attribute *attr;
13874 struct call_site *call_site, call_site_local;
13875 void **slot;
13876 int nparams;
13877 struct die_info *child_die;
13878
13879 baseaddr = objfile->text_section_offset ();
13880
13881 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13882 if (attr == NULL)
13883 {
13884 /* This was a pre-DWARF-5 GNU extension alias
13885 for DW_AT_call_return_pc. */
13886 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13887 }
13888 if (!attr)
13889 {
13890 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13891 "DIE %s [in module %s]"),
13892 sect_offset_str (die->sect_off), objfile_name (objfile));
13893 return;
13894 }
13895 pc = attr->as_address () + baseaddr;
13896 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13897
13898 if (cu->call_site_htab == NULL)
13899 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13900 NULL, &objfile->objfile_obstack,
13901 hashtab_obstack_allocate, NULL);
13902 call_site_local.pc = pc;
13903 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13904 if (*slot != NULL)
13905 {
13906 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13907 "DIE %s [in module %s]"),
13908 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13909 objfile_name (objfile));
13910 return;
13911 }
13912
13913 /* Count parameters at the caller. */
13914
13915 nparams = 0;
13916 for (child_die = die->child; child_die && child_die->tag;
13917 child_die = child_die->sibling)
13918 {
13919 if (child_die->tag != DW_TAG_call_site_parameter
13920 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13921 {
13922 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13923 "DW_TAG_call_site child DIE %s [in module %s]"),
13924 child_die->tag, sect_offset_str (child_die->sect_off),
13925 objfile_name (objfile));
13926 continue;
13927 }
13928
13929 nparams++;
13930 }
13931
13932 call_site
13933 = ((struct call_site *)
13934 obstack_alloc (&objfile->objfile_obstack,
13935 sizeof (*call_site)
13936 + (sizeof (*call_site->parameter) * (nparams - 1))));
13937 *slot = call_site;
13938 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13939 call_site->pc = pc;
13940
13941 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13942 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13943 {
13944 struct die_info *func_die;
13945
13946 /* Skip also over DW_TAG_inlined_subroutine. */
13947 for (func_die = die->parent;
13948 func_die && func_die->tag != DW_TAG_subprogram
13949 && func_die->tag != DW_TAG_subroutine_type;
13950 func_die = func_die->parent);
13951
13952 /* DW_AT_call_all_calls is a superset
13953 of DW_AT_call_all_tail_calls. */
13954 if (func_die
13955 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13956 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13957 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13958 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13959 {
13960 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13961 not complete. But keep CALL_SITE for look ups via call_site_htab,
13962 both the initial caller containing the real return address PC and
13963 the final callee containing the current PC of a chain of tail
13964 calls do not need to have the tail call list complete. But any
13965 function candidate for a virtual tail call frame searched via
13966 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13967 determined unambiguously. */
13968 }
13969 else
13970 {
13971 struct type *func_type = NULL;
13972
13973 if (func_die)
13974 func_type = get_die_type (func_die, cu);
13975 if (func_type != NULL)
13976 {
13977 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13978
13979 /* Enlist this call site to the function. */
13980 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13981 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13982 }
13983 else
13984 complaint (_("Cannot find function owning DW_TAG_call_site "
13985 "DIE %s [in module %s]"),
13986 sect_offset_str (die->sect_off), objfile_name (objfile));
13987 }
13988 }
13989
13990 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13991 if (attr == NULL)
13992 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13993 if (attr == NULL)
13994 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13995 if (attr == NULL)
13996 {
13997 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13998 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13999 }
14000 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14001 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
14002 /* Keep NULL DWARF_BLOCK. */;
14003 else if (attr->form_is_block ())
14004 {
14005 struct dwarf2_locexpr_baton *dlbaton;
14006 struct dwarf_block *block = attr->as_block ();
14007
14008 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14009 dlbaton->data = block->data;
14010 dlbaton->size = block->size;
14011 dlbaton->per_objfile = per_objfile;
14012 dlbaton->per_cu = cu->per_cu;
14013
14014 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14015 }
14016 else if (attr->form_is_ref ())
14017 {
14018 struct dwarf2_cu *target_cu = cu;
14019 struct die_info *target_die;
14020
14021 target_die = follow_die_ref (die, attr, &target_cu);
14022 gdb_assert (target_cu->per_objfile->objfile == objfile);
14023 if (die_is_declaration (target_die, target_cu))
14024 {
14025 const char *target_physname;
14026
14027 /* Prefer the mangled name; otherwise compute the demangled one. */
14028 target_physname = dw2_linkage_name (target_die, target_cu);
14029 if (target_physname == NULL)
14030 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14031 if (target_physname == NULL)
14032 complaint (_("DW_AT_call_target target DIE has invalid "
14033 "physname, for referencing DIE %s [in module %s]"),
14034 sect_offset_str (die->sect_off), objfile_name (objfile));
14035 else
14036 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14037 }
14038 else
14039 {
14040 CORE_ADDR lowpc;
14041
14042 /* DW_AT_entry_pc should be preferred. */
14043 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14044 <= PC_BOUNDS_INVALID)
14045 complaint (_("DW_AT_call_target target DIE has invalid "
14046 "low pc, for referencing DIE %s [in module %s]"),
14047 sect_offset_str (die->sect_off), objfile_name (objfile));
14048 else
14049 {
14050 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14051 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14052 }
14053 }
14054 }
14055 else
14056 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14057 "block nor reference, for DIE %s [in module %s]"),
14058 sect_offset_str (die->sect_off), objfile_name (objfile));
14059
14060 call_site->per_cu = cu->per_cu;
14061 call_site->per_objfile = per_objfile;
14062
14063 for (child_die = die->child;
14064 child_die && child_die->tag;
14065 child_die = child_die->sibling)
14066 {
14067 struct call_site_parameter *parameter;
14068 struct attribute *loc, *origin;
14069
14070 if (child_die->tag != DW_TAG_call_site_parameter
14071 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14072 {
14073 /* Already printed the complaint above. */
14074 continue;
14075 }
14076
14077 gdb_assert (call_site->parameter_count < nparams);
14078 parameter = &call_site->parameter[call_site->parameter_count];
14079
14080 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14081 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14082 register is contained in DW_AT_call_value. */
14083
14084 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14085 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14086 if (origin == NULL)
14087 {
14088 /* This was a pre-DWARF-5 GNU extension alias
14089 for DW_AT_call_parameter. */
14090 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14091 }
14092 if (loc == NULL && origin != NULL && origin->form_is_ref ())
14093 {
14094 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14095
14096 sect_offset sect_off = origin->get_ref_die_offset ();
14097 if (!cu->header.offset_in_cu_p (sect_off))
14098 {
14099 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14100 binding can be done only inside one CU. Such referenced DIE
14101 therefore cannot be even moved to DW_TAG_partial_unit. */
14102 complaint (_("DW_AT_call_parameter offset is not in CU for "
14103 "DW_TAG_call_site child DIE %s [in module %s]"),
14104 sect_offset_str (child_die->sect_off),
14105 objfile_name (objfile));
14106 continue;
14107 }
14108 parameter->u.param_cu_off
14109 = (cu_offset) (sect_off - cu->header.sect_off);
14110 }
14111 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
14112 {
14113 complaint (_("No DW_FORM_block* DW_AT_location for "
14114 "DW_TAG_call_site child DIE %s [in module %s]"),
14115 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14116 continue;
14117 }
14118 else
14119 {
14120 struct dwarf_block *block = loc->as_block ();
14121
14122 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14123 (block->data, &block->data[block->size]);
14124 if (parameter->u.dwarf_reg != -1)
14125 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14126 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
14127 &block->data[block->size],
14128 &parameter->u.fb_offset))
14129 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14130 else
14131 {
14132 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14133 "for DW_FORM_block* DW_AT_location is supported for "
14134 "DW_TAG_call_site child DIE %s "
14135 "[in module %s]"),
14136 sect_offset_str (child_die->sect_off),
14137 objfile_name (objfile));
14138 continue;
14139 }
14140 }
14141
14142 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14143 if (attr == NULL)
14144 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14145 if (attr == NULL || !attr->form_is_block ())
14146 {
14147 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14148 "DW_TAG_call_site child DIE %s [in module %s]"),
14149 sect_offset_str (child_die->sect_off),
14150 objfile_name (objfile));
14151 continue;
14152 }
14153
14154 struct dwarf_block *block = attr->as_block ();
14155 parameter->value = block->data;
14156 parameter->value_size = block->size;
14157
14158 /* Parameters are not pre-cleared by memset above. */
14159 parameter->data_value = NULL;
14160 parameter->data_value_size = 0;
14161 call_site->parameter_count++;
14162
14163 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14164 if (attr == NULL)
14165 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14166 if (attr != nullptr)
14167 {
14168 if (!attr->form_is_block ())
14169 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14170 "DW_TAG_call_site child DIE %s [in module %s]"),
14171 sect_offset_str (child_die->sect_off),
14172 objfile_name (objfile));
14173 else
14174 {
14175 block = attr->as_block ();
14176 parameter->data_value = block->data;
14177 parameter->data_value_size = block->size;
14178 }
14179 }
14180 }
14181 }
14182
14183 /* Helper function for read_variable. If DIE represents a virtual
14184 table, then return the type of the concrete object that is
14185 associated with the virtual table. Otherwise, return NULL. */
14186
14187 static struct type *
14188 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14189 {
14190 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14191 if (attr == NULL)
14192 return NULL;
14193
14194 /* Find the type DIE. */
14195 struct die_info *type_die = NULL;
14196 struct dwarf2_cu *type_cu = cu;
14197
14198 if (attr->form_is_ref ())
14199 type_die = follow_die_ref (die, attr, &type_cu);
14200 if (type_die == NULL)
14201 return NULL;
14202
14203 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14204 return NULL;
14205 return die_containing_type (type_die, type_cu);
14206 }
14207
14208 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14209
14210 static void
14211 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14212 {
14213 struct rust_vtable_symbol *storage = NULL;
14214
14215 if (cu->language == language_rust)
14216 {
14217 struct type *containing_type = rust_containing_type (die, cu);
14218
14219 if (containing_type != NULL)
14220 {
14221 struct objfile *objfile = cu->per_objfile->objfile;
14222
14223 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
14224 storage->concrete_type = containing_type;
14225 storage->subclass = SYMBOL_RUST_VTABLE;
14226 }
14227 }
14228
14229 struct symbol *res = new_symbol (die, NULL, cu, storage);
14230 struct attribute *abstract_origin
14231 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14232 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14233 if (res == NULL && loc && abstract_origin)
14234 {
14235 /* We have a variable without a name, but with a location and an abstract
14236 origin. This may be a concrete instance of an abstract variable
14237 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14238 later. */
14239 struct dwarf2_cu *origin_cu = cu;
14240 struct die_info *origin_die
14241 = follow_die_ref (die, abstract_origin, &origin_cu);
14242 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14243 per_objfile->per_bfd->abstract_to_concrete
14244 [origin_die->sect_off].push_back (die->sect_off);
14245 }
14246 }
14247
14248 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14249 reading .debug_rnglists.
14250 Callback's type should be:
14251 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14252 Return true if the attributes are present and valid, otherwise,
14253 return false. */
14254
14255 template <typename Callback>
14256 static bool
14257 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14258 dwarf_tag tag, Callback &&callback)
14259 {
14260 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14261 struct objfile *objfile = per_objfile->objfile;
14262 bfd *obfd = objfile->obfd;
14263 /* Base address selection entry. */
14264 gdb::optional<CORE_ADDR> base;
14265 const gdb_byte *buffer;
14266 CORE_ADDR baseaddr;
14267 bool overflow = false;
14268 ULONGEST addr_index;
14269 struct dwarf2_section_info *rnglists_section;
14270
14271 base = cu->base_address;
14272 rnglists_section = cu_debug_rnglists_section (cu, tag);
14273 rnglists_section->read (objfile);
14274
14275 if (offset >= rnglists_section->size)
14276 {
14277 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14278 offset);
14279 return false;
14280 }
14281 buffer = rnglists_section->buffer + offset;
14282
14283 baseaddr = objfile->text_section_offset ();
14284
14285 while (1)
14286 {
14287 /* Initialize it due to a false compiler warning. */
14288 CORE_ADDR range_beginning = 0, range_end = 0;
14289 const gdb_byte *buf_end = (rnglists_section->buffer
14290 + rnglists_section->size);
14291 unsigned int bytes_read;
14292
14293 if (buffer == buf_end)
14294 {
14295 overflow = true;
14296 break;
14297 }
14298 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14299 switch (rlet)
14300 {
14301 case DW_RLE_end_of_list:
14302 break;
14303 case DW_RLE_base_address:
14304 if (buffer + cu->header.addr_size > buf_end)
14305 {
14306 overflow = true;
14307 break;
14308 }
14309 base = cu->header.read_address (obfd, buffer, &bytes_read);
14310 buffer += bytes_read;
14311 break;
14312 case DW_RLE_base_addressx:
14313 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14314 buffer += bytes_read;
14315 base = read_addr_index (cu, addr_index);
14316 break;
14317 case DW_RLE_start_length:
14318 if (buffer + cu->header.addr_size > buf_end)
14319 {
14320 overflow = true;
14321 break;
14322 }
14323 range_beginning = cu->header.read_address (obfd, buffer,
14324 &bytes_read);
14325 buffer += bytes_read;
14326 range_end = (range_beginning
14327 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14328 buffer += bytes_read;
14329 if (buffer > buf_end)
14330 {
14331 overflow = true;
14332 break;
14333 }
14334 break;
14335 case DW_RLE_startx_length:
14336 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14337 buffer += bytes_read;
14338 range_beginning = read_addr_index (cu, addr_index);
14339 if (buffer > buf_end)
14340 {
14341 overflow = true;
14342 break;
14343 }
14344 range_end = (range_beginning
14345 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14346 buffer += bytes_read;
14347 break;
14348 case DW_RLE_offset_pair:
14349 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14350 buffer += bytes_read;
14351 if (buffer > buf_end)
14352 {
14353 overflow = true;
14354 break;
14355 }
14356 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14357 buffer += bytes_read;
14358 if (buffer > buf_end)
14359 {
14360 overflow = true;
14361 break;
14362 }
14363 break;
14364 case DW_RLE_start_end:
14365 if (buffer + 2 * cu->header.addr_size > buf_end)
14366 {
14367 overflow = true;
14368 break;
14369 }
14370 range_beginning = cu->header.read_address (obfd, buffer,
14371 &bytes_read);
14372 buffer += bytes_read;
14373 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
14374 buffer += bytes_read;
14375 break;
14376 case DW_RLE_startx_endx:
14377 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14378 buffer += bytes_read;
14379 range_beginning = read_addr_index (cu, addr_index);
14380 if (buffer > buf_end)
14381 {
14382 overflow = true;
14383 break;
14384 }
14385 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14386 buffer += bytes_read;
14387 range_end = read_addr_index (cu, addr_index);
14388 break;
14389 default:
14390 complaint (_("Invalid .debug_rnglists data (no base address)"));
14391 return false;
14392 }
14393 if (rlet == DW_RLE_end_of_list || overflow)
14394 break;
14395 if (rlet == DW_RLE_base_address)
14396 continue;
14397
14398 if (range_beginning > range_end)
14399 {
14400 /* Inverted range entries are invalid. */
14401 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14402 return false;
14403 }
14404
14405 /* Empty range entries have no effect. */
14406 if (range_beginning == range_end)
14407 continue;
14408
14409 /* Only DW_RLE_offset_pair needs the base address added. */
14410 if (rlet == DW_RLE_offset_pair)
14411 {
14412 if (!base.has_value ())
14413 {
14414 /* We have no valid base address for the DW_RLE_offset_pair. */
14415 complaint (_("Invalid .debug_rnglists data (no base address for "
14416 "DW_RLE_offset_pair)"));
14417 return false;
14418 }
14419
14420 range_beginning += *base;
14421 range_end += *base;
14422 }
14423
14424 /* A not-uncommon case of bad debug info.
14425 Don't pollute the addrmap with bad data. */
14426 if (range_beginning + baseaddr == 0
14427 && !per_objfile->per_bfd->has_section_at_zero)
14428 {
14429 complaint (_(".debug_rnglists entry has start address of zero"
14430 " [in module %s]"), objfile_name (objfile));
14431 continue;
14432 }
14433
14434 callback (range_beginning, range_end);
14435 }
14436
14437 if (overflow)
14438 {
14439 complaint (_("Offset %d is not terminated "
14440 "for DW_AT_ranges attribute"),
14441 offset);
14442 return false;
14443 }
14444
14445 return true;
14446 }
14447
14448 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14449 Callback's type should be:
14450 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14451 Return 1 if the attributes are present and valid, otherwise, return 0. */
14452
14453 template <typename Callback>
14454 static int
14455 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
14456 Callback &&callback)
14457 {
14458 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14459 struct objfile *objfile = per_objfile->objfile;
14460 struct comp_unit_head *cu_header = &cu->header;
14461 bfd *obfd = objfile->obfd;
14462 unsigned int addr_size = cu_header->addr_size;
14463 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14464 /* Base address selection entry. */
14465 gdb::optional<CORE_ADDR> base;
14466 unsigned int dummy;
14467 const gdb_byte *buffer;
14468 CORE_ADDR baseaddr;
14469
14470 if (cu_header->version >= 5)
14471 return dwarf2_rnglists_process (offset, cu, tag, callback);
14472
14473 base = cu->base_address;
14474
14475 per_objfile->per_bfd->ranges.read (objfile);
14476 if (offset >= per_objfile->per_bfd->ranges.size)
14477 {
14478 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14479 offset);
14480 return 0;
14481 }
14482 buffer = per_objfile->per_bfd->ranges.buffer + offset;
14483
14484 baseaddr = objfile->text_section_offset ();
14485
14486 while (1)
14487 {
14488 CORE_ADDR range_beginning, range_end;
14489
14490 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
14491 buffer += addr_size;
14492 range_end = cu->header.read_address (obfd, buffer, &dummy);
14493 buffer += addr_size;
14494 offset += 2 * addr_size;
14495
14496 /* An end of list marker is a pair of zero addresses. */
14497 if (range_beginning == 0 && range_end == 0)
14498 /* Found the end of list entry. */
14499 break;
14500
14501 /* Each base address selection entry is a pair of 2 values.
14502 The first is the largest possible address, the second is
14503 the base address. Check for a base address here. */
14504 if ((range_beginning & mask) == mask)
14505 {
14506 /* If we found the largest possible address, then we already
14507 have the base address in range_end. */
14508 base = range_end;
14509 continue;
14510 }
14511
14512 if (!base.has_value ())
14513 {
14514 /* We have no valid base address for the ranges
14515 data. */
14516 complaint (_("Invalid .debug_ranges data (no base address)"));
14517 return 0;
14518 }
14519
14520 if (range_beginning > range_end)
14521 {
14522 /* Inverted range entries are invalid. */
14523 complaint (_("Invalid .debug_ranges data (inverted range)"));
14524 return 0;
14525 }
14526
14527 /* Empty range entries have no effect. */
14528 if (range_beginning == range_end)
14529 continue;
14530
14531 range_beginning += *base;
14532 range_end += *base;
14533
14534 /* A not-uncommon case of bad debug info.
14535 Don't pollute the addrmap with bad data. */
14536 if (range_beginning + baseaddr == 0
14537 && !per_objfile->per_bfd->has_section_at_zero)
14538 {
14539 complaint (_(".debug_ranges entry has start address of zero"
14540 " [in module %s]"), objfile_name (objfile));
14541 continue;
14542 }
14543
14544 callback (range_beginning, range_end);
14545 }
14546
14547 return 1;
14548 }
14549
14550 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14551 Return 1 if the attributes are present and valid, otherwise, return 0.
14552 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14553
14554 static int
14555 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14556 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14557 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
14558 {
14559 struct objfile *objfile = cu->per_objfile->objfile;
14560 struct gdbarch *gdbarch = objfile->arch ();
14561 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14562 int low_set = 0;
14563 CORE_ADDR low = 0;
14564 CORE_ADDR high = 0;
14565 int retval;
14566
14567 retval = dwarf2_ranges_process (offset, cu, tag,
14568 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14569 {
14570 if (ranges_pst != NULL)
14571 {
14572 CORE_ADDR lowpc;
14573 CORE_ADDR highpc;
14574
14575 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14576 range_beginning + baseaddr)
14577 - baseaddr);
14578 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14579 range_end + baseaddr)
14580 - baseaddr);
14581 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14582 lowpc, highpc - 1, ranges_pst);
14583 }
14584
14585 /* FIXME: This is recording everything as a low-high
14586 segment of consecutive addresses. We should have a
14587 data structure for discontiguous block ranges
14588 instead. */
14589 if (! low_set)
14590 {
14591 low = range_beginning;
14592 high = range_end;
14593 low_set = 1;
14594 }
14595 else
14596 {
14597 if (range_beginning < low)
14598 low = range_beginning;
14599 if (range_end > high)
14600 high = range_end;
14601 }
14602 });
14603 if (!retval)
14604 return 0;
14605
14606 if (! low_set)
14607 /* If the first entry is an end-of-list marker, the range
14608 describes an empty scope, i.e. no instructions. */
14609 return 0;
14610
14611 if (low_return)
14612 *low_return = low;
14613 if (high_return)
14614 *high_return = high;
14615 return 1;
14616 }
14617
14618 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14619 definition for the return value. *LOWPC and *HIGHPC are set iff
14620 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14621
14622 static enum pc_bounds_kind
14623 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14624 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14625 dwarf2_psymtab *pst)
14626 {
14627 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14628 struct attribute *attr;
14629 struct attribute *attr_high;
14630 CORE_ADDR low = 0;
14631 CORE_ADDR high = 0;
14632 enum pc_bounds_kind ret;
14633
14634 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14635 if (attr_high)
14636 {
14637 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14638 if (attr != nullptr)
14639 {
14640 low = attr->as_address ();
14641 high = attr_high->as_address ();
14642 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14643 high += low;
14644 }
14645 else
14646 /* Found high w/o low attribute. */
14647 return PC_BOUNDS_INVALID;
14648
14649 /* Found consecutive range of addresses. */
14650 ret = PC_BOUNDS_HIGH_LOW;
14651 }
14652 else
14653 {
14654 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14655 if (attr != nullptr && attr->form_is_unsigned ())
14656 {
14657 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14658 We take advantage of the fact that DW_AT_ranges does not appear
14659 in DW_TAG_compile_unit of DWO files.
14660
14661 Attributes of the form DW_FORM_rnglistx have already had their
14662 value changed by read_rnglist_index and already include
14663 DW_AT_rnglists_base, so don't need to add the ranges base,
14664 either. */
14665 int need_ranges_base = (die->tag != DW_TAG_compile_unit
14666 && attr->form != DW_FORM_rnglistx);
14667 unsigned int ranges_offset = (attr->as_unsigned ()
14668 + (need_ranges_base
14669 ? cu->ranges_base
14670 : 0));
14671
14672 /* Value of the DW_AT_ranges attribute is the offset in the
14673 .debug_ranges section. */
14674 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14675 die->tag))
14676 return PC_BOUNDS_INVALID;
14677 /* Found discontinuous range of addresses. */
14678 ret = PC_BOUNDS_RANGES;
14679 }
14680 else
14681 return PC_BOUNDS_NOT_PRESENT;
14682 }
14683
14684 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14685 if (high <= low)
14686 return PC_BOUNDS_INVALID;
14687
14688 /* When using the GNU linker, .gnu.linkonce. sections are used to
14689 eliminate duplicate copies of functions and vtables and such.
14690 The linker will arbitrarily choose one and discard the others.
14691 The AT_*_pc values for such functions refer to local labels in
14692 these sections. If the section from that file was discarded, the
14693 labels are not in the output, so the relocs get a value of 0.
14694 If this is a discarded function, mark the pc bounds as invalid,
14695 so that GDB will ignore it. */
14696 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14697 return PC_BOUNDS_INVALID;
14698
14699 *lowpc = low;
14700 if (highpc)
14701 *highpc = high;
14702 return ret;
14703 }
14704
14705 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14706 its low and high PC addresses. Do nothing if these addresses could not
14707 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14708 and HIGHPC to the high address if greater than HIGHPC. */
14709
14710 static void
14711 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14712 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14713 struct dwarf2_cu *cu)
14714 {
14715 CORE_ADDR low, high;
14716 struct die_info *child = die->child;
14717
14718 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14719 {
14720 *lowpc = std::min (*lowpc, low);
14721 *highpc = std::max (*highpc, high);
14722 }
14723
14724 /* If the language does not allow nested subprograms (either inside
14725 subprograms or lexical blocks), we're done. */
14726 if (cu->language != language_ada)
14727 return;
14728
14729 /* Check all the children of the given DIE. If it contains nested
14730 subprograms, then check their pc bounds. Likewise, we need to
14731 check lexical blocks as well, as they may also contain subprogram
14732 definitions. */
14733 while (child && child->tag)
14734 {
14735 if (child->tag == DW_TAG_subprogram
14736 || child->tag == DW_TAG_lexical_block)
14737 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14738 child = child->sibling;
14739 }
14740 }
14741
14742 /* Get the low and high pc's represented by the scope DIE, and store
14743 them in *LOWPC and *HIGHPC. If the correct values can't be
14744 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14745
14746 static void
14747 get_scope_pc_bounds (struct die_info *die,
14748 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14749 struct dwarf2_cu *cu)
14750 {
14751 CORE_ADDR best_low = (CORE_ADDR) -1;
14752 CORE_ADDR best_high = (CORE_ADDR) 0;
14753 CORE_ADDR current_low, current_high;
14754
14755 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14756 >= PC_BOUNDS_RANGES)
14757 {
14758 best_low = current_low;
14759 best_high = current_high;
14760 }
14761 else
14762 {
14763 struct die_info *child = die->child;
14764
14765 while (child && child->tag)
14766 {
14767 switch (child->tag) {
14768 case DW_TAG_subprogram:
14769 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14770 break;
14771 case DW_TAG_namespace:
14772 case DW_TAG_module:
14773 /* FIXME: carlton/2004-01-16: Should we do this for
14774 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14775 that current GCC's always emit the DIEs corresponding
14776 to definitions of methods of classes as children of a
14777 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14778 the DIEs giving the declarations, which could be
14779 anywhere). But I don't see any reason why the
14780 standards says that they have to be there. */
14781 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14782
14783 if (current_low != ((CORE_ADDR) -1))
14784 {
14785 best_low = std::min (best_low, current_low);
14786 best_high = std::max (best_high, current_high);
14787 }
14788 break;
14789 default:
14790 /* Ignore. */
14791 break;
14792 }
14793
14794 child = child->sibling;
14795 }
14796 }
14797
14798 *lowpc = best_low;
14799 *highpc = best_high;
14800 }
14801
14802 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14803 in DIE. */
14804
14805 static void
14806 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14807 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14808 {
14809 struct objfile *objfile = cu->per_objfile->objfile;
14810 struct gdbarch *gdbarch = objfile->arch ();
14811 struct attribute *attr;
14812 struct attribute *attr_high;
14813
14814 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14815 if (attr_high)
14816 {
14817 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14818 if (attr != nullptr)
14819 {
14820 CORE_ADDR low = attr->as_address ();
14821 CORE_ADDR high = attr_high->as_address ();
14822
14823 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14824 high += low;
14825
14826 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14827 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14828 cu->get_builder ()->record_block_range (block, low, high - 1);
14829 }
14830 }
14831
14832 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14833 if (attr != nullptr && attr->form_is_unsigned ())
14834 {
14835 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14836 We take advantage of the fact that DW_AT_ranges does not appear
14837 in DW_TAG_compile_unit of DWO files.
14838
14839 Attributes of the form DW_FORM_rnglistx have already had their
14840 value changed by read_rnglist_index and already include
14841 DW_AT_rnglists_base, so don't need to add the ranges base,
14842 either. */
14843 int need_ranges_base = (die->tag != DW_TAG_compile_unit
14844 && attr->form != DW_FORM_rnglistx);
14845
14846 /* The value of the DW_AT_ranges attribute is the offset of the
14847 address range list in the .debug_ranges section. */
14848 unsigned long offset = (attr->as_unsigned ()
14849 + (need_ranges_base ? cu->ranges_base : 0));
14850
14851 std::vector<blockrange> blockvec;
14852 dwarf2_ranges_process (offset, cu, die->tag,
14853 [&] (CORE_ADDR start, CORE_ADDR end)
14854 {
14855 start += baseaddr;
14856 end += baseaddr;
14857 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14858 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14859 cu->get_builder ()->record_block_range (block, start, end - 1);
14860 blockvec.emplace_back (start, end);
14861 });
14862
14863 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14864 }
14865 }
14866
14867 /* Check whether the producer field indicates either of GCC < 4.6, or the
14868 Intel C/C++ compiler, and cache the result in CU. */
14869
14870 static void
14871 check_producer (struct dwarf2_cu *cu)
14872 {
14873 int major, minor;
14874
14875 if (cu->producer == NULL)
14876 {
14877 /* For unknown compilers expect their behavior is DWARF version
14878 compliant.
14879
14880 GCC started to support .debug_types sections by -gdwarf-4 since
14881 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14882 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14883 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14884 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14885 }
14886 else if (producer_is_gcc (cu->producer, &major, &minor))
14887 {
14888 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14889 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14890 }
14891 else if (producer_is_icc (cu->producer, &major, &minor))
14892 {
14893 cu->producer_is_icc = true;
14894 cu->producer_is_icc_lt_14 = major < 14;
14895 }
14896 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14897 cu->producer_is_codewarrior = true;
14898 else
14899 {
14900 /* For other non-GCC compilers, expect their behavior is DWARF version
14901 compliant. */
14902 }
14903
14904 cu->checked_producer = true;
14905 }
14906
14907 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14908 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14909 during 4.6.0 experimental. */
14910
14911 static bool
14912 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14913 {
14914 if (!cu->checked_producer)
14915 check_producer (cu);
14916
14917 return cu->producer_is_gxx_lt_4_6;
14918 }
14919
14920
14921 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14922 with incorrect is_stmt attributes. */
14923
14924 static bool
14925 producer_is_codewarrior (struct dwarf2_cu *cu)
14926 {
14927 if (!cu->checked_producer)
14928 check_producer (cu);
14929
14930 return cu->producer_is_codewarrior;
14931 }
14932
14933 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14934 If that attribute is not available, return the appropriate
14935 default. */
14936
14937 static enum dwarf_access_attribute
14938 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14939 {
14940 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14941 if (attr != nullptr)
14942 {
14943 LONGEST value = attr->constant_value (-1);
14944 if (value == DW_ACCESS_public
14945 || value == DW_ACCESS_protected
14946 || value == DW_ACCESS_private)
14947 return (dwarf_access_attribute) value;
14948 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14949 plongest (value));
14950 }
14951
14952 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14953 {
14954 /* The default DWARF 2 accessibility for members is public, the default
14955 accessibility for inheritance is private. */
14956
14957 if (die->tag != DW_TAG_inheritance)
14958 return DW_ACCESS_public;
14959 else
14960 return DW_ACCESS_private;
14961 }
14962 else
14963 {
14964 /* DWARF 3+ defines the default accessibility a different way. The same
14965 rules apply now for DW_TAG_inheritance as for the members and it only
14966 depends on the container kind. */
14967
14968 if (die->parent->tag == DW_TAG_class_type)
14969 return DW_ACCESS_private;
14970 else
14971 return DW_ACCESS_public;
14972 }
14973 }
14974
14975 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14976 offset. If the attribute was not found return 0, otherwise return
14977 1. If it was found but could not properly be handled, set *OFFSET
14978 to 0. */
14979
14980 static int
14981 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14982 LONGEST *offset)
14983 {
14984 struct attribute *attr;
14985
14986 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14987 if (attr != NULL)
14988 {
14989 *offset = 0;
14990
14991 /* Note that we do not check for a section offset first here.
14992 This is because DW_AT_data_member_location is new in DWARF 4,
14993 so if we see it, we can assume that a constant form is really
14994 a constant and not a section offset. */
14995 if (attr->form_is_constant ())
14996 *offset = attr->constant_value (0);
14997 else if (attr->form_is_section_offset ())
14998 dwarf2_complex_location_expr_complaint ();
14999 else if (attr->form_is_block ())
15000 *offset = decode_locdesc (attr->as_block (), cu);
15001 else
15002 dwarf2_complex_location_expr_complaint ();
15003
15004 return 1;
15005 }
15006
15007 return 0;
15008 }
15009
15010 /* Look for DW_AT_data_member_location and store the results in FIELD. */
15011
15012 static void
15013 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15014 struct field *field)
15015 {
15016 struct attribute *attr;
15017
15018 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15019 if (attr != NULL)
15020 {
15021 if (attr->form_is_constant ())
15022 {
15023 LONGEST offset = attr->constant_value (0);
15024 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
15025 }
15026 else if (attr->form_is_section_offset ())
15027 dwarf2_complex_location_expr_complaint ();
15028 else if (attr->form_is_block ())
15029 {
15030 bool handled;
15031 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
15032 if (handled)
15033 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
15034 else
15035 {
15036 dwarf2_per_objfile *per_objfile = cu->per_objfile;
15037 struct objfile *objfile = per_objfile->objfile;
15038 struct dwarf2_locexpr_baton *dlbaton
15039 = XOBNEW (&objfile->objfile_obstack,
15040 struct dwarf2_locexpr_baton);
15041 dlbaton->data = attr->as_block ()->data;
15042 dlbaton->size = attr->as_block ()->size;
15043 /* When using this baton, we want to compute the address
15044 of the field, not the value. This is why
15045 is_reference is set to false here. */
15046 dlbaton->is_reference = false;
15047 dlbaton->per_objfile = per_objfile;
15048 dlbaton->per_cu = cu->per_cu;
15049
15050 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
15051 }
15052 }
15053 else
15054 dwarf2_complex_location_expr_complaint ();
15055 }
15056 }
15057
15058 /* Add an aggregate field to the field list. */
15059
15060 static void
15061 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15062 struct dwarf2_cu *cu)
15063 {
15064 struct objfile *objfile = cu->per_objfile->objfile;
15065 struct gdbarch *gdbarch = objfile->arch ();
15066 struct nextfield *new_field;
15067 struct attribute *attr;
15068 struct field *fp;
15069 const char *fieldname = "";
15070
15071 if (die->tag == DW_TAG_inheritance)
15072 {
15073 fip->baseclasses.emplace_back ();
15074 new_field = &fip->baseclasses.back ();
15075 }
15076 else
15077 {
15078 fip->fields.emplace_back ();
15079 new_field = &fip->fields.back ();
15080 }
15081
15082 new_field->offset = die->sect_off;
15083
15084 new_field->accessibility = dwarf2_access_attribute (die, cu);
15085 if (new_field->accessibility != DW_ACCESS_public)
15086 fip->non_public_fields = true;
15087
15088 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15089 if (attr != nullptr)
15090 new_field->virtuality = attr->as_virtuality ();
15091 else
15092 new_field->virtuality = DW_VIRTUALITY_none;
15093
15094 fp = &new_field->field;
15095
15096 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15097 {
15098 /* Data member other than a C++ static data member. */
15099
15100 /* Get type of field. */
15101 fp->set_type (die_type (die, cu));
15102
15103 SET_FIELD_BITPOS (*fp, 0);
15104
15105 /* Get bit size of field (zero if none). */
15106 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15107 if (attr != nullptr)
15108 {
15109 FIELD_BITSIZE (*fp) = attr->constant_value (0);
15110 }
15111 else
15112 {
15113 FIELD_BITSIZE (*fp) = 0;
15114 }
15115
15116 /* Get bit offset of field. */
15117 handle_data_member_location (die, cu, fp);
15118 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15119 if (attr != nullptr && attr->form_is_constant ())
15120 {
15121 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15122 {
15123 /* For big endian bits, the DW_AT_bit_offset gives the
15124 additional bit offset from the MSB of the containing
15125 anonymous object to the MSB of the field. We don't
15126 have to do anything special since we don't need to
15127 know the size of the anonymous object. */
15128 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15129 + attr->constant_value (0)));
15130 }
15131 else
15132 {
15133 /* For little endian bits, compute the bit offset to the
15134 MSB of the anonymous object, subtract off the number of
15135 bits from the MSB of the field to the MSB of the
15136 object, and then subtract off the number of bits of
15137 the field itself. The result is the bit offset of
15138 the LSB of the field. */
15139 int anonymous_size;
15140 int bit_offset = attr->constant_value (0);
15141
15142 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15143 if (attr != nullptr && attr->form_is_constant ())
15144 {
15145 /* The size of the anonymous object containing
15146 the bit field is explicit, so use the
15147 indicated size (in bytes). */
15148 anonymous_size = attr->constant_value (0);
15149 }
15150 else
15151 {
15152 /* The size of the anonymous object containing
15153 the bit field must be inferred from the type
15154 attribute of the data member containing the
15155 bit field. */
15156 anonymous_size = TYPE_LENGTH (fp->type ());
15157 }
15158 SET_FIELD_BITPOS (*fp,
15159 (FIELD_BITPOS (*fp)
15160 + anonymous_size * bits_per_byte
15161 - bit_offset - FIELD_BITSIZE (*fp)));
15162 }
15163 }
15164 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15165 if (attr != NULL)
15166 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15167 + attr->constant_value (0)));
15168
15169 /* Get name of field. */
15170 fieldname = dwarf2_name (die, cu);
15171 if (fieldname == NULL)
15172 fieldname = "";
15173
15174 /* The name is already allocated along with this objfile, so we don't
15175 need to duplicate it for the type. */
15176 fp->name = fieldname;
15177
15178 /* Change accessibility for artificial fields (e.g. virtual table
15179 pointer or virtual base class pointer) to private. */
15180 if (dwarf2_attr (die, DW_AT_artificial, cu))
15181 {
15182 FIELD_ARTIFICIAL (*fp) = 1;
15183 new_field->accessibility = DW_ACCESS_private;
15184 fip->non_public_fields = true;
15185 }
15186 }
15187 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15188 {
15189 /* C++ static member. */
15190
15191 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15192 is a declaration, but all versions of G++ as of this writing
15193 (so through at least 3.2.1) incorrectly generate
15194 DW_TAG_variable tags. */
15195
15196 const char *physname;
15197
15198 /* Get name of field. */
15199 fieldname = dwarf2_name (die, cu);
15200 if (fieldname == NULL)
15201 return;
15202
15203 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15204 if (attr
15205 /* Only create a symbol if this is an external value.
15206 new_symbol checks this and puts the value in the global symbol
15207 table, which we want. If it is not external, new_symbol
15208 will try to put the value in cu->list_in_scope which is wrong. */
15209 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15210 {
15211 /* A static const member, not much different than an enum as far as
15212 we're concerned, except that we can support more types. */
15213 new_symbol (die, NULL, cu);
15214 }
15215
15216 /* Get physical name. */
15217 physname = dwarf2_physname (fieldname, die, cu);
15218
15219 /* The name is already allocated along with this objfile, so we don't
15220 need to duplicate it for the type. */
15221 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15222 fp->set_type (die_type (die, cu));
15223 FIELD_NAME (*fp) = fieldname;
15224 }
15225 else if (die->tag == DW_TAG_inheritance)
15226 {
15227 /* C++ base class field. */
15228 handle_data_member_location (die, cu, fp);
15229 FIELD_BITSIZE (*fp) = 0;
15230 fp->set_type (die_type (die, cu));
15231 FIELD_NAME (*fp) = fp->type ()->name ();
15232 }
15233 else
15234 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15235 }
15236
15237 /* Can the type given by DIE define another type? */
15238
15239 static bool
15240 type_can_define_types (const struct die_info *die)
15241 {
15242 switch (die->tag)
15243 {
15244 case DW_TAG_typedef:
15245 case DW_TAG_class_type:
15246 case DW_TAG_structure_type:
15247 case DW_TAG_union_type:
15248 case DW_TAG_enumeration_type:
15249 return true;
15250
15251 default:
15252 return false;
15253 }
15254 }
15255
15256 /* Add a type definition defined in the scope of the FIP's class. */
15257
15258 static void
15259 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15260 struct dwarf2_cu *cu)
15261 {
15262 struct decl_field fp;
15263 memset (&fp, 0, sizeof (fp));
15264
15265 gdb_assert (type_can_define_types (die));
15266
15267 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15268 fp.name = dwarf2_name (die, cu);
15269 fp.type = read_type_die (die, cu);
15270
15271 /* Save accessibility. */
15272 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15273 switch (accessibility)
15274 {
15275 case DW_ACCESS_public:
15276 /* The assumed value if neither private nor protected. */
15277 break;
15278 case DW_ACCESS_private:
15279 fp.is_private = 1;
15280 break;
15281 case DW_ACCESS_protected:
15282 fp.is_protected = 1;
15283 break;
15284 }
15285
15286 if (die->tag == DW_TAG_typedef)
15287 fip->typedef_field_list.push_back (fp);
15288 else
15289 fip->nested_types_list.push_back (fp);
15290 }
15291
15292 /* A convenience typedef that's used when finding the discriminant
15293 field for a variant part. */
15294 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15295 offset_map_type;
15296
15297 /* Compute the discriminant range for a given variant. OBSTACK is
15298 where the results will be stored. VARIANT is the variant to
15299 process. IS_UNSIGNED indicates whether the discriminant is signed
15300 or unsigned. */
15301
15302 static const gdb::array_view<discriminant_range>
15303 convert_variant_range (struct obstack *obstack, const variant_field &variant,
15304 bool is_unsigned)
15305 {
15306 std::vector<discriminant_range> ranges;
15307
15308 if (variant.default_branch)
15309 return {};
15310
15311 if (variant.discr_list_data == nullptr)
15312 {
15313 discriminant_range r
15314 = {variant.discriminant_value, variant.discriminant_value};
15315 ranges.push_back (r);
15316 }
15317 else
15318 {
15319 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15320 variant.discr_list_data->size);
15321 while (!data.empty ())
15322 {
15323 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15324 {
15325 complaint (_("invalid discriminant marker: %d"), data[0]);
15326 break;
15327 }
15328 bool is_range = data[0] == DW_DSC_range;
15329 data = data.slice (1);
15330
15331 ULONGEST low, high;
15332 unsigned int bytes_read;
15333
15334 if (data.empty ())
15335 {
15336 complaint (_("DW_AT_discr_list missing low value"));
15337 break;
15338 }
15339 if (is_unsigned)
15340 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15341 else
15342 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15343 &bytes_read);
15344 data = data.slice (bytes_read);
15345
15346 if (is_range)
15347 {
15348 if (data.empty ())
15349 {
15350 complaint (_("DW_AT_discr_list missing high value"));
15351 break;
15352 }
15353 if (is_unsigned)
15354 high = read_unsigned_leb128 (nullptr, data.data (),
15355 &bytes_read);
15356 else
15357 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15358 &bytes_read);
15359 data = data.slice (bytes_read);
15360 }
15361 else
15362 high = low;
15363
15364 ranges.push_back ({ low, high });
15365 }
15366 }
15367
15368 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15369 ranges.size ());
15370 std::copy (ranges.begin (), ranges.end (), result);
15371 return gdb::array_view<discriminant_range> (result, ranges.size ());
15372 }
15373
15374 static const gdb::array_view<variant_part> create_variant_parts
15375 (struct obstack *obstack,
15376 const offset_map_type &offset_map,
15377 struct field_info *fi,
15378 const std::vector<variant_part_builder> &variant_parts);
15379
15380 /* Fill in a "struct variant" for a given variant field. RESULT is
15381 the variant to fill in. OBSTACK is where any needed allocations
15382 will be done. OFFSET_MAP holds the mapping from section offsets to
15383 fields for the type. FI describes the fields of the type we're
15384 processing. FIELD is the variant field we're converting. */
15385
15386 static void
15387 create_one_variant (variant &result, struct obstack *obstack,
15388 const offset_map_type &offset_map,
15389 struct field_info *fi, const variant_field &field)
15390 {
15391 result.discriminants = convert_variant_range (obstack, field, false);
15392 result.first_field = field.first_field + fi->baseclasses.size ();
15393 result.last_field = field.last_field + fi->baseclasses.size ();
15394 result.parts = create_variant_parts (obstack, offset_map, fi,
15395 field.variant_parts);
15396 }
15397
15398 /* Fill in a "struct variant_part" for a given variant part. RESULT
15399 is the variant part to fill in. OBSTACK is where any needed
15400 allocations will be done. OFFSET_MAP holds the mapping from
15401 section offsets to fields for the type. FI describes the fields of
15402 the type we're processing. BUILDER is the variant part to be
15403 converted. */
15404
15405 static void
15406 create_one_variant_part (variant_part &result,
15407 struct obstack *obstack,
15408 const offset_map_type &offset_map,
15409 struct field_info *fi,
15410 const variant_part_builder &builder)
15411 {
15412 auto iter = offset_map.find (builder.discriminant_offset);
15413 if (iter == offset_map.end ())
15414 {
15415 result.discriminant_index = -1;
15416 /* Doesn't matter. */
15417 result.is_unsigned = false;
15418 }
15419 else
15420 {
15421 result.discriminant_index = iter->second;
15422 result.is_unsigned
15423 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
15424 }
15425
15426 size_t n = builder.variants.size ();
15427 variant *output = new (obstack) variant[n];
15428 for (size_t i = 0; i < n; ++i)
15429 create_one_variant (output[i], obstack, offset_map, fi,
15430 builder.variants[i]);
15431
15432 result.variants = gdb::array_view<variant> (output, n);
15433 }
15434
15435 /* Create a vector of variant parts that can be attached to a type.
15436 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15437 holds the mapping from section offsets to fields for the type. FI
15438 describes the fields of the type we're processing. VARIANT_PARTS
15439 is the vector to convert. */
15440
15441 static const gdb::array_view<variant_part>
15442 create_variant_parts (struct obstack *obstack,
15443 const offset_map_type &offset_map,
15444 struct field_info *fi,
15445 const std::vector<variant_part_builder> &variant_parts)
15446 {
15447 if (variant_parts.empty ())
15448 return {};
15449
15450 size_t n = variant_parts.size ();
15451 variant_part *result = new (obstack) variant_part[n];
15452 for (size_t i = 0; i < n; ++i)
15453 create_one_variant_part (result[i], obstack, offset_map, fi,
15454 variant_parts[i]);
15455
15456 return gdb::array_view<variant_part> (result, n);
15457 }
15458
15459 /* Compute the variant part vector for FIP, attaching it to TYPE when
15460 done. */
15461
15462 static void
15463 add_variant_property (struct field_info *fip, struct type *type,
15464 struct dwarf2_cu *cu)
15465 {
15466 /* Map section offsets of fields to their field index. Note the
15467 field index here does not take the number of baseclasses into
15468 account. */
15469 offset_map_type offset_map;
15470 for (int i = 0; i < fip->fields.size (); ++i)
15471 offset_map[fip->fields[i].offset] = i;
15472
15473 struct objfile *objfile = cu->per_objfile->objfile;
15474 gdb::array_view<variant_part> parts
15475 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15476 fip->variant_parts);
15477
15478 struct dynamic_prop prop;
15479 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15480 obstack_copy (&objfile->objfile_obstack, &parts,
15481 sizeof (parts)));
15482
15483 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
15484 }
15485
15486 /* Create the vector of fields, and attach it to the type. */
15487
15488 static void
15489 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15490 struct dwarf2_cu *cu)
15491 {
15492 int nfields = fip->nfields ();
15493
15494 /* Record the field count, allocate space for the array of fields,
15495 and create blank accessibility bitfields if necessary. */
15496 type->set_num_fields (nfields);
15497 type->set_fields
15498 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
15499
15500 if (fip->non_public_fields && cu->language != language_ada)
15501 {
15502 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15503
15504 TYPE_FIELD_PRIVATE_BITS (type) =
15505 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15506 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15507
15508 TYPE_FIELD_PROTECTED_BITS (type) =
15509 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15510 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15511
15512 TYPE_FIELD_IGNORE_BITS (type) =
15513 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15514 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15515 }
15516
15517 /* If the type has baseclasses, allocate and clear a bit vector for
15518 TYPE_FIELD_VIRTUAL_BITS. */
15519 if (!fip->baseclasses.empty () && cu->language != language_ada)
15520 {
15521 int num_bytes = B_BYTES (fip->baseclasses.size ());
15522 unsigned char *pointer;
15523
15524 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15525 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15526 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15527 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15528 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15529 }
15530
15531 if (!fip->variant_parts.empty ())
15532 add_variant_property (fip, type, cu);
15533
15534 /* Copy the saved-up fields into the field vector. */
15535 for (int i = 0; i < nfields; ++i)
15536 {
15537 struct nextfield &field
15538 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15539 : fip->fields[i - fip->baseclasses.size ()]);
15540
15541 type->field (i) = field.field;
15542 switch (field.accessibility)
15543 {
15544 case DW_ACCESS_private:
15545 if (cu->language != language_ada)
15546 SET_TYPE_FIELD_PRIVATE (type, i);
15547 break;
15548
15549 case DW_ACCESS_protected:
15550 if (cu->language != language_ada)
15551 SET_TYPE_FIELD_PROTECTED (type, i);
15552 break;
15553
15554 case DW_ACCESS_public:
15555 break;
15556
15557 default:
15558 /* Unknown accessibility. Complain and treat it as public. */
15559 {
15560 complaint (_("unsupported accessibility %d"),
15561 field.accessibility);
15562 }
15563 break;
15564 }
15565 if (i < fip->baseclasses.size ())
15566 {
15567 switch (field.virtuality)
15568 {
15569 case DW_VIRTUALITY_virtual:
15570 case DW_VIRTUALITY_pure_virtual:
15571 if (cu->language == language_ada)
15572 error (_("unexpected virtuality in component of Ada type"));
15573 SET_TYPE_FIELD_VIRTUAL (type, i);
15574 break;
15575 }
15576 }
15577 }
15578 }
15579
15580 /* Return true if this member function is a constructor, false
15581 otherwise. */
15582
15583 static int
15584 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15585 {
15586 const char *fieldname;
15587 const char *type_name;
15588 int len;
15589
15590 if (die->parent == NULL)
15591 return 0;
15592
15593 if (die->parent->tag != DW_TAG_structure_type
15594 && die->parent->tag != DW_TAG_union_type
15595 && die->parent->tag != DW_TAG_class_type)
15596 return 0;
15597
15598 fieldname = dwarf2_name (die, cu);
15599 type_name = dwarf2_name (die->parent, cu);
15600 if (fieldname == NULL || type_name == NULL)
15601 return 0;
15602
15603 len = strlen (fieldname);
15604 return (strncmp (fieldname, type_name, len) == 0
15605 && (type_name[len] == '\0' || type_name[len] == '<'));
15606 }
15607
15608 /* Add a member function to the proper fieldlist. */
15609
15610 static void
15611 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15612 struct type *type, struct dwarf2_cu *cu)
15613 {
15614 struct objfile *objfile = cu->per_objfile->objfile;
15615 struct attribute *attr;
15616 int i;
15617 struct fnfieldlist *flp = nullptr;
15618 struct fn_field *fnp;
15619 const char *fieldname;
15620 struct type *this_type;
15621
15622 if (cu->language == language_ada)
15623 error (_("unexpected member function in Ada type"));
15624
15625 /* Get name of member function. */
15626 fieldname = dwarf2_name (die, cu);
15627 if (fieldname == NULL)
15628 return;
15629
15630 /* Look up member function name in fieldlist. */
15631 for (i = 0; i < fip->fnfieldlists.size (); i++)
15632 {
15633 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15634 {
15635 flp = &fip->fnfieldlists[i];
15636 break;
15637 }
15638 }
15639
15640 /* Create a new fnfieldlist if necessary. */
15641 if (flp == nullptr)
15642 {
15643 fip->fnfieldlists.emplace_back ();
15644 flp = &fip->fnfieldlists.back ();
15645 flp->name = fieldname;
15646 i = fip->fnfieldlists.size () - 1;
15647 }
15648
15649 /* Create a new member function field and add it to the vector of
15650 fnfieldlists. */
15651 flp->fnfields.emplace_back ();
15652 fnp = &flp->fnfields.back ();
15653
15654 /* Delay processing of the physname until later. */
15655 if (cu->language == language_cplus)
15656 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15657 die, cu);
15658 else
15659 {
15660 const char *physname = dwarf2_physname (fieldname, die, cu);
15661 fnp->physname = physname ? physname : "";
15662 }
15663
15664 fnp->type = alloc_type (objfile);
15665 this_type = read_type_die (die, cu);
15666 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15667 {
15668 int nparams = this_type->num_fields ();
15669
15670 /* TYPE is the domain of this method, and THIS_TYPE is the type
15671 of the method itself (TYPE_CODE_METHOD). */
15672 smash_to_method_type (fnp->type, type,
15673 TYPE_TARGET_TYPE (this_type),
15674 this_type->fields (),
15675 this_type->num_fields (),
15676 this_type->has_varargs ());
15677
15678 /* Handle static member functions.
15679 Dwarf2 has no clean way to discern C++ static and non-static
15680 member functions. G++ helps GDB by marking the first
15681 parameter for non-static member functions (which is the this
15682 pointer) as artificial. We obtain this information from
15683 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15684 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15685 fnp->voffset = VOFFSET_STATIC;
15686 }
15687 else
15688 complaint (_("member function type missing for '%s'"),
15689 dwarf2_full_name (fieldname, die, cu));
15690
15691 /* Get fcontext from DW_AT_containing_type if present. */
15692 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15693 fnp->fcontext = die_containing_type (die, cu);
15694
15695 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15696 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15697
15698 /* Get accessibility. */
15699 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15700 switch (accessibility)
15701 {
15702 case DW_ACCESS_private:
15703 fnp->is_private = 1;
15704 break;
15705 case DW_ACCESS_protected:
15706 fnp->is_protected = 1;
15707 break;
15708 }
15709
15710 /* Check for artificial methods. */
15711 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15712 if (attr && attr->as_boolean ())
15713 fnp->is_artificial = 1;
15714
15715 /* Check for defaulted methods. */
15716 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15717 if (attr != nullptr)
15718 fnp->defaulted = attr->defaulted ();
15719
15720 /* Check for deleted methods. */
15721 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15722 if (attr != nullptr && attr->as_boolean ())
15723 fnp->is_deleted = 1;
15724
15725 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15726
15727 /* Get index in virtual function table if it is a virtual member
15728 function. For older versions of GCC, this is an offset in the
15729 appropriate virtual table, as specified by DW_AT_containing_type.
15730 For everyone else, it is an expression to be evaluated relative
15731 to the object address. */
15732
15733 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15734 if (attr != nullptr)
15735 {
15736 if (attr->form_is_block () && attr->as_block ()->size > 0)
15737 {
15738 struct dwarf_block *block = attr->as_block ();
15739
15740 if (block->data[0] == DW_OP_constu)
15741 {
15742 /* Old-style GCC. */
15743 fnp->voffset = decode_locdesc (block, cu) + 2;
15744 }
15745 else if (block->data[0] == DW_OP_deref
15746 || (block->size > 1
15747 && block->data[0] == DW_OP_deref_size
15748 && block->data[1] == cu->header.addr_size))
15749 {
15750 fnp->voffset = decode_locdesc (block, cu);
15751 if ((fnp->voffset % cu->header.addr_size) != 0)
15752 dwarf2_complex_location_expr_complaint ();
15753 else
15754 fnp->voffset /= cu->header.addr_size;
15755 fnp->voffset += 2;
15756 }
15757 else
15758 dwarf2_complex_location_expr_complaint ();
15759
15760 if (!fnp->fcontext)
15761 {
15762 /* If there is no `this' field and no DW_AT_containing_type,
15763 we cannot actually find a base class context for the
15764 vtable! */
15765 if (this_type->num_fields () == 0
15766 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15767 {
15768 complaint (_("cannot determine context for virtual member "
15769 "function \"%s\" (offset %s)"),
15770 fieldname, sect_offset_str (die->sect_off));
15771 }
15772 else
15773 {
15774 fnp->fcontext
15775 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15776 }
15777 }
15778 }
15779 else if (attr->form_is_section_offset ())
15780 {
15781 dwarf2_complex_location_expr_complaint ();
15782 }
15783 else
15784 {
15785 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15786 fieldname);
15787 }
15788 }
15789 else
15790 {
15791 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15792 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15793 {
15794 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15795 complaint (_("Member function \"%s\" (offset %s) is virtual "
15796 "but the vtable offset is not specified"),
15797 fieldname, sect_offset_str (die->sect_off));
15798 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15799 TYPE_CPLUS_DYNAMIC (type) = 1;
15800 }
15801 }
15802 }
15803
15804 /* Create the vector of member function fields, and attach it to the type. */
15805
15806 static void
15807 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15808 struct dwarf2_cu *cu)
15809 {
15810 if (cu->language == language_ada)
15811 error (_("unexpected member functions in Ada type"));
15812
15813 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15814 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15815 TYPE_ALLOC (type,
15816 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15817
15818 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15819 {
15820 struct fnfieldlist &nf = fip->fnfieldlists[i];
15821 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15822
15823 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15824 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15825 fn_flp->fn_fields = (struct fn_field *)
15826 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15827
15828 for (int k = 0; k < nf.fnfields.size (); ++k)
15829 fn_flp->fn_fields[k] = nf.fnfields[k];
15830 }
15831
15832 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15833 }
15834
15835 /* Returns non-zero if NAME is the name of a vtable member in CU's
15836 language, zero otherwise. */
15837 static int
15838 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15839 {
15840 static const char vptr[] = "_vptr";
15841
15842 /* Look for the C++ form of the vtable. */
15843 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15844 return 1;
15845
15846 return 0;
15847 }
15848
15849 /* GCC outputs unnamed structures that are really pointers to member
15850 functions, with the ABI-specified layout. If TYPE describes
15851 such a structure, smash it into a member function type.
15852
15853 GCC shouldn't do this; it should just output pointer to member DIEs.
15854 This is GCC PR debug/28767. */
15855
15856 static void
15857 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15858 {
15859 struct type *pfn_type, *self_type, *new_type;
15860
15861 /* Check for a structure with no name and two children. */
15862 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15863 return;
15864
15865 /* Check for __pfn and __delta members. */
15866 if (TYPE_FIELD_NAME (type, 0) == NULL
15867 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15868 || TYPE_FIELD_NAME (type, 1) == NULL
15869 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15870 return;
15871
15872 /* Find the type of the method. */
15873 pfn_type = type->field (0).type ();
15874 if (pfn_type == NULL
15875 || pfn_type->code () != TYPE_CODE_PTR
15876 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15877 return;
15878
15879 /* Look for the "this" argument. */
15880 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15881 if (pfn_type->num_fields () == 0
15882 /* || pfn_type->field (0).type () == NULL */
15883 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15884 return;
15885
15886 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15887 new_type = alloc_type (objfile);
15888 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15889 pfn_type->fields (), pfn_type->num_fields (),
15890 pfn_type->has_varargs ());
15891 smash_to_methodptr_type (type, new_type);
15892 }
15893
15894 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15895 requires rewriting, then copy it and return the updated copy.
15896 Otherwise return nullptr. */
15897
15898 static struct type *
15899 rewrite_array_type (struct type *type)
15900 {
15901 if (type->code () != TYPE_CODE_ARRAY)
15902 return nullptr;
15903
15904 struct type *index_type = type->index_type ();
15905 range_bounds *current_bounds = index_type->bounds ();
15906
15907 /* Handle multi-dimensional arrays. */
15908 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15909 if (new_target == nullptr)
15910 {
15911 /* Maybe we don't need to rewrite this array. */
15912 if (current_bounds->low.kind () == PROP_CONST
15913 && current_bounds->high.kind () == PROP_CONST)
15914 return nullptr;
15915 }
15916
15917 /* Either the target type was rewritten, or the bounds have to be
15918 updated. Either way we want to copy the type and update
15919 everything. */
15920 struct type *copy = copy_type (type);
15921 int nfields = copy->num_fields ();
15922 field *new_fields
15923 = ((struct field *) TYPE_ZALLOC (copy,
15924 nfields * sizeof (struct field)));
15925 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15926 copy->set_fields (new_fields);
15927 if (new_target != nullptr)
15928 TYPE_TARGET_TYPE (copy) = new_target;
15929
15930 struct type *index_copy = copy_type (index_type);
15931 range_bounds *bounds
15932 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15933 sizeof (range_bounds));
15934 *bounds = *current_bounds;
15935 bounds->low.set_const_val (1);
15936 bounds->high.set_const_val (0);
15937 index_copy->set_bounds (bounds);
15938 copy->set_index_type (index_copy);
15939
15940 return copy;
15941 }
15942
15943 /* While some versions of GCC will generate complicated DWARF for an
15944 array (see quirk_ada_thick_pointer), more recent versions were
15945 modified to emit an explicit thick pointer structure. However, in
15946 this case, the array still has DWARF expressions for its ranges,
15947 and these must be ignored. */
15948
15949 static void
15950 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15951 struct type *type)
15952 {
15953 gdb_assert (cu->language == language_ada);
15954
15955 /* Check for a structure with two children. */
15956 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15957 return;
15958
15959 /* Check for P_ARRAY and P_BOUNDS members. */
15960 if (TYPE_FIELD_NAME (type, 0) == NULL
15961 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15962 || TYPE_FIELD_NAME (type, 1) == NULL
15963 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15964 return;
15965
15966 /* Make sure we're looking at a pointer to an array. */
15967 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15968 return;
15969
15970 /* The Ada code already knows how to handle these types, so all that
15971 we need to do is turn the bounds into static bounds. However, we
15972 don't want to rewrite existing array or index types in-place,
15973 because those may be referenced in other contexts where this
15974 rewriting is undesirable. */
15975 struct type *new_ary_type
15976 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15977 if (new_ary_type != nullptr)
15978 type->field (0).set_type (lookup_pointer_type (new_ary_type));
15979 }
15980
15981 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15982 appropriate error checking and issuing complaints if there is a
15983 problem. */
15984
15985 static ULONGEST
15986 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15987 {
15988 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15989
15990 if (attr == nullptr)
15991 return 0;
15992
15993 if (!attr->form_is_constant ())
15994 {
15995 complaint (_("DW_AT_alignment must have constant form"
15996 " - DIE at %s [in module %s]"),
15997 sect_offset_str (die->sect_off),
15998 objfile_name (cu->per_objfile->objfile));
15999 return 0;
16000 }
16001
16002 LONGEST val = attr->constant_value (0);
16003 if (val < 0)
16004 {
16005 complaint (_("DW_AT_alignment value must not be negative"
16006 " - DIE at %s [in module %s]"),
16007 sect_offset_str (die->sect_off),
16008 objfile_name (cu->per_objfile->objfile));
16009 return 0;
16010 }
16011 ULONGEST align = val;
16012
16013 if (align == 0)
16014 {
16015 complaint (_("DW_AT_alignment value must not be zero"
16016 " - DIE at %s [in module %s]"),
16017 sect_offset_str (die->sect_off),
16018 objfile_name (cu->per_objfile->objfile));
16019 return 0;
16020 }
16021 if ((align & (align - 1)) != 0)
16022 {
16023 complaint (_("DW_AT_alignment value must be a power of 2"
16024 " - DIE at %s [in module %s]"),
16025 sect_offset_str (die->sect_off),
16026 objfile_name (cu->per_objfile->objfile));
16027 return 0;
16028 }
16029
16030 return align;
16031 }
16032
16033 /* If the DIE has a DW_AT_alignment attribute, use its value to set
16034 the alignment for TYPE. */
16035
16036 static void
16037 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
16038 struct type *type)
16039 {
16040 if (!set_type_align (type, get_alignment (cu, die)))
16041 complaint (_("DW_AT_alignment value too large"
16042 " - DIE at %s [in module %s]"),
16043 sect_offset_str (die->sect_off),
16044 objfile_name (cu->per_objfile->objfile));
16045 }
16046
16047 /* Check if the given VALUE is a valid enum dwarf_calling_convention
16048 constant for a type, according to DWARF5 spec, Table 5.5. */
16049
16050 static bool
16051 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
16052 {
16053 switch (value)
16054 {
16055 case DW_CC_normal:
16056 case DW_CC_pass_by_reference:
16057 case DW_CC_pass_by_value:
16058 return true;
16059
16060 default:
16061 complaint (_("unrecognized DW_AT_calling_convention value "
16062 "(%s) for a type"), pulongest (value));
16063 return false;
16064 }
16065 }
16066
16067 /* Check if the given VALUE is a valid enum dwarf_calling_convention
16068 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
16069 also according to GNU-specific values (see include/dwarf2.h). */
16070
16071 static bool
16072 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
16073 {
16074 switch (value)
16075 {
16076 case DW_CC_normal:
16077 case DW_CC_program:
16078 case DW_CC_nocall:
16079 return true;
16080
16081 case DW_CC_GNU_renesas_sh:
16082 case DW_CC_GNU_borland_fastcall_i386:
16083 case DW_CC_GDB_IBM_OpenCL:
16084 return true;
16085
16086 default:
16087 complaint (_("unrecognized DW_AT_calling_convention value "
16088 "(%s) for a subroutine"), pulongest (value));
16089 return false;
16090 }
16091 }
16092
16093 /* Called when we find the DIE that starts a structure or union scope
16094 (definition) to create a type for the structure or union. Fill in
16095 the type's name and general properties; the members will not be
16096 processed until process_structure_scope. A symbol table entry for
16097 the type will also not be done until process_structure_scope (assuming
16098 the type has a name).
16099
16100 NOTE: we need to call these functions regardless of whether or not the
16101 DIE has a DW_AT_name attribute, since it might be an anonymous
16102 structure or union. This gets the type entered into our set of
16103 user defined types. */
16104
16105 static struct type *
16106 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
16107 {
16108 struct objfile *objfile = cu->per_objfile->objfile;
16109 struct type *type;
16110 struct attribute *attr;
16111 const char *name;
16112
16113 /* If the definition of this type lives in .debug_types, read that type.
16114 Don't follow DW_AT_specification though, that will take us back up
16115 the chain and we want to go down. */
16116 attr = die->attr (DW_AT_signature);
16117 if (attr != nullptr)
16118 {
16119 type = get_DW_AT_signature_type (die, attr, cu);
16120
16121 /* The type's CU may not be the same as CU.
16122 Ensure TYPE is recorded with CU in die_type_hash. */
16123 return set_die_type (die, type, cu);
16124 }
16125
16126 type = alloc_type (objfile);
16127 INIT_CPLUS_SPECIFIC (type);
16128
16129 name = dwarf2_name (die, cu);
16130 if (name != NULL)
16131 {
16132 if (cu->language == language_cplus
16133 || cu->language == language_d
16134 || cu->language == language_rust)
16135 {
16136 const char *full_name = dwarf2_full_name (name, die, cu);
16137
16138 /* dwarf2_full_name might have already finished building the DIE's
16139 type. If so, there is no need to continue. */
16140 if (get_die_type (die, cu) != NULL)
16141 return get_die_type (die, cu);
16142
16143 type->set_name (full_name);
16144 }
16145 else
16146 {
16147 /* The name is already allocated along with this objfile, so
16148 we don't need to duplicate it for the type. */
16149 type->set_name (name);
16150 }
16151 }
16152
16153 if (die->tag == DW_TAG_structure_type)
16154 {
16155 type->set_code (TYPE_CODE_STRUCT);
16156 }
16157 else if (die->tag == DW_TAG_union_type)
16158 {
16159 type->set_code (TYPE_CODE_UNION);
16160 }
16161 else
16162 {
16163 type->set_code (TYPE_CODE_STRUCT);
16164 }
16165
16166 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16167 TYPE_DECLARED_CLASS (type) = 1;
16168
16169 /* Store the calling convention in the type if it's available in
16170 the die. Otherwise the calling convention remains set to
16171 the default value DW_CC_normal. */
16172 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16173 if (attr != nullptr
16174 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
16175 {
16176 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16177 TYPE_CPLUS_CALLING_CONVENTION (type)
16178 = (enum dwarf_calling_convention) (attr->constant_value (0));
16179 }
16180
16181 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16182 if (attr != nullptr)
16183 {
16184 if (attr->form_is_constant ())
16185 TYPE_LENGTH (type) = attr->constant_value (0);
16186 else
16187 {
16188 struct dynamic_prop prop;
16189 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
16190 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
16191 TYPE_LENGTH (type) = 0;
16192 }
16193 }
16194 else
16195 {
16196 TYPE_LENGTH (type) = 0;
16197 }
16198
16199 maybe_set_alignment (cu, die, type);
16200
16201 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
16202 {
16203 /* ICC<14 does not output the required DW_AT_declaration on
16204 incomplete types, but gives them a size of zero. */
16205 type->set_is_stub (true);
16206 }
16207 else
16208 type->set_stub_is_supported (true);
16209
16210 if (die_is_declaration (die, cu))
16211 type->set_is_stub (true);
16212 else if (attr == NULL && die->child == NULL
16213 && producer_is_realview (cu->producer))
16214 /* RealView does not output the required DW_AT_declaration
16215 on incomplete types. */
16216 type->set_is_stub (true);
16217
16218 /* We need to add the type field to the die immediately so we don't
16219 infinitely recurse when dealing with pointers to the structure
16220 type within the structure itself. */
16221 set_die_type (die, type, cu);
16222
16223 /* set_die_type should be already done. */
16224 set_descriptive_type (type, die, cu);
16225
16226 return type;
16227 }
16228
16229 static void handle_struct_member_die
16230 (struct die_info *child_die,
16231 struct type *type,
16232 struct field_info *fi,
16233 std::vector<struct symbol *> *template_args,
16234 struct dwarf2_cu *cu);
16235
16236 /* A helper for handle_struct_member_die that handles
16237 DW_TAG_variant_part. */
16238
16239 static void
16240 handle_variant_part (struct die_info *die, struct type *type,
16241 struct field_info *fi,
16242 std::vector<struct symbol *> *template_args,
16243 struct dwarf2_cu *cu)
16244 {
16245 variant_part_builder *new_part;
16246 if (fi->current_variant_part == nullptr)
16247 {
16248 fi->variant_parts.emplace_back ();
16249 new_part = &fi->variant_parts.back ();
16250 }
16251 else if (!fi->current_variant_part->processing_variant)
16252 {
16253 complaint (_("nested DW_TAG_variant_part seen "
16254 "- DIE at %s [in module %s]"),
16255 sect_offset_str (die->sect_off),
16256 objfile_name (cu->per_objfile->objfile));
16257 return;
16258 }
16259 else
16260 {
16261 variant_field &current = fi->current_variant_part->variants.back ();
16262 current.variant_parts.emplace_back ();
16263 new_part = &current.variant_parts.back ();
16264 }
16265
16266 /* When we recurse, we want callees to add to this new variant
16267 part. */
16268 scoped_restore save_current_variant_part
16269 = make_scoped_restore (&fi->current_variant_part, new_part);
16270
16271 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16272 if (discr == NULL)
16273 {
16274 /* It's a univariant form, an extension we support. */
16275 }
16276 else if (discr->form_is_ref ())
16277 {
16278 struct dwarf2_cu *target_cu = cu;
16279 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16280
16281 new_part->discriminant_offset = target_die->sect_off;
16282 }
16283 else
16284 {
16285 complaint (_("DW_AT_discr does not have DIE reference form"
16286 " - DIE at %s [in module %s]"),
16287 sect_offset_str (die->sect_off),
16288 objfile_name (cu->per_objfile->objfile));
16289 }
16290
16291 for (die_info *child_die = die->child;
16292 child_die != NULL;
16293 child_die = child_die->sibling)
16294 handle_struct_member_die (child_die, type, fi, template_args, cu);
16295 }
16296
16297 /* A helper for handle_struct_member_die that handles
16298 DW_TAG_variant. */
16299
16300 static void
16301 handle_variant (struct die_info *die, struct type *type,
16302 struct field_info *fi,
16303 std::vector<struct symbol *> *template_args,
16304 struct dwarf2_cu *cu)
16305 {
16306 if (fi->current_variant_part == nullptr)
16307 {
16308 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16309 "- DIE at %s [in module %s]"),
16310 sect_offset_str (die->sect_off),
16311 objfile_name (cu->per_objfile->objfile));
16312 return;
16313 }
16314 if (fi->current_variant_part->processing_variant)
16315 {
16316 complaint (_("nested DW_TAG_variant seen "
16317 "- DIE at %s [in module %s]"),
16318 sect_offset_str (die->sect_off),
16319 objfile_name (cu->per_objfile->objfile));
16320 return;
16321 }
16322
16323 scoped_restore save_processing_variant
16324 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16325 true);
16326
16327 fi->current_variant_part->variants.emplace_back ();
16328 variant_field &variant = fi->current_variant_part->variants.back ();
16329 variant.first_field = fi->fields.size ();
16330
16331 /* In a variant we want to get the discriminant and also add a
16332 field for our sole member child. */
16333 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
16334 if (discr == nullptr || !discr->form_is_constant ())
16335 {
16336 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
16337 if (discr == nullptr || discr->as_block ()->size == 0)
16338 variant.default_branch = true;
16339 else
16340 variant.discr_list_data = discr->as_block ();
16341 }
16342 else
16343 variant.discriminant_value = discr->constant_value (0);
16344
16345 for (die_info *variant_child = die->child;
16346 variant_child != NULL;
16347 variant_child = variant_child->sibling)
16348 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16349
16350 variant.last_field = fi->fields.size ();
16351 }
16352
16353 /* A helper for process_structure_scope that handles a single member
16354 DIE. */
16355
16356 static void
16357 handle_struct_member_die (struct die_info *child_die, struct type *type,
16358 struct field_info *fi,
16359 std::vector<struct symbol *> *template_args,
16360 struct dwarf2_cu *cu)
16361 {
16362 if (child_die->tag == DW_TAG_member
16363 || child_die->tag == DW_TAG_variable)
16364 {
16365 /* NOTE: carlton/2002-11-05: A C++ static data member
16366 should be a DW_TAG_member that is a declaration, but
16367 all versions of G++ as of this writing (so through at
16368 least 3.2.1) incorrectly generate DW_TAG_variable
16369 tags for them instead. */
16370 dwarf2_add_field (fi, child_die, cu);
16371 }
16372 else if (child_die->tag == DW_TAG_subprogram)
16373 {
16374 /* Rust doesn't have member functions in the C++ sense.
16375 However, it does emit ordinary functions as children
16376 of a struct DIE. */
16377 if (cu->language == language_rust)
16378 read_func_scope (child_die, cu);
16379 else
16380 {
16381 /* C++ member function. */
16382 dwarf2_add_member_fn (fi, child_die, type, cu);
16383 }
16384 }
16385 else if (child_die->tag == DW_TAG_inheritance)
16386 {
16387 /* C++ base class field. */
16388 dwarf2_add_field (fi, child_die, cu);
16389 }
16390 else if (type_can_define_types (child_die))
16391 dwarf2_add_type_defn (fi, child_die, cu);
16392 else if (child_die->tag == DW_TAG_template_type_param
16393 || child_die->tag == DW_TAG_template_value_param)
16394 {
16395 struct symbol *arg = new_symbol (child_die, NULL, cu);
16396
16397 if (arg != NULL)
16398 template_args->push_back (arg);
16399 }
16400 else if (child_die->tag == DW_TAG_variant_part)
16401 handle_variant_part (child_die, type, fi, template_args, cu);
16402 else if (child_die->tag == DW_TAG_variant)
16403 handle_variant (child_die, type, fi, template_args, cu);
16404 }
16405
16406 /* Finish creating a structure or union type, including filling in
16407 its members and creating a symbol for it. */
16408
16409 static void
16410 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16411 {
16412 struct objfile *objfile = cu->per_objfile->objfile;
16413 struct die_info *child_die;
16414 struct type *type;
16415
16416 type = get_die_type (die, cu);
16417 if (type == NULL)
16418 type = read_structure_type (die, cu);
16419
16420 bool has_template_parameters = false;
16421 if (die->child != NULL && ! die_is_declaration (die, cu))
16422 {
16423 struct field_info fi;
16424 std::vector<struct symbol *> template_args;
16425
16426 child_die = die->child;
16427
16428 while (child_die && child_die->tag)
16429 {
16430 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16431 child_die = child_die->sibling;
16432 }
16433
16434 /* Attach template arguments to type. */
16435 if (!template_args.empty ())
16436 {
16437 has_template_parameters = true;
16438 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16439 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16440 TYPE_TEMPLATE_ARGUMENTS (type)
16441 = XOBNEWVEC (&objfile->objfile_obstack,
16442 struct symbol *,
16443 TYPE_N_TEMPLATE_ARGUMENTS (type));
16444 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16445 template_args.data (),
16446 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16447 * sizeof (struct symbol *)));
16448 }
16449
16450 /* Attach fields and member functions to the type. */
16451 if (fi.nfields () > 0)
16452 dwarf2_attach_fields_to_type (&fi, type, cu);
16453 if (!fi.fnfieldlists.empty ())
16454 {
16455 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16456
16457 /* Get the type which refers to the base class (possibly this
16458 class itself) which contains the vtable pointer for the current
16459 class from the DW_AT_containing_type attribute. This use of
16460 DW_AT_containing_type is a GNU extension. */
16461
16462 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16463 {
16464 struct type *t = die_containing_type (die, cu);
16465
16466 set_type_vptr_basetype (type, t);
16467 if (type == t)
16468 {
16469 int i;
16470
16471 /* Our own class provides vtbl ptr. */
16472 for (i = t->num_fields () - 1;
16473 i >= TYPE_N_BASECLASSES (t);
16474 --i)
16475 {
16476 const char *fieldname = TYPE_FIELD_NAME (t, i);
16477
16478 if (is_vtable_name (fieldname, cu))
16479 {
16480 set_type_vptr_fieldno (type, i);
16481 break;
16482 }
16483 }
16484
16485 /* Complain if virtual function table field not found. */
16486 if (i < TYPE_N_BASECLASSES (t))
16487 complaint (_("virtual function table pointer "
16488 "not found when defining class '%s'"),
16489 type->name () ? type->name () : "");
16490 }
16491 else
16492 {
16493 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16494 }
16495 }
16496 else if (cu->producer
16497 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16498 {
16499 /* The IBM XLC compiler does not provide direct indication
16500 of the containing type, but the vtable pointer is
16501 always named __vfp. */
16502
16503 int i;
16504
16505 for (i = type->num_fields () - 1;
16506 i >= TYPE_N_BASECLASSES (type);
16507 --i)
16508 {
16509 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16510 {
16511 set_type_vptr_fieldno (type, i);
16512 set_type_vptr_basetype (type, type);
16513 break;
16514 }
16515 }
16516 }
16517 }
16518
16519 /* Copy fi.typedef_field_list linked list elements content into the
16520 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16521 if (!fi.typedef_field_list.empty ())
16522 {
16523 int count = fi.typedef_field_list.size ();
16524
16525 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16526 TYPE_TYPEDEF_FIELD_ARRAY (type)
16527 = ((struct decl_field *)
16528 TYPE_ALLOC (type,
16529 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16530 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16531
16532 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16533 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16534 }
16535
16536 /* Copy fi.nested_types_list linked list elements content into the
16537 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16538 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16539 {
16540 int count = fi.nested_types_list.size ();
16541
16542 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16543 TYPE_NESTED_TYPES_ARRAY (type)
16544 = ((struct decl_field *)
16545 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16546 TYPE_NESTED_TYPES_COUNT (type) = count;
16547
16548 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16549 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16550 }
16551 }
16552
16553 quirk_gcc_member_function_pointer (type, objfile);
16554 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16555 cu->rust_unions.push_back (type);
16556 else if (cu->language == language_ada)
16557 quirk_ada_thick_pointer_struct (die, cu, type);
16558
16559 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16560 snapshots) has been known to create a die giving a declaration
16561 for a class that has, as a child, a die giving a definition for a
16562 nested class. So we have to process our children even if the
16563 current die is a declaration. Normally, of course, a declaration
16564 won't have any children at all. */
16565
16566 child_die = die->child;
16567
16568 while (child_die != NULL && child_die->tag)
16569 {
16570 if (child_die->tag == DW_TAG_member
16571 || child_die->tag == DW_TAG_variable
16572 || child_die->tag == DW_TAG_inheritance
16573 || child_die->tag == DW_TAG_template_value_param
16574 || child_die->tag == DW_TAG_template_type_param)
16575 {
16576 /* Do nothing. */
16577 }
16578 else
16579 process_die (child_die, cu);
16580
16581 child_die = child_die->sibling;
16582 }
16583
16584 /* Do not consider external references. According to the DWARF standard,
16585 these DIEs are identified by the fact that they have no byte_size
16586 attribute, and a declaration attribute. */
16587 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16588 || !die_is_declaration (die, cu)
16589 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16590 {
16591 struct symbol *sym = new_symbol (die, type, cu);
16592
16593 if (has_template_parameters)
16594 {
16595 struct symtab *symtab;
16596 if (sym != nullptr)
16597 symtab = symbol_symtab (sym);
16598 else if (cu->line_header != nullptr)
16599 {
16600 /* Any related symtab will do. */
16601 symtab
16602 = cu->line_header->file_names ()[0].symtab;
16603 }
16604 else
16605 {
16606 symtab = nullptr;
16607 complaint (_("could not find suitable "
16608 "symtab for template parameter"
16609 " - DIE at %s [in module %s]"),
16610 sect_offset_str (die->sect_off),
16611 objfile_name (objfile));
16612 }
16613
16614 if (symtab != nullptr)
16615 {
16616 /* Make sure that the symtab is set on the new symbols.
16617 Even though they don't appear in this symtab directly,
16618 other parts of gdb assume that symbols do, and this is
16619 reasonably true. */
16620 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16621 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16622 }
16623 }
16624 }
16625 }
16626
16627 /* Assuming DIE is an enumeration type, and TYPE is its associated
16628 type, update TYPE using some information only available in DIE's
16629 children. In particular, the fields are computed. */
16630
16631 static void
16632 update_enumeration_type_from_children (struct die_info *die,
16633 struct type *type,
16634 struct dwarf2_cu *cu)
16635 {
16636 struct die_info *child_die;
16637 int unsigned_enum = 1;
16638 int flag_enum = 1;
16639
16640 auto_obstack obstack;
16641 std::vector<struct field> fields;
16642
16643 for (child_die = die->child;
16644 child_die != NULL && child_die->tag;
16645 child_die = child_die->sibling)
16646 {
16647 struct attribute *attr;
16648 LONGEST value;
16649 const gdb_byte *bytes;
16650 struct dwarf2_locexpr_baton *baton;
16651 const char *name;
16652
16653 if (child_die->tag != DW_TAG_enumerator)
16654 continue;
16655
16656 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16657 if (attr == NULL)
16658 continue;
16659
16660 name = dwarf2_name (child_die, cu);
16661 if (name == NULL)
16662 name = "<anonymous enumerator>";
16663
16664 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16665 &value, &bytes, &baton);
16666 if (value < 0)
16667 {
16668 unsigned_enum = 0;
16669 flag_enum = 0;
16670 }
16671 else
16672 {
16673 if (count_one_bits_ll (value) >= 2)
16674 flag_enum = 0;
16675 }
16676
16677 fields.emplace_back ();
16678 struct field &field = fields.back ();
16679 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16680 SET_FIELD_ENUMVAL (field, value);
16681 }
16682
16683 if (!fields.empty ())
16684 {
16685 type->set_num_fields (fields.size ());
16686 type->set_fields
16687 ((struct field *)
16688 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16689 memcpy (type->fields (), fields.data (),
16690 sizeof (struct field) * fields.size ());
16691 }
16692
16693 if (unsigned_enum)
16694 type->set_is_unsigned (true);
16695
16696 if (flag_enum)
16697 TYPE_FLAG_ENUM (type) = 1;
16698 }
16699
16700 /* Given a DW_AT_enumeration_type die, set its type. We do not
16701 complete the type's fields yet, or create any symbols. */
16702
16703 static struct type *
16704 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16705 {
16706 struct objfile *objfile = cu->per_objfile->objfile;
16707 struct type *type;
16708 struct attribute *attr;
16709 const char *name;
16710
16711 /* If the definition of this type lives in .debug_types, read that type.
16712 Don't follow DW_AT_specification though, that will take us back up
16713 the chain and we want to go down. */
16714 attr = die->attr (DW_AT_signature);
16715 if (attr != nullptr)
16716 {
16717 type = get_DW_AT_signature_type (die, attr, cu);
16718
16719 /* The type's CU may not be the same as CU.
16720 Ensure TYPE is recorded with CU in die_type_hash. */
16721 return set_die_type (die, type, cu);
16722 }
16723
16724 type = alloc_type (objfile);
16725
16726 type->set_code (TYPE_CODE_ENUM);
16727 name = dwarf2_full_name (NULL, die, cu);
16728 if (name != NULL)
16729 type->set_name (name);
16730
16731 attr = dwarf2_attr (die, DW_AT_type, cu);
16732 if (attr != NULL)
16733 {
16734 struct type *underlying_type = die_type (die, cu);
16735
16736 TYPE_TARGET_TYPE (type) = underlying_type;
16737 }
16738
16739 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16740 if (attr != nullptr)
16741 {
16742 TYPE_LENGTH (type) = attr->constant_value (0);
16743 }
16744 else
16745 {
16746 TYPE_LENGTH (type) = 0;
16747 }
16748
16749 maybe_set_alignment (cu, die, type);
16750
16751 /* The enumeration DIE can be incomplete. In Ada, any type can be
16752 declared as private in the package spec, and then defined only
16753 inside the package body. Such types are known as Taft Amendment
16754 Types. When another package uses such a type, an incomplete DIE
16755 may be generated by the compiler. */
16756 if (die_is_declaration (die, cu))
16757 type->set_is_stub (true);
16758
16759 /* If this type has an underlying type that is not a stub, then we
16760 may use its attributes. We always use the "unsigned" attribute
16761 in this situation, because ordinarily we guess whether the type
16762 is unsigned -- but the guess can be wrong and the underlying type
16763 can tell us the reality. However, we defer to a local size
16764 attribute if one exists, because this lets the compiler override
16765 the underlying type if needed. */
16766 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16767 {
16768 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16769 underlying_type = check_typedef (underlying_type);
16770
16771 type->set_is_unsigned (underlying_type->is_unsigned ());
16772
16773 if (TYPE_LENGTH (type) == 0)
16774 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16775
16776 if (TYPE_RAW_ALIGN (type) == 0
16777 && TYPE_RAW_ALIGN (underlying_type) != 0)
16778 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16779 }
16780
16781 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16782
16783 set_die_type (die, type, cu);
16784
16785 /* Finish the creation of this type by using the enum's children.
16786 Note that, as usual, this must come after set_die_type to avoid
16787 infinite recursion when trying to compute the names of the
16788 enumerators. */
16789 update_enumeration_type_from_children (die, type, cu);
16790
16791 return type;
16792 }
16793
16794 /* Given a pointer to a die which begins an enumeration, process all
16795 the dies that define the members of the enumeration, and create the
16796 symbol for the enumeration type.
16797
16798 NOTE: We reverse the order of the element list. */
16799
16800 static void
16801 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16802 {
16803 struct type *this_type;
16804
16805 this_type = get_die_type (die, cu);
16806 if (this_type == NULL)
16807 this_type = read_enumeration_type (die, cu);
16808
16809 if (die->child != NULL)
16810 {
16811 struct die_info *child_die;
16812 const char *name;
16813
16814 child_die = die->child;
16815 while (child_die && child_die->tag)
16816 {
16817 if (child_die->tag != DW_TAG_enumerator)
16818 {
16819 process_die (child_die, cu);
16820 }
16821 else
16822 {
16823 name = dwarf2_name (child_die, cu);
16824 if (name)
16825 new_symbol (child_die, this_type, cu);
16826 }
16827
16828 child_die = child_die->sibling;
16829 }
16830 }
16831
16832 /* If we are reading an enum from a .debug_types unit, and the enum
16833 is a declaration, and the enum is not the signatured type in the
16834 unit, then we do not want to add a symbol for it. Adding a
16835 symbol would in some cases obscure the true definition of the
16836 enum, giving users an incomplete type when the definition is
16837 actually available. Note that we do not want to do this for all
16838 enums which are just declarations, because C++0x allows forward
16839 enum declarations. */
16840 if (cu->per_cu->is_debug_types
16841 && die_is_declaration (die, cu))
16842 {
16843 struct signatured_type *sig_type;
16844
16845 sig_type = (struct signatured_type *) cu->per_cu;
16846 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16847 if (sig_type->type_offset_in_section != die->sect_off)
16848 return;
16849 }
16850
16851 new_symbol (die, this_type, cu);
16852 }
16853
16854 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16855 expression for an index type and finds the corresponding field
16856 offset in the hidden "P_BOUNDS" structure. Returns true on success
16857 and updates *FIELD, false if it fails to recognize an
16858 expression. */
16859
16860 static bool
16861 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16862 int *bounds_offset, struct field *field,
16863 struct dwarf2_cu *cu)
16864 {
16865 struct attribute *attr = dwarf2_attr (die, name, cu);
16866 if (attr == nullptr || !attr->form_is_block ())
16867 return false;
16868
16869 const struct dwarf_block *block = attr->as_block ();
16870 const gdb_byte *start = block->data;
16871 const gdb_byte *end = block->data + block->size;
16872
16873 /* The expression to recognize generally looks like:
16874
16875 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16876 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16877
16878 However, the second "plus_uconst" may be missing:
16879
16880 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16881 DW_OP_deref_size: 4)
16882
16883 This happens when the field is at the start of the structure.
16884
16885 Also, the final deref may not be sized:
16886
16887 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16888 DW_OP_deref)
16889
16890 This happens when the size of the index type happens to be the
16891 same as the architecture's word size. This can occur with or
16892 without the second plus_uconst. */
16893
16894 if (end - start < 2)
16895 return false;
16896 if (*start++ != DW_OP_push_object_address)
16897 return false;
16898 if (*start++ != DW_OP_plus_uconst)
16899 return false;
16900
16901 uint64_t this_bound_off;
16902 start = gdb_read_uleb128 (start, end, &this_bound_off);
16903 if (start == nullptr || (int) this_bound_off != this_bound_off)
16904 return false;
16905 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16906 is consistent among all bounds. */
16907 if (*bounds_offset == -1)
16908 *bounds_offset = this_bound_off;
16909 else if (*bounds_offset != this_bound_off)
16910 return false;
16911
16912 if (start == end || *start++ != DW_OP_deref)
16913 return false;
16914
16915 int offset = 0;
16916 if (start ==end)
16917 return false;
16918 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16919 {
16920 /* This means an offset of 0. */
16921 }
16922 else if (*start++ != DW_OP_plus_uconst)
16923 return false;
16924 else
16925 {
16926 /* The size is the parameter to DW_OP_plus_uconst. */
16927 uint64_t val;
16928 start = gdb_read_uleb128 (start, end, &val);
16929 if (start == nullptr)
16930 return false;
16931 if ((int) val != val)
16932 return false;
16933 offset = val;
16934 }
16935
16936 if (start == end)
16937 return false;
16938
16939 uint64_t size;
16940 if (*start == DW_OP_deref_size)
16941 {
16942 start = gdb_read_uleb128 (start + 1, end, &size);
16943 if (start == nullptr)
16944 return false;
16945 }
16946 else if (*start == DW_OP_deref)
16947 {
16948 size = cu->header.addr_size;
16949 ++start;
16950 }
16951 else
16952 return false;
16953
16954 SET_FIELD_BITPOS (*field, 8 * offset);
16955 if (size != TYPE_LENGTH (field->type ()))
16956 FIELD_BITSIZE (*field) = 8 * size;
16957
16958 return true;
16959 }
16960
16961 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16962 some kinds of Ada arrays:
16963
16964 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16965 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16966 <11e0> DW_AT_data_location: 2 byte block: 97 6
16967 (DW_OP_push_object_address; DW_OP_deref)
16968 <11e3> DW_AT_type : <0x1173>
16969 <11e7> DW_AT_sibling : <0x1201>
16970 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16971 <11ec> DW_AT_type : <0x1206>
16972 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16973 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16974 DW_OP_deref_size: 4)
16975 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16976 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16977 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16978
16979 This actually represents a "thick pointer", which is a structure
16980 with two elements: one that is a pointer to the array data, and one
16981 that is a pointer to another structure; this second structure holds
16982 the array bounds.
16983
16984 This returns a new type on success, or nullptr if this didn't
16985 recognize the type. */
16986
16987 static struct type *
16988 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16989 struct type *type)
16990 {
16991 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16992 /* So far we've only seen this with block form. */
16993 if (attr == nullptr || !attr->form_is_block ())
16994 return nullptr;
16995
16996 /* Note that this will fail if the structure layout is changed by
16997 the compiler. However, we have no good way to recognize some
16998 other layout, because we don't know what expression the compiler
16999 might choose to emit should this happen. */
17000 struct dwarf_block *blk = attr->as_block ();
17001 if (blk->size != 2
17002 || blk->data[0] != DW_OP_push_object_address
17003 || blk->data[1] != DW_OP_deref)
17004 return nullptr;
17005
17006 int bounds_offset = -1;
17007 int max_align = -1;
17008 std::vector<struct field> range_fields;
17009 for (struct die_info *child_die = die->child;
17010 child_die;
17011 child_die = child_die->sibling)
17012 {
17013 if (child_die->tag == DW_TAG_subrange_type)
17014 {
17015 struct type *underlying = read_subrange_index_type (child_die, cu);
17016
17017 int this_align = type_align (underlying);
17018 if (this_align > max_align)
17019 max_align = this_align;
17020
17021 range_fields.emplace_back ();
17022 range_fields.emplace_back ();
17023
17024 struct field &lower = range_fields[range_fields.size () - 2];
17025 struct field &upper = range_fields[range_fields.size () - 1];
17026
17027 lower.set_type (underlying);
17028 FIELD_ARTIFICIAL (lower) = 1;
17029
17030 upper.set_type (underlying);
17031 FIELD_ARTIFICIAL (upper) = 1;
17032
17033 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
17034 &bounds_offset, &lower, cu)
17035 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
17036 &bounds_offset, &upper, cu))
17037 return nullptr;
17038 }
17039 }
17040
17041 /* This shouldn't really happen, but double-check that we found
17042 where the bounds are stored. */
17043 if (bounds_offset == -1)
17044 return nullptr;
17045
17046 struct objfile *objfile = cu->per_objfile->objfile;
17047 for (int i = 0; i < range_fields.size (); i += 2)
17048 {
17049 char name[20];
17050
17051 /* Set the name of each field in the bounds. */
17052 xsnprintf (name, sizeof (name), "LB%d", i / 2);
17053 FIELD_NAME (range_fields[i]) = objfile->intern (name);
17054 xsnprintf (name, sizeof (name), "UB%d", i / 2);
17055 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
17056 }
17057
17058 struct type *bounds = alloc_type (objfile);
17059 bounds->set_code (TYPE_CODE_STRUCT);
17060
17061 bounds->set_num_fields (range_fields.size ());
17062 bounds->set_fields
17063 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
17064 * sizeof (struct field))));
17065 memcpy (bounds->fields (), range_fields.data (),
17066 bounds->num_fields () * sizeof (struct field));
17067
17068 int last_fieldno = range_fields.size () - 1;
17069 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
17070 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
17071 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
17072
17073 /* Rewrite the existing array type in place. Specifically, we
17074 remove any dynamic properties we might have read, and we replace
17075 the index types. */
17076 struct type *iter = type;
17077 for (int i = 0; i < range_fields.size (); i += 2)
17078 {
17079 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
17080 iter->main_type->dyn_prop_list = nullptr;
17081 iter->set_index_type
17082 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
17083 iter = TYPE_TARGET_TYPE (iter);
17084 }
17085
17086 struct type *result = alloc_type (objfile);
17087 result->set_code (TYPE_CODE_STRUCT);
17088
17089 result->set_num_fields (2);
17090 result->set_fields
17091 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
17092 * sizeof (struct field))));
17093
17094 /* The names are chosen to coincide with what the compiler does with
17095 -fgnat-encodings=all, which the Ada code in gdb already
17096 understands. */
17097 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
17098 result->field (0).set_type (lookup_pointer_type (type));
17099
17100 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
17101 result->field (1).set_type (lookup_pointer_type (bounds));
17102 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
17103
17104 result->set_name (type->name ());
17105 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
17106 + TYPE_LENGTH (result->field (1).type ()));
17107
17108 return result;
17109 }
17110
17111 /* Extract all information from a DW_TAG_array_type DIE and put it in
17112 the DIE's type field. For now, this only handles one dimensional
17113 arrays. */
17114
17115 static struct type *
17116 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
17117 {
17118 struct objfile *objfile = cu->per_objfile->objfile;
17119 struct die_info *child_die;
17120 struct type *type;
17121 struct type *element_type, *range_type, *index_type;
17122 struct attribute *attr;
17123 const char *name;
17124 struct dynamic_prop *byte_stride_prop = NULL;
17125 unsigned int bit_stride = 0;
17126
17127 element_type = die_type (die, cu);
17128
17129 /* The die_type call above may have already set the type for this DIE. */
17130 type = get_die_type (die, cu);
17131 if (type)
17132 return type;
17133
17134 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
17135 if (attr != NULL)
17136 {
17137 int stride_ok;
17138 struct type *prop_type = cu->addr_sized_int_type (false);
17139
17140 byte_stride_prop
17141 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
17142 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
17143 prop_type);
17144 if (!stride_ok)
17145 {
17146 complaint (_("unable to read array DW_AT_byte_stride "
17147 " - DIE at %s [in module %s]"),
17148 sect_offset_str (die->sect_off),
17149 objfile_name (cu->per_objfile->objfile));
17150 /* Ignore this attribute. We will likely not be able to print
17151 arrays of this type correctly, but there is little we can do
17152 to help if we cannot read the attribute's value. */
17153 byte_stride_prop = NULL;
17154 }
17155 }
17156
17157 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
17158 if (attr != NULL)
17159 bit_stride = attr->constant_value (0);
17160
17161 /* Irix 6.2 native cc creates array types without children for
17162 arrays with unspecified length. */
17163 if (die->child == NULL)
17164 {
17165 index_type = objfile_type (objfile)->builtin_int;
17166 range_type = create_static_range_type (NULL, index_type, 0, -1);
17167 type = create_array_type_with_stride (NULL, element_type, range_type,
17168 byte_stride_prop, bit_stride);
17169 return set_die_type (die, type, cu);
17170 }
17171
17172 std::vector<struct type *> range_types;
17173 child_die = die->child;
17174 while (child_die && child_die->tag)
17175 {
17176 if (child_die->tag == DW_TAG_subrange_type)
17177 {
17178 struct type *child_type = read_type_die (child_die, cu);
17179
17180 if (child_type != NULL)
17181 {
17182 /* The range type was succesfully read. Save it for the
17183 array type creation. */
17184 range_types.push_back (child_type);
17185 }
17186 }
17187 child_die = child_die->sibling;
17188 }
17189
17190 /* Dwarf2 dimensions are output from left to right, create the
17191 necessary array types in backwards order. */
17192
17193 type = element_type;
17194
17195 if (read_array_order (die, cu) == DW_ORD_col_major)
17196 {
17197 int i = 0;
17198
17199 while (i < range_types.size ())
17200 {
17201 type = create_array_type_with_stride (NULL, type, range_types[i++],
17202 byte_stride_prop, bit_stride);
17203 bit_stride = 0;
17204 byte_stride_prop = nullptr;
17205 }
17206 }
17207 else
17208 {
17209 size_t ndim = range_types.size ();
17210 while (ndim-- > 0)
17211 {
17212 type = create_array_type_with_stride (NULL, type, range_types[ndim],
17213 byte_stride_prop, bit_stride);
17214 bit_stride = 0;
17215 byte_stride_prop = nullptr;
17216 }
17217 }
17218
17219 /* Understand Dwarf2 support for vector types (like they occur on
17220 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
17221 array type. This is not part of the Dwarf2/3 standard yet, but a
17222 custom vendor extension. The main difference between a regular
17223 array and the vector variant is that vectors are passed by value
17224 to functions. */
17225 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
17226 if (attr != nullptr)
17227 make_vector_type (type);
17228
17229 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
17230 implementation may choose to implement triple vectors using this
17231 attribute. */
17232 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17233 if (attr != nullptr && attr->form_is_unsigned ())
17234 {
17235 if (attr->as_unsigned () >= TYPE_LENGTH (type))
17236 TYPE_LENGTH (type) = attr->as_unsigned ();
17237 else
17238 complaint (_("DW_AT_byte_size for array type smaller "
17239 "than the total size of elements"));
17240 }
17241
17242 name = dwarf2_name (die, cu);
17243 if (name)
17244 type->set_name (name);
17245
17246 maybe_set_alignment (cu, die, type);
17247
17248 struct type *replacement_type = nullptr;
17249 if (cu->language == language_ada)
17250 {
17251 replacement_type = quirk_ada_thick_pointer (die, cu, type);
17252 if (replacement_type != nullptr)
17253 type = replacement_type;
17254 }
17255
17256 /* Install the type in the die. */
17257 set_die_type (die, type, cu, replacement_type != nullptr);
17258
17259 /* set_die_type should be already done. */
17260 set_descriptive_type (type, die, cu);
17261
17262 return type;
17263 }
17264
17265 static enum dwarf_array_dim_ordering
17266 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
17267 {
17268 struct attribute *attr;
17269
17270 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17271
17272 if (attr != nullptr)
17273 {
17274 LONGEST val = attr->constant_value (-1);
17275 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
17276 return (enum dwarf_array_dim_ordering) val;
17277 }
17278
17279 /* GNU F77 is a special case, as at 08/2004 array type info is the
17280 opposite order to the dwarf2 specification, but data is still
17281 laid out as per normal fortran.
17282
17283 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17284 version checking. */
17285
17286 if (cu->language == language_fortran
17287 && cu->producer && strstr (cu->producer, "GNU F77"))
17288 {
17289 return DW_ORD_row_major;
17290 }
17291
17292 switch (cu->language_defn->array_ordering ())
17293 {
17294 case array_column_major:
17295 return DW_ORD_col_major;
17296 case array_row_major:
17297 default:
17298 return DW_ORD_row_major;
17299 };
17300 }
17301
17302 /* Extract all information from a DW_TAG_set_type DIE and put it in
17303 the DIE's type field. */
17304
17305 static struct type *
17306 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17307 {
17308 struct type *domain_type, *set_type;
17309 struct attribute *attr;
17310
17311 domain_type = die_type (die, cu);
17312
17313 /* The die_type call above may have already set the type for this DIE. */
17314 set_type = get_die_type (die, cu);
17315 if (set_type)
17316 return set_type;
17317
17318 set_type = create_set_type (NULL, domain_type);
17319
17320 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17321 if (attr != nullptr && attr->form_is_unsigned ())
17322 TYPE_LENGTH (set_type) = attr->as_unsigned ();
17323
17324 maybe_set_alignment (cu, die, set_type);
17325
17326 return set_die_type (die, set_type, cu);
17327 }
17328
17329 /* A helper for read_common_block that creates a locexpr baton.
17330 SYM is the symbol which we are marking as computed.
17331 COMMON_DIE is the DIE for the common block.
17332 COMMON_LOC is the location expression attribute for the common
17333 block itself.
17334 MEMBER_LOC is the location expression attribute for the particular
17335 member of the common block that we are processing.
17336 CU is the CU from which the above come. */
17337
17338 static void
17339 mark_common_block_symbol_computed (struct symbol *sym,
17340 struct die_info *common_die,
17341 struct attribute *common_loc,
17342 struct attribute *member_loc,
17343 struct dwarf2_cu *cu)
17344 {
17345 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17346 struct objfile *objfile = per_objfile->objfile;
17347 struct dwarf2_locexpr_baton *baton;
17348 gdb_byte *ptr;
17349 unsigned int cu_off;
17350 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
17351 LONGEST offset = 0;
17352
17353 gdb_assert (common_loc && member_loc);
17354 gdb_assert (common_loc->form_is_block ());
17355 gdb_assert (member_loc->form_is_block ()
17356 || member_loc->form_is_constant ());
17357
17358 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
17359 baton->per_objfile = per_objfile;
17360 baton->per_cu = cu->per_cu;
17361 gdb_assert (baton->per_cu);
17362
17363 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17364
17365 if (member_loc->form_is_constant ())
17366 {
17367 offset = member_loc->constant_value (0);
17368 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17369 }
17370 else
17371 baton->size += member_loc->as_block ()->size;
17372
17373 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
17374 baton->data = ptr;
17375
17376 *ptr++ = DW_OP_call4;
17377 cu_off = common_die->sect_off - cu->per_cu->sect_off;
17378 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17379 ptr += 4;
17380
17381 if (member_loc->form_is_constant ())
17382 {
17383 *ptr++ = DW_OP_addr;
17384 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17385 ptr += cu->header.addr_size;
17386 }
17387 else
17388 {
17389 /* We have to copy the data here, because DW_OP_call4 will only
17390 use a DW_AT_location attribute. */
17391 struct dwarf_block *block = member_loc->as_block ();
17392 memcpy (ptr, block->data, block->size);
17393 ptr += block->size;
17394 }
17395
17396 *ptr++ = DW_OP_plus;
17397 gdb_assert (ptr - baton->data == baton->size);
17398
17399 SYMBOL_LOCATION_BATON (sym) = baton;
17400 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
17401 }
17402
17403 /* Create appropriate locally-scoped variables for all the
17404 DW_TAG_common_block entries. Also create a struct common_block
17405 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
17406 is used to separate the common blocks name namespace from regular
17407 variable names. */
17408
17409 static void
17410 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
17411 {
17412 struct attribute *attr;
17413
17414 attr = dwarf2_attr (die, DW_AT_location, cu);
17415 if (attr != nullptr)
17416 {
17417 /* Support the .debug_loc offsets. */
17418 if (attr->form_is_block ())
17419 {
17420 /* Ok. */
17421 }
17422 else if (attr->form_is_section_offset ())
17423 {
17424 dwarf2_complex_location_expr_complaint ();
17425 attr = NULL;
17426 }
17427 else
17428 {
17429 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17430 "common block member");
17431 attr = NULL;
17432 }
17433 }
17434
17435 if (die->child != NULL)
17436 {
17437 struct objfile *objfile = cu->per_objfile->objfile;
17438 struct die_info *child_die;
17439 size_t n_entries = 0, size;
17440 struct common_block *common_block;
17441 struct symbol *sym;
17442
17443 for (child_die = die->child;
17444 child_die && child_die->tag;
17445 child_die = child_die->sibling)
17446 ++n_entries;
17447
17448 size = (sizeof (struct common_block)
17449 + (n_entries - 1) * sizeof (struct symbol *));
17450 common_block
17451 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17452 size);
17453 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17454 common_block->n_entries = 0;
17455
17456 for (child_die = die->child;
17457 child_die && child_die->tag;
17458 child_die = child_die->sibling)
17459 {
17460 /* Create the symbol in the DW_TAG_common_block block in the current
17461 symbol scope. */
17462 sym = new_symbol (child_die, NULL, cu);
17463 if (sym != NULL)
17464 {
17465 struct attribute *member_loc;
17466
17467 common_block->contents[common_block->n_entries++] = sym;
17468
17469 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17470 cu);
17471 if (member_loc)
17472 {
17473 /* GDB has handled this for a long time, but it is
17474 not specified by DWARF. It seems to have been
17475 emitted by gfortran at least as recently as:
17476 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17477 complaint (_("Variable in common block has "
17478 "DW_AT_data_member_location "
17479 "- DIE at %s [in module %s]"),
17480 sect_offset_str (child_die->sect_off),
17481 objfile_name (objfile));
17482
17483 if (member_loc->form_is_section_offset ())
17484 dwarf2_complex_location_expr_complaint ();
17485 else if (member_loc->form_is_constant ()
17486 || member_loc->form_is_block ())
17487 {
17488 if (attr != nullptr)
17489 mark_common_block_symbol_computed (sym, die, attr,
17490 member_loc, cu);
17491 }
17492 else
17493 dwarf2_complex_location_expr_complaint ();
17494 }
17495 }
17496 }
17497
17498 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17499 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
17500 }
17501 }
17502
17503 /* Create a type for a C++ namespace. */
17504
17505 static struct type *
17506 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17507 {
17508 struct objfile *objfile = cu->per_objfile->objfile;
17509 const char *previous_prefix, *name;
17510 int is_anonymous;
17511 struct type *type;
17512
17513 /* For extensions, reuse the type of the original namespace. */
17514 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17515 {
17516 struct die_info *ext_die;
17517 struct dwarf2_cu *ext_cu = cu;
17518
17519 ext_die = dwarf2_extension (die, &ext_cu);
17520 type = read_type_die (ext_die, ext_cu);
17521
17522 /* EXT_CU may not be the same as CU.
17523 Ensure TYPE is recorded with CU in die_type_hash. */
17524 return set_die_type (die, type, cu);
17525 }
17526
17527 name = namespace_name (die, &is_anonymous, cu);
17528
17529 /* Now build the name of the current namespace. */
17530
17531 previous_prefix = determine_prefix (die, cu);
17532 if (previous_prefix[0] != '\0')
17533 name = typename_concat (&objfile->objfile_obstack,
17534 previous_prefix, name, 0, cu);
17535
17536 /* Create the type. */
17537 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17538
17539 return set_die_type (die, type, cu);
17540 }
17541
17542 /* Read a namespace scope. */
17543
17544 static void
17545 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17546 {
17547 struct objfile *objfile = cu->per_objfile->objfile;
17548 int is_anonymous;
17549
17550 /* Add a symbol associated to this if we haven't seen the namespace
17551 before. Also, add a using directive if it's an anonymous
17552 namespace. */
17553
17554 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17555 {
17556 struct type *type;
17557
17558 type = read_type_die (die, cu);
17559 new_symbol (die, type, cu);
17560
17561 namespace_name (die, &is_anonymous, cu);
17562 if (is_anonymous)
17563 {
17564 const char *previous_prefix = determine_prefix (die, cu);
17565
17566 std::vector<const char *> excludes;
17567 add_using_directive (using_directives (cu),
17568 previous_prefix, type->name (), NULL,
17569 NULL, excludes, 0, &objfile->objfile_obstack);
17570 }
17571 }
17572
17573 if (die->child != NULL)
17574 {
17575 struct die_info *child_die = die->child;
17576
17577 while (child_die && child_die->tag)
17578 {
17579 process_die (child_die, cu);
17580 child_die = child_die->sibling;
17581 }
17582 }
17583 }
17584
17585 /* Read a Fortran module as type. This DIE can be only a declaration used for
17586 imported module. Still we need that type as local Fortran "use ... only"
17587 declaration imports depend on the created type in determine_prefix. */
17588
17589 static struct type *
17590 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17591 {
17592 struct objfile *objfile = cu->per_objfile->objfile;
17593 const char *module_name;
17594 struct type *type;
17595
17596 module_name = dwarf2_name (die, cu);
17597 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17598
17599 return set_die_type (die, type, cu);
17600 }
17601
17602 /* Read a Fortran module. */
17603
17604 static void
17605 read_module (struct die_info *die, struct dwarf2_cu *cu)
17606 {
17607 struct die_info *child_die = die->child;
17608 struct type *type;
17609
17610 type = read_type_die (die, cu);
17611 new_symbol (die, type, cu);
17612
17613 while (child_die && child_die->tag)
17614 {
17615 process_die (child_die, cu);
17616 child_die = child_die->sibling;
17617 }
17618 }
17619
17620 /* Return the name of the namespace represented by DIE. Set
17621 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17622 namespace. */
17623
17624 static const char *
17625 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17626 {
17627 struct die_info *current_die;
17628 const char *name = NULL;
17629
17630 /* Loop through the extensions until we find a name. */
17631
17632 for (current_die = die;
17633 current_die != NULL;
17634 current_die = dwarf2_extension (die, &cu))
17635 {
17636 /* We don't use dwarf2_name here so that we can detect the absence
17637 of a name -> anonymous namespace. */
17638 name = dwarf2_string_attr (die, DW_AT_name, cu);
17639
17640 if (name != NULL)
17641 break;
17642 }
17643
17644 /* Is it an anonymous namespace? */
17645
17646 *is_anonymous = (name == NULL);
17647 if (*is_anonymous)
17648 name = CP_ANONYMOUS_NAMESPACE_STR;
17649
17650 return name;
17651 }
17652
17653 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17654 the user defined type vector. */
17655
17656 static struct type *
17657 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17658 {
17659 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17660 struct comp_unit_head *cu_header = &cu->header;
17661 struct type *type;
17662 struct attribute *attr_byte_size;
17663 struct attribute *attr_address_class;
17664 int byte_size, addr_class;
17665 struct type *target_type;
17666
17667 target_type = die_type (die, cu);
17668
17669 /* The die_type call above may have already set the type for this DIE. */
17670 type = get_die_type (die, cu);
17671 if (type)
17672 return type;
17673
17674 type = lookup_pointer_type (target_type);
17675
17676 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17677 if (attr_byte_size)
17678 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17679 else
17680 byte_size = cu_header->addr_size;
17681
17682 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17683 if (attr_address_class)
17684 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17685 else
17686 addr_class = DW_ADDR_none;
17687
17688 ULONGEST alignment = get_alignment (cu, die);
17689
17690 /* If the pointer size, alignment, or address class is different
17691 than the default, create a type variant marked as such and set
17692 the length accordingly. */
17693 if (TYPE_LENGTH (type) != byte_size
17694 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17695 && alignment != TYPE_RAW_ALIGN (type))
17696 || addr_class != DW_ADDR_none)
17697 {
17698 if (gdbarch_address_class_type_flags_p (gdbarch))
17699 {
17700 type_instance_flags type_flags
17701 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17702 addr_class);
17703 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17704 == 0);
17705 type = make_type_with_address_space (type, type_flags);
17706 }
17707 else if (TYPE_LENGTH (type) != byte_size)
17708 {
17709 complaint (_("invalid pointer size %d"), byte_size);
17710 }
17711 else if (TYPE_RAW_ALIGN (type) != alignment)
17712 {
17713 complaint (_("Invalid DW_AT_alignment"
17714 " - DIE at %s [in module %s]"),
17715 sect_offset_str (die->sect_off),
17716 objfile_name (cu->per_objfile->objfile));
17717 }
17718 else
17719 {
17720 /* Should we also complain about unhandled address classes? */
17721 }
17722 }
17723
17724 TYPE_LENGTH (type) = byte_size;
17725 set_type_align (type, alignment);
17726 return set_die_type (die, type, cu);
17727 }
17728
17729 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17730 the user defined type vector. */
17731
17732 static struct type *
17733 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17734 {
17735 struct type *type;
17736 struct type *to_type;
17737 struct type *domain;
17738
17739 to_type = die_type (die, cu);
17740 domain = die_containing_type (die, cu);
17741
17742 /* The calls above may have already set the type for this DIE. */
17743 type = get_die_type (die, cu);
17744 if (type)
17745 return type;
17746
17747 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17748 type = lookup_methodptr_type (to_type);
17749 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17750 {
17751 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17752
17753 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17754 to_type->fields (), to_type->num_fields (),
17755 to_type->has_varargs ());
17756 type = lookup_methodptr_type (new_type);
17757 }
17758 else
17759 type = lookup_memberptr_type (to_type, domain);
17760
17761 return set_die_type (die, type, cu);
17762 }
17763
17764 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17765 the user defined type vector. */
17766
17767 static struct type *
17768 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17769 enum type_code refcode)
17770 {
17771 struct comp_unit_head *cu_header = &cu->header;
17772 struct type *type, *target_type;
17773 struct attribute *attr;
17774
17775 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17776
17777 target_type = die_type (die, cu);
17778
17779 /* The die_type call above may have already set the type for this DIE. */
17780 type = get_die_type (die, cu);
17781 if (type)
17782 return type;
17783
17784 type = lookup_reference_type (target_type, refcode);
17785 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17786 if (attr != nullptr)
17787 {
17788 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17789 }
17790 else
17791 {
17792 TYPE_LENGTH (type) = cu_header->addr_size;
17793 }
17794 maybe_set_alignment (cu, die, type);
17795 return set_die_type (die, type, cu);
17796 }
17797
17798 /* Add the given cv-qualifiers to the element type of the array. GCC
17799 outputs DWARF type qualifiers that apply to an array, not the
17800 element type. But GDB relies on the array element type to carry
17801 the cv-qualifiers. This mimics section 6.7.3 of the C99
17802 specification. */
17803
17804 static struct type *
17805 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17806 struct type *base_type, int cnst, int voltl)
17807 {
17808 struct type *el_type, *inner_array;
17809
17810 base_type = copy_type (base_type);
17811 inner_array = base_type;
17812
17813 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17814 {
17815 TYPE_TARGET_TYPE (inner_array) =
17816 copy_type (TYPE_TARGET_TYPE (inner_array));
17817 inner_array = TYPE_TARGET_TYPE (inner_array);
17818 }
17819
17820 el_type = TYPE_TARGET_TYPE (inner_array);
17821 cnst |= TYPE_CONST (el_type);
17822 voltl |= TYPE_VOLATILE (el_type);
17823 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17824
17825 return set_die_type (die, base_type, cu);
17826 }
17827
17828 static struct type *
17829 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17830 {
17831 struct type *base_type, *cv_type;
17832
17833 base_type = die_type (die, cu);
17834
17835 /* The die_type call above may have already set the type for this DIE. */
17836 cv_type = get_die_type (die, cu);
17837 if (cv_type)
17838 return cv_type;
17839
17840 /* In case the const qualifier is applied to an array type, the element type
17841 is so qualified, not the array type (section 6.7.3 of C99). */
17842 if (base_type->code () == TYPE_CODE_ARRAY)
17843 return add_array_cv_type (die, cu, base_type, 1, 0);
17844
17845 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17846 return set_die_type (die, cv_type, cu);
17847 }
17848
17849 static struct type *
17850 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17851 {
17852 struct type *base_type, *cv_type;
17853
17854 base_type = die_type (die, cu);
17855
17856 /* The die_type call above may have already set the type for this DIE. */
17857 cv_type = get_die_type (die, cu);
17858 if (cv_type)
17859 return cv_type;
17860
17861 /* In case the volatile qualifier is applied to an array type, the
17862 element type is so qualified, not the array type (section 6.7.3
17863 of C99). */
17864 if (base_type->code () == TYPE_CODE_ARRAY)
17865 return add_array_cv_type (die, cu, base_type, 0, 1);
17866
17867 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17868 return set_die_type (die, cv_type, cu);
17869 }
17870
17871 /* Handle DW_TAG_restrict_type. */
17872
17873 static struct type *
17874 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17875 {
17876 struct type *base_type, *cv_type;
17877
17878 base_type = die_type (die, cu);
17879
17880 /* The die_type call above may have already set the type for this DIE. */
17881 cv_type = get_die_type (die, cu);
17882 if (cv_type)
17883 return cv_type;
17884
17885 cv_type = make_restrict_type (base_type);
17886 return set_die_type (die, cv_type, cu);
17887 }
17888
17889 /* Handle DW_TAG_atomic_type. */
17890
17891 static struct type *
17892 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17893 {
17894 struct type *base_type, *cv_type;
17895
17896 base_type = die_type (die, cu);
17897
17898 /* The die_type call above may have already set the type for this DIE. */
17899 cv_type = get_die_type (die, cu);
17900 if (cv_type)
17901 return cv_type;
17902
17903 cv_type = make_atomic_type (base_type);
17904 return set_die_type (die, cv_type, cu);
17905 }
17906
17907 /* Extract all information from a DW_TAG_string_type DIE and add to
17908 the user defined type vector. It isn't really a user defined type,
17909 but it behaves like one, with other DIE's using an AT_user_def_type
17910 attribute to reference it. */
17911
17912 static struct type *
17913 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17914 {
17915 struct objfile *objfile = cu->per_objfile->objfile;
17916 struct gdbarch *gdbarch = objfile->arch ();
17917 struct type *type, *range_type, *index_type, *char_type;
17918 struct attribute *attr;
17919 struct dynamic_prop prop;
17920 bool length_is_constant = true;
17921 LONGEST length;
17922
17923 /* There are a couple of places where bit sizes might be made use of
17924 when parsing a DW_TAG_string_type, however, no producer that we know
17925 of make use of these. Handling bit sizes that are a multiple of the
17926 byte size is easy enough, but what about other bit sizes? Lets deal
17927 with that problem when we have to. Warn about these attributes being
17928 unsupported, then parse the type and ignore them like we always
17929 have. */
17930 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17931 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17932 {
17933 static bool warning_printed = false;
17934 if (!warning_printed)
17935 {
17936 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17937 "currently supported on DW_TAG_string_type."));
17938 warning_printed = true;
17939 }
17940 }
17941
17942 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17943 if (attr != nullptr && !attr->form_is_constant ())
17944 {
17945 /* The string length describes the location at which the length of
17946 the string can be found. The size of the length field can be
17947 specified with one of the attributes below. */
17948 struct type *prop_type;
17949 struct attribute *len
17950 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17951 if (len == nullptr)
17952 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17953 if (len != nullptr && len->form_is_constant ())
17954 {
17955 /* Pass 0 as the default as we know this attribute is constant
17956 and the default value will not be returned. */
17957 LONGEST sz = len->constant_value (0);
17958 prop_type = cu->per_objfile->int_type (sz, true);
17959 }
17960 else
17961 {
17962 /* If the size is not specified then we assume it is the size of
17963 an address on this target. */
17964 prop_type = cu->addr_sized_int_type (true);
17965 }
17966
17967 /* Convert the attribute into a dynamic property. */
17968 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17969 length = 1;
17970 else
17971 length_is_constant = false;
17972 }
17973 else if (attr != nullptr)
17974 {
17975 /* This DW_AT_string_length just contains the length with no
17976 indirection. There's no need to create a dynamic property in this
17977 case. Pass 0 for the default value as we know it will not be
17978 returned in this case. */
17979 length = attr->constant_value (0);
17980 }
17981 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17982 {
17983 /* We don't currently support non-constant byte sizes for strings. */
17984 length = attr->constant_value (1);
17985 }
17986 else
17987 {
17988 /* Use 1 as a fallback length if we have nothing else. */
17989 length = 1;
17990 }
17991
17992 index_type = objfile_type (objfile)->builtin_int;
17993 if (length_is_constant)
17994 range_type = create_static_range_type (NULL, index_type, 1, length);
17995 else
17996 {
17997 struct dynamic_prop low_bound;
17998
17999 low_bound.set_const_val (1);
18000 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
18001 }
18002 char_type = language_string_char_type (cu->language_defn, gdbarch);
18003 type = create_string_type (NULL, char_type, range_type);
18004
18005 return set_die_type (die, type, cu);
18006 }
18007
18008 /* Assuming that DIE corresponds to a function, returns nonzero
18009 if the function is prototyped. */
18010
18011 static int
18012 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
18013 {
18014 struct attribute *attr;
18015
18016 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
18017 if (attr && attr->as_boolean ())
18018 return 1;
18019
18020 /* The DWARF standard implies that the DW_AT_prototyped attribute
18021 is only meaningful for C, but the concept also extends to other
18022 languages that allow unprototyped functions (Eg: Objective C).
18023 For all other languages, assume that functions are always
18024 prototyped. */
18025 if (cu->language != language_c
18026 && cu->language != language_objc
18027 && cu->language != language_opencl)
18028 return 1;
18029
18030 /* RealView does not emit DW_AT_prototyped. We can not distinguish
18031 prototyped and unprototyped functions; default to prototyped,
18032 since that is more common in modern code (and RealView warns
18033 about unprototyped functions). */
18034 if (producer_is_realview (cu->producer))
18035 return 1;
18036
18037 return 0;
18038 }
18039
18040 /* Handle DIES due to C code like:
18041
18042 struct foo
18043 {
18044 int (*funcp)(int a, long l);
18045 int b;
18046 };
18047
18048 ('funcp' generates a DW_TAG_subroutine_type DIE). */
18049
18050 static struct type *
18051 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
18052 {
18053 struct objfile *objfile = cu->per_objfile->objfile;
18054 struct type *type; /* Type that this function returns. */
18055 struct type *ftype; /* Function that returns above type. */
18056 struct attribute *attr;
18057
18058 type = die_type (die, cu);
18059
18060 /* The die_type call above may have already set the type for this DIE. */
18061 ftype = get_die_type (die, cu);
18062 if (ftype)
18063 return ftype;
18064
18065 ftype = lookup_function_type (type);
18066
18067 if (prototyped_function_p (die, cu))
18068 ftype->set_is_prototyped (true);
18069
18070 /* Store the calling convention in the type if it's available in
18071 the subroutine die. Otherwise set the calling convention to
18072 the default value DW_CC_normal. */
18073 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
18074 if (attr != nullptr
18075 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
18076 TYPE_CALLING_CONVENTION (ftype)
18077 = (enum dwarf_calling_convention) attr->constant_value (0);
18078 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
18079 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
18080 else
18081 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
18082
18083 /* Record whether the function returns normally to its caller or not
18084 if the DWARF producer set that information. */
18085 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
18086 if (attr && attr->as_boolean ())
18087 TYPE_NO_RETURN (ftype) = 1;
18088
18089 /* We need to add the subroutine type to the die immediately so
18090 we don't infinitely recurse when dealing with parameters
18091 declared as the same subroutine type. */
18092 set_die_type (die, ftype, cu);
18093
18094 if (die->child != NULL)
18095 {
18096 struct type *void_type = objfile_type (objfile)->builtin_void;
18097 struct die_info *child_die;
18098 int nparams, iparams;
18099
18100 /* Count the number of parameters.
18101 FIXME: GDB currently ignores vararg functions, but knows about
18102 vararg member functions. */
18103 nparams = 0;
18104 child_die = die->child;
18105 while (child_die && child_die->tag)
18106 {
18107 if (child_die->tag == DW_TAG_formal_parameter)
18108 nparams++;
18109 else if (child_die->tag == DW_TAG_unspecified_parameters)
18110 ftype->set_has_varargs (true);
18111
18112 child_die = child_die->sibling;
18113 }
18114
18115 /* Allocate storage for parameters and fill them in. */
18116 ftype->set_num_fields (nparams);
18117 ftype->set_fields
18118 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
18119
18120 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
18121 even if we error out during the parameters reading below. */
18122 for (iparams = 0; iparams < nparams; iparams++)
18123 ftype->field (iparams).set_type (void_type);
18124
18125 iparams = 0;
18126 child_die = die->child;
18127 while (child_die && child_die->tag)
18128 {
18129 if (child_die->tag == DW_TAG_formal_parameter)
18130 {
18131 struct type *arg_type;
18132
18133 /* DWARF version 2 has no clean way to discern C++
18134 static and non-static member functions. G++ helps
18135 GDB by marking the first parameter for non-static
18136 member functions (which is the this pointer) as
18137 artificial. We pass this information to
18138 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
18139
18140 DWARF version 3 added DW_AT_object_pointer, which GCC
18141 4.5 does not yet generate. */
18142 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
18143 if (attr != nullptr)
18144 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
18145 else
18146 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
18147 arg_type = die_type (child_die, cu);
18148
18149 /* RealView does not mark THIS as const, which the testsuite
18150 expects. GCC marks THIS as const in method definitions,
18151 but not in the class specifications (GCC PR 43053). */
18152 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
18153 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
18154 {
18155 int is_this = 0;
18156 struct dwarf2_cu *arg_cu = cu;
18157 const char *name = dwarf2_name (child_die, cu);
18158
18159 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
18160 if (attr != nullptr)
18161 {
18162 /* If the compiler emits this, use it. */
18163 if (follow_die_ref (die, attr, &arg_cu) == child_die)
18164 is_this = 1;
18165 }
18166 else if (name && strcmp (name, "this") == 0)
18167 /* Function definitions will have the argument names. */
18168 is_this = 1;
18169 else if (name == NULL && iparams == 0)
18170 /* Declarations may not have the names, so like
18171 elsewhere in GDB, assume an artificial first
18172 argument is "this". */
18173 is_this = 1;
18174
18175 if (is_this)
18176 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
18177 arg_type, 0);
18178 }
18179
18180 ftype->field (iparams).set_type (arg_type);
18181 iparams++;
18182 }
18183 child_die = child_die->sibling;
18184 }
18185 }
18186
18187 return ftype;
18188 }
18189
18190 static struct type *
18191 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
18192 {
18193 struct objfile *objfile = cu->per_objfile->objfile;
18194 const char *name = NULL;
18195 struct type *this_type, *target_type;
18196
18197 name = dwarf2_full_name (NULL, die, cu);
18198 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
18199 this_type->set_target_is_stub (true);
18200 set_die_type (die, this_type, cu);
18201 target_type = die_type (die, cu);
18202 if (target_type != this_type)
18203 TYPE_TARGET_TYPE (this_type) = target_type;
18204 else
18205 {
18206 /* Self-referential typedefs are, it seems, not allowed by the DWARF
18207 spec and cause infinite loops in GDB. */
18208 complaint (_("Self-referential DW_TAG_typedef "
18209 "- DIE at %s [in module %s]"),
18210 sect_offset_str (die->sect_off), objfile_name (objfile));
18211 TYPE_TARGET_TYPE (this_type) = NULL;
18212 }
18213 if (name == NULL)
18214 {
18215 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
18216 anonymous typedefs, which is, strictly speaking, invalid DWARF.
18217 Handle these by just returning the target type, rather than
18218 constructing an anonymous typedef type and trying to handle this
18219 elsewhere. */
18220 set_die_type (die, target_type, cu);
18221 return target_type;
18222 }
18223 return this_type;
18224 }
18225
18226 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
18227 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
18228
18229 If the numerator and/or numerator attribute is missing,
18230 a complaint is filed, and NUMERATOR and DENOMINATOR are left
18231 untouched. */
18232
18233 static void
18234 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
18235 gdb_mpz *numerator, gdb_mpz *denominator)
18236 {
18237 struct attribute *num_attr, *denom_attr;
18238
18239 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
18240 if (num_attr == nullptr)
18241 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
18242 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18243
18244 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
18245 if (denom_attr == nullptr)
18246 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
18247 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18248
18249 if (num_attr == nullptr || denom_attr == nullptr)
18250 return;
18251
18252 if (num_attr->form_is_block ())
18253 {
18254 dwarf_block *blk = num_attr->as_block ();
18255 mpz_import (numerator->val, blk->size,
18256 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18257 1, 0, 0, blk->data);
18258 }
18259 else
18260 *numerator = gdb_mpz (num_attr->constant_value (1));
18261
18262 if (denom_attr->form_is_block ())
18263 {
18264 dwarf_block *blk = denom_attr->as_block ();
18265 mpz_import (denominator->val, blk->size,
18266 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18267 1, 0, 0, blk->data);
18268 }
18269 else
18270 *denominator = gdb_mpz (denom_attr->constant_value (1));
18271 }
18272
18273 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
18274 rational constant, rather than a signed one.
18275
18276 If the rational constant has a negative value, a complaint
18277 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
18278
18279 static void
18280 get_dwarf2_unsigned_rational_constant (struct die_info *die,
18281 struct dwarf2_cu *cu,
18282 gdb_mpz *numerator,
18283 gdb_mpz *denominator)
18284 {
18285 gdb_mpz num (1);
18286 gdb_mpz denom (1);
18287
18288 get_dwarf2_rational_constant (die, cu, &num, &denom);
18289 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
18290 {
18291 mpz_neg (num.val, num.val);
18292 mpz_neg (denom.val, denom.val);
18293 }
18294 else if (mpz_sgn (num.val) == -1)
18295 {
18296 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
18297 " in DIE at %s"),
18298 sect_offset_str (die->sect_off));
18299 return;
18300 }
18301 else if (mpz_sgn (denom.val) == -1)
18302 {
18303 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
18304 " in DIE at %s"),
18305 sect_offset_str (die->sect_off));
18306 return;
18307 }
18308
18309 *numerator = std::move (num);
18310 *denominator = std::move (denom);
18311 }
18312
18313 /* Assuming DIE corresponds to a fixed point type, finish the creation
18314 of the corresponding TYPE by setting its type-specific data.
18315 CU is the DIE's CU. */
18316
18317 static void
18318 finish_fixed_point_type (struct type *type, struct die_info *die,
18319 struct dwarf2_cu *cu)
18320 {
18321 struct attribute *attr;
18322
18323 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
18324 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
18325
18326 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
18327 if (!attr)
18328 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
18329 if (!attr)
18330 attr = dwarf2_attr (die, DW_AT_small, cu);
18331
18332 /* Numerator and denominator of our fixed-point type's scaling factor.
18333 The default is a scaling factor of 1, which we use as a fallback
18334 when we are not able to decode it (problem with the debugging info,
18335 unsupported forms, bug in GDB, etc...). Using that as the default
18336 allows us to at least print the unscaled value, which might still
18337 be useful to a user. */
18338 gdb_mpz scale_num (1);
18339 gdb_mpz scale_denom (1);
18340
18341 if (attr == nullptr)
18342 {
18343 /* Scaling factor not found. Assume a scaling factor of 1,
18344 and hope for the best. At least the user will be able to see
18345 the encoded value. */
18346 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18347 sect_offset_str (die->sect_off));
18348 }
18349 else if (attr->name == DW_AT_binary_scale)
18350 {
18351 LONGEST scale_exp = attr->constant_value (0);
18352 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18353
18354 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
18355 }
18356 else if (attr->name == DW_AT_decimal_scale)
18357 {
18358 LONGEST scale_exp = attr->constant_value (0);
18359 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18360
18361 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
18362 }
18363 else if (attr->name == DW_AT_small)
18364 {
18365 struct die_info *scale_die;
18366 struct dwarf2_cu *scale_cu = cu;
18367
18368 scale_die = follow_die_ref (die, attr, &scale_cu);
18369 if (scale_die->tag == DW_TAG_constant)
18370 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18371 &scale_num, &scale_denom);
18372 else
18373 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18374 " (DIE at %s)"),
18375 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18376 }
18377 else
18378 {
18379 complaint (_("unsupported scale attribute %s for fixed-point type"
18380 " (DIE at %s)"),
18381 dwarf_attr_name (attr->name),
18382 sect_offset_str (die->sect_off));
18383 }
18384
18385 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
18386 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18387 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
18388 mpq_canonicalize (scaling_factor.val);
18389 }
18390
18391 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18392 (which may be different from NAME) to the architecture back-end to allow
18393 it to guess the correct format if necessary. */
18394
18395 static struct type *
18396 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
18397 const char *name_hint, enum bfd_endian byte_order)
18398 {
18399 struct gdbarch *gdbarch = objfile->arch ();
18400 const struct floatformat **format;
18401 struct type *type;
18402
18403 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18404 if (format)
18405 type = init_float_type (objfile, bits, name, format, byte_order);
18406 else
18407 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18408
18409 return type;
18410 }
18411
18412 /* Allocate an integer type of size BITS and name NAME. */
18413
18414 static struct type *
18415 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18416 int bits, int unsigned_p, const char *name)
18417 {
18418 struct type *type;
18419
18420 /* Versions of Intel's C Compiler generate an integer type called "void"
18421 instead of using DW_TAG_unspecified_type. This has been seen on
18422 at least versions 14, 17, and 18. */
18423 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18424 && strcmp (name, "void") == 0)
18425 type = objfile_type (objfile)->builtin_void;
18426 else
18427 type = init_integer_type (objfile, bits, unsigned_p, name);
18428
18429 return type;
18430 }
18431
18432 /* Return true if DIE has a DW_AT_small attribute whose value is
18433 a constant rational, where both the numerator and denominator
18434 are equal to zero.
18435
18436 CU is the DIE's Compilation Unit. */
18437
18438 static bool
18439 has_zero_over_zero_small_attribute (struct die_info *die,
18440 struct dwarf2_cu *cu)
18441 {
18442 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18443 if (attr == nullptr)
18444 return false;
18445
18446 struct dwarf2_cu *scale_cu = cu;
18447 struct die_info *scale_die
18448 = follow_die_ref (die, attr, &scale_cu);
18449
18450 if (scale_die->tag != DW_TAG_constant)
18451 return false;
18452
18453 gdb_mpz num (1), denom (1);
18454 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
18455 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
18456 }
18457
18458 /* Initialise and return a floating point type of size BITS suitable for
18459 use as a component of a complex number. The NAME_HINT is passed through
18460 when initialising the floating point type and is the name of the complex
18461 type.
18462
18463 As DWARF doesn't currently provide an explicit name for the components
18464 of a complex number, but it can be helpful to have these components
18465 named, we try to select a suitable name based on the size of the
18466 component. */
18467 static struct type *
18468 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18469 struct objfile *objfile,
18470 int bits, const char *name_hint,
18471 enum bfd_endian byte_order)
18472 {
18473 gdbarch *gdbarch = objfile->arch ();
18474 struct type *tt = nullptr;
18475
18476 /* Try to find a suitable floating point builtin type of size BITS.
18477 We're going to use the name of this type as the name for the complex
18478 target type that we are about to create. */
18479 switch (cu->language)
18480 {
18481 case language_fortran:
18482 switch (bits)
18483 {
18484 case 32:
18485 tt = builtin_f_type (gdbarch)->builtin_real;
18486 break;
18487 case 64:
18488 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18489 break;
18490 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18491 case 128:
18492 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18493 break;
18494 }
18495 break;
18496 default:
18497 switch (bits)
18498 {
18499 case 32:
18500 tt = builtin_type (gdbarch)->builtin_float;
18501 break;
18502 case 64:
18503 tt = builtin_type (gdbarch)->builtin_double;
18504 break;
18505 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18506 case 128:
18507 tt = builtin_type (gdbarch)->builtin_long_double;
18508 break;
18509 }
18510 break;
18511 }
18512
18513 /* If the type we found doesn't match the size we were looking for, then
18514 pretend we didn't find a type at all, the complex target type we
18515 create will then be nameless. */
18516 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
18517 tt = nullptr;
18518
18519 const char *name = (tt == nullptr) ? nullptr : tt->name ();
18520 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
18521 }
18522
18523 /* Find a representation of a given base type and install
18524 it in the TYPE field of the die. */
18525
18526 static struct type *
18527 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
18528 {
18529 struct objfile *objfile = cu->per_objfile->objfile;
18530 struct type *type;
18531 struct attribute *attr;
18532 int encoding = 0, bits = 0;
18533 const char *name;
18534 gdbarch *arch;
18535
18536 attr = dwarf2_attr (die, DW_AT_encoding, cu);
18537 if (attr != nullptr && attr->form_is_constant ())
18538 encoding = attr->constant_value (0);
18539 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18540 if (attr != nullptr)
18541 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
18542 name = dwarf2_name (die, cu);
18543 if (!name)
18544 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
18545
18546 arch = objfile->arch ();
18547 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18548
18549 attr = dwarf2_attr (die, DW_AT_endianity, cu);
18550 if (attr != nullptr && attr->form_is_constant ())
18551 {
18552 int endianity = attr->constant_value (0);
18553
18554 switch (endianity)
18555 {
18556 case DW_END_big:
18557 byte_order = BFD_ENDIAN_BIG;
18558 break;
18559 case DW_END_little:
18560 byte_order = BFD_ENDIAN_LITTLE;
18561 break;
18562 default:
18563 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18564 break;
18565 }
18566 }
18567
18568 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18569 && cu->language == language_ada
18570 && has_zero_over_zero_small_attribute (die, cu))
18571 {
18572 /* brobecker/2018-02-24: This is a fixed point type for which
18573 the scaling factor is represented as fraction whose value
18574 does not make sense (zero divided by zero), so we should
18575 normally never see these. However, there is a small category
18576 of fixed point types for which GNAT is unable to provide
18577 the scaling factor via the standard DWARF mechanisms, and
18578 for which the info is provided via the GNAT encodings instead.
18579 This is likely what this DIE is about.
18580
18581 Ideally, GNAT should be declaring this type the same way
18582 it declares other fixed point types when using the legacy
18583 GNAT encoding, which is to use a simple signed or unsigned
18584 base type. A report to the GNAT team has been created to
18585 look into it. In the meantime, pretend this type is a simple
18586 signed or unsigned integral, rather than a fixed point type,
18587 to avoid any confusion later on as to how to process this type. */
18588 encoding = (encoding == DW_ATE_signed_fixed
18589 ? DW_ATE_signed
18590 : DW_ATE_unsigned);
18591 }
18592
18593 switch (encoding)
18594 {
18595 case DW_ATE_address:
18596 /* Turn DW_ATE_address into a void * pointer. */
18597 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18598 type = init_pointer_type (objfile, bits, name, type);
18599 break;
18600 case DW_ATE_boolean:
18601 type = init_boolean_type (objfile, bits, 1, name);
18602 break;
18603 case DW_ATE_complex_float:
18604 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18605 byte_order);
18606 if (type->code () == TYPE_CODE_ERROR)
18607 {
18608 if (name == nullptr)
18609 {
18610 struct obstack *obstack
18611 = &cu->per_objfile->objfile->objfile_obstack;
18612 name = obconcat (obstack, "_Complex ", type->name (),
18613 nullptr);
18614 }
18615 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18616 }
18617 else
18618 type = init_complex_type (name, type);
18619 break;
18620 case DW_ATE_decimal_float:
18621 type = init_decfloat_type (objfile, bits, name);
18622 break;
18623 case DW_ATE_float:
18624 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18625 break;
18626 case DW_ATE_signed:
18627 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18628 break;
18629 case DW_ATE_unsigned:
18630 if (cu->language == language_fortran
18631 && name
18632 && startswith (name, "character("))
18633 type = init_character_type (objfile, bits, 1, name);
18634 else
18635 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18636 break;
18637 case DW_ATE_signed_char:
18638 if (cu->language == language_ada || cu->language == language_m2
18639 || cu->language == language_pascal
18640 || cu->language == language_fortran)
18641 type = init_character_type (objfile, bits, 0, name);
18642 else
18643 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18644 break;
18645 case DW_ATE_unsigned_char:
18646 if (cu->language == language_ada || cu->language == language_m2
18647 || cu->language == language_pascal
18648 || cu->language == language_fortran
18649 || cu->language == language_rust)
18650 type = init_character_type (objfile, bits, 1, name);
18651 else
18652 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18653 break;
18654 case DW_ATE_UTF:
18655 {
18656 if (bits == 16)
18657 type = builtin_type (arch)->builtin_char16;
18658 else if (bits == 32)
18659 type = builtin_type (arch)->builtin_char32;
18660 else
18661 {
18662 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18663 bits);
18664 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18665 }
18666 return set_die_type (die, type, cu);
18667 }
18668 break;
18669 case DW_ATE_signed_fixed:
18670 type = init_fixed_point_type (objfile, bits, 0, name);
18671 finish_fixed_point_type (type, die, cu);
18672 break;
18673 case DW_ATE_unsigned_fixed:
18674 type = init_fixed_point_type (objfile, bits, 1, name);
18675 finish_fixed_point_type (type, die, cu);
18676 break;
18677
18678 default:
18679 complaint (_("unsupported DW_AT_encoding: '%s'"),
18680 dwarf_type_encoding_name (encoding));
18681 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18682 break;
18683 }
18684
18685 if (name && strcmp (name, "char") == 0)
18686 type->set_has_no_signedness (true);
18687
18688 maybe_set_alignment (cu, die, type);
18689
18690 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18691
18692 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18693 {
18694 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18695 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18696 {
18697 unsigned real_bit_size = attr->as_unsigned ();
18698 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18699 /* Only use the attributes if they make sense together. */
18700 if (attr == nullptr
18701 || (attr->as_unsigned () + real_bit_size
18702 <= 8 * TYPE_LENGTH (type)))
18703 {
18704 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18705 = real_bit_size;
18706 if (attr != nullptr)
18707 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18708 = attr->as_unsigned ();
18709 }
18710 }
18711 }
18712
18713 return set_die_type (die, type, cu);
18714 }
18715
18716 /* Parse dwarf attribute if it's a block, reference or constant and put the
18717 resulting value of the attribute into struct bound_prop.
18718 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18719
18720 static int
18721 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18722 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18723 struct type *default_type)
18724 {
18725 struct dwarf2_property_baton *baton;
18726 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18727 struct objfile *objfile = per_objfile->objfile;
18728 struct obstack *obstack = &objfile->objfile_obstack;
18729
18730 gdb_assert (default_type != NULL);
18731
18732 if (attr == NULL || prop == NULL)
18733 return 0;
18734
18735 if (attr->form_is_block ())
18736 {
18737 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18738 baton->property_type = default_type;
18739 baton->locexpr.per_cu = cu->per_cu;
18740 baton->locexpr.per_objfile = per_objfile;
18741
18742 struct dwarf_block *block = attr->as_block ();
18743 baton->locexpr.size = block->size;
18744 baton->locexpr.data = block->data;
18745 switch (attr->name)
18746 {
18747 case DW_AT_string_length:
18748 baton->locexpr.is_reference = true;
18749 break;
18750 default:
18751 baton->locexpr.is_reference = false;
18752 break;
18753 }
18754
18755 prop->set_locexpr (baton);
18756 gdb_assert (prop->baton () != NULL);
18757 }
18758 else if (attr->form_is_ref ())
18759 {
18760 struct dwarf2_cu *target_cu = cu;
18761 struct die_info *target_die;
18762 struct attribute *target_attr;
18763
18764 target_die = follow_die_ref (die, attr, &target_cu);
18765 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18766 if (target_attr == NULL)
18767 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18768 target_cu);
18769 if (target_attr == NULL)
18770 return 0;
18771
18772 switch (target_attr->name)
18773 {
18774 case DW_AT_location:
18775 if (target_attr->form_is_section_offset ())
18776 {
18777 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18778 baton->property_type = die_type (target_die, target_cu);
18779 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18780 prop->set_loclist (baton);
18781 gdb_assert (prop->baton () != NULL);
18782 }
18783 else if (target_attr->form_is_block ())
18784 {
18785 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18786 baton->property_type = die_type (target_die, target_cu);
18787 baton->locexpr.per_cu = cu->per_cu;
18788 baton->locexpr.per_objfile = per_objfile;
18789 struct dwarf_block *block = target_attr->as_block ();
18790 baton->locexpr.size = block->size;
18791 baton->locexpr.data = block->data;
18792 baton->locexpr.is_reference = true;
18793 prop->set_locexpr (baton);
18794 gdb_assert (prop->baton () != NULL);
18795 }
18796 else
18797 {
18798 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18799 "dynamic property");
18800 return 0;
18801 }
18802 break;
18803 case DW_AT_data_member_location:
18804 {
18805 LONGEST offset;
18806
18807 if (!handle_data_member_location (target_die, target_cu,
18808 &offset))
18809 return 0;
18810
18811 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18812 baton->property_type = read_type_die (target_die->parent,
18813 target_cu);
18814 baton->offset_info.offset = offset;
18815 baton->offset_info.type = die_type (target_die, target_cu);
18816 prop->set_addr_offset (baton);
18817 break;
18818 }
18819 }
18820 }
18821 else if (attr->form_is_constant ())
18822 prop->set_const_val (attr->constant_value (0));
18823 else
18824 {
18825 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18826 dwarf2_name (die, cu));
18827 return 0;
18828 }
18829
18830 return 1;
18831 }
18832
18833 /* See read.h. */
18834
18835 struct type *
18836 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
18837 {
18838 struct type *int_type;
18839
18840 /* Helper macro to examine the various builtin types. */
18841 #define TRY_TYPE(F) \
18842 int_type = (unsigned_p \
18843 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18844 : objfile_type (objfile)->builtin_ ## F); \
18845 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18846 return int_type
18847
18848 TRY_TYPE (char);
18849 TRY_TYPE (short);
18850 TRY_TYPE (int);
18851 TRY_TYPE (long);
18852 TRY_TYPE (long_long);
18853
18854 #undef TRY_TYPE
18855
18856 gdb_assert_not_reached ("unable to find suitable integer type");
18857 }
18858
18859 /* See read.h. */
18860
18861 struct type *
18862 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
18863 {
18864 int addr_size = this->per_cu->addr_size ();
18865 return this->per_objfile->int_type (addr_size, unsigned_p);
18866 }
18867
18868 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18869 present (which is valid) then compute the default type based on the
18870 compilation units address size. */
18871
18872 static struct type *
18873 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18874 {
18875 struct type *index_type = die_type (die, cu);
18876
18877 /* Dwarf-2 specifications explicitly allows to create subrange types
18878 without specifying a base type.
18879 In that case, the base type must be set to the type of
18880 the lower bound, upper bound or count, in that order, if any of these
18881 three attributes references an object that has a type.
18882 If no base type is found, the Dwarf-2 specifications say that
18883 a signed integer type of size equal to the size of an address should
18884 be used.
18885 For the following C code: `extern char gdb_int [];'
18886 GCC produces an empty range DIE.
18887 FIXME: muller/2010-05-28: Possible references to object for low bound,
18888 high bound or count are not yet handled by this code. */
18889 if (index_type->code () == TYPE_CODE_VOID)
18890 index_type = cu->addr_sized_int_type (false);
18891
18892 return index_type;
18893 }
18894
18895 /* Read the given DW_AT_subrange DIE. */
18896
18897 static struct type *
18898 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18899 {
18900 struct type *base_type, *orig_base_type;
18901 struct type *range_type;
18902 struct attribute *attr;
18903 struct dynamic_prop low, high;
18904 int low_default_is_valid;
18905 int high_bound_is_count = 0;
18906 const char *name;
18907 ULONGEST negative_mask;
18908
18909 orig_base_type = read_subrange_index_type (die, cu);
18910
18911 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18912 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18913 creating the range type, but we use the result of check_typedef
18914 when examining properties of the type. */
18915 base_type = check_typedef (orig_base_type);
18916
18917 /* The die_type call above may have already set the type for this DIE. */
18918 range_type = get_die_type (die, cu);
18919 if (range_type)
18920 return range_type;
18921
18922 high.set_const_val (0);
18923
18924 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18925 omitting DW_AT_lower_bound. */
18926 switch (cu->language)
18927 {
18928 case language_c:
18929 case language_cplus:
18930 low.set_const_val (0);
18931 low_default_is_valid = 1;
18932 break;
18933 case language_fortran:
18934 low.set_const_val (1);
18935 low_default_is_valid = 1;
18936 break;
18937 case language_d:
18938 case language_objc:
18939 case language_rust:
18940 low.set_const_val (0);
18941 low_default_is_valid = (cu->header.version >= 4);
18942 break;
18943 case language_ada:
18944 case language_m2:
18945 case language_pascal:
18946 low.set_const_val (1);
18947 low_default_is_valid = (cu->header.version >= 4);
18948 break;
18949 default:
18950 low.set_const_val (0);
18951 low_default_is_valid = 0;
18952 break;
18953 }
18954
18955 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18956 if (attr != nullptr)
18957 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18958 else if (!low_default_is_valid)
18959 complaint (_("Missing DW_AT_lower_bound "
18960 "- DIE at %s [in module %s]"),
18961 sect_offset_str (die->sect_off),
18962 objfile_name (cu->per_objfile->objfile));
18963
18964 struct attribute *attr_ub, *attr_count;
18965 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18966 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18967 {
18968 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18969 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18970 {
18971 /* If bounds are constant do the final calculation here. */
18972 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18973 high.set_const_val (low.const_val () + high.const_val () - 1);
18974 else
18975 high_bound_is_count = 1;
18976 }
18977 else
18978 {
18979 if (attr_ub != NULL)
18980 complaint (_("Unresolved DW_AT_upper_bound "
18981 "- DIE at %s [in module %s]"),
18982 sect_offset_str (die->sect_off),
18983 objfile_name (cu->per_objfile->objfile));
18984 if (attr_count != NULL)
18985 complaint (_("Unresolved DW_AT_count "
18986 "- DIE at %s [in module %s]"),
18987 sect_offset_str (die->sect_off),
18988 objfile_name (cu->per_objfile->objfile));
18989 }
18990 }
18991
18992 LONGEST bias = 0;
18993 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18994 if (bias_attr != nullptr && bias_attr->form_is_constant ())
18995 bias = bias_attr->constant_value (0);
18996
18997 /* Normally, the DWARF producers are expected to use a signed
18998 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18999 But this is unfortunately not always the case, as witnessed
19000 with GCC, for instance, where the ambiguous DW_FORM_dataN form
19001 is used instead. To work around that ambiguity, we treat
19002 the bounds as signed, and thus sign-extend their values, when
19003 the base type is signed. */
19004 negative_mask =
19005 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
19006 if (low.kind () == PROP_CONST
19007 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
19008 low.set_const_val (low.const_val () | negative_mask);
19009 if (high.kind () == PROP_CONST
19010 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
19011 high.set_const_val (high.const_val () | negative_mask);
19012
19013 /* Check for bit and byte strides. */
19014 struct dynamic_prop byte_stride_prop;
19015 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
19016 if (attr_byte_stride != nullptr)
19017 {
19018 struct type *prop_type = cu->addr_sized_int_type (false);
19019 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
19020 prop_type);
19021 }
19022
19023 struct dynamic_prop bit_stride_prop;
19024 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
19025 if (attr_bit_stride != nullptr)
19026 {
19027 /* It only makes sense to have either a bit or byte stride. */
19028 if (attr_byte_stride != nullptr)
19029 {
19030 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
19031 "- DIE at %s [in module %s]"),
19032 sect_offset_str (die->sect_off),
19033 objfile_name (cu->per_objfile->objfile));
19034 attr_bit_stride = nullptr;
19035 }
19036 else
19037 {
19038 struct type *prop_type = cu->addr_sized_int_type (false);
19039 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
19040 prop_type);
19041 }
19042 }
19043
19044 if (attr_byte_stride != nullptr
19045 || attr_bit_stride != nullptr)
19046 {
19047 bool byte_stride_p = (attr_byte_stride != nullptr);
19048 struct dynamic_prop *stride
19049 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
19050
19051 range_type
19052 = create_range_type_with_stride (NULL, orig_base_type, &low,
19053 &high, bias, stride, byte_stride_p);
19054 }
19055 else
19056 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
19057
19058 if (high_bound_is_count)
19059 range_type->bounds ()->flag_upper_bound_is_count = 1;
19060
19061 /* Ada expects an empty array on no boundary attributes. */
19062 if (attr == NULL && cu->language != language_ada)
19063 range_type->bounds ()->high.set_undefined ();
19064
19065 name = dwarf2_name (die, cu);
19066 if (name)
19067 range_type->set_name (name);
19068
19069 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
19070 if (attr != nullptr)
19071 TYPE_LENGTH (range_type) = attr->constant_value (0);
19072
19073 maybe_set_alignment (cu, die, range_type);
19074
19075 set_die_type (die, range_type, cu);
19076
19077 /* set_die_type should be already done. */
19078 set_descriptive_type (range_type, die, cu);
19079
19080 return range_type;
19081 }
19082
19083 static struct type *
19084 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
19085 {
19086 struct type *type;
19087
19088 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
19089 type->set_name (dwarf2_name (die, cu));
19090
19091 /* In Ada, an unspecified type is typically used when the description
19092 of the type is deferred to a different unit. When encountering
19093 such a type, we treat it as a stub, and try to resolve it later on,
19094 when needed. */
19095 if (cu->language == language_ada)
19096 type->set_is_stub (true);
19097
19098 return set_die_type (die, type, cu);
19099 }
19100
19101 /* Read a single die and all its descendents. Set the die's sibling
19102 field to NULL; set other fields in the die correctly, and set all
19103 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
19104 location of the info_ptr after reading all of those dies. PARENT
19105 is the parent of the die in question. */
19106
19107 static struct die_info *
19108 read_die_and_children (const struct die_reader_specs *reader,
19109 const gdb_byte *info_ptr,
19110 const gdb_byte **new_info_ptr,
19111 struct die_info *parent)
19112 {
19113 struct die_info *die;
19114 const gdb_byte *cur_ptr;
19115
19116 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
19117 if (die == NULL)
19118 {
19119 *new_info_ptr = cur_ptr;
19120 return NULL;
19121 }
19122 store_in_ref_table (die, reader->cu);
19123
19124 if (die->has_children)
19125 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
19126 else
19127 {
19128 die->child = NULL;
19129 *new_info_ptr = cur_ptr;
19130 }
19131
19132 die->sibling = NULL;
19133 die->parent = parent;
19134 return die;
19135 }
19136
19137 /* Read a die, all of its descendents, and all of its siblings; set
19138 all of the fields of all of the dies correctly. Arguments are as
19139 in read_die_and_children. */
19140
19141 static struct die_info *
19142 read_die_and_siblings_1 (const struct die_reader_specs *reader,
19143 const gdb_byte *info_ptr,
19144 const gdb_byte **new_info_ptr,
19145 struct die_info *parent)
19146 {
19147 struct die_info *first_die, *last_sibling;
19148 const gdb_byte *cur_ptr;
19149
19150 cur_ptr = info_ptr;
19151 first_die = last_sibling = NULL;
19152
19153 while (1)
19154 {
19155 struct die_info *die
19156 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
19157
19158 if (die == NULL)
19159 {
19160 *new_info_ptr = cur_ptr;
19161 return first_die;
19162 }
19163
19164 if (!first_die)
19165 first_die = die;
19166 else
19167 last_sibling->sibling = die;
19168
19169 last_sibling = die;
19170 }
19171 }
19172
19173 /* Read a die, all of its descendents, and all of its siblings; set
19174 all of the fields of all of the dies correctly. Arguments are as
19175 in read_die_and_children.
19176 This the main entry point for reading a DIE and all its children. */
19177
19178 static struct die_info *
19179 read_die_and_siblings (const struct die_reader_specs *reader,
19180 const gdb_byte *info_ptr,
19181 const gdb_byte **new_info_ptr,
19182 struct die_info *parent)
19183 {
19184 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
19185 new_info_ptr, parent);
19186
19187 if (dwarf_die_debug)
19188 {
19189 fprintf_unfiltered (gdb_stdlog,
19190 "Read die from %s@0x%x of %s:\n",
19191 reader->die_section->get_name (),
19192 (unsigned) (info_ptr - reader->die_section->buffer),
19193 bfd_get_filename (reader->abfd));
19194 dump_die (die, dwarf_die_debug);
19195 }
19196
19197 return die;
19198 }
19199
19200 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
19201 attributes.
19202 The caller is responsible for filling in the extra attributes
19203 and updating (*DIEP)->num_attrs.
19204 Set DIEP to point to a newly allocated die with its information,
19205 except for its child, sibling, and parent fields. */
19206
19207 static const gdb_byte *
19208 read_full_die_1 (const struct die_reader_specs *reader,
19209 struct die_info **diep, const gdb_byte *info_ptr,
19210 int num_extra_attrs)
19211 {
19212 unsigned int abbrev_number, bytes_read, i;
19213 struct abbrev_info *abbrev;
19214 struct die_info *die;
19215 struct dwarf2_cu *cu = reader->cu;
19216 bfd *abfd = reader->abfd;
19217
19218 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
19219 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19220 info_ptr += bytes_read;
19221 if (!abbrev_number)
19222 {
19223 *diep = NULL;
19224 return info_ptr;
19225 }
19226
19227 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
19228 if (!abbrev)
19229 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
19230 abbrev_number,
19231 bfd_get_filename (abfd));
19232
19233 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
19234 die->sect_off = sect_off;
19235 die->tag = abbrev->tag;
19236 die->abbrev = abbrev_number;
19237 die->has_children = abbrev->has_children;
19238
19239 /* Make the result usable.
19240 The caller needs to update num_attrs after adding the extra
19241 attributes. */
19242 die->num_attrs = abbrev->num_attrs;
19243
19244 bool any_need_reprocess = false;
19245 for (i = 0; i < abbrev->num_attrs; ++i)
19246 {
19247 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
19248 info_ptr);
19249 if (die->attrs[i].requires_reprocessing_p ())
19250 any_need_reprocess = true;
19251 }
19252
19253 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
19254 if (attr != nullptr && attr->form_is_unsigned ())
19255 cu->str_offsets_base = attr->as_unsigned ();
19256
19257 attr = die->attr (DW_AT_loclists_base);
19258 if (attr != nullptr)
19259 cu->loclist_base = attr->as_unsigned ();
19260
19261 auto maybe_addr_base = die->addr_base ();
19262 if (maybe_addr_base.has_value ())
19263 cu->addr_base = *maybe_addr_base;
19264
19265 attr = die->attr (DW_AT_rnglists_base);
19266 if (attr != nullptr)
19267 cu->ranges_base = attr->as_unsigned ();
19268
19269 if (any_need_reprocess)
19270 {
19271 for (i = 0; i < abbrev->num_attrs; ++i)
19272 {
19273 if (die->attrs[i].requires_reprocessing_p ())
19274 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
19275 }
19276 }
19277 *diep = die;
19278 return info_ptr;
19279 }
19280
19281 /* Read a die and all its attributes.
19282 Set DIEP to point to a newly allocated die with its information,
19283 except for its child, sibling, and parent fields. */
19284
19285 static const gdb_byte *
19286 read_full_die (const struct die_reader_specs *reader,
19287 struct die_info **diep, const gdb_byte *info_ptr)
19288 {
19289 const gdb_byte *result;
19290
19291 result = read_full_die_1 (reader, diep, info_ptr, 0);
19292
19293 if (dwarf_die_debug)
19294 {
19295 fprintf_unfiltered (gdb_stdlog,
19296 "Read die from %s@0x%x of %s:\n",
19297 reader->die_section->get_name (),
19298 (unsigned) (info_ptr - reader->die_section->buffer),
19299 bfd_get_filename (reader->abfd));
19300 dump_die (*diep, dwarf_die_debug);
19301 }
19302
19303 return result;
19304 }
19305 \f
19306
19307 /* Returns nonzero if TAG represents a type that we might generate a partial
19308 symbol for. */
19309
19310 static int
19311 is_type_tag_for_partial (int tag, enum language lang)
19312 {
19313 switch (tag)
19314 {
19315 #if 0
19316 /* Some types that would be reasonable to generate partial symbols for,
19317 that we don't at present. Note that normally this does not
19318 matter, mainly because C compilers don't give names to these
19319 types, but instead emit DW_TAG_typedef. */
19320 case DW_TAG_file_type:
19321 case DW_TAG_ptr_to_member_type:
19322 case DW_TAG_set_type:
19323 case DW_TAG_string_type:
19324 case DW_TAG_subroutine_type:
19325 #endif
19326
19327 /* GNAT may emit an array with a name, but no typedef, so we
19328 need to make a symbol in this case. */
19329 case DW_TAG_array_type:
19330 return lang == language_ada;
19331
19332 case DW_TAG_base_type:
19333 case DW_TAG_class_type:
19334 case DW_TAG_interface_type:
19335 case DW_TAG_enumeration_type:
19336 case DW_TAG_structure_type:
19337 case DW_TAG_subrange_type:
19338 case DW_TAG_typedef:
19339 case DW_TAG_union_type:
19340 return 1;
19341 default:
19342 return 0;
19343 }
19344 }
19345
19346 /* Load all DIEs that are interesting for partial symbols into memory. */
19347
19348 static struct partial_die_info *
19349 load_partial_dies (const struct die_reader_specs *reader,
19350 const gdb_byte *info_ptr, int building_psymtab)
19351 {
19352 struct dwarf2_cu *cu = reader->cu;
19353 struct objfile *objfile = cu->per_objfile->objfile;
19354 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
19355 unsigned int bytes_read;
19356 unsigned int load_all = 0;
19357 int nesting_level = 1;
19358
19359 parent_die = NULL;
19360 last_die = NULL;
19361
19362 gdb_assert (cu->per_cu != NULL);
19363 if (cu->per_cu->load_all_dies)
19364 load_all = 1;
19365
19366 cu->partial_dies
19367 = htab_create_alloc_ex (cu->header.length / 12,
19368 partial_die_hash,
19369 partial_die_eq,
19370 NULL,
19371 &cu->comp_unit_obstack,
19372 hashtab_obstack_allocate,
19373 dummy_obstack_deallocate);
19374
19375 while (1)
19376 {
19377 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
19378
19379 /* A NULL abbrev means the end of a series of children. */
19380 if (abbrev == NULL)
19381 {
19382 if (--nesting_level == 0)
19383 return first_die;
19384
19385 info_ptr += bytes_read;
19386 last_die = parent_die;
19387 parent_die = parent_die->die_parent;
19388 continue;
19389 }
19390
19391 /* Check for template arguments. We never save these; if
19392 they're seen, we just mark the parent, and go on our way. */
19393 if (parent_die != NULL
19394 && cu->language == language_cplus
19395 && (abbrev->tag == DW_TAG_template_type_param
19396 || abbrev->tag == DW_TAG_template_value_param))
19397 {
19398 parent_die->has_template_arguments = 1;
19399
19400 if (!load_all)
19401 {
19402 /* We don't need a partial DIE for the template argument. */
19403 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19404 continue;
19405 }
19406 }
19407
19408 /* We only recurse into c++ subprograms looking for template arguments.
19409 Skip their other children. */
19410 if (!load_all
19411 && cu->language == language_cplus
19412 && parent_die != NULL
19413 && parent_die->tag == DW_TAG_subprogram
19414 && abbrev->tag != DW_TAG_inlined_subroutine)
19415 {
19416 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19417 continue;
19418 }
19419
19420 /* Check whether this DIE is interesting enough to save. Normally
19421 we would not be interested in members here, but there may be
19422 later variables referencing them via DW_AT_specification (for
19423 static members). */
19424 if (!load_all
19425 && !is_type_tag_for_partial (abbrev->tag, cu->language)
19426 && abbrev->tag != DW_TAG_constant
19427 && abbrev->tag != DW_TAG_enumerator
19428 && abbrev->tag != DW_TAG_subprogram
19429 && abbrev->tag != DW_TAG_inlined_subroutine
19430 && abbrev->tag != DW_TAG_lexical_block
19431 && abbrev->tag != DW_TAG_variable
19432 && abbrev->tag != DW_TAG_namespace
19433 && abbrev->tag != DW_TAG_module
19434 && abbrev->tag != DW_TAG_member
19435 && abbrev->tag != DW_TAG_imported_unit
19436 && abbrev->tag != DW_TAG_imported_declaration)
19437 {
19438 /* Otherwise we skip to the next sibling, if any. */
19439 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19440 continue;
19441 }
19442
19443 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19444 abbrev);
19445
19446 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
19447
19448 /* This two-pass algorithm for processing partial symbols has a
19449 high cost in cache pressure. Thus, handle some simple cases
19450 here which cover the majority of C partial symbols. DIEs
19451 which neither have specification tags in them, nor could have
19452 specification tags elsewhere pointing at them, can simply be
19453 processed and discarded.
19454
19455 This segment is also optional; scan_partial_symbols and
19456 add_partial_symbol will handle these DIEs if we chain
19457 them in normally. When compilers which do not emit large
19458 quantities of duplicate debug information are more common,
19459 this code can probably be removed. */
19460
19461 /* Any complete simple types at the top level (pretty much all
19462 of them, for a language without namespaces), can be processed
19463 directly. */
19464 if (parent_die == NULL
19465 && pdi.has_specification == 0
19466 && pdi.is_declaration == 0
19467 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19468 || pdi.tag == DW_TAG_base_type
19469 || pdi.tag == DW_TAG_array_type
19470 || pdi.tag == DW_TAG_subrange_type))
19471 {
19472 if (building_psymtab && pdi.raw_name != NULL)
19473 add_partial_symbol (&pdi, cu);
19474
19475 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19476 continue;
19477 }
19478
19479 /* The exception for DW_TAG_typedef with has_children above is
19480 a workaround of GCC PR debug/47510. In the case of this complaint
19481 type_name_or_error will error on such types later.
19482
19483 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19484 it could not find the child DIEs referenced later, this is checked
19485 above. In correct DWARF DW_TAG_typedef should have no children. */
19486
19487 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
19488 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
19489 "- DIE at %s [in module %s]"),
19490 sect_offset_str (pdi.sect_off), objfile_name (objfile));
19491
19492 /* If we're at the second level, and we're an enumerator, and
19493 our parent has no specification (meaning possibly lives in a
19494 namespace elsewhere), then we can add the partial symbol now
19495 instead of queueing it. */
19496 if (pdi.tag == DW_TAG_enumerator
19497 && parent_die != NULL
19498 && parent_die->die_parent == NULL
19499 && parent_die->tag == DW_TAG_enumeration_type
19500 && parent_die->has_specification == 0)
19501 {
19502 if (pdi.raw_name == NULL)
19503 complaint (_("malformed enumerator DIE ignored"));
19504 else if (building_psymtab)
19505 add_partial_symbol (&pdi, cu);
19506
19507 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19508 continue;
19509 }
19510
19511 struct partial_die_info *part_die
19512 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
19513
19514 /* We'll save this DIE so link it in. */
19515 part_die->die_parent = parent_die;
19516 part_die->die_sibling = NULL;
19517 part_die->die_child = NULL;
19518
19519 if (last_die && last_die == parent_die)
19520 last_die->die_child = part_die;
19521 else if (last_die)
19522 last_die->die_sibling = part_die;
19523
19524 last_die = part_die;
19525
19526 if (first_die == NULL)
19527 first_die = part_die;
19528
19529 /* Maybe add the DIE to the hash table. Not all DIEs that we
19530 find interesting need to be in the hash table, because we
19531 also have the parent/sibling/child chains; only those that we
19532 might refer to by offset later during partial symbol reading.
19533
19534 For now this means things that might have be the target of a
19535 DW_AT_specification, DW_AT_abstract_origin, or
19536 DW_AT_extension. DW_AT_extension will refer only to
19537 namespaces; DW_AT_abstract_origin refers to functions (and
19538 many things under the function DIE, but we do not recurse
19539 into function DIEs during partial symbol reading) and
19540 possibly variables as well; DW_AT_specification refers to
19541 declarations. Declarations ought to have the DW_AT_declaration
19542 flag. It happens that GCC forgets to put it in sometimes, but
19543 only for functions, not for types.
19544
19545 Adding more things than necessary to the hash table is harmless
19546 except for the performance cost. Adding too few will result in
19547 wasted time in find_partial_die, when we reread the compilation
19548 unit with load_all_dies set. */
19549
19550 if (load_all
19551 || abbrev->tag == DW_TAG_constant
19552 || abbrev->tag == DW_TAG_subprogram
19553 || abbrev->tag == DW_TAG_variable
19554 || abbrev->tag == DW_TAG_namespace
19555 || part_die->is_declaration)
19556 {
19557 void **slot;
19558
19559 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19560 to_underlying (part_die->sect_off),
19561 INSERT);
19562 *slot = part_die;
19563 }
19564
19565 /* For some DIEs we want to follow their children (if any). For C
19566 we have no reason to follow the children of structures; for other
19567 languages we have to, so that we can get at method physnames
19568 to infer fully qualified class names, for DW_AT_specification,
19569 and for C++ template arguments. For C++, we also look one level
19570 inside functions to find template arguments (if the name of the
19571 function does not already contain the template arguments).
19572
19573 For Ada and Fortran, we need to scan the children of subprograms
19574 and lexical blocks as well because these languages allow the
19575 definition of nested entities that could be interesting for the
19576 debugger, such as nested subprograms for instance. */
19577 if (last_die->has_children
19578 && (load_all
19579 || last_die->tag == DW_TAG_namespace
19580 || last_die->tag == DW_TAG_module
19581 || last_die->tag == DW_TAG_enumeration_type
19582 || (cu->language == language_cplus
19583 && last_die->tag == DW_TAG_subprogram
19584 && (last_die->raw_name == NULL
19585 || strchr (last_die->raw_name, '<') == NULL))
19586 || (cu->language != language_c
19587 && (last_die->tag == DW_TAG_class_type
19588 || last_die->tag == DW_TAG_interface_type
19589 || last_die->tag == DW_TAG_structure_type
19590 || last_die->tag == DW_TAG_union_type))
19591 || ((cu->language == language_ada
19592 || cu->language == language_fortran)
19593 && (last_die->tag == DW_TAG_subprogram
19594 || last_die->tag == DW_TAG_lexical_block))))
19595 {
19596 nesting_level++;
19597 parent_die = last_die;
19598 continue;
19599 }
19600
19601 /* Otherwise we skip to the next sibling, if any. */
19602 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19603
19604 /* Back to the top, do it again. */
19605 }
19606 }
19607
19608 partial_die_info::partial_die_info (sect_offset sect_off_,
19609 struct abbrev_info *abbrev)
19610 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19611 {
19612 }
19613
19614 /* See class definition. */
19615
19616 const char *
19617 partial_die_info::name (dwarf2_cu *cu)
19618 {
19619 if (!canonical_name && raw_name != nullptr)
19620 {
19621 struct objfile *objfile = cu->per_objfile->objfile;
19622 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19623 canonical_name = 1;
19624 }
19625
19626 return raw_name;
19627 }
19628
19629 /* Read a minimal amount of information into the minimal die structure.
19630 INFO_PTR should point just after the initial uleb128 of a DIE. */
19631
19632 const gdb_byte *
19633 partial_die_info::read (const struct die_reader_specs *reader,
19634 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19635 {
19636 struct dwarf2_cu *cu = reader->cu;
19637 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19638 unsigned int i;
19639 int has_low_pc_attr = 0;
19640 int has_high_pc_attr = 0;
19641 int high_pc_relative = 0;
19642
19643 for (i = 0; i < abbrev.num_attrs; ++i)
19644 {
19645 attribute attr;
19646 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19647 /* String and address offsets that need to do the reprocessing have
19648 already been read at this point, so there is no need to wait until
19649 the loop terminates to do the reprocessing. */
19650 if (attr.requires_reprocessing_p ())
19651 read_attribute_reprocess (reader, &attr, tag);
19652 /* Store the data if it is of an attribute we want to keep in a
19653 partial symbol table. */
19654 switch (attr.name)
19655 {
19656 case DW_AT_name:
19657 switch (tag)
19658 {
19659 case DW_TAG_compile_unit:
19660 case DW_TAG_partial_unit:
19661 case DW_TAG_type_unit:
19662 /* Compilation units have a DW_AT_name that is a filename, not
19663 a source language identifier. */
19664 case DW_TAG_enumeration_type:
19665 case DW_TAG_enumerator:
19666 /* These tags always have simple identifiers already; no need
19667 to canonicalize them. */
19668 canonical_name = 1;
19669 raw_name = attr.as_string ();
19670 break;
19671 default:
19672 canonical_name = 0;
19673 raw_name = attr.as_string ();
19674 break;
19675 }
19676 break;
19677 case DW_AT_linkage_name:
19678 case DW_AT_MIPS_linkage_name:
19679 /* Note that both forms of linkage name might appear. We
19680 assume they will be the same, and we only store the last
19681 one we see. */
19682 linkage_name = attr.as_string ();
19683 break;
19684 case DW_AT_low_pc:
19685 has_low_pc_attr = 1;
19686 lowpc = attr.as_address ();
19687 break;
19688 case DW_AT_high_pc:
19689 has_high_pc_attr = 1;
19690 highpc = attr.as_address ();
19691 if (cu->header.version >= 4 && attr.form_is_constant ())
19692 high_pc_relative = 1;
19693 break;
19694 case DW_AT_location:
19695 /* Support the .debug_loc offsets. */
19696 if (attr.form_is_block ())
19697 {
19698 d.locdesc = attr.as_block ();
19699 }
19700 else if (attr.form_is_section_offset ())
19701 {
19702 dwarf2_complex_location_expr_complaint ();
19703 }
19704 else
19705 {
19706 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19707 "partial symbol information");
19708 }
19709 break;
19710 case DW_AT_external:
19711 is_external = attr.as_boolean ();
19712 break;
19713 case DW_AT_declaration:
19714 is_declaration = attr.as_boolean ();
19715 break;
19716 case DW_AT_type:
19717 has_type = 1;
19718 break;
19719 case DW_AT_abstract_origin:
19720 case DW_AT_specification:
19721 case DW_AT_extension:
19722 has_specification = 1;
19723 spec_offset = attr.get_ref_die_offset ();
19724 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19725 || cu->per_cu->is_dwz);
19726 break;
19727 case DW_AT_sibling:
19728 /* Ignore absolute siblings, they might point outside of
19729 the current compile unit. */
19730 if (attr.form == DW_FORM_ref_addr)
19731 complaint (_("ignoring absolute DW_AT_sibling"));
19732 else
19733 {
19734 const gdb_byte *buffer = reader->buffer;
19735 sect_offset off = attr.get_ref_die_offset ();
19736 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19737
19738 if (sibling_ptr < info_ptr)
19739 complaint (_("DW_AT_sibling points backwards"));
19740 else if (sibling_ptr > reader->buffer_end)
19741 reader->die_section->overflow_complaint ();
19742 else
19743 sibling = sibling_ptr;
19744 }
19745 break;
19746 case DW_AT_byte_size:
19747 has_byte_size = 1;
19748 break;
19749 case DW_AT_const_value:
19750 has_const_value = 1;
19751 break;
19752 case DW_AT_calling_convention:
19753 /* DWARF doesn't provide a way to identify a program's source-level
19754 entry point. DW_AT_calling_convention attributes are only meant
19755 to describe functions' calling conventions.
19756
19757 However, because it's a necessary piece of information in
19758 Fortran, and before DWARF 4 DW_CC_program was the only
19759 piece of debugging information whose definition refers to
19760 a 'main program' at all, several compilers marked Fortran
19761 main programs with DW_CC_program --- even when those
19762 functions use the standard calling conventions.
19763
19764 Although DWARF now specifies a way to provide this
19765 information, we support this practice for backward
19766 compatibility. */
19767 if (attr.constant_value (0) == DW_CC_program
19768 && cu->language == language_fortran)
19769 main_subprogram = 1;
19770 break;
19771 case DW_AT_inline:
19772 {
19773 LONGEST value = attr.constant_value (-1);
19774 if (value == DW_INL_inlined
19775 || value == DW_INL_declared_inlined)
19776 may_be_inlined = 1;
19777 }
19778 break;
19779
19780 case DW_AT_import:
19781 if (tag == DW_TAG_imported_unit)
19782 {
19783 d.sect_off = attr.get_ref_die_offset ();
19784 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19785 || cu->per_cu->is_dwz);
19786 }
19787 break;
19788
19789 case DW_AT_main_subprogram:
19790 main_subprogram = attr.as_boolean ();
19791 break;
19792
19793 case DW_AT_ranges:
19794 {
19795 /* DW_AT_rnglists_base does not apply to DIEs from the DWO
19796 skeleton. We take advantage of the fact the DW_AT_ranges
19797 does not appear in DW_TAG_compile_unit of DWO files.
19798
19799 Attributes of the form DW_FORM_rnglistx have already had
19800 their value changed by read_rnglist_index and already
19801 include DW_AT_rnglists_base, so don't need to add the ranges
19802 base, either. */
19803 int need_ranges_base = (tag != DW_TAG_compile_unit
19804 && attr.form != DW_FORM_rnglistx);
19805 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19806 but that requires a full DIE, so instead we just
19807 reimplement it. */
19808 unsigned int ranges_offset = (attr.constant_value (0)
19809 + (need_ranges_base
19810 ? cu->ranges_base
19811 : 0));
19812
19813 /* Value of the DW_AT_ranges attribute is the offset in the
19814 .debug_ranges section. */
19815 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19816 nullptr, tag))
19817 has_pc_info = 1;
19818 }
19819 break;
19820
19821 default:
19822 break;
19823 }
19824 }
19825
19826 /* For Ada, if both the name and the linkage name appear, we prefer
19827 the latter. This lets "catch exception" work better, regardless
19828 of the order in which the name and linkage name were emitted.
19829 Really, though, this is just a workaround for the fact that gdb
19830 doesn't store both the name and the linkage name. */
19831 if (cu->language == language_ada && linkage_name != nullptr)
19832 raw_name = linkage_name;
19833
19834 if (high_pc_relative)
19835 highpc += lowpc;
19836
19837 if (has_low_pc_attr && has_high_pc_attr)
19838 {
19839 /* When using the GNU linker, .gnu.linkonce. sections are used to
19840 eliminate duplicate copies of functions and vtables and such.
19841 The linker will arbitrarily choose one and discard the others.
19842 The AT_*_pc values for such functions refer to local labels in
19843 these sections. If the section from that file was discarded, the
19844 labels are not in the output, so the relocs get a value of 0.
19845 If this is a discarded function, mark the pc bounds as invalid,
19846 so that GDB will ignore it. */
19847 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19848 {
19849 struct objfile *objfile = per_objfile->objfile;
19850 struct gdbarch *gdbarch = objfile->arch ();
19851
19852 complaint (_("DW_AT_low_pc %s is zero "
19853 "for DIE at %s [in module %s]"),
19854 paddress (gdbarch, lowpc),
19855 sect_offset_str (sect_off),
19856 objfile_name (objfile));
19857 }
19858 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19859 else if (lowpc >= highpc)
19860 {
19861 struct objfile *objfile = per_objfile->objfile;
19862 struct gdbarch *gdbarch = objfile->arch ();
19863
19864 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19865 "for DIE at %s [in module %s]"),
19866 paddress (gdbarch, lowpc),
19867 paddress (gdbarch, highpc),
19868 sect_offset_str (sect_off),
19869 objfile_name (objfile));
19870 }
19871 else
19872 has_pc_info = 1;
19873 }
19874
19875 return info_ptr;
19876 }
19877
19878 /* Find a cached partial DIE at OFFSET in CU. */
19879
19880 struct partial_die_info *
19881 dwarf2_cu::find_partial_die (sect_offset sect_off)
19882 {
19883 struct partial_die_info *lookup_die = NULL;
19884 struct partial_die_info part_die (sect_off);
19885
19886 lookup_die = ((struct partial_die_info *)
19887 htab_find_with_hash (partial_dies, &part_die,
19888 to_underlying (sect_off)));
19889
19890 return lookup_die;
19891 }
19892
19893 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19894 except in the case of .debug_types DIEs which do not reference
19895 outside their CU (they do however referencing other types via
19896 DW_FORM_ref_sig8). */
19897
19898 static const struct cu_partial_die_info
19899 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19900 {
19901 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19902 struct objfile *objfile = per_objfile->objfile;
19903 struct partial_die_info *pd = NULL;
19904
19905 if (offset_in_dwz == cu->per_cu->is_dwz
19906 && cu->header.offset_in_cu_p (sect_off))
19907 {
19908 pd = cu->find_partial_die (sect_off);
19909 if (pd != NULL)
19910 return { cu, pd };
19911 /* We missed recording what we needed.
19912 Load all dies and try again. */
19913 }
19914 else
19915 {
19916 /* TUs don't reference other CUs/TUs (except via type signatures). */
19917 if (cu->per_cu->is_debug_types)
19918 {
19919 error (_("Dwarf Error: Type Unit at offset %s contains"
19920 " external reference to offset %s [in module %s].\n"),
19921 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19922 bfd_get_filename (objfile->obfd));
19923 }
19924 dwarf2_per_cu_data *per_cu
19925 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19926 per_objfile);
19927
19928 cu = per_objfile->get_cu (per_cu);
19929 if (cu == NULL || cu->partial_dies == NULL)
19930 load_partial_comp_unit (per_cu, per_objfile, nullptr);
19931
19932 cu = per_objfile->get_cu (per_cu);
19933
19934 cu->last_used = 0;
19935 pd = cu->find_partial_die (sect_off);
19936 }
19937
19938 /* If we didn't find it, and not all dies have been loaded,
19939 load them all and try again. */
19940
19941 if (pd == NULL && cu->per_cu->load_all_dies == 0)
19942 {
19943 cu->per_cu->load_all_dies = 1;
19944
19945 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19946 THIS_CU->cu may already be in use. So we can't just free it and
19947 replace its DIEs with the ones we read in. Instead, we leave those
19948 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19949 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19950 set. */
19951 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
19952
19953 pd = cu->find_partial_die (sect_off);
19954 }
19955
19956 if (pd == NULL)
19957 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
19958 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19959 return { cu, pd };
19960 }
19961
19962 /* See if we can figure out if the class lives in a namespace. We do
19963 this by looking for a member function; its demangled name will
19964 contain namespace info, if there is any. */
19965
19966 static void
19967 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19968 struct dwarf2_cu *cu)
19969 {
19970 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19971 what template types look like, because the demangler
19972 frequently doesn't give the same name as the debug info. We
19973 could fix this by only using the demangled name to get the
19974 prefix (but see comment in read_structure_type). */
19975
19976 struct partial_die_info *real_pdi;
19977 struct partial_die_info *child_pdi;
19978
19979 /* If this DIE (this DIE's specification, if any) has a parent, then
19980 we should not do this. We'll prepend the parent's fully qualified
19981 name when we create the partial symbol. */
19982
19983 real_pdi = struct_pdi;
19984 while (real_pdi->has_specification)
19985 {
19986 auto res = find_partial_die (real_pdi->spec_offset,
19987 real_pdi->spec_is_dwz, cu);
19988 real_pdi = res.pdi;
19989 cu = res.cu;
19990 }
19991
19992 if (real_pdi->die_parent != NULL)
19993 return;
19994
19995 for (child_pdi = struct_pdi->die_child;
19996 child_pdi != NULL;
19997 child_pdi = child_pdi->die_sibling)
19998 {
19999 if (child_pdi->tag == DW_TAG_subprogram
20000 && child_pdi->linkage_name != NULL)
20001 {
20002 gdb::unique_xmalloc_ptr<char> actual_class_name
20003 (cu->language_defn->class_name_from_physname
20004 (child_pdi->linkage_name));
20005 if (actual_class_name != NULL)
20006 {
20007 struct objfile *objfile = cu->per_objfile->objfile;
20008 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
20009 struct_pdi->canonical_name = 1;
20010 }
20011 break;
20012 }
20013 }
20014 }
20015
20016 /* Return true if a DIE with TAG may have the DW_AT_const_value
20017 attribute. */
20018
20019 static bool
20020 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
20021 {
20022 switch (tag)
20023 {
20024 case DW_TAG_constant:
20025 case DW_TAG_enumerator:
20026 case DW_TAG_formal_parameter:
20027 case DW_TAG_template_value_param:
20028 case DW_TAG_variable:
20029 return true;
20030 }
20031
20032 return false;
20033 }
20034
20035 void
20036 partial_die_info::fixup (struct dwarf2_cu *cu)
20037 {
20038 /* Once we've fixed up a die, there's no point in doing so again.
20039 This also avoids a memory leak if we were to call
20040 guess_partial_die_structure_name multiple times. */
20041 if (fixup_called)
20042 return;
20043
20044 /* If we found a reference attribute and the DIE has no name, try
20045 to find a name in the referred to DIE. */
20046
20047 if (raw_name == NULL && has_specification)
20048 {
20049 struct partial_die_info *spec_die;
20050
20051 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20052 spec_die = res.pdi;
20053 cu = res.cu;
20054
20055 spec_die->fixup (cu);
20056
20057 if (spec_die->raw_name)
20058 {
20059 raw_name = spec_die->raw_name;
20060 canonical_name = spec_die->canonical_name;
20061
20062 /* Copy DW_AT_external attribute if it is set. */
20063 if (spec_die->is_external)
20064 is_external = spec_die->is_external;
20065 }
20066 }
20067
20068 if (!has_const_value && has_specification
20069 && can_have_DW_AT_const_value_p (tag))
20070 {
20071 struct partial_die_info *spec_die;
20072
20073 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20074 spec_die = res.pdi;
20075 cu = res.cu;
20076
20077 spec_die->fixup (cu);
20078
20079 if (spec_die->has_const_value)
20080 {
20081 /* Copy DW_AT_const_value attribute if it is set. */
20082 has_const_value = spec_die->has_const_value;
20083 }
20084 }
20085
20086 /* Set default names for some unnamed DIEs. */
20087
20088 if (raw_name == NULL && tag == DW_TAG_namespace)
20089 {
20090 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
20091 canonical_name = 1;
20092 }
20093
20094 /* If there is no parent die to provide a namespace, and there are
20095 children, see if we can determine the namespace from their linkage
20096 name. */
20097 if (cu->language == language_cplus
20098 && !cu->per_objfile->per_bfd->types.empty ()
20099 && die_parent == NULL
20100 && has_children
20101 && (tag == DW_TAG_class_type
20102 || tag == DW_TAG_structure_type
20103 || tag == DW_TAG_union_type))
20104 guess_partial_die_structure_name (this, cu);
20105
20106 /* GCC might emit a nameless struct or union that has a linkage
20107 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20108 if (raw_name == NULL
20109 && (tag == DW_TAG_class_type
20110 || tag == DW_TAG_interface_type
20111 || tag == DW_TAG_structure_type
20112 || tag == DW_TAG_union_type)
20113 && linkage_name != NULL)
20114 {
20115 gdb::unique_xmalloc_ptr<char> demangled
20116 (gdb_demangle (linkage_name, DMGL_TYPES));
20117 if (demangled != nullptr)
20118 {
20119 const char *base;
20120
20121 /* Strip any leading namespaces/classes, keep only the base name.
20122 DW_AT_name for named DIEs does not contain the prefixes. */
20123 base = strrchr (demangled.get (), ':');
20124 if (base && base > demangled.get () && base[-1] == ':')
20125 base++;
20126 else
20127 base = demangled.get ();
20128
20129 struct objfile *objfile = cu->per_objfile->objfile;
20130 raw_name = objfile->intern (base);
20131 canonical_name = 1;
20132 }
20133 }
20134
20135 fixup_called = 1;
20136 }
20137
20138 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
20139 contents from the given SECTION in the HEADER. */
20140 static void
20141 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
20142 struct dwarf2_section_info *section)
20143 {
20144 unsigned int bytes_read;
20145 bfd *abfd = section->get_bfd_owner ();
20146 const gdb_byte *info_ptr = section->buffer;
20147 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
20148 info_ptr += bytes_read;
20149 header->version = read_2_bytes (abfd, info_ptr);
20150 info_ptr += 2;
20151 header->addr_size = read_1_byte (abfd, info_ptr);
20152 info_ptr += 1;
20153 header->segment_collector_size = read_1_byte (abfd, info_ptr);
20154 info_ptr += 1;
20155 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
20156 }
20157
20158 /* Return the DW_AT_loclists_base value for the CU. */
20159 static ULONGEST
20160 lookup_loclist_base (struct dwarf2_cu *cu)
20161 {
20162 /* For the .dwo unit, the loclist_base points to the first offset following
20163 the header. The header consists of the following entities-
20164 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
20165 bit format)
20166 2. version (2 bytes)
20167 3. address size (1 byte)
20168 4. segment selector size (1 byte)
20169 5. offset entry count (4 bytes)
20170 These sizes are derived as per the DWARFv5 standard. */
20171 if (cu->dwo_unit != nullptr)
20172 {
20173 if (cu->header.initial_length_size == 4)
20174 return LOCLIST_HEADER_SIZE32;
20175 return LOCLIST_HEADER_SIZE64;
20176 }
20177 return cu->loclist_base;
20178 }
20179
20180 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
20181 array of offsets in the .debug_loclists section. */
20182 static CORE_ADDR
20183 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
20184 {
20185 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20186 struct objfile *objfile = per_objfile->objfile;
20187 bfd *abfd = objfile->obfd;
20188 ULONGEST loclist_base = lookup_loclist_base (cu);
20189 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20190
20191 section->read (objfile);
20192 if (section->buffer == NULL)
20193 complaint (_("DW_FORM_loclistx used without .debug_loclists "
20194 "section [in module %s]"), objfile_name (objfile));
20195 struct loclists_rnglists_header header;
20196 read_loclists_rnglists_header (&header, section);
20197 if (loclist_index >= header.offset_entry_count)
20198 complaint (_("DW_FORM_loclistx pointing outside of "
20199 ".debug_loclists offset array [in module %s]"),
20200 objfile_name (objfile));
20201 if (loclist_base + loclist_index * cu->header.offset_size
20202 >= section->size)
20203 complaint (_("DW_FORM_loclistx pointing outside of "
20204 ".debug_loclists section [in module %s]"),
20205 objfile_name (objfile));
20206 const gdb_byte *info_ptr
20207 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
20208
20209 if (cu->header.offset_size == 4)
20210 return bfd_get_32 (abfd, info_ptr) + loclist_base;
20211 else
20212 return bfd_get_64 (abfd, info_ptr) + loclist_base;
20213 }
20214
20215 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
20216 array of offsets in the .debug_rnglists section. */
20217 static CORE_ADDR
20218 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
20219 dwarf_tag tag)
20220 {
20221 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20222 struct objfile *objfile = dwarf2_per_objfile->objfile;
20223 bfd *abfd = objfile->obfd;
20224 ULONGEST rnglist_header_size =
20225 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
20226 : RNGLIST_HEADER_SIZE64);
20227 ULONGEST rnglist_base =
20228 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->ranges_base;
20229 ULONGEST start_offset =
20230 rnglist_base + rnglist_index * cu->header.offset_size;
20231
20232 /* Get rnglists section. */
20233 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
20234
20235 /* Read the rnglists section content. */
20236 section->read (objfile);
20237 if (section->buffer == nullptr)
20238 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
20239 "[in module %s]"),
20240 objfile_name (objfile));
20241
20242 /* Verify the rnglist index is valid. */
20243 struct loclists_rnglists_header header;
20244 read_loclists_rnglists_header (&header, section);
20245 if (rnglist_index >= header.offset_entry_count)
20246 error (_("DW_FORM_rnglistx index pointing outside of "
20247 ".debug_rnglists offset array [in module %s]"),
20248 objfile_name (objfile));
20249
20250 /* Validate that the offset is within the section's range. */
20251 if (start_offset >= section->size)
20252 error (_("DW_FORM_rnglistx pointing outside of "
20253 ".debug_rnglists section [in module %s]"),
20254 objfile_name (objfile));
20255
20256 /* Validate that reading won't go beyond the end of the section. */
20257 if (start_offset + cu->header.offset_size > rnglist_base + section->size)
20258 error (_("Reading DW_FORM_rnglistx index beyond end of"
20259 ".debug_rnglists section [in module %s]"),
20260 objfile_name (objfile));
20261
20262 const gdb_byte *info_ptr = section->buffer + start_offset;
20263
20264 if (cu->header.offset_size == 4)
20265 return read_4_bytes (abfd, info_ptr) + rnglist_base;
20266 else
20267 return read_8_bytes (abfd, info_ptr) + rnglist_base;
20268 }
20269
20270 /* Process the attributes that had to be skipped in the first round. These
20271 attributes are the ones that need str_offsets_base or addr_base attributes.
20272 They could not have been processed in the first round, because at the time
20273 the values of str_offsets_base or addr_base may not have been known. */
20274 static void
20275 read_attribute_reprocess (const struct die_reader_specs *reader,
20276 struct attribute *attr, dwarf_tag tag)
20277 {
20278 struct dwarf2_cu *cu = reader->cu;
20279 switch (attr->form)
20280 {
20281 case DW_FORM_addrx:
20282 case DW_FORM_GNU_addr_index:
20283 attr->set_address (read_addr_index (cu,
20284 attr->as_unsigned_reprocess ()));
20285 break;
20286 case DW_FORM_loclistx:
20287 attr->set_address (read_loclist_index (cu, attr->as_unsigned ()));
20288 break;
20289 case DW_FORM_rnglistx:
20290 attr->set_address (read_rnglist_index (cu, attr->as_unsigned (), tag));
20291 break;
20292 case DW_FORM_strx:
20293 case DW_FORM_strx1:
20294 case DW_FORM_strx2:
20295 case DW_FORM_strx3:
20296 case DW_FORM_strx4:
20297 case DW_FORM_GNU_str_index:
20298 {
20299 unsigned int str_index = attr->as_unsigned_reprocess ();
20300 gdb_assert (!attr->canonical_string_p ());
20301 if (reader->dwo_file != NULL)
20302 attr->set_string_noncanonical (read_dwo_str_index (reader,
20303 str_index));
20304 else
20305 attr->set_string_noncanonical (read_stub_str_index (cu,
20306 str_index));
20307 break;
20308 }
20309 default:
20310 gdb_assert_not_reached (_("Unexpected DWARF form."));
20311 }
20312 }
20313
20314 /* Read an attribute value described by an attribute form. */
20315
20316 static const gdb_byte *
20317 read_attribute_value (const struct die_reader_specs *reader,
20318 struct attribute *attr, unsigned form,
20319 LONGEST implicit_const, const gdb_byte *info_ptr)
20320 {
20321 struct dwarf2_cu *cu = reader->cu;
20322 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20323 struct objfile *objfile = per_objfile->objfile;
20324 bfd *abfd = reader->abfd;
20325 struct comp_unit_head *cu_header = &cu->header;
20326 unsigned int bytes_read;
20327 struct dwarf_block *blk;
20328
20329 attr->form = (enum dwarf_form) form;
20330 switch (form)
20331 {
20332 case DW_FORM_ref_addr:
20333 if (cu->header.version == 2)
20334 attr->set_unsigned (cu->header.read_address (abfd, info_ptr,
20335 &bytes_read));
20336 else
20337 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20338 &bytes_read));
20339 info_ptr += bytes_read;
20340 break;
20341 case DW_FORM_GNU_ref_alt:
20342 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20343 &bytes_read));
20344 info_ptr += bytes_read;
20345 break;
20346 case DW_FORM_addr:
20347 {
20348 struct gdbarch *gdbarch = objfile->arch ();
20349 CORE_ADDR addr = cu->header.read_address (abfd, info_ptr, &bytes_read);
20350 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20351 attr->set_address (addr);
20352 info_ptr += bytes_read;
20353 }
20354 break;
20355 case DW_FORM_block2:
20356 blk = dwarf_alloc_block (cu);
20357 blk->size = read_2_bytes (abfd, info_ptr);
20358 info_ptr += 2;
20359 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20360 info_ptr += blk->size;
20361 attr->set_block (blk);
20362 break;
20363 case DW_FORM_block4:
20364 blk = dwarf_alloc_block (cu);
20365 blk->size = read_4_bytes (abfd, info_ptr);
20366 info_ptr += 4;
20367 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20368 info_ptr += blk->size;
20369 attr->set_block (blk);
20370 break;
20371 case DW_FORM_data2:
20372 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
20373 info_ptr += 2;
20374 break;
20375 case DW_FORM_data4:
20376 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
20377 info_ptr += 4;
20378 break;
20379 case DW_FORM_data8:
20380 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
20381 info_ptr += 8;
20382 break;
20383 case DW_FORM_data16:
20384 blk = dwarf_alloc_block (cu);
20385 blk->size = 16;
20386 blk->data = read_n_bytes (abfd, info_ptr, 16);
20387 info_ptr += 16;
20388 attr->set_block (blk);
20389 break;
20390 case DW_FORM_sec_offset:
20391 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20392 &bytes_read));
20393 info_ptr += bytes_read;
20394 break;
20395 case DW_FORM_loclistx:
20396 {
20397 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20398 &bytes_read));
20399 info_ptr += bytes_read;
20400 }
20401 break;
20402 case DW_FORM_string:
20403 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20404 &bytes_read));
20405 info_ptr += bytes_read;
20406 break;
20407 case DW_FORM_strp:
20408 if (!cu->per_cu->is_dwz)
20409 {
20410 attr->set_string_noncanonical
20411 (read_indirect_string (per_objfile,
20412 abfd, info_ptr, cu_header,
20413 &bytes_read));
20414 info_ptr += bytes_read;
20415 break;
20416 }
20417 /* FALLTHROUGH */
20418 case DW_FORM_line_strp:
20419 if (!cu->per_cu->is_dwz)
20420 {
20421 attr->set_string_noncanonical
20422 (per_objfile->read_line_string (info_ptr, cu_header,
20423 &bytes_read));
20424 info_ptr += bytes_read;
20425 break;
20426 }
20427 /* FALLTHROUGH */
20428 case DW_FORM_GNU_strp_alt:
20429 {
20430 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
20431 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20432 &bytes_read);
20433
20434 attr->set_string_noncanonical
20435 (dwz->read_string (objfile, str_offset));
20436 info_ptr += bytes_read;
20437 }
20438 break;
20439 case DW_FORM_exprloc:
20440 case DW_FORM_block:
20441 blk = dwarf_alloc_block (cu);
20442 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20443 info_ptr += bytes_read;
20444 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20445 info_ptr += blk->size;
20446 attr->set_block (blk);
20447 break;
20448 case DW_FORM_block1:
20449 blk = dwarf_alloc_block (cu);
20450 blk->size = read_1_byte (abfd, info_ptr);
20451 info_ptr += 1;
20452 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20453 info_ptr += blk->size;
20454 attr->set_block (blk);
20455 break;
20456 case DW_FORM_data1:
20457 case DW_FORM_flag:
20458 attr->set_unsigned (read_1_byte (abfd, info_ptr));
20459 info_ptr += 1;
20460 break;
20461 case DW_FORM_flag_present:
20462 attr->set_unsigned (1);
20463 break;
20464 case DW_FORM_sdata:
20465 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
20466 info_ptr += bytes_read;
20467 break;
20468 case DW_FORM_rnglistx:
20469 {
20470 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20471 &bytes_read));
20472 info_ptr += bytes_read;
20473 }
20474 break;
20475 case DW_FORM_udata:
20476 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
20477 info_ptr += bytes_read;
20478 break;
20479 case DW_FORM_ref1:
20480 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20481 + read_1_byte (abfd, info_ptr)));
20482 info_ptr += 1;
20483 break;
20484 case DW_FORM_ref2:
20485 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20486 + read_2_bytes (abfd, info_ptr)));
20487 info_ptr += 2;
20488 break;
20489 case DW_FORM_ref4:
20490 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20491 + read_4_bytes (abfd, info_ptr)));
20492 info_ptr += 4;
20493 break;
20494 case DW_FORM_ref8:
20495 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20496 + read_8_bytes (abfd, info_ptr)));
20497 info_ptr += 8;
20498 break;
20499 case DW_FORM_ref_sig8:
20500 attr->set_signature (read_8_bytes (abfd, info_ptr));
20501 info_ptr += 8;
20502 break;
20503 case DW_FORM_ref_udata:
20504 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20505 + read_unsigned_leb128 (abfd, info_ptr,
20506 &bytes_read)));
20507 info_ptr += bytes_read;
20508 break;
20509 case DW_FORM_indirect:
20510 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20511 info_ptr += bytes_read;
20512 if (form == DW_FORM_implicit_const)
20513 {
20514 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20515 info_ptr += bytes_read;
20516 }
20517 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20518 info_ptr);
20519 break;
20520 case DW_FORM_implicit_const:
20521 attr->set_signed (implicit_const);
20522 break;
20523 case DW_FORM_addrx:
20524 case DW_FORM_GNU_addr_index:
20525 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20526 &bytes_read));
20527 info_ptr += bytes_read;
20528 break;
20529 case DW_FORM_strx:
20530 case DW_FORM_strx1:
20531 case DW_FORM_strx2:
20532 case DW_FORM_strx3:
20533 case DW_FORM_strx4:
20534 case DW_FORM_GNU_str_index:
20535 {
20536 ULONGEST str_index;
20537 if (form == DW_FORM_strx1)
20538 {
20539 str_index = read_1_byte (abfd, info_ptr);
20540 info_ptr += 1;
20541 }
20542 else if (form == DW_FORM_strx2)
20543 {
20544 str_index = read_2_bytes (abfd, info_ptr);
20545 info_ptr += 2;
20546 }
20547 else if (form == DW_FORM_strx3)
20548 {
20549 str_index = read_3_bytes (abfd, info_ptr);
20550 info_ptr += 3;
20551 }
20552 else if (form == DW_FORM_strx4)
20553 {
20554 str_index = read_4_bytes (abfd, info_ptr);
20555 info_ptr += 4;
20556 }
20557 else
20558 {
20559 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20560 info_ptr += bytes_read;
20561 }
20562 attr->set_unsigned_reprocess (str_index);
20563 }
20564 break;
20565 default:
20566 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20567 dwarf_form_name (form),
20568 bfd_get_filename (abfd));
20569 }
20570
20571 /* Super hack. */
20572 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20573 attr->form = DW_FORM_GNU_ref_alt;
20574
20575 /* We have seen instances where the compiler tried to emit a byte
20576 size attribute of -1 which ended up being encoded as an unsigned
20577 0xffffffff. Although 0xffffffff is technically a valid size value,
20578 an object of this size seems pretty unlikely so we can relatively
20579 safely treat these cases as if the size attribute was invalid and
20580 treat them as zero by default. */
20581 if (attr->name == DW_AT_byte_size
20582 && form == DW_FORM_data4
20583 && attr->as_unsigned () >= 0xffffffff)
20584 {
20585 complaint
20586 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20587 hex_string (attr->as_unsigned ()));
20588 attr->set_unsigned (0);
20589 }
20590
20591 return info_ptr;
20592 }
20593
20594 /* Read an attribute described by an abbreviated attribute. */
20595
20596 static const gdb_byte *
20597 read_attribute (const struct die_reader_specs *reader,
20598 struct attribute *attr, struct attr_abbrev *abbrev,
20599 const gdb_byte *info_ptr)
20600 {
20601 attr->name = abbrev->name;
20602 attr->string_is_canonical = 0;
20603 attr->requires_reprocessing = 0;
20604 return read_attribute_value (reader, attr, abbrev->form,
20605 abbrev->implicit_const, info_ptr);
20606 }
20607
20608 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20609
20610 static const char *
20611 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20612 LONGEST str_offset)
20613 {
20614 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20615 str_offset, "DW_FORM_strp");
20616 }
20617
20618 /* Return pointer to string at .debug_str offset as read from BUF.
20619 BUF is assumed to be in a compilation unit described by CU_HEADER.
20620 Return *BYTES_READ_PTR count of bytes read from BUF. */
20621
20622 static const char *
20623 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20624 const gdb_byte *buf,
20625 const struct comp_unit_head *cu_header,
20626 unsigned int *bytes_read_ptr)
20627 {
20628 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20629
20630 return read_indirect_string_at_offset (per_objfile, str_offset);
20631 }
20632
20633 /* See read.h. */
20634
20635 const char *
20636 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20637 const struct comp_unit_head *cu_header,
20638 unsigned int *bytes_read_ptr)
20639 {
20640 bfd *abfd = objfile->obfd;
20641 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20642
20643 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20644 }
20645
20646 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20647 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20648 ADDR_SIZE is the size of addresses from the CU header. */
20649
20650 static CORE_ADDR
20651 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20652 gdb::optional<ULONGEST> addr_base, int addr_size)
20653 {
20654 struct objfile *objfile = per_objfile->objfile;
20655 bfd *abfd = objfile->obfd;
20656 const gdb_byte *info_ptr;
20657 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20658
20659 per_objfile->per_bfd->addr.read (objfile);
20660 if (per_objfile->per_bfd->addr.buffer == NULL)
20661 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20662 objfile_name (objfile));
20663 if (addr_base_or_zero + addr_index * addr_size
20664 >= per_objfile->per_bfd->addr.size)
20665 error (_("DW_FORM_addr_index pointing outside of "
20666 ".debug_addr section [in module %s]"),
20667 objfile_name (objfile));
20668 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20669 + addr_index * addr_size);
20670 if (addr_size == 4)
20671 return bfd_get_32 (abfd, info_ptr);
20672 else
20673 return bfd_get_64 (abfd, info_ptr);
20674 }
20675
20676 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20677
20678 static CORE_ADDR
20679 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20680 {
20681 return read_addr_index_1 (cu->per_objfile, addr_index,
20682 cu->addr_base, cu->header.addr_size);
20683 }
20684
20685 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20686
20687 static CORE_ADDR
20688 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20689 unsigned int *bytes_read)
20690 {
20691 bfd *abfd = cu->per_objfile->objfile->obfd;
20692 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20693
20694 return read_addr_index (cu, addr_index);
20695 }
20696
20697 /* See read.h. */
20698
20699 CORE_ADDR
20700 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20701 dwarf2_per_objfile *per_objfile,
20702 unsigned int addr_index)
20703 {
20704 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20705 gdb::optional<ULONGEST> addr_base;
20706 int addr_size;
20707
20708 /* We need addr_base and addr_size.
20709 If we don't have PER_CU->cu, we have to get it.
20710 Nasty, but the alternative is storing the needed info in PER_CU,
20711 which at this point doesn't seem justified: it's not clear how frequently
20712 it would get used and it would increase the size of every PER_CU.
20713 Entry points like dwarf2_per_cu_addr_size do a similar thing
20714 so we're not in uncharted territory here.
20715 Alas we need to be a bit more complicated as addr_base is contained
20716 in the DIE.
20717
20718 We don't need to read the entire CU(/TU).
20719 We just need the header and top level die.
20720
20721 IWBN to use the aging mechanism to let us lazily later discard the CU.
20722 For now we skip this optimization. */
20723
20724 if (cu != NULL)
20725 {
20726 addr_base = cu->addr_base;
20727 addr_size = cu->header.addr_size;
20728 }
20729 else
20730 {
20731 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20732 addr_base = reader.cu->addr_base;
20733 addr_size = reader.cu->header.addr_size;
20734 }
20735
20736 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20737 }
20738
20739 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20740 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20741 DWO file. */
20742
20743 static const char *
20744 read_str_index (struct dwarf2_cu *cu,
20745 struct dwarf2_section_info *str_section,
20746 struct dwarf2_section_info *str_offsets_section,
20747 ULONGEST str_offsets_base, ULONGEST str_index)
20748 {
20749 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20750 struct objfile *objfile = per_objfile->objfile;
20751 const char *objf_name = objfile_name (objfile);
20752 bfd *abfd = objfile->obfd;
20753 const gdb_byte *info_ptr;
20754 ULONGEST str_offset;
20755 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20756
20757 str_section->read (objfile);
20758 str_offsets_section->read (objfile);
20759 if (str_section->buffer == NULL)
20760 error (_("%s used without %s section"
20761 " in CU at offset %s [in module %s]"),
20762 form_name, str_section->get_name (),
20763 sect_offset_str (cu->header.sect_off), objf_name);
20764 if (str_offsets_section->buffer == NULL)
20765 error (_("%s used without %s section"
20766 " in CU at offset %s [in module %s]"),
20767 form_name, str_section->get_name (),
20768 sect_offset_str (cu->header.sect_off), objf_name);
20769 info_ptr = (str_offsets_section->buffer
20770 + str_offsets_base
20771 + str_index * cu->header.offset_size);
20772 if (cu->header.offset_size == 4)
20773 str_offset = bfd_get_32 (abfd, info_ptr);
20774 else
20775 str_offset = bfd_get_64 (abfd, info_ptr);
20776 if (str_offset >= str_section->size)
20777 error (_("Offset from %s pointing outside of"
20778 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20779 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20780 return (const char *) (str_section->buffer + str_offset);
20781 }
20782
20783 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20784
20785 static const char *
20786 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20787 {
20788 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20789 ? reader->cu->header.addr_size : 0;
20790 return read_str_index (reader->cu,
20791 &reader->dwo_file->sections.str,
20792 &reader->dwo_file->sections.str_offsets,
20793 str_offsets_base, str_index);
20794 }
20795
20796 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20797
20798 static const char *
20799 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20800 {
20801 struct objfile *objfile = cu->per_objfile->objfile;
20802 const char *objf_name = objfile_name (objfile);
20803 static const char form_name[] = "DW_FORM_GNU_str_index";
20804 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20805
20806 if (!cu->str_offsets_base.has_value ())
20807 error (_("%s used in Fission stub without %s"
20808 " in CU at offset 0x%lx [in module %s]"),
20809 form_name, str_offsets_attr_name,
20810 (long) cu->header.offset_size, objf_name);
20811
20812 return read_str_index (cu,
20813 &cu->per_objfile->per_bfd->str,
20814 &cu->per_objfile->per_bfd->str_offsets,
20815 *cu->str_offsets_base, str_index);
20816 }
20817
20818 /* Return the length of an LEB128 number in BUF. */
20819
20820 static int
20821 leb128_size (const gdb_byte *buf)
20822 {
20823 const gdb_byte *begin = buf;
20824 gdb_byte byte;
20825
20826 while (1)
20827 {
20828 byte = *buf++;
20829 if ((byte & 128) == 0)
20830 return buf - begin;
20831 }
20832 }
20833
20834 static void
20835 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20836 {
20837 switch (lang)
20838 {
20839 case DW_LANG_C89:
20840 case DW_LANG_C99:
20841 case DW_LANG_C11:
20842 case DW_LANG_C:
20843 case DW_LANG_UPC:
20844 cu->language = language_c;
20845 break;
20846 case DW_LANG_Java:
20847 case DW_LANG_C_plus_plus:
20848 case DW_LANG_C_plus_plus_11:
20849 case DW_LANG_C_plus_plus_14:
20850 cu->language = language_cplus;
20851 break;
20852 case DW_LANG_D:
20853 cu->language = language_d;
20854 break;
20855 case DW_LANG_Fortran77:
20856 case DW_LANG_Fortran90:
20857 case DW_LANG_Fortran95:
20858 case DW_LANG_Fortran03:
20859 case DW_LANG_Fortran08:
20860 cu->language = language_fortran;
20861 break;
20862 case DW_LANG_Go:
20863 cu->language = language_go;
20864 break;
20865 case DW_LANG_Mips_Assembler:
20866 cu->language = language_asm;
20867 break;
20868 case DW_LANG_Ada83:
20869 case DW_LANG_Ada95:
20870 cu->language = language_ada;
20871 break;
20872 case DW_LANG_Modula2:
20873 cu->language = language_m2;
20874 break;
20875 case DW_LANG_Pascal83:
20876 cu->language = language_pascal;
20877 break;
20878 case DW_LANG_ObjC:
20879 cu->language = language_objc;
20880 break;
20881 case DW_LANG_Rust:
20882 case DW_LANG_Rust_old:
20883 cu->language = language_rust;
20884 break;
20885 case DW_LANG_Cobol74:
20886 case DW_LANG_Cobol85:
20887 default:
20888 cu->language = language_minimal;
20889 break;
20890 }
20891 cu->language_defn = language_def (cu->language);
20892 }
20893
20894 /* Return the named attribute or NULL if not there. */
20895
20896 static struct attribute *
20897 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20898 {
20899 for (;;)
20900 {
20901 unsigned int i;
20902 struct attribute *spec = NULL;
20903
20904 for (i = 0; i < die->num_attrs; ++i)
20905 {
20906 if (die->attrs[i].name == name)
20907 return &die->attrs[i];
20908 if (die->attrs[i].name == DW_AT_specification
20909 || die->attrs[i].name == DW_AT_abstract_origin)
20910 spec = &die->attrs[i];
20911 }
20912
20913 if (!spec)
20914 break;
20915
20916 die = follow_die_ref (die, spec, &cu);
20917 }
20918
20919 return NULL;
20920 }
20921
20922 /* Return the string associated with a string-typed attribute, or NULL if it
20923 is either not found or is of an incorrect type. */
20924
20925 static const char *
20926 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20927 {
20928 struct attribute *attr;
20929 const char *str = NULL;
20930
20931 attr = dwarf2_attr (die, name, cu);
20932
20933 if (attr != NULL)
20934 {
20935 str = attr->as_string ();
20936 if (str == nullptr)
20937 complaint (_("string type expected for attribute %s for "
20938 "DIE at %s in module %s"),
20939 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20940 objfile_name (cu->per_objfile->objfile));
20941 }
20942
20943 return str;
20944 }
20945
20946 /* Return the dwo name or NULL if not present. If present, it is in either
20947 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20948 static const char *
20949 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20950 {
20951 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20952 if (dwo_name == nullptr)
20953 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20954 return dwo_name;
20955 }
20956
20957 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20958 and holds a non-zero value. This function should only be used for
20959 DW_FORM_flag or DW_FORM_flag_present attributes. */
20960
20961 static int
20962 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20963 {
20964 struct attribute *attr = dwarf2_attr (die, name, cu);
20965
20966 return attr != nullptr && attr->as_boolean ();
20967 }
20968
20969 static int
20970 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20971 {
20972 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20973 which value is non-zero. However, we have to be careful with
20974 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20975 (via dwarf2_flag_true_p) follows this attribute. So we may
20976 end up accidently finding a declaration attribute that belongs
20977 to a different DIE referenced by the specification attribute,
20978 even though the given DIE does not have a declaration attribute. */
20979 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20980 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20981 }
20982
20983 /* Return the die giving the specification for DIE, if there is
20984 one. *SPEC_CU is the CU containing DIE on input, and the CU
20985 containing the return value on output. If there is no
20986 specification, but there is an abstract origin, that is
20987 returned. */
20988
20989 static struct die_info *
20990 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20991 {
20992 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20993 *spec_cu);
20994
20995 if (spec_attr == NULL)
20996 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20997
20998 if (spec_attr == NULL)
20999 return NULL;
21000 else
21001 return follow_die_ref (die, spec_attr, spec_cu);
21002 }
21003
21004 /* Stub for free_line_header to match void * callback types. */
21005
21006 static void
21007 free_line_header_voidp (void *arg)
21008 {
21009 struct line_header *lh = (struct line_header *) arg;
21010
21011 delete lh;
21012 }
21013
21014 /* A convenience function to find the proper .debug_line section for a CU. */
21015
21016 static struct dwarf2_section_info *
21017 get_debug_line_section (struct dwarf2_cu *cu)
21018 {
21019 struct dwarf2_section_info *section;
21020 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21021
21022 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
21023 DWO file. */
21024 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21025 section = &cu->dwo_unit->dwo_file->sections.line;
21026 else if (cu->per_cu->is_dwz)
21027 {
21028 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
21029
21030 section = &dwz->line;
21031 }
21032 else
21033 section = &per_objfile->per_bfd->line;
21034
21035 return section;
21036 }
21037
21038 /* Read the statement program header starting at OFFSET in
21039 .debug_line, or .debug_line.dwo. Return a pointer
21040 to a struct line_header, allocated using xmalloc.
21041 Returns NULL if there is a problem reading the header, e.g., if it
21042 has a version we don't understand.
21043
21044 NOTE: the strings in the include directory and file name tables of
21045 the returned object point into the dwarf line section buffer,
21046 and must not be freed. */
21047
21048 static line_header_up
21049 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
21050 {
21051 struct dwarf2_section_info *section;
21052 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21053
21054 section = get_debug_line_section (cu);
21055 section->read (per_objfile->objfile);
21056 if (section->buffer == NULL)
21057 {
21058 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21059 complaint (_("missing .debug_line.dwo section"));
21060 else
21061 complaint (_("missing .debug_line section"));
21062 return 0;
21063 }
21064
21065 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
21066 per_objfile, section, &cu->header);
21067 }
21068
21069 /* Subroutine of dwarf_decode_lines to simplify it.
21070 Return the file name of the psymtab for the given file_entry.
21071 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21072 If space for the result is malloc'd, *NAME_HOLDER will be set.
21073 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
21074
21075 static const char *
21076 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
21077 const dwarf2_psymtab *pst,
21078 const char *comp_dir,
21079 gdb::unique_xmalloc_ptr<char> *name_holder)
21080 {
21081 const char *include_name = fe.name;
21082 const char *include_name_to_compare = include_name;
21083 const char *pst_filename;
21084 int file_is_pst;
21085
21086 const char *dir_name = fe.include_dir (lh);
21087
21088 gdb::unique_xmalloc_ptr<char> hold_compare;
21089 if (!IS_ABSOLUTE_PATH (include_name)
21090 && (dir_name != NULL || comp_dir != NULL))
21091 {
21092 /* Avoid creating a duplicate psymtab for PST.
21093 We do this by comparing INCLUDE_NAME and PST_FILENAME.
21094 Before we do the comparison, however, we need to account
21095 for DIR_NAME and COMP_DIR.
21096 First prepend dir_name (if non-NULL). If we still don't
21097 have an absolute path prepend comp_dir (if non-NULL).
21098 However, the directory we record in the include-file's
21099 psymtab does not contain COMP_DIR (to match the
21100 corresponding symtab(s)).
21101
21102 Example:
21103
21104 bash$ cd /tmp
21105 bash$ gcc -g ./hello.c
21106 include_name = "hello.c"
21107 dir_name = "."
21108 DW_AT_comp_dir = comp_dir = "/tmp"
21109 DW_AT_name = "./hello.c"
21110
21111 */
21112
21113 if (dir_name != NULL)
21114 {
21115 name_holder->reset (concat (dir_name, SLASH_STRING,
21116 include_name, (char *) NULL));
21117 include_name = name_holder->get ();
21118 include_name_to_compare = include_name;
21119 }
21120 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
21121 {
21122 hold_compare.reset (concat (comp_dir, SLASH_STRING,
21123 include_name, (char *) NULL));
21124 include_name_to_compare = hold_compare.get ();
21125 }
21126 }
21127
21128 pst_filename = pst->filename;
21129 gdb::unique_xmalloc_ptr<char> copied_name;
21130 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21131 {
21132 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21133 pst_filename, (char *) NULL));
21134 pst_filename = copied_name.get ();
21135 }
21136
21137 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
21138
21139 if (file_is_pst)
21140 return NULL;
21141 return include_name;
21142 }
21143
21144 /* State machine to track the state of the line number program. */
21145
21146 class lnp_state_machine
21147 {
21148 public:
21149 /* Initialize a machine state for the start of a line number
21150 program. */
21151 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21152 bool record_lines_p);
21153
21154 file_entry *current_file ()
21155 {
21156 /* lh->file_names is 0-based, but the file name numbers in the
21157 statement program are 1-based. */
21158 return m_line_header->file_name_at (m_file);
21159 }
21160
21161 /* Record the line in the state machine. END_SEQUENCE is true if
21162 we're processing the end of a sequence. */
21163 void record_line (bool end_sequence);
21164
21165 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
21166 nop-out rest of the lines in this sequence. */
21167 void check_line_address (struct dwarf2_cu *cu,
21168 const gdb_byte *line_ptr,
21169 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
21170
21171 void handle_set_discriminator (unsigned int discriminator)
21172 {
21173 m_discriminator = discriminator;
21174 m_line_has_non_zero_discriminator |= discriminator != 0;
21175 }
21176
21177 /* Handle DW_LNE_set_address. */
21178 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21179 {
21180 m_op_index = 0;
21181 address += baseaddr;
21182 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21183 }
21184
21185 /* Handle DW_LNS_advance_pc. */
21186 void handle_advance_pc (CORE_ADDR adjust);
21187
21188 /* Handle a special opcode. */
21189 void handle_special_opcode (unsigned char op_code);
21190
21191 /* Handle DW_LNS_advance_line. */
21192 void handle_advance_line (int line_delta)
21193 {
21194 advance_line (line_delta);
21195 }
21196
21197 /* Handle DW_LNS_set_file. */
21198 void handle_set_file (file_name_index file);
21199
21200 /* Handle DW_LNS_negate_stmt. */
21201 void handle_negate_stmt ()
21202 {
21203 m_is_stmt = !m_is_stmt;
21204 }
21205
21206 /* Handle DW_LNS_const_add_pc. */
21207 void handle_const_add_pc ();
21208
21209 /* Handle DW_LNS_fixed_advance_pc. */
21210 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21211 {
21212 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21213 m_op_index = 0;
21214 }
21215
21216 /* Handle DW_LNS_copy. */
21217 void handle_copy ()
21218 {
21219 record_line (false);
21220 m_discriminator = 0;
21221 }
21222
21223 /* Handle DW_LNE_end_sequence. */
21224 void handle_end_sequence ()
21225 {
21226 m_currently_recording_lines = true;
21227 }
21228
21229 private:
21230 /* Advance the line by LINE_DELTA. */
21231 void advance_line (int line_delta)
21232 {
21233 m_line += line_delta;
21234
21235 if (line_delta != 0)
21236 m_line_has_non_zero_discriminator = m_discriminator != 0;
21237 }
21238
21239 struct dwarf2_cu *m_cu;
21240
21241 gdbarch *m_gdbarch;
21242
21243 /* True if we're recording lines.
21244 Otherwise we're building partial symtabs and are just interested in
21245 finding include files mentioned by the line number program. */
21246 bool m_record_lines_p;
21247
21248 /* The line number header. */
21249 line_header *m_line_header;
21250
21251 /* These are part of the standard DWARF line number state machine,
21252 and initialized according to the DWARF spec. */
21253
21254 unsigned char m_op_index = 0;
21255 /* The line table index of the current file. */
21256 file_name_index m_file = 1;
21257 unsigned int m_line = 1;
21258
21259 /* These are initialized in the constructor. */
21260
21261 CORE_ADDR m_address;
21262 bool m_is_stmt;
21263 unsigned int m_discriminator;
21264
21265 /* Additional bits of state we need to track. */
21266
21267 /* The last file that we called dwarf2_start_subfile for.
21268 This is only used for TLLs. */
21269 unsigned int m_last_file = 0;
21270 /* The last file a line number was recorded for. */
21271 struct subfile *m_last_subfile = NULL;
21272
21273 /* The address of the last line entry. */
21274 CORE_ADDR m_last_address;
21275
21276 /* Set to true when a previous line at the same address (using
21277 m_last_address) had m_is_stmt true. This is reset to false when a
21278 line entry at a new address (m_address different to m_last_address) is
21279 processed. */
21280 bool m_stmt_at_address = false;
21281
21282 /* When true, record the lines we decode. */
21283 bool m_currently_recording_lines = false;
21284
21285 /* The last line number that was recorded, used to coalesce
21286 consecutive entries for the same line. This can happen, for
21287 example, when discriminators are present. PR 17276. */
21288 unsigned int m_last_line = 0;
21289 bool m_line_has_non_zero_discriminator = false;
21290 };
21291
21292 void
21293 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21294 {
21295 CORE_ADDR addr_adj = (((m_op_index + adjust)
21296 / m_line_header->maximum_ops_per_instruction)
21297 * m_line_header->minimum_instruction_length);
21298 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21299 m_op_index = ((m_op_index + adjust)
21300 % m_line_header->maximum_ops_per_instruction);
21301 }
21302
21303 void
21304 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21305 {
21306 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21307 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21308 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21309 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
21310 / m_line_header->maximum_ops_per_instruction)
21311 * m_line_header->minimum_instruction_length);
21312 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21313 m_op_index = ((m_op_index + adj_opcode_d)
21314 % m_line_header->maximum_ops_per_instruction);
21315
21316 int line_delta = m_line_header->line_base + adj_opcode_r;
21317 advance_line (line_delta);
21318 record_line (false);
21319 m_discriminator = 0;
21320 }
21321
21322 void
21323 lnp_state_machine::handle_set_file (file_name_index file)
21324 {
21325 m_file = file;
21326
21327 const file_entry *fe = current_file ();
21328 if (fe == NULL)
21329 dwarf2_debug_line_missing_file_complaint ();
21330 else if (m_record_lines_p)
21331 {
21332 const char *dir = fe->include_dir (m_line_header);
21333
21334 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21335 m_line_has_non_zero_discriminator = m_discriminator != 0;
21336 dwarf2_start_subfile (m_cu, fe->name, dir);
21337 }
21338 }
21339
21340 void
21341 lnp_state_machine::handle_const_add_pc ()
21342 {
21343 CORE_ADDR adjust
21344 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21345
21346 CORE_ADDR addr_adj
21347 = (((m_op_index + adjust)
21348 / m_line_header->maximum_ops_per_instruction)
21349 * m_line_header->minimum_instruction_length);
21350
21351 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21352 m_op_index = ((m_op_index + adjust)
21353 % m_line_header->maximum_ops_per_instruction);
21354 }
21355
21356 /* Return non-zero if we should add LINE to the line number table.
21357 LINE is the line to add, LAST_LINE is the last line that was added,
21358 LAST_SUBFILE is the subfile for LAST_LINE.
21359 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21360 had a non-zero discriminator.
21361
21362 We have to be careful in the presence of discriminators.
21363 E.g., for this line:
21364
21365 for (i = 0; i < 100000; i++);
21366
21367 clang can emit four line number entries for that one line,
21368 each with a different discriminator.
21369 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21370
21371 However, we want gdb to coalesce all four entries into one.
21372 Otherwise the user could stepi into the middle of the line and
21373 gdb would get confused about whether the pc really was in the
21374 middle of the line.
21375
21376 Things are further complicated by the fact that two consecutive
21377 line number entries for the same line is a heuristic used by gcc
21378 to denote the end of the prologue. So we can't just discard duplicate
21379 entries, we have to be selective about it. The heuristic we use is
21380 that we only collapse consecutive entries for the same line if at least
21381 one of those entries has a non-zero discriminator. PR 17276.
21382
21383 Note: Addresses in the line number state machine can never go backwards
21384 within one sequence, thus this coalescing is ok. */
21385
21386 static int
21387 dwarf_record_line_p (struct dwarf2_cu *cu,
21388 unsigned int line, unsigned int last_line,
21389 int line_has_non_zero_discriminator,
21390 struct subfile *last_subfile)
21391 {
21392 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21393 return 1;
21394 if (line != last_line)
21395 return 1;
21396 /* Same line for the same file that we've seen already.
21397 As a last check, for pr 17276, only record the line if the line
21398 has never had a non-zero discriminator. */
21399 if (!line_has_non_zero_discriminator)
21400 return 1;
21401 return 0;
21402 }
21403
21404 /* Use the CU's builder to record line number LINE beginning at
21405 address ADDRESS in the line table of subfile SUBFILE. */
21406
21407 static void
21408 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21409 unsigned int line, CORE_ADDR address, bool is_stmt,
21410 struct dwarf2_cu *cu)
21411 {
21412 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21413
21414 if (dwarf_line_debug)
21415 {
21416 fprintf_unfiltered (gdb_stdlog,
21417 "Recording line %u, file %s, address %s\n",
21418 line, lbasename (subfile->name),
21419 paddress (gdbarch, address));
21420 }
21421
21422 if (cu != nullptr)
21423 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
21424 }
21425
21426 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21427 Mark the end of a set of line number records.
21428 The arguments are the same as for dwarf_record_line_1.
21429 If SUBFILE is NULL the request is ignored. */
21430
21431 static void
21432 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21433 CORE_ADDR address, struct dwarf2_cu *cu)
21434 {
21435 if (subfile == NULL)
21436 return;
21437
21438 if (dwarf_line_debug)
21439 {
21440 fprintf_unfiltered (gdb_stdlog,
21441 "Finishing current line, file %s, address %s\n",
21442 lbasename (subfile->name),
21443 paddress (gdbarch, address));
21444 }
21445
21446 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
21447 }
21448
21449 void
21450 lnp_state_machine::record_line (bool end_sequence)
21451 {
21452 if (dwarf_line_debug)
21453 {
21454 fprintf_unfiltered (gdb_stdlog,
21455 "Processing actual line %u: file %u,"
21456 " address %s, is_stmt %u, discrim %u%s\n",
21457 m_line, m_file,
21458 paddress (m_gdbarch, m_address),
21459 m_is_stmt, m_discriminator,
21460 (end_sequence ? "\t(end sequence)" : ""));
21461 }
21462
21463 file_entry *fe = current_file ();
21464
21465 if (fe == NULL)
21466 dwarf2_debug_line_missing_file_complaint ();
21467 /* For now we ignore lines not starting on an instruction boundary.
21468 But not when processing end_sequence for compatibility with the
21469 previous version of the code. */
21470 else if (m_op_index == 0 || end_sequence)
21471 {
21472 fe->included_p = 1;
21473 if (m_record_lines_p)
21474 {
21475 /* When we switch files we insert an end maker in the first file,
21476 switch to the second file and add a new line entry. The
21477 problem is that the end marker inserted in the first file will
21478 discard any previous line entries at the same address. If the
21479 line entries in the first file are marked as is-stmt, while
21480 the new line in the second file is non-stmt, then this means
21481 the end marker will discard is-stmt lines so we can have a
21482 non-stmt line. This means that there are less addresses at
21483 which the user can insert a breakpoint.
21484
21485 To improve this we track the last address in m_last_address,
21486 and whether we have seen an is-stmt at this address. Then
21487 when switching files, if we have seen a stmt at the current
21488 address, and we are switching to create a non-stmt line, then
21489 discard the new line. */
21490 bool file_changed
21491 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21492 bool ignore_this_line
21493 = ((file_changed && !end_sequence && m_last_address == m_address
21494 && !m_is_stmt && m_stmt_at_address)
21495 || (!end_sequence && m_line == 0));
21496
21497 if ((file_changed && !ignore_this_line) || end_sequence)
21498 {
21499 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21500 m_currently_recording_lines ? m_cu : nullptr);
21501 }
21502
21503 if (!end_sequence && !ignore_this_line)
21504 {
21505 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21506
21507 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21508 m_line_has_non_zero_discriminator,
21509 m_last_subfile))
21510 {
21511 buildsym_compunit *builder = m_cu->get_builder ();
21512 dwarf_record_line_1 (m_gdbarch,
21513 builder->get_current_subfile (),
21514 m_line, m_address, is_stmt,
21515 m_currently_recording_lines ? m_cu : nullptr);
21516 }
21517 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21518 m_last_line = m_line;
21519 }
21520 }
21521 }
21522
21523 /* Track whether we have seen any m_is_stmt true at m_address in case we
21524 have multiple line table entries all at m_address. */
21525 if (m_last_address != m_address)
21526 {
21527 m_stmt_at_address = false;
21528 m_last_address = m_address;
21529 }
21530 m_stmt_at_address |= m_is_stmt;
21531 }
21532
21533 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21534 line_header *lh, bool record_lines_p)
21535 {
21536 m_cu = cu;
21537 m_gdbarch = arch;
21538 m_record_lines_p = record_lines_p;
21539 m_line_header = lh;
21540
21541 m_currently_recording_lines = true;
21542
21543 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21544 was a line entry for it so that the backend has a chance to adjust it
21545 and also record it in case it needs it. This is currently used by MIPS
21546 code, cf. `mips_adjust_dwarf2_line'. */
21547 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21548 m_is_stmt = lh->default_is_stmt;
21549 m_discriminator = 0;
21550
21551 m_last_address = m_address;
21552 m_stmt_at_address = false;
21553 }
21554
21555 void
21556 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21557 const gdb_byte *line_ptr,
21558 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21559 {
21560 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21561 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21562 located at 0x0. In this case, additionally check that if
21563 ADDRESS < UNRELOCATED_LOWPC. */
21564
21565 if ((address == 0 && address < unrelocated_lowpc)
21566 || address == (CORE_ADDR) -1)
21567 {
21568 /* This line table is for a function which has been
21569 GCd by the linker. Ignore it. PR gdb/12528 */
21570
21571 struct objfile *objfile = cu->per_objfile->objfile;
21572 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21573
21574 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21575 line_offset, objfile_name (objfile));
21576 m_currently_recording_lines = false;
21577 /* Note: m_currently_recording_lines is left as false until we see
21578 DW_LNE_end_sequence. */
21579 }
21580 }
21581
21582 /* Subroutine of dwarf_decode_lines to simplify it.
21583 Process the line number information in LH.
21584 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21585 program in order to set included_p for every referenced header. */
21586
21587 static void
21588 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21589 const int decode_for_pst_p, CORE_ADDR lowpc)
21590 {
21591 const gdb_byte *line_ptr, *extended_end;
21592 const gdb_byte *line_end;
21593 unsigned int bytes_read, extended_len;
21594 unsigned char op_code, extended_op;
21595 CORE_ADDR baseaddr;
21596 struct objfile *objfile = cu->per_objfile->objfile;
21597 bfd *abfd = objfile->obfd;
21598 struct gdbarch *gdbarch = objfile->arch ();
21599 /* True if we're recording line info (as opposed to building partial
21600 symtabs and just interested in finding include files mentioned by
21601 the line number program). */
21602 bool record_lines_p = !decode_for_pst_p;
21603
21604 baseaddr = objfile->text_section_offset ();
21605
21606 line_ptr = lh->statement_program_start;
21607 line_end = lh->statement_program_end;
21608
21609 /* Read the statement sequences until there's nothing left. */
21610 while (line_ptr < line_end)
21611 {
21612 /* The DWARF line number program state machine. Reset the state
21613 machine at the start of each sequence. */
21614 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21615 bool end_sequence = false;
21616
21617 if (record_lines_p)
21618 {
21619 /* Start a subfile for the current file of the state
21620 machine. */
21621 const file_entry *fe = state_machine.current_file ();
21622
21623 if (fe != NULL)
21624 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21625 }
21626
21627 /* Decode the table. */
21628 while (line_ptr < line_end && !end_sequence)
21629 {
21630 op_code = read_1_byte (abfd, line_ptr);
21631 line_ptr += 1;
21632
21633 if (op_code >= lh->opcode_base)
21634 {
21635 /* Special opcode. */
21636 state_machine.handle_special_opcode (op_code);
21637 }
21638 else switch (op_code)
21639 {
21640 case DW_LNS_extended_op:
21641 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21642 &bytes_read);
21643 line_ptr += bytes_read;
21644 extended_end = line_ptr + extended_len;
21645 extended_op = read_1_byte (abfd, line_ptr);
21646 line_ptr += 1;
21647 if (DW_LNE_lo_user <= extended_op
21648 && extended_op <= DW_LNE_hi_user)
21649 {
21650 /* Vendor extension, ignore. */
21651 line_ptr = extended_end;
21652 break;
21653 }
21654 switch (extended_op)
21655 {
21656 case DW_LNE_end_sequence:
21657 state_machine.handle_end_sequence ();
21658 end_sequence = true;
21659 break;
21660 case DW_LNE_set_address:
21661 {
21662 CORE_ADDR address
21663 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21664 line_ptr += bytes_read;
21665
21666 state_machine.check_line_address (cu, line_ptr,
21667 lowpc - baseaddr, address);
21668 state_machine.handle_set_address (baseaddr, address);
21669 }
21670 break;
21671 case DW_LNE_define_file:
21672 {
21673 const char *cur_file;
21674 unsigned int mod_time, length;
21675 dir_index dindex;
21676
21677 cur_file = read_direct_string (abfd, line_ptr,
21678 &bytes_read);
21679 line_ptr += bytes_read;
21680 dindex = (dir_index)
21681 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21682 line_ptr += bytes_read;
21683 mod_time =
21684 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21685 line_ptr += bytes_read;
21686 length =
21687 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21688 line_ptr += bytes_read;
21689 lh->add_file_name (cur_file, dindex, mod_time, length);
21690 }
21691 break;
21692 case DW_LNE_set_discriminator:
21693 {
21694 /* The discriminator is not interesting to the
21695 debugger; just ignore it. We still need to
21696 check its value though:
21697 if there are consecutive entries for the same
21698 (non-prologue) line we want to coalesce them.
21699 PR 17276. */
21700 unsigned int discr
21701 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21702 line_ptr += bytes_read;
21703
21704 state_machine.handle_set_discriminator (discr);
21705 }
21706 break;
21707 default:
21708 complaint (_("mangled .debug_line section"));
21709 return;
21710 }
21711 /* Make sure that we parsed the extended op correctly. If e.g.
21712 we expected a different address size than the producer used,
21713 we may have read the wrong number of bytes. */
21714 if (line_ptr != extended_end)
21715 {
21716 complaint (_("mangled .debug_line section"));
21717 return;
21718 }
21719 break;
21720 case DW_LNS_copy:
21721 state_machine.handle_copy ();
21722 break;
21723 case DW_LNS_advance_pc:
21724 {
21725 CORE_ADDR adjust
21726 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21727 line_ptr += bytes_read;
21728
21729 state_machine.handle_advance_pc (adjust);
21730 }
21731 break;
21732 case DW_LNS_advance_line:
21733 {
21734 int line_delta
21735 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21736 line_ptr += bytes_read;
21737
21738 state_machine.handle_advance_line (line_delta);
21739 }
21740 break;
21741 case DW_LNS_set_file:
21742 {
21743 file_name_index file
21744 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21745 &bytes_read);
21746 line_ptr += bytes_read;
21747
21748 state_machine.handle_set_file (file);
21749 }
21750 break;
21751 case DW_LNS_set_column:
21752 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21753 line_ptr += bytes_read;
21754 break;
21755 case DW_LNS_negate_stmt:
21756 state_machine.handle_negate_stmt ();
21757 break;
21758 case DW_LNS_set_basic_block:
21759 break;
21760 /* Add to the address register of the state machine the
21761 address increment value corresponding to special opcode
21762 255. I.e., this value is scaled by the minimum
21763 instruction length since special opcode 255 would have
21764 scaled the increment. */
21765 case DW_LNS_const_add_pc:
21766 state_machine.handle_const_add_pc ();
21767 break;
21768 case DW_LNS_fixed_advance_pc:
21769 {
21770 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21771 line_ptr += 2;
21772
21773 state_machine.handle_fixed_advance_pc (addr_adj);
21774 }
21775 break;
21776 default:
21777 {
21778 /* Unknown standard opcode, ignore it. */
21779 int i;
21780
21781 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21782 {
21783 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21784 line_ptr += bytes_read;
21785 }
21786 }
21787 }
21788 }
21789
21790 if (!end_sequence)
21791 dwarf2_debug_line_missing_end_sequence_complaint ();
21792
21793 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21794 in which case we still finish recording the last line). */
21795 state_machine.record_line (true);
21796 }
21797 }
21798
21799 /* Decode the Line Number Program (LNP) for the given line_header
21800 structure and CU. The actual information extracted and the type
21801 of structures created from the LNP depends on the value of PST.
21802
21803 1. If PST is NULL, then this procedure uses the data from the program
21804 to create all necessary symbol tables, and their linetables.
21805
21806 2. If PST is not NULL, this procedure reads the program to determine
21807 the list of files included by the unit represented by PST, and
21808 builds all the associated partial symbol tables.
21809
21810 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21811 It is used for relative paths in the line table.
21812 NOTE: When processing partial symtabs (pst != NULL),
21813 comp_dir == pst->dirname.
21814
21815 NOTE: It is important that psymtabs have the same file name (via strcmp)
21816 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21817 symtab we don't use it in the name of the psymtabs we create.
21818 E.g. expand_line_sal requires this when finding psymtabs to expand.
21819 A good testcase for this is mb-inline.exp.
21820
21821 LOWPC is the lowest address in CU (or 0 if not known).
21822
21823 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21824 for its PC<->lines mapping information. Otherwise only the filename
21825 table is read in. */
21826
21827 static void
21828 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21829 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21830 CORE_ADDR lowpc, int decode_mapping)
21831 {
21832 struct objfile *objfile = cu->per_objfile->objfile;
21833 const int decode_for_pst_p = (pst != NULL);
21834
21835 if (decode_mapping)
21836 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21837
21838 if (decode_for_pst_p)
21839 {
21840 /* Now that we're done scanning the Line Header Program, we can
21841 create the psymtab of each included file. */
21842 for (auto &file_entry : lh->file_names ())
21843 if (file_entry.included_p == 1)
21844 {
21845 gdb::unique_xmalloc_ptr<char> name_holder;
21846 const char *include_name =
21847 psymtab_include_file_name (lh, file_entry, pst,
21848 comp_dir, &name_holder);
21849 if (include_name != NULL)
21850 dwarf2_create_include_psymtab (include_name, pst, objfile);
21851 }
21852 }
21853 else
21854 {
21855 /* Make sure a symtab is created for every file, even files
21856 which contain only variables (i.e. no code with associated
21857 line numbers). */
21858 buildsym_compunit *builder = cu->get_builder ();
21859 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21860
21861 for (auto &fe : lh->file_names ())
21862 {
21863 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21864 if (builder->get_current_subfile ()->symtab == NULL)
21865 {
21866 builder->get_current_subfile ()->symtab
21867 = allocate_symtab (cust,
21868 builder->get_current_subfile ()->name);
21869 }
21870 fe.symtab = builder->get_current_subfile ()->symtab;
21871 }
21872 }
21873 }
21874
21875 /* Start a subfile for DWARF. FILENAME is the name of the file and
21876 DIRNAME the name of the source directory which contains FILENAME
21877 or NULL if not known.
21878 This routine tries to keep line numbers from identical absolute and
21879 relative file names in a common subfile.
21880
21881 Using the `list' example from the GDB testsuite, which resides in
21882 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21883 of /srcdir/list0.c yields the following debugging information for list0.c:
21884
21885 DW_AT_name: /srcdir/list0.c
21886 DW_AT_comp_dir: /compdir
21887 files.files[0].name: list0.h
21888 files.files[0].dir: /srcdir
21889 files.files[1].name: list0.c
21890 files.files[1].dir: /srcdir
21891
21892 The line number information for list0.c has to end up in a single
21893 subfile, so that `break /srcdir/list0.c:1' works as expected.
21894 start_subfile will ensure that this happens provided that we pass the
21895 concatenation of files.files[1].dir and files.files[1].name as the
21896 subfile's name. */
21897
21898 static void
21899 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21900 const char *dirname)
21901 {
21902 gdb::unique_xmalloc_ptr<char> copy;
21903
21904 /* In order not to lose the line information directory,
21905 we concatenate it to the filename when it makes sense.
21906 Note that the Dwarf3 standard says (speaking of filenames in line
21907 information): ``The directory index is ignored for file names
21908 that represent full path names''. Thus ignoring dirname in the
21909 `else' branch below isn't an issue. */
21910
21911 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21912 {
21913 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21914 filename = copy.get ();
21915 }
21916
21917 cu->get_builder ()->start_subfile (filename);
21918 }
21919
21920 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21921 buildsym_compunit constructor. */
21922
21923 struct compunit_symtab *
21924 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21925 CORE_ADDR low_pc)
21926 {
21927 gdb_assert (m_builder == nullptr);
21928
21929 m_builder.reset (new struct buildsym_compunit
21930 (this->per_objfile->objfile,
21931 name, comp_dir, language, low_pc));
21932
21933 list_in_scope = get_builder ()->get_file_symbols ();
21934
21935 get_builder ()->record_debugformat ("DWARF 2");
21936 get_builder ()->record_producer (producer);
21937
21938 processing_has_namespace_info = false;
21939
21940 return get_builder ()->get_compunit_symtab ();
21941 }
21942
21943 static void
21944 var_decode_location (struct attribute *attr, struct symbol *sym,
21945 struct dwarf2_cu *cu)
21946 {
21947 struct objfile *objfile = cu->per_objfile->objfile;
21948 struct comp_unit_head *cu_header = &cu->header;
21949
21950 /* NOTE drow/2003-01-30: There used to be a comment and some special
21951 code here to turn a symbol with DW_AT_external and a
21952 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21953 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21954 with some versions of binutils) where shared libraries could have
21955 relocations against symbols in their debug information - the
21956 minimal symbol would have the right address, but the debug info
21957 would not. It's no longer necessary, because we will explicitly
21958 apply relocations when we read in the debug information now. */
21959
21960 /* A DW_AT_location attribute with no contents indicates that a
21961 variable has been optimized away. */
21962 if (attr->form_is_block () && attr->as_block ()->size == 0)
21963 {
21964 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21965 return;
21966 }
21967
21968 /* Handle one degenerate form of location expression specially, to
21969 preserve GDB's previous behavior when section offsets are
21970 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21971 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21972
21973 if (attr->form_is_block ())
21974 {
21975 struct dwarf_block *block = attr->as_block ();
21976
21977 if ((block->data[0] == DW_OP_addr
21978 && block->size == 1 + cu_header->addr_size)
21979 || ((block->data[0] == DW_OP_GNU_addr_index
21980 || block->data[0] == DW_OP_addrx)
21981 && (block->size
21982 == 1 + leb128_size (&block->data[1]))))
21983 {
21984 unsigned int dummy;
21985
21986 if (block->data[0] == DW_OP_addr)
21987 SET_SYMBOL_VALUE_ADDRESS
21988 (sym, cu->header.read_address (objfile->obfd,
21989 block->data + 1,
21990 &dummy));
21991 else
21992 SET_SYMBOL_VALUE_ADDRESS
21993 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21994 &dummy));
21995 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21996 fixup_symbol_section (sym, objfile);
21997 SET_SYMBOL_VALUE_ADDRESS
21998 (sym,
21999 SYMBOL_VALUE_ADDRESS (sym)
22000 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
22001 return;
22002 }
22003 }
22004
22005 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
22006 expression evaluator, and use LOC_COMPUTED only when necessary
22007 (i.e. when the value of a register or memory location is
22008 referenced, or a thread-local block, etc.). Then again, it might
22009 not be worthwhile. I'm assuming that it isn't unless performance
22010 or memory numbers show me otherwise. */
22011
22012 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
22013
22014 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
22015 cu->has_loclist = true;
22016 }
22017
22018 /* Given a pointer to a DWARF information entry, figure out if we need
22019 to make a symbol table entry for it, and if so, create a new entry
22020 and return a pointer to it.
22021 If TYPE is NULL, determine symbol type from the die, otherwise
22022 used the passed type.
22023 If SPACE is not NULL, use it to hold the new symbol. If it is
22024 NULL, allocate a new symbol on the objfile's obstack. */
22025
22026 static struct symbol *
22027 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
22028 struct symbol *space)
22029 {
22030 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22031 struct objfile *objfile = per_objfile->objfile;
22032 struct gdbarch *gdbarch = objfile->arch ();
22033 struct symbol *sym = NULL;
22034 const char *name;
22035 struct attribute *attr = NULL;
22036 struct attribute *attr2 = NULL;
22037 CORE_ADDR baseaddr;
22038 struct pending **list_to_add = NULL;
22039
22040 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
22041
22042 baseaddr = objfile->text_section_offset ();
22043
22044 name = dwarf2_name (die, cu);
22045 if (name)
22046 {
22047 int suppress_add = 0;
22048
22049 if (space)
22050 sym = space;
22051 else
22052 sym = new (&objfile->objfile_obstack) symbol;
22053 OBJSTAT (objfile, n_syms++);
22054
22055 /* Cache this symbol's name and the name's demangled form (if any). */
22056 sym->set_language (cu->language, &objfile->objfile_obstack);
22057 /* Fortran does not have mangling standard and the mangling does differ
22058 between gfortran, iFort etc. */
22059 const char *physname
22060 = (cu->language == language_fortran
22061 ? dwarf2_full_name (name, die, cu)
22062 : dwarf2_physname (name, die, cu));
22063 const char *linkagename = dw2_linkage_name (die, cu);
22064
22065 if (linkagename == nullptr || cu->language == language_ada)
22066 sym->set_linkage_name (physname);
22067 else
22068 {
22069 sym->set_demangled_name (physname, &objfile->objfile_obstack);
22070 sym->set_linkage_name (linkagename);
22071 }
22072
22073 /* Default assumptions.
22074 Use the passed type or decode it from the die. */
22075 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22076 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22077 if (type != NULL)
22078 SYMBOL_TYPE (sym) = type;
22079 else
22080 SYMBOL_TYPE (sym) = die_type (die, cu);
22081 attr = dwarf2_attr (die,
22082 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
22083 cu);
22084 if (attr != nullptr)
22085 SYMBOL_LINE (sym) = attr->constant_value (0);
22086
22087 attr = dwarf2_attr (die,
22088 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
22089 cu);
22090 if (attr != nullptr && attr->form_is_unsigned ())
22091 {
22092 file_name_index file_index
22093 = (file_name_index) attr->as_unsigned ();
22094 struct file_entry *fe;
22095
22096 if (cu->line_header != NULL)
22097 fe = cu->line_header->file_name_at (file_index);
22098 else
22099 fe = NULL;
22100
22101 if (fe == NULL)
22102 complaint (_("file index out of range"));
22103 else
22104 symbol_set_symtab (sym, fe->symtab);
22105 }
22106
22107 switch (die->tag)
22108 {
22109 case DW_TAG_label:
22110 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
22111 if (attr != nullptr)
22112 {
22113 CORE_ADDR addr;
22114
22115 addr = attr->as_address ();
22116 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
22117 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
22118 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
22119 }
22120 else
22121 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22122 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
22123 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
22124 add_symbol_to_list (sym, cu->list_in_scope);
22125 break;
22126 case DW_TAG_subprogram:
22127 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22128 finish_block. */
22129 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22130 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22131 if ((attr2 != nullptr && attr2->as_boolean ())
22132 || cu->language == language_ada
22133 || cu->language == language_fortran)
22134 {
22135 /* Subprograms marked external are stored as a global symbol.
22136 Ada and Fortran subprograms, whether marked external or
22137 not, are always stored as a global symbol, because we want
22138 to be able to access them globally. For instance, we want
22139 to be able to break on a nested subprogram without having
22140 to specify the context. */
22141 list_to_add = cu->get_builder ()->get_global_symbols ();
22142 }
22143 else
22144 {
22145 list_to_add = cu->list_in_scope;
22146 }
22147 break;
22148 case DW_TAG_inlined_subroutine:
22149 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22150 finish_block. */
22151 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22152 SYMBOL_INLINED (sym) = 1;
22153 list_to_add = cu->list_in_scope;
22154 break;
22155 case DW_TAG_template_value_param:
22156 suppress_add = 1;
22157 /* Fall through. */
22158 case DW_TAG_constant:
22159 case DW_TAG_variable:
22160 case DW_TAG_member:
22161 /* Compilation with minimal debug info may result in
22162 variables with missing type entries. Change the
22163 misleading `void' type to something sensible. */
22164 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
22165 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
22166
22167 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22168 /* In the case of DW_TAG_member, we should only be called for
22169 static const members. */
22170 if (die->tag == DW_TAG_member)
22171 {
22172 /* dwarf2_add_field uses die_is_declaration,
22173 so we do the same. */
22174 gdb_assert (die_is_declaration (die, cu));
22175 gdb_assert (attr);
22176 }
22177 if (attr != nullptr)
22178 {
22179 dwarf2_const_value (attr, sym, cu);
22180 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22181 if (!suppress_add)
22182 {
22183 if (attr2 != nullptr && attr2->as_boolean ())
22184 list_to_add = cu->get_builder ()->get_global_symbols ();
22185 else
22186 list_to_add = cu->list_in_scope;
22187 }
22188 break;
22189 }
22190 attr = dwarf2_attr (die, DW_AT_location, cu);
22191 if (attr != nullptr)
22192 {
22193 var_decode_location (attr, sym, cu);
22194 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22195
22196 /* Fortran explicitly imports any global symbols to the local
22197 scope by DW_TAG_common_block. */
22198 if (cu->language == language_fortran && die->parent
22199 && die->parent->tag == DW_TAG_common_block)
22200 attr2 = NULL;
22201
22202 if (SYMBOL_CLASS (sym) == LOC_STATIC
22203 && SYMBOL_VALUE_ADDRESS (sym) == 0
22204 && !per_objfile->per_bfd->has_section_at_zero)
22205 {
22206 /* When a static variable is eliminated by the linker,
22207 the corresponding debug information is not stripped
22208 out, but the variable address is set to null;
22209 do not add such variables into symbol table. */
22210 }
22211 else if (attr2 != nullptr && attr2->as_boolean ())
22212 {
22213 if (SYMBOL_CLASS (sym) == LOC_STATIC
22214 && (objfile->flags & OBJF_MAINLINE) == 0
22215 && per_objfile->per_bfd->can_copy)
22216 {
22217 /* A global static variable might be subject to
22218 copy relocation. We first check for a local
22219 minsym, though, because maybe the symbol was
22220 marked hidden, in which case this would not
22221 apply. */
22222 bound_minimal_symbol found
22223 = (lookup_minimal_symbol_linkage
22224 (sym->linkage_name (), objfile));
22225 if (found.minsym != nullptr)
22226 sym->maybe_copied = 1;
22227 }
22228
22229 /* A variable with DW_AT_external is never static,
22230 but it may be block-scoped. */
22231 list_to_add
22232 = ((cu->list_in_scope
22233 == cu->get_builder ()->get_file_symbols ())
22234 ? cu->get_builder ()->get_global_symbols ()
22235 : cu->list_in_scope);
22236 }
22237 else
22238 list_to_add = cu->list_in_scope;
22239 }
22240 else
22241 {
22242 /* We do not know the address of this symbol.
22243 If it is an external symbol and we have type information
22244 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22245 The address of the variable will then be determined from
22246 the minimal symbol table whenever the variable is
22247 referenced. */
22248 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22249
22250 /* Fortran explicitly imports any global symbols to the local
22251 scope by DW_TAG_common_block. */
22252 if (cu->language == language_fortran && die->parent
22253 && die->parent->tag == DW_TAG_common_block)
22254 {
22255 /* SYMBOL_CLASS doesn't matter here because
22256 read_common_block is going to reset it. */
22257 if (!suppress_add)
22258 list_to_add = cu->list_in_scope;
22259 }
22260 else if (attr2 != nullptr && attr2->as_boolean ()
22261 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22262 {
22263 /* A variable with DW_AT_external is never static, but it
22264 may be block-scoped. */
22265 list_to_add
22266 = ((cu->list_in_scope
22267 == cu->get_builder ()->get_file_symbols ())
22268 ? cu->get_builder ()->get_global_symbols ()
22269 : cu->list_in_scope);
22270
22271 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
22272 }
22273 else if (!die_is_declaration (die, cu))
22274 {
22275 /* Use the default LOC_OPTIMIZED_OUT class. */
22276 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22277 if (!suppress_add)
22278 list_to_add = cu->list_in_scope;
22279 }
22280 }
22281 break;
22282 case DW_TAG_formal_parameter:
22283 {
22284 /* If we are inside a function, mark this as an argument. If
22285 not, we might be looking at an argument to an inlined function
22286 when we do not have enough information to show inlined frames;
22287 pretend it's a local variable in that case so that the user can
22288 still see it. */
22289 struct context_stack *curr
22290 = cu->get_builder ()->get_current_context_stack ();
22291 if (curr != nullptr && curr->name != nullptr)
22292 SYMBOL_IS_ARGUMENT (sym) = 1;
22293 attr = dwarf2_attr (die, DW_AT_location, cu);
22294 if (attr != nullptr)
22295 {
22296 var_decode_location (attr, sym, cu);
22297 }
22298 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22299 if (attr != nullptr)
22300 {
22301 dwarf2_const_value (attr, sym, cu);
22302 }
22303
22304 list_to_add = cu->list_in_scope;
22305 }
22306 break;
22307 case DW_TAG_unspecified_parameters:
22308 /* From varargs functions; gdb doesn't seem to have any
22309 interest in this information, so just ignore it for now.
22310 (FIXME?) */
22311 break;
22312 case DW_TAG_template_type_param:
22313 suppress_add = 1;
22314 /* Fall through. */
22315 case DW_TAG_class_type:
22316 case DW_TAG_interface_type:
22317 case DW_TAG_structure_type:
22318 case DW_TAG_union_type:
22319 case DW_TAG_set_type:
22320 case DW_TAG_enumeration_type:
22321 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22322 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22323
22324 {
22325 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22326 really ever be static objects: otherwise, if you try
22327 to, say, break of a class's method and you're in a file
22328 which doesn't mention that class, it won't work unless
22329 the check for all static symbols in lookup_symbol_aux
22330 saves you. See the OtherFileClass tests in
22331 gdb.c++/namespace.exp. */
22332
22333 if (!suppress_add)
22334 {
22335 buildsym_compunit *builder = cu->get_builder ();
22336 list_to_add
22337 = (cu->list_in_scope == builder->get_file_symbols ()
22338 && cu->language == language_cplus
22339 ? builder->get_global_symbols ()
22340 : cu->list_in_scope);
22341
22342 /* The semantics of C++ state that "struct foo {
22343 ... }" also defines a typedef for "foo". */
22344 if (cu->language == language_cplus
22345 || cu->language == language_ada
22346 || cu->language == language_d
22347 || cu->language == language_rust)
22348 {
22349 /* The symbol's name is already allocated along
22350 with this objfile, so we don't need to
22351 duplicate it for the type. */
22352 if (SYMBOL_TYPE (sym)->name () == 0)
22353 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
22354 }
22355 }
22356 }
22357 break;
22358 case DW_TAG_typedef:
22359 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22360 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22361 list_to_add = cu->list_in_scope;
22362 break;
22363 case DW_TAG_array_type:
22364 case DW_TAG_base_type:
22365 case DW_TAG_subrange_type:
22366 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22367 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22368 list_to_add = cu->list_in_scope;
22369 break;
22370 case DW_TAG_enumerator:
22371 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22372 if (attr != nullptr)
22373 {
22374 dwarf2_const_value (attr, sym, cu);
22375 }
22376 {
22377 /* NOTE: carlton/2003-11-10: See comment above in the
22378 DW_TAG_class_type, etc. block. */
22379
22380 list_to_add
22381 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22382 && cu->language == language_cplus
22383 ? cu->get_builder ()->get_global_symbols ()
22384 : cu->list_in_scope);
22385 }
22386 break;
22387 case DW_TAG_imported_declaration:
22388 case DW_TAG_namespace:
22389 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22390 list_to_add = cu->get_builder ()->get_global_symbols ();
22391 break;
22392 case DW_TAG_module:
22393 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22394 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22395 list_to_add = cu->get_builder ()->get_global_symbols ();
22396 break;
22397 case DW_TAG_common_block:
22398 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22399 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22400 add_symbol_to_list (sym, cu->list_in_scope);
22401 break;
22402 default:
22403 /* Not a tag we recognize. Hopefully we aren't processing
22404 trash data, but since we must specifically ignore things
22405 we don't recognize, there is nothing else we should do at
22406 this point. */
22407 complaint (_("unsupported tag: '%s'"),
22408 dwarf_tag_name (die->tag));
22409 break;
22410 }
22411
22412 if (suppress_add)
22413 {
22414 sym->hash_next = objfile->template_symbols;
22415 objfile->template_symbols = sym;
22416 list_to_add = NULL;
22417 }
22418
22419 if (list_to_add != NULL)
22420 add_symbol_to_list (sym, list_to_add);
22421
22422 /* For the benefit of old versions of GCC, check for anonymous
22423 namespaces based on the demangled name. */
22424 if (!cu->processing_has_namespace_info
22425 && cu->language == language_cplus)
22426 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22427 }
22428 return (sym);
22429 }
22430
22431 /* Given an attr with a DW_FORM_dataN value in host byte order,
22432 zero-extend it as appropriate for the symbol's type. The DWARF
22433 standard (v4) is not entirely clear about the meaning of using
22434 DW_FORM_dataN for a constant with a signed type, where the type is
22435 wider than the data. The conclusion of a discussion on the DWARF
22436 list was that this is unspecified. We choose to always zero-extend
22437 because that is the interpretation long in use by GCC. */
22438
22439 static gdb_byte *
22440 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22441 struct dwarf2_cu *cu, LONGEST *value, int bits)
22442 {
22443 struct objfile *objfile = cu->per_objfile->objfile;
22444 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22445 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22446 LONGEST l = attr->constant_value (0);
22447
22448 if (bits < sizeof (*value) * 8)
22449 {
22450 l &= ((LONGEST) 1 << bits) - 1;
22451 *value = l;
22452 }
22453 else if (bits == sizeof (*value) * 8)
22454 *value = l;
22455 else
22456 {
22457 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22458 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22459 return bytes;
22460 }
22461
22462 return NULL;
22463 }
22464
22465 /* Read a constant value from an attribute. Either set *VALUE, or if
22466 the value does not fit in *VALUE, set *BYTES - either already
22467 allocated on the objfile obstack, or newly allocated on OBSTACK,
22468 or, set *BATON, if we translated the constant to a location
22469 expression. */
22470
22471 static void
22472 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22473 const char *name, struct obstack *obstack,
22474 struct dwarf2_cu *cu,
22475 LONGEST *value, const gdb_byte **bytes,
22476 struct dwarf2_locexpr_baton **baton)
22477 {
22478 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22479 struct objfile *objfile = per_objfile->objfile;
22480 struct comp_unit_head *cu_header = &cu->header;
22481 struct dwarf_block *blk;
22482 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22483 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22484
22485 *value = 0;
22486 *bytes = NULL;
22487 *baton = NULL;
22488
22489 switch (attr->form)
22490 {
22491 case DW_FORM_addr:
22492 case DW_FORM_addrx:
22493 case DW_FORM_GNU_addr_index:
22494 {
22495 gdb_byte *data;
22496
22497 if (TYPE_LENGTH (type) != cu_header->addr_size)
22498 dwarf2_const_value_length_mismatch_complaint (name,
22499 cu_header->addr_size,
22500 TYPE_LENGTH (type));
22501 /* Symbols of this form are reasonably rare, so we just
22502 piggyback on the existing location code rather than writing
22503 a new implementation of symbol_computed_ops. */
22504 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22505 (*baton)->per_objfile = per_objfile;
22506 (*baton)->per_cu = cu->per_cu;
22507 gdb_assert ((*baton)->per_cu);
22508
22509 (*baton)->size = 2 + cu_header->addr_size;
22510 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22511 (*baton)->data = data;
22512
22513 data[0] = DW_OP_addr;
22514 store_unsigned_integer (&data[1], cu_header->addr_size,
22515 byte_order, attr->as_address ());
22516 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22517 }
22518 break;
22519 case DW_FORM_string:
22520 case DW_FORM_strp:
22521 case DW_FORM_strx:
22522 case DW_FORM_GNU_str_index:
22523 case DW_FORM_GNU_strp_alt:
22524 /* The string is already allocated on the objfile obstack, point
22525 directly to it. */
22526 *bytes = (const gdb_byte *) attr->as_string ();
22527 break;
22528 case DW_FORM_block1:
22529 case DW_FORM_block2:
22530 case DW_FORM_block4:
22531 case DW_FORM_block:
22532 case DW_FORM_exprloc:
22533 case DW_FORM_data16:
22534 blk = attr->as_block ();
22535 if (TYPE_LENGTH (type) != blk->size)
22536 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22537 TYPE_LENGTH (type));
22538 *bytes = blk->data;
22539 break;
22540
22541 /* The DW_AT_const_value attributes are supposed to carry the
22542 symbol's value "represented as it would be on the target
22543 architecture." By the time we get here, it's already been
22544 converted to host endianness, so we just need to sign- or
22545 zero-extend it as appropriate. */
22546 case DW_FORM_data1:
22547 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22548 break;
22549 case DW_FORM_data2:
22550 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22551 break;
22552 case DW_FORM_data4:
22553 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22554 break;
22555 case DW_FORM_data8:
22556 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22557 break;
22558
22559 case DW_FORM_sdata:
22560 case DW_FORM_implicit_const:
22561 *value = attr->as_signed ();
22562 break;
22563
22564 case DW_FORM_udata:
22565 *value = attr->as_unsigned ();
22566 break;
22567
22568 default:
22569 complaint (_("unsupported const value attribute form: '%s'"),
22570 dwarf_form_name (attr->form));
22571 *value = 0;
22572 break;
22573 }
22574 }
22575
22576
22577 /* Copy constant value from an attribute to a symbol. */
22578
22579 static void
22580 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22581 struct dwarf2_cu *cu)
22582 {
22583 struct objfile *objfile = cu->per_objfile->objfile;
22584 LONGEST value;
22585 const gdb_byte *bytes;
22586 struct dwarf2_locexpr_baton *baton;
22587
22588 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22589 sym->print_name (),
22590 &objfile->objfile_obstack, cu,
22591 &value, &bytes, &baton);
22592
22593 if (baton != NULL)
22594 {
22595 SYMBOL_LOCATION_BATON (sym) = baton;
22596 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22597 }
22598 else if (bytes != NULL)
22599 {
22600 SYMBOL_VALUE_BYTES (sym) = bytes;
22601 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22602 }
22603 else
22604 {
22605 SYMBOL_VALUE (sym) = value;
22606 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22607 }
22608 }
22609
22610 /* Return the type of the die in question using its DW_AT_type attribute. */
22611
22612 static struct type *
22613 die_type (struct die_info *die, struct dwarf2_cu *cu)
22614 {
22615 struct attribute *type_attr;
22616
22617 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22618 if (!type_attr)
22619 {
22620 struct objfile *objfile = cu->per_objfile->objfile;
22621 /* A missing DW_AT_type represents a void type. */
22622 return objfile_type (objfile)->builtin_void;
22623 }
22624
22625 return lookup_die_type (die, type_attr, cu);
22626 }
22627
22628 /* True iff CU's producer generates GNAT Ada auxiliary information
22629 that allows to find parallel types through that information instead
22630 of having to do expensive parallel lookups by type name. */
22631
22632 static int
22633 need_gnat_info (struct dwarf2_cu *cu)
22634 {
22635 /* Assume that the Ada compiler was GNAT, which always produces
22636 the auxiliary information. */
22637 return (cu->language == language_ada);
22638 }
22639
22640 /* Return the auxiliary type of the die in question using its
22641 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22642 attribute is not present. */
22643
22644 static struct type *
22645 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22646 {
22647 struct attribute *type_attr;
22648
22649 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22650 if (!type_attr)
22651 return NULL;
22652
22653 return lookup_die_type (die, type_attr, cu);
22654 }
22655
22656 /* If DIE has a descriptive_type attribute, then set the TYPE's
22657 descriptive type accordingly. */
22658
22659 static void
22660 set_descriptive_type (struct type *type, struct die_info *die,
22661 struct dwarf2_cu *cu)
22662 {
22663 struct type *descriptive_type = die_descriptive_type (die, cu);
22664
22665 if (descriptive_type)
22666 {
22667 ALLOCATE_GNAT_AUX_TYPE (type);
22668 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22669 }
22670 }
22671
22672 /* Return the containing type of the die in question using its
22673 DW_AT_containing_type attribute. */
22674
22675 static struct type *
22676 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22677 {
22678 struct attribute *type_attr;
22679 struct objfile *objfile = cu->per_objfile->objfile;
22680
22681 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22682 if (!type_attr)
22683 error (_("Dwarf Error: Problem turning containing type into gdb type "
22684 "[in module %s]"), objfile_name (objfile));
22685
22686 return lookup_die_type (die, type_attr, cu);
22687 }
22688
22689 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22690
22691 static struct type *
22692 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22693 {
22694 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22695 struct objfile *objfile = per_objfile->objfile;
22696 char *saved;
22697
22698 std::string message
22699 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22700 objfile_name (objfile),
22701 sect_offset_str (cu->header.sect_off),
22702 sect_offset_str (die->sect_off));
22703 saved = obstack_strdup (&objfile->objfile_obstack, message);
22704
22705 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22706 }
22707
22708 /* Look up the type of DIE in CU using its type attribute ATTR.
22709 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22710 DW_AT_containing_type.
22711 If there is no type substitute an error marker. */
22712
22713 static struct type *
22714 lookup_die_type (struct die_info *die, const struct attribute *attr,
22715 struct dwarf2_cu *cu)
22716 {
22717 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22718 struct objfile *objfile = per_objfile->objfile;
22719 struct type *this_type;
22720
22721 gdb_assert (attr->name == DW_AT_type
22722 || attr->name == DW_AT_GNAT_descriptive_type
22723 || attr->name == DW_AT_containing_type);
22724
22725 /* First see if we have it cached. */
22726
22727 if (attr->form == DW_FORM_GNU_ref_alt)
22728 {
22729 struct dwarf2_per_cu_data *per_cu;
22730 sect_offset sect_off = attr->get_ref_die_offset ();
22731
22732 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22733 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22734 }
22735 else if (attr->form_is_ref ())
22736 {
22737 sect_offset sect_off = attr->get_ref_die_offset ();
22738
22739 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22740 }
22741 else if (attr->form == DW_FORM_ref_sig8)
22742 {
22743 ULONGEST signature = attr->as_signature ();
22744
22745 return get_signatured_type (die, signature, cu);
22746 }
22747 else
22748 {
22749 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22750 " at %s [in module %s]"),
22751 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22752 objfile_name (objfile));
22753 return build_error_marker_type (cu, die);
22754 }
22755
22756 /* If not cached we need to read it in. */
22757
22758 if (this_type == NULL)
22759 {
22760 struct die_info *type_die = NULL;
22761 struct dwarf2_cu *type_cu = cu;
22762
22763 if (attr->form_is_ref ())
22764 type_die = follow_die_ref (die, attr, &type_cu);
22765 if (type_die == NULL)
22766 return build_error_marker_type (cu, die);
22767 /* If we find the type now, it's probably because the type came
22768 from an inter-CU reference and the type's CU got expanded before
22769 ours. */
22770 this_type = read_type_die (type_die, type_cu);
22771 }
22772
22773 /* If we still don't have a type use an error marker. */
22774
22775 if (this_type == NULL)
22776 return build_error_marker_type (cu, die);
22777
22778 return this_type;
22779 }
22780
22781 /* Return the type in DIE, CU.
22782 Returns NULL for invalid types.
22783
22784 This first does a lookup in die_type_hash,
22785 and only reads the die in if necessary.
22786
22787 NOTE: This can be called when reading in partial or full symbols. */
22788
22789 static struct type *
22790 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22791 {
22792 struct type *this_type;
22793
22794 this_type = get_die_type (die, cu);
22795 if (this_type)
22796 return this_type;
22797
22798 return read_type_die_1 (die, cu);
22799 }
22800
22801 /* Read the type in DIE, CU.
22802 Returns NULL for invalid types. */
22803
22804 static struct type *
22805 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22806 {
22807 struct type *this_type = NULL;
22808
22809 switch (die->tag)
22810 {
22811 case DW_TAG_class_type:
22812 case DW_TAG_interface_type:
22813 case DW_TAG_structure_type:
22814 case DW_TAG_union_type:
22815 this_type = read_structure_type (die, cu);
22816 break;
22817 case DW_TAG_enumeration_type:
22818 this_type = read_enumeration_type (die, cu);
22819 break;
22820 case DW_TAG_subprogram:
22821 case DW_TAG_subroutine_type:
22822 case DW_TAG_inlined_subroutine:
22823 this_type = read_subroutine_type (die, cu);
22824 break;
22825 case DW_TAG_array_type:
22826 this_type = read_array_type (die, cu);
22827 break;
22828 case DW_TAG_set_type:
22829 this_type = read_set_type (die, cu);
22830 break;
22831 case DW_TAG_pointer_type:
22832 this_type = read_tag_pointer_type (die, cu);
22833 break;
22834 case DW_TAG_ptr_to_member_type:
22835 this_type = read_tag_ptr_to_member_type (die, cu);
22836 break;
22837 case DW_TAG_reference_type:
22838 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22839 break;
22840 case DW_TAG_rvalue_reference_type:
22841 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22842 break;
22843 case DW_TAG_const_type:
22844 this_type = read_tag_const_type (die, cu);
22845 break;
22846 case DW_TAG_volatile_type:
22847 this_type = read_tag_volatile_type (die, cu);
22848 break;
22849 case DW_TAG_restrict_type:
22850 this_type = read_tag_restrict_type (die, cu);
22851 break;
22852 case DW_TAG_string_type:
22853 this_type = read_tag_string_type (die, cu);
22854 break;
22855 case DW_TAG_typedef:
22856 this_type = read_typedef (die, cu);
22857 break;
22858 case DW_TAG_subrange_type:
22859 this_type = read_subrange_type (die, cu);
22860 break;
22861 case DW_TAG_base_type:
22862 this_type = read_base_type (die, cu);
22863 break;
22864 case DW_TAG_unspecified_type:
22865 this_type = read_unspecified_type (die, cu);
22866 break;
22867 case DW_TAG_namespace:
22868 this_type = read_namespace_type (die, cu);
22869 break;
22870 case DW_TAG_module:
22871 this_type = read_module_type (die, cu);
22872 break;
22873 case DW_TAG_atomic_type:
22874 this_type = read_tag_atomic_type (die, cu);
22875 break;
22876 default:
22877 complaint (_("unexpected tag in read_type_die: '%s'"),
22878 dwarf_tag_name (die->tag));
22879 break;
22880 }
22881
22882 return this_type;
22883 }
22884
22885 /* See if we can figure out if the class lives in a namespace. We do
22886 this by looking for a member function; its demangled name will
22887 contain namespace info, if there is any.
22888 Return the computed name or NULL.
22889 Space for the result is allocated on the objfile's obstack.
22890 This is the full-die version of guess_partial_die_structure_name.
22891 In this case we know DIE has no useful parent. */
22892
22893 static const char *
22894 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22895 {
22896 struct die_info *spec_die;
22897 struct dwarf2_cu *spec_cu;
22898 struct die_info *child;
22899 struct objfile *objfile = cu->per_objfile->objfile;
22900
22901 spec_cu = cu;
22902 spec_die = die_specification (die, &spec_cu);
22903 if (spec_die != NULL)
22904 {
22905 die = spec_die;
22906 cu = spec_cu;
22907 }
22908
22909 for (child = die->child;
22910 child != NULL;
22911 child = child->sibling)
22912 {
22913 if (child->tag == DW_TAG_subprogram)
22914 {
22915 const char *linkage_name = dw2_linkage_name (child, cu);
22916
22917 if (linkage_name != NULL)
22918 {
22919 gdb::unique_xmalloc_ptr<char> actual_name
22920 (cu->language_defn->class_name_from_physname (linkage_name));
22921 const char *name = NULL;
22922
22923 if (actual_name != NULL)
22924 {
22925 const char *die_name = dwarf2_name (die, cu);
22926
22927 if (die_name != NULL
22928 && strcmp (die_name, actual_name.get ()) != 0)
22929 {
22930 /* Strip off the class name from the full name.
22931 We want the prefix. */
22932 int die_name_len = strlen (die_name);
22933 int actual_name_len = strlen (actual_name.get ());
22934 const char *ptr = actual_name.get ();
22935
22936 /* Test for '::' as a sanity check. */
22937 if (actual_name_len > die_name_len + 2
22938 && ptr[actual_name_len - die_name_len - 1] == ':')
22939 name = obstack_strndup (
22940 &objfile->per_bfd->storage_obstack,
22941 ptr, actual_name_len - die_name_len - 2);
22942 }
22943 }
22944 return name;
22945 }
22946 }
22947 }
22948
22949 return NULL;
22950 }
22951
22952 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22953 prefix part in such case. See
22954 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22955
22956 static const char *
22957 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22958 {
22959 struct attribute *attr;
22960 const char *base;
22961
22962 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22963 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22964 return NULL;
22965
22966 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22967 return NULL;
22968
22969 attr = dw2_linkage_name_attr (die, cu);
22970 const char *attr_name = attr->as_string ();
22971 if (attr == NULL || attr_name == NULL)
22972 return NULL;
22973
22974 /* dwarf2_name had to be already called. */
22975 gdb_assert (attr->canonical_string_p ());
22976
22977 /* Strip the base name, keep any leading namespaces/classes. */
22978 base = strrchr (attr_name, ':');
22979 if (base == NULL || base == attr_name || base[-1] != ':')
22980 return "";
22981
22982 struct objfile *objfile = cu->per_objfile->objfile;
22983 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22984 attr_name,
22985 &base[-1] - attr_name);
22986 }
22987
22988 /* Return the name of the namespace/class that DIE is defined within,
22989 or "" if we can't tell. The caller should not xfree the result.
22990
22991 For example, if we're within the method foo() in the following
22992 code:
22993
22994 namespace N {
22995 class C {
22996 void foo () {
22997 }
22998 };
22999 }
23000
23001 then determine_prefix on foo's die will return "N::C". */
23002
23003 static const char *
23004 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
23005 {
23006 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23007 struct die_info *parent, *spec_die;
23008 struct dwarf2_cu *spec_cu;
23009 struct type *parent_type;
23010 const char *retval;
23011
23012 if (cu->language != language_cplus
23013 && cu->language != language_fortran && cu->language != language_d
23014 && cu->language != language_rust)
23015 return "";
23016
23017 retval = anonymous_struct_prefix (die, cu);
23018 if (retval)
23019 return retval;
23020
23021 /* We have to be careful in the presence of DW_AT_specification.
23022 For example, with GCC 3.4, given the code
23023
23024 namespace N {
23025 void foo() {
23026 // Definition of N::foo.
23027 }
23028 }
23029
23030 then we'll have a tree of DIEs like this:
23031
23032 1: DW_TAG_compile_unit
23033 2: DW_TAG_namespace // N
23034 3: DW_TAG_subprogram // declaration of N::foo
23035 4: DW_TAG_subprogram // definition of N::foo
23036 DW_AT_specification // refers to die #3
23037
23038 Thus, when processing die #4, we have to pretend that we're in
23039 the context of its DW_AT_specification, namely the contex of die
23040 #3. */
23041 spec_cu = cu;
23042 spec_die = die_specification (die, &spec_cu);
23043 if (spec_die == NULL)
23044 parent = die->parent;
23045 else
23046 {
23047 parent = spec_die->parent;
23048 cu = spec_cu;
23049 }
23050
23051 if (parent == NULL)
23052 return "";
23053 else if (parent->building_fullname)
23054 {
23055 const char *name;
23056 const char *parent_name;
23057
23058 /* It has been seen on RealView 2.2 built binaries,
23059 DW_TAG_template_type_param types actually _defined_ as
23060 children of the parent class:
23061
23062 enum E {};
23063 template class <class Enum> Class{};
23064 Class<enum E> class_e;
23065
23066 1: DW_TAG_class_type (Class)
23067 2: DW_TAG_enumeration_type (E)
23068 3: DW_TAG_enumerator (enum1:0)
23069 3: DW_TAG_enumerator (enum2:1)
23070 ...
23071 2: DW_TAG_template_type_param
23072 DW_AT_type DW_FORM_ref_udata (E)
23073
23074 Besides being broken debug info, it can put GDB into an
23075 infinite loop. Consider:
23076
23077 When we're building the full name for Class<E>, we'll start
23078 at Class, and go look over its template type parameters,
23079 finding E. We'll then try to build the full name of E, and
23080 reach here. We're now trying to build the full name of E,
23081 and look over the parent DIE for containing scope. In the
23082 broken case, if we followed the parent DIE of E, we'd again
23083 find Class, and once again go look at its template type
23084 arguments, etc., etc. Simply don't consider such parent die
23085 as source-level parent of this die (it can't be, the language
23086 doesn't allow it), and break the loop here. */
23087 name = dwarf2_name (die, cu);
23088 parent_name = dwarf2_name (parent, cu);
23089 complaint (_("template param type '%s' defined within parent '%s'"),
23090 name ? name : "<unknown>",
23091 parent_name ? parent_name : "<unknown>");
23092 return "";
23093 }
23094 else
23095 switch (parent->tag)
23096 {
23097 case DW_TAG_namespace:
23098 parent_type = read_type_die (parent, cu);
23099 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
23100 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
23101 Work around this problem here. */
23102 if (cu->language == language_cplus
23103 && strcmp (parent_type->name (), "::") == 0)
23104 return "";
23105 /* We give a name to even anonymous namespaces. */
23106 return parent_type->name ();
23107 case DW_TAG_class_type:
23108 case DW_TAG_interface_type:
23109 case DW_TAG_structure_type:
23110 case DW_TAG_union_type:
23111 case DW_TAG_module:
23112 parent_type = read_type_die (parent, cu);
23113 if (parent_type->name () != NULL)
23114 return parent_type->name ();
23115 else
23116 /* An anonymous structure is only allowed non-static data
23117 members; no typedefs, no member functions, et cetera.
23118 So it does not need a prefix. */
23119 return "";
23120 case DW_TAG_compile_unit:
23121 case DW_TAG_partial_unit:
23122 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
23123 if (cu->language == language_cplus
23124 && !per_objfile->per_bfd->types.empty ()
23125 && die->child != NULL
23126 && (die->tag == DW_TAG_class_type
23127 || die->tag == DW_TAG_structure_type
23128 || die->tag == DW_TAG_union_type))
23129 {
23130 const char *name = guess_full_die_structure_name (die, cu);
23131 if (name != NULL)
23132 return name;
23133 }
23134 return "";
23135 case DW_TAG_subprogram:
23136 /* Nested subroutines in Fortran get a prefix with the name
23137 of the parent's subroutine. */
23138 if (cu->language == language_fortran)
23139 {
23140 if ((die->tag == DW_TAG_subprogram)
23141 && (dwarf2_name (parent, cu) != NULL))
23142 return dwarf2_name (parent, cu);
23143 }
23144 return determine_prefix (parent, cu);
23145 case DW_TAG_enumeration_type:
23146 parent_type = read_type_die (parent, cu);
23147 if (TYPE_DECLARED_CLASS (parent_type))
23148 {
23149 if (parent_type->name () != NULL)
23150 return parent_type->name ();
23151 return "";
23152 }
23153 /* Fall through. */
23154 default:
23155 return determine_prefix (parent, cu);
23156 }
23157 }
23158
23159 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
23160 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
23161 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
23162 an obconcat, otherwise allocate storage for the result. The CU argument is
23163 used to determine the language and hence, the appropriate separator. */
23164
23165 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
23166
23167 static char *
23168 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
23169 int physname, struct dwarf2_cu *cu)
23170 {
23171 const char *lead = "";
23172 const char *sep;
23173
23174 if (suffix == NULL || suffix[0] == '\0'
23175 || prefix == NULL || prefix[0] == '\0')
23176 sep = "";
23177 else if (cu->language == language_d)
23178 {
23179 /* For D, the 'main' function could be defined in any module, but it
23180 should never be prefixed. */
23181 if (strcmp (suffix, "D main") == 0)
23182 {
23183 prefix = "";
23184 sep = "";
23185 }
23186 else
23187 sep = ".";
23188 }
23189 else if (cu->language == language_fortran && physname)
23190 {
23191 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23192 DW_AT_MIPS_linkage_name is preferred and used instead. */
23193
23194 lead = "__";
23195 sep = "_MOD_";
23196 }
23197 else
23198 sep = "::";
23199
23200 if (prefix == NULL)
23201 prefix = "";
23202 if (suffix == NULL)
23203 suffix = "";
23204
23205 if (obs == NULL)
23206 {
23207 char *retval
23208 = ((char *)
23209 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
23210
23211 strcpy (retval, lead);
23212 strcat (retval, prefix);
23213 strcat (retval, sep);
23214 strcat (retval, suffix);
23215 return retval;
23216 }
23217 else
23218 {
23219 /* We have an obstack. */
23220 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
23221 }
23222 }
23223
23224 /* Get name of a die, return NULL if not found. */
23225
23226 static const char *
23227 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
23228 struct objfile *objfile)
23229 {
23230 if (name && cu->language == language_cplus)
23231 {
23232 gdb::unique_xmalloc_ptr<char> canon_name
23233 = cp_canonicalize_string (name);
23234
23235 if (canon_name != nullptr)
23236 name = objfile->intern (canon_name.get ());
23237 }
23238
23239 return name;
23240 }
23241
23242 /* Get name of a die, return NULL if not found.
23243 Anonymous namespaces are converted to their magic string. */
23244
23245 static const char *
23246 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
23247 {
23248 struct attribute *attr;
23249 struct objfile *objfile = cu->per_objfile->objfile;
23250
23251 attr = dwarf2_attr (die, DW_AT_name, cu);
23252 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23253 if (attr_name == nullptr
23254 && die->tag != DW_TAG_namespace
23255 && die->tag != DW_TAG_class_type
23256 && die->tag != DW_TAG_interface_type
23257 && die->tag != DW_TAG_structure_type
23258 && die->tag != DW_TAG_union_type)
23259 return NULL;
23260
23261 switch (die->tag)
23262 {
23263 case DW_TAG_compile_unit:
23264 case DW_TAG_partial_unit:
23265 /* Compilation units have a DW_AT_name that is a filename, not
23266 a source language identifier. */
23267 case DW_TAG_enumeration_type:
23268 case DW_TAG_enumerator:
23269 /* These tags always have simple identifiers already; no need
23270 to canonicalize them. */
23271 return attr_name;
23272
23273 case DW_TAG_namespace:
23274 if (attr_name != nullptr)
23275 return attr_name;
23276 return CP_ANONYMOUS_NAMESPACE_STR;
23277
23278 case DW_TAG_class_type:
23279 case DW_TAG_interface_type:
23280 case DW_TAG_structure_type:
23281 case DW_TAG_union_type:
23282 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23283 structures or unions. These were of the form "._%d" in GCC 4.1,
23284 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23285 and GCC 4.4. We work around this problem by ignoring these. */
23286 if (attr_name != nullptr
23287 && (startswith (attr_name, "._")
23288 || startswith (attr_name, "<anonymous")))
23289 return NULL;
23290
23291 /* GCC might emit a nameless typedef that has a linkage name. See
23292 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23293 if (!attr || attr_name == NULL)
23294 {
23295 attr = dw2_linkage_name_attr (die, cu);
23296 attr_name = attr == nullptr ? nullptr : attr->as_string ();
23297 if (attr == NULL || attr_name == NULL)
23298 return NULL;
23299
23300 /* Avoid demangling attr_name the second time on a second
23301 call for the same DIE. */
23302 if (!attr->canonical_string_p ())
23303 {
23304 gdb::unique_xmalloc_ptr<char> demangled
23305 (gdb_demangle (attr_name, DMGL_TYPES));
23306 if (demangled == nullptr)
23307 return nullptr;
23308
23309 attr->set_string_canonical (objfile->intern (demangled.get ()));
23310 attr_name = attr->as_string ();
23311 }
23312
23313 /* Strip any leading namespaces/classes, keep only the
23314 base name. DW_AT_name for named DIEs does not
23315 contain the prefixes. */
23316 const char *base = strrchr (attr_name, ':');
23317 if (base && base > attr_name && base[-1] == ':')
23318 return &base[1];
23319 else
23320 return attr_name;
23321 }
23322 break;
23323
23324 default:
23325 break;
23326 }
23327
23328 if (!attr->canonical_string_p ())
23329 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23330 objfile));
23331 return attr->as_string ();
23332 }
23333
23334 /* Return the die that this die in an extension of, or NULL if there
23335 is none. *EXT_CU is the CU containing DIE on input, and the CU
23336 containing the return value on output. */
23337
23338 static struct die_info *
23339 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23340 {
23341 struct attribute *attr;
23342
23343 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23344 if (attr == NULL)
23345 return NULL;
23346
23347 return follow_die_ref (die, attr, ext_cu);
23348 }
23349
23350 static void
23351 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23352 {
23353 unsigned int i;
23354
23355 print_spaces (indent, f);
23356 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23357 dwarf_tag_name (die->tag), die->abbrev,
23358 sect_offset_str (die->sect_off));
23359
23360 if (die->parent != NULL)
23361 {
23362 print_spaces (indent, f);
23363 fprintf_unfiltered (f, " parent at offset: %s\n",
23364 sect_offset_str (die->parent->sect_off));
23365 }
23366
23367 print_spaces (indent, f);
23368 fprintf_unfiltered (f, " has children: %s\n",
23369 dwarf_bool_name (die->child != NULL));
23370
23371 print_spaces (indent, f);
23372 fprintf_unfiltered (f, " attributes:\n");
23373
23374 for (i = 0; i < die->num_attrs; ++i)
23375 {
23376 print_spaces (indent, f);
23377 fprintf_unfiltered (f, " %s (%s) ",
23378 dwarf_attr_name (die->attrs[i].name),
23379 dwarf_form_name (die->attrs[i].form));
23380
23381 switch (die->attrs[i].form)
23382 {
23383 case DW_FORM_addr:
23384 case DW_FORM_addrx:
23385 case DW_FORM_GNU_addr_index:
23386 fprintf_unfiltered (f, "address: ");
23387 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
23388 break;
23389 case DW_FORM_block2:
23390 case DW_FORM_block4:
23391 case DW_FORM_block:
23392 case DW_FORM_block1:
23393 fprintf_unfiltered (f, "block: size %s",
23394 pulongest (die->attrs[i].as_block ()->size));
23395 break;
23396 case DW_FORM_exprloc:
23397 fprintf_unfiltered (f, "expression: size %s",
23398 pulongest (die->attrs[i].as_block ()->size));
23399 break;
23400 case DW_FORM_data16:
23401 fprintf_unfiltered (f, "constant of 16 bytes");
23402 break;
23403 case DW_FORM_ref_addr:
23404 fprintf_unfiltered (f, "ref address: ");
23405 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23406 break;
23407 case DW_FORM_GNU_ref_alt:
23408 fprintf_unfiltered (f, "alt ref address: ");
23409 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23410 break;
23411 case DW_FORM_ref1:
23412 case DW_FORM_ref2:
23413 case DW_FORM_ref4:
23414 case DW_FORM_ref8:
23415 case DW_FORM_ref_udata:
23416 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23417 (long) (die->attrs[i].as_unsigned ()));
23418 break;
23419 case DW_FORM_data1:
23420 case DW_FORM_data2:
23421 case DW_FORM_data4:
23422 case DW_FORM_data8:
23423 case DW_FORM_udata:
23424 fprintf_unfiltered (f, "constant: %s",
23425 pulongest (die->attrs[i].as_unsigned ()));
23426 break;
23427 case DW_FORM_sec_offset:
23428 fprintf_unfiltered (f, "section offset: %s",
23429 pulongest (die->attrs[i].as_unsigned ()));
23430 break;
23431 case DW_FORM_ref_sig8:
23432 fprintf_unfiltered (f, "signature: %s",
23433 hex_string (die->attrs[i].as_signature ()));
23434 break;
23435 case DW_FORM_string:
23436 case DW_FORM_strp:
23437 case DW_FORM_line_strp:
23438 case DW_FORM_strx:
23439 case DW_FORM_GNU_str_index:
23440 case DW_FORM_GNU_strp_alt:
23441 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23442 die->attrs[i].as_string ()
23443 ? die->attrs[i].as_string () : "",
23444 die->attrs[i].canonical_string_p () ? "is" : "not");
23445 break;
23446 case DW_FORM_flag:
23447 if (die->attrs[i].as_boolean ())
23448 fprintf_unfiltered (f, "flag: TRUE");
23449 else
23450 fprintf_unfiltered (f, "flag: FALSE");
23451 break;
23452 case DW_FORM_flag_present:
23453 fprintf_unfiltered (f, "flag: TRUE");
23454 break;
23455 case DW_FORM_indirect:
23456 /* The reader will have reduced the indirect form to
23457 the "base form" so this form should not occur. */
23458 fprintf_unfiltered (f,
23459 "unexpected attribute form: DW_FORM_indirect");
23460 break;
23461 case DW_FORM_sdata:
23462 case DW_FORM_implicit_const:
23463 fprintf_unfiltered (f, "constant: %s",
23464 plongest (die->attrs[i].as_signed ()));
23465 break;
23466 default:
23467 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23468 die->attrs[i].form);
23469 break;
23470 }
23471 fprintf_unfiltered (f, "\n");
23472 }
23473 }
23474
23475 static void
23476 dump_die_for_error (struct die_info *die)
23477 {
23478 dump_die_shallow (gdb_stderr, 0, die);
23479 }
23480
23481 static void
23482 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23483 {
23484 int indent = level * 4;
23485
23486 gdb_assert (die != NULL);
23487
23488 if (level >= max_level)
23489 return;
23490
23491 dump_die_shallow (f, indent, die);
23492
23493 if (die->child != NULL)
23494 {
23495 print_spaces (indent, f);
23496 fprintf_unfiltered (f, " Children:");
23497 if (level + 1 < max_level)
23498 {
23499 fprintf_unfiltered (f, "\n");
23500 dump_die_1 (f, level + 1, max_level, die->child);
23501 }
23502 else
23503 {
23504 fprintf_unfiltered (f,
23505 " [not printed, max nesting level reached]\n");
23506 }
23507 }
23508
23509 if (die->sibling != NULL && level > 0)
23510 {
23511 dump_die_1 (f, level, max_level, die->sibling);
23512 }
23513 }
23514
23515 /* This is called from the pdie macro in gdbinit.in.
23516 It's not static so gcc will keep a copy callable from gdb. */
23517
23518 void
23519 dump_die (struct die_info *die, int max_level)
23520 {
23521 dump_die_1 (gdb_stdlog, 0, max_level, die);
23522 }
23523
23524 static void
23525 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23526 {
23527 void **slot;
23528
23529 slot = htab_find_slot_with_hash (cu->die_hash, die,
23530 to_underlying (die->sect_off),
23531 INSERT);
23532
23533 *slot = die;
23534 }
23535
23536 /* Follow reference or signature attribute ATTR of SRC_DIE.
23537 On entry *REF_CU is the CU of SRC_DIE.
23538 On exit *REF_CU is the CU of the result. */
23539
23540 static struct die_info *
23541 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23542 struct dwarf2_cu **ref_cu)
23543 {
23544 struct die_info *die;
23545
23546 if (attr->form_is_ref ())
23547 die = follow_die_ref (src_die, attr, ref_cu);
23548 else if (attr->form == DW_FORM_ref_sig8)
23549 die = follow_die_sig (src_die, attr, ref_cu);
23550 else
23551 {
23552 dump_die_for_error (src_die);
23553 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23554 objfile_name ((*ref_cu)->per_objfile->objfile));
23555 }
23556
23557 return die;
23558 }
23559
23560 /* Follow reference OFFSET.
23561 On entry *REF_CU is the CU of the source die referencing OFFSET.
23562 On exit *REF_CU is the CU of the result.
23563 Returns NULL if OFFSET is invalid. */
23564
23565 static struct die_info *
23566 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23567 struct dwarf2_cu **ref_cu)
23568 {
23569 struct die_info temp_die;
23570 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23571 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23572
23573 gdb_assert (cu->per_cu != NULL);
23574
23575 target_cu = cu;
23576
23577 if (cu->per_cu->is_debug_types)
23578 {
23579 /* .debug_types CUs cannot reference anything outside their CU.
23580 If they need to, they have to reference a signatured type via
23581 DW_FORM_ref_sig8. */
23582 if (!cu->header.offset_in_cu_p (sect_off))
23583 return NULL;
23584 }
23585 else if (offset_in_dwz != cu->per_cu->is_dwz
23586 || !cu->header.offset_in_cu_p (sect_off))
23587 {
23588 struct dwarf2_per_cu_data *per_cu;
23589
23590 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23591 per_objfile);
23592
23593 /* If necessary, add it to the queue and load its DIEs. */
23594 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
23595 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23596 false, cu->language);
23597
23598 target_cu = per_objfile->get_cu (per_cu);
23599 }
23600 else if (cu->dies == NULL)
23601 {
23602 /* We're loading full DIEs during partial symbol reading. */
23603 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23604 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23605 language_minimal);
23606 }
23607
23608 *ref_cu = target_cu;
23609 temp_die.sect_off = sect_off;
23610
23611 if (target_cu != cu)
23612 target_cu->ancestor = cu;
23613
23614 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23615 &temp_die,
23616 to_underlying (sect_off));
23617 }
23618
23619 /* Follow reference attribute ATTR of SRC_DIE.
23620 On entry *REF_CU is the CU of SRC_DIE.
23621 On exit *REF_CU is the CU of the result. */
23622
23623 static struct die_info *
23624 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23625 struct dwarf2_cu **ref_cu)
23626 {
23627 sect_offset sect_off = attr->get_ref_die_offset ();
23628 struct dwarf2_cu *cu = *ref_cu;
23629 struct die_info *die;
23630
23631 die = follow_die_offset (sect_off,
23632 (attr->form == DW_FORM_GNU_ref_alt
23633 || cu->per_cu->is_dwz),
23634 ref_cu);
23635 if (!die)
23636 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23637 "at %s [in module %s]"),
23638 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23639 objfile_name (cu->per_objfile->objfile));
23640
23641 return die;
23642 }
23643
23644 /* See read.h. */
23645
23646 struct dwarf2_locexpr_baton
23647 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23648 dwarf2_per_cu_data *per_cu,
23649 dwarf2_per_objfile *per_objfile,
23650 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23651 bool resolve_abstract_p)
23652 {
23653 struct die_info *die;
23654 struct attribute *attr;
23655 struct dwarf2_locexpr_baton retval;
23656 struct objfile *objfile = per_objfile->objfile;
23657
23658 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23659 if (cu == nullptr)
23660 cu = load_cu (per_cu, per_objfile, false);
23661
23662 if (cu == nullptr)
23663 {
23664 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23665 Instead just throw an error, not much else we can do. */
23666 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23667 sect_offset_str (sect_off), objfile_name (objfile));
23668 }
23669
23670 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23671 if (!die)
23672 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23673 sect_offset_str (sect_off), objfile_name (objfile));
23674
23675 attr = dwarf2_attr (die, DW_AT_location, cu);
23676 if (!attr && resolve_abstract_p
23677 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23678 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23679 {
23680 CORE_ADDR pc = get_frame_pc ();
23681 CORE_ADDR baseaddr = objfile->text_section_offset ();
23682 struct gdbarch *gdbarch = objfile->arch ();
23683
23684 for (const auto &cand_off
23685 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23686 {
23687 struct dwarf2_cu *cand_cu = cu;
23688 struct die_info *cand
23689 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23690 if (!cand
23691 || !cand->parent
23692 || cand->parent->tag != DW_TAG_subprogram)
23693 continue;
23694
23695 CORE_ADDR pc_low, pc_high;
23696 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23697 if (pc_low == ((CORE_ADDR) -1))
23698 continue;
23699 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23700 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23701 if (!(pc_low <= pc && pc < pc_high))
23702 continue;
23703
23704 die = cand;
23705 attr = dwarf2_attr (die, DW_AT_location, cu);
23706 break;
23707 }
23708 }
23709
23710 if (!attr)
23711 {
23712 /* DWARF: "If there is no such attribute, then there is no effect.".
23713 DATA is ignored if SIZE is 0. */
23714
23715 retval.data = NULL;
23716 retval.size = 0;
23717 }
23718 else if (attr->form_is_section_offset ())
23719 {
23720 struct dwarf2_loclist_baton loclist_baton;
23721 CORE_ADDR pc = get_frame_pc ();
23722 size_t size;
23723
23724 fill_in_loclist_baton (cu, &loclist_baton, attr);
23725
23726 retval.data = dwarf2_find_location_expression (&loclist_baton,
23727 &size, pc);
23728 retval.size = size;
23729 }
23730 else
23731 {
23732 if (!attr->form_is_block ())
23733 error (_("Dwarf Error: DIE at %s referenced in module %s "
23734 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23735 sect_offset_str (sect_off), objfile_name (objfile));
23736
23737 struct dwarf_block *block = attr->as_block ();
23738 retval.data = block->data;
23739 retval.size = block->size;
23740 }
23741 retval.per_objfile = per_objfile;
23742 retval.per_cu = cu->per_cu;
23743
23744 per_objfile->age_comp_units ();
23745
23746 return retval;
23747 }
23748
23749 /* See read.h. */
23750
23751 struct dwarf2_locexpr_baton
23752 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23753 dwarf2_per_cu_data *per_cu,
23754 dwarf2_per_objfile *per_objfile,
23755 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23756 {
23757 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23758
23759 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23760 get_frame_pc);
23761 }
23762
23763 /* Write a constant of a given type as target-ordered bytes into
23764 OBSTACK. */
23765
23766 static const gdb_byte *
23767 write_constant_as_bytes (struct obstack *obstack,
23768 enum bfd_endian byte_order,
23769 struct type *type,
23770 ULONGEST value,
23771 LONGEST *len)
23772 {
23773 gdb_byte *result;
23774
23775 *len = TYPE_LENGTH (type);
23776 result = (gdb_byte *) obstack_alloc (obstack, *len);
23777 store_unsigned_integer (result, *len, byte_order, value);
23778
23779 return result;
23780 }
23781
23782 /* See read.h. */
23783
23784 const gdb_byte *
23785 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23786 dwarf2_per_cu_data *per_cu,
23787 dwarf2_per_objfile *per_objfile,
23788 obstack *obstack,
23789 LONGEST *len)
23790 {
23791 struct die_info *die;
23792 struct attribute *attr;
23793 const gdb_byte *result = NULL;
23794 struct type *type;
23795 LONGEST value;
23796 enum bfd_endian byte_order;
23797 struct objfile *objfile = per_objfile->objfile;
23798
23799 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23800 if (cu == nullptr)
23801 cu = load_cu (per_cu, per_objfile, false);
23802
23803 if (cu == nullptr)
23804 {
23805 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23806 Instead just throw an error, not much else we can do. */
23807 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23808 sect_offset_str (sect_off), objfile_name (objfile));
23809 }
23810
23811 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23812 if (!die)
23813 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23814 sect_offset_str (sect_off), objfile_name (objfile));
23815
23816 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23817 if (attr == NULL)
23818 return NULL;
23819
23820 byte_order = (bfd_big_endian (objfile->obfd)
23821 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23822
23823 switch (attr->form)
23824 {
23825 case DW_FORM_addr:
23826 case DW_FORM_addrx:
23827 case DW_FORM_GNU_addr_index:
23828 {
23829 gdb_byte *tem;
23830
23831 *len = cu->header.addr_size;
23832 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23833 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
23834 result = tem;
23835 }
23836 break;
23837 case DW_FORM_string:
23838 case DW_FORM_strp:
23839 case DW_FORM_strx:
23840 case DW_FORM_GNU_str_index:
23841 case DW_FORM_GNU_strp_alt:
23842 /* The string is already allocated on the objfile obstack, point
23843 directly to it. */
23844 {
23845 const char *attr_name = attr->as_string ();
23846 result = (const gdb_byte *) attr_name;
23847 *len = strlen (attr_name);
23848 }
23849 break;
23850 case DW_FORM_block1:
23851 case DW_FORM_block2:
23852 case DW_FORM_block4:
23853 case DW_FORM_block:
23854 case DW_FORM_exprloc:
23855 case DW_FORM_data16:
23856 {
23857 struct dwarf_block *block = attr->as_block ();
23858 result = block->data;
23859 *len = block->size;
23860 }
23861 break;
23862
23863 /* The DW_AT_const_value attributes are supposed to carry the
23864 symbol's value "represented as it would be on the target
23865 architecture." By the time we get here, it's already been
23866 converted to host endianness, so we just need to sign- or
23867 zero-extend it as appropriate. */
23868 case DW_FORM_data1:
23869 type = die_type (die, cu);
23870 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23871 if (result == NULL)
23872 result = write_constant_as_bytes (obstack, byte_order,
23873 type, value, len);
23874 break;
23875 case DW_FORM_data2:
23876 type = die_type (die, cu);
23877 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23878 if (result == NULL)
23879 result = write_constant_as_bytes (obstack, byte_order,
23880 type, value, len);
23881 break;
23882 case DW_FORM_data4:
23883 type = die_type (die, cu);
23884 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23885 if (result == NULL)
23886 result = write_constant_as_bytes (obstack, byte_order,
23887 type, value, len);
23888 break;
23889 case DW_FORM_data8:
23890 type = die_type (die, cu);
23891 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23892 if (result == NULL)
23893 result = write_constant_as_bytes (obstack, byte_order,
23894 type, value, len);
23895 break;
23896
23897 case DW_FORM_sdata:
23898 case DW_FORM_implicit_const:
23899 type = die_type (die, cu);
23900 result = write_constant_as_bytes (obstack, byte_order,
23901 type, attr->as_signed (), len);
23902 break;
23903
23904 case DW_FORM_udata:
23905 type = die_type (die, cu);
23906 result = write_constant_as_bytes (obstack, byte_order,
23907 type, attr->as_unsigned (), len);
23908 break;
23909
23910 default:
23911 complaint (_("unsupported const value attribute form: '%s'"),
23912 dwarf_form_name (attr->form));
23913 break;
23914 }
23915
23916 return result;
23917 }
23918
23919 /* See read.h. */
23920
23921 struct type *
23922 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23923 dwarf2_per_cu_data *per_cu,
23924 dwarf2_per_objfile *per_objfile)
23925 {
23926 struct die_info *die;
23927
23928 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23929 if (cu == nullptr)
23930 cu = load_cu (per_cu, per_objfile, false);
23931
23932 if (cu == nullptr)
23933 return nullptr;
23934
23935 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23936 if (!die)
23937 return NULL;
23938
23939 return die_type (die, cu);
23940 }
23941
23942 /* See read.h. */
23943
23944 struct type *
23945 dwarf2_get_die_type (cu_offset die_offset,
23946 dwarf2_per_cu_data *per_cu,
23947 dwarf2_per_objfile *per_objfile)
23948 {
23949 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23950 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
23951 }
23952
23953 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23954 On entry *REF_CU is the CU of SRC_DIE.
23955 On exit *REF_CU is the CU of the result.
23956 Returns NULL if the referenced DIE isn't found. */
23957
23958 static struct die_info *
23959 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23960 struct dwarf2_cu **ref_cu)
23961 {
23962 struct die_info temp_die;
23963 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23964 struct die_info *die;
23965 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
23966
23967
23968 /* While it might be nice to assert sig_type->type == NULL here,
23969 we can get here for DW_AT_imported_declaration where we need
23970 the DIE not the type. */
23971
23972 /* If necessary, add it to the queue and load its DIEs. */
23973
23974 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
23975 language_minimal))
23976 read_signatured_type (sig_type, per_objfile);
23977
23978 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
23979 gdb_assert (sig_cu != NULL);
23980 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23981 temp_die.sect_off = sig_type->type_offset_in_section;
23982 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23983 to_underlying (temp_die.sect_off));
23984 if (die)
23985 {
23986 /* For .gdb_index version 7 keep track of included TUs.
23987 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23988 if (per_objfile->per_bfd->index_table != NULL
23989 && per_objfile->per_bfd->index_table->version <= 7)
23990 {
23991 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23992 }
23993
23994 *ref_cu = sig_cu;
23995 if (sig_cu != cu)
23996 sig_cu->ancestor = cu;
23997
23998 return die;
23999 }
24000
24001 return NULL;
24002 }
24003
24004 /* Follow signatured type referenced by ATTR in SRC_DIE.
24005 On entry *REF_CU is the CU of SRC_DIE.
24006 On exit *REF_CU is the CU of the result.
24007 The result is the DIE of the type.
24008 If the referenced type cannot be found an error is thrown. */
24009
24010 static struct die_info *
24011 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
24012 struct dwarf2_cu **ref_cu)
24013 {
24014 ULONGEST signature = attr->as_signature ();
24015 struct signatured_type *sig_type;
24016 struct die_info *die;
24017
24018 gdb_assert (attr->form == DW_FORM_ref_sig8);
24019
24020 sig_type = lookup_signatured_type (*ref_cu, signature);
24021 /* sig_type will be NULL if the signatured type is missing from
24022 the debug info. */
24023 if (sig_type == NULL)
24024 {
24025 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24026 " from DIE at %s [in module %s]"),
24027 hex_string (signature), sect_offset_str (src_die->sect_off),
24028 objfile_name ((*ref_cu)->per_objfile->objfile));
24029 }
24030
24031 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
24032 if (die == NULL)
24033 {
24034 dump_die_for_error (src_die);
24035 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24036 " from DIE at %s [in module %s]"),
24037 hex_string (signature), sect_offset_str (src_die->sect_off),
24038 objfile_name ((*ref_cu)->per_objfile->objfile));
24039 }
24040
24041 return die;
24042 }
24043
24044 /* Get the type specified by SIGNATURE referenced in DIE/CU,
24045 reading in and processing the type unit if necessary. */
24046
24047 static struct type *
24048 get_signatured_type (struct die_info *die, ULONGEST signature,
24049 struct dwarf2_cu *cu)
24050 {
24051 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24052 struct signatured_type *sig_type;
24053 struct dwarf2_cu *type_cu;
24054 struct die_info *type_die;
24055 struct type *type;
24056
24057 sig_type = lookup_signatured_type (cu, signature);
24058 /* sig_type will be NULL if the signatured type is missing from
24059 the debug info. */
24060 if (sig_type == NULL)
24061 {
24062 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24063 " from DIE at %s [in module %s]"),
24064 hex_string (signature), sect_offset_str (die->sect_off),
24065 objfile_name (per_objfile->objfile));
24066 return build_error_marker_type (cu, die);
24067 }
24068
24069 /* If we already know the type we're done. */
24070 type = per_objfile->get_type_for_signatured_type (sig_type);
24071 if (type != nullptr)
24072 return type;
24073
24074 type_cu = cu;
24075 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24076 if (type_die != NULL)
24077 {
24078 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24079 is created. This is important, for example, because for c++ classes
24080 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24081 type = read_type_die (type_die, type_cu);
24082 if (type == NULL)
24083 {
24084 complaint (_("Dwarf Error: Cannot build signatured type %s"
24085 " referenced from DIE at %s [in module %s]"),
24086 hex_string (signature), sect_offset_str (die->sect_off),
24087 objfile_name (per_objfile->objfile));
24088 type = build_error_marker_type (cu, die);
24089 }
24090 }
24091 else
24092 {
24093 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24094 " from DIE at %s [in module %s]"),
24095 hex_string (signature), sect_offset_str (die->sect_off),
24096 objfile_name (per_objfile->objfile));
24097 type = build_error_marker_type (cu, die);
24098 }
24099
24100 per_objfile->set_type_for_signatured_type (sig_type, type);
24101
24102 return type;
24103 }
24104
24105 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24106 reading in and processing the type unit if necessary. */
24107
24108 static struct type *
24109 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
24110 struct dwarf2_cu *cu) /* ARI: editCase function */
24111 {
24112 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
24113 if (attr->form_is_ref ())
24114 {
24115 struct dwarf2_cu *type_cu = cu;
24116 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24117
24118 return read_type_die (type_die, type_cu);
24119 }
24120 else if (attr->form == DW_FORM_ref_sig8)
24121 {
24122 return get_signatured_type (die, attr->as_signature (), cu);
24123 }
24124 else
24125 {
24126 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24127
24128 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
24129 " at %s [in module %s]"),
24130 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
24131 objfile_name (per_objfile->objfile));
24132 return build_error_marker_type (cu, die);
24133 }
24134 }
24135
24136 /* Load the DIEs associated with type unit PER_CU into memory. */
24137
24138 static void
24139 load_full_type_unit (dwarf2_per_cu_data *per_cu,
24140 dwarf2_per_objfile *per_objfile)
24141 {
24142 struct signatured_type *sig_type;
24143
24144 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24145 gdb_assert (! per_cu->type_unit_group_p ());
24146
24147 /* We have the per_cu, but we need the signatured_type.
24148 Fortunately this is an easy translation. */
24149 gdb_assert (per_cu->is_debug_types);
24150 sig_type = (struct signatured_type *) per_cu;
24151
24152 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24153
24154 read_signatured_type (sig_type, per_objfile);
24155
24156 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
24157 }
24158
24159 /* Read in a signatured type and build its CU and DIEs.
24160 If the type is a stub for the real type in a DWO file,
24161 read in the real type from the DWO file as well. */
24162
24163 static void
24164 read_signatured_type (signatured_type *sig_type,
24165 dwarf2_per_objfile *per_objfile)
24166 {
24167 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
24168
24169 gdb_assert (per_cu->is_debug_types);
24170 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24171
24172 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
24173
24174 if (!reader.dummy_p)
24175 {
24176 struct dwarf2_cu *cu = reader.cu;
24177 const gdb_byte *info_ptr = reader.info_ptr;
24178
24179 gdb_assert (cu->die_hash == NULL);
24180 cu->die_hash =
24181 htab_create_alloc_ex (cu->header.length / 12,
24182 die_hash,
24183 die_eq,
24184 NULL,
24185 &cu->comp_unit_obstack,
24186 hashtab_obstack_allocate,
24187 dummy_obstack_deallocate);
24188
24189 if (reader.comp_unit_die->has_children)
24190 reader.comp_unit_die->child
24191 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24192 reader.comp_unit_die);
24193 cu->dies = reader.comp_unit_die;
24194 /* comp_unit_die is not stored in die_hash, no need. */
24195
24196 /* We try not to read any attributes in this function, because
24197 not all CUs needed for references have been loaded yet, and
24198 symbol table processing isn't initialized. But we have to
24199 set the CU language, or we won't be able to build types
24200 correctly. Similarly, if we do not read the producer, we can
24201 not apply producer-specific interpretation. */
24202 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24203
24204 reader.keep ();
24205 }
24206
24207 sig_type->per_cu.tu_read = 1;
24208 }
24209
24210 /* Decode simple location descriptions.
24211 Given a pointer to a dwarf block that defines a location, compute
24212 the location and return the value. If COMPUTED is non-null, it is
24213 set to true to indicate that decoding was successful, and false
24214 otherwise. If COMPUTED is null, then this function may emit a
24215 complaint. */
24216
24217 static CORE_ADDR
24218 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
24219 {
24220 struct objfile *objfile = cu->per_objfile->objfile;
24221 size_t i;
24222 size_t size = blk->size;
24223 const gdb_byte *data = blk->data;
24224 CORE_ADDR stack[64];
24225 int stacki;
24226 unsigned int bytes_read, unsnd;
24227 gdb_byte op;
24228
24229 if (computed != nullptr)
24230 *computed = false;
24231
24232 i = 0;
24233 stacki = 0;
24234 stack[stacki] = 0;
24235 stack[++stacki] = 0;
24236
24237 while (i < size)
24238 {
24239 op = data[i++];
24240 switch (op)
24241 {
24242 case DW_OP_lit0:
24243 case DW_OP_lit1:
24244 case DW_OP_lit2:
24245 case DW_OP_lit3:
24246 case DW_OP_lit4:
24247 case DW_OP_lit5:
24248 case DW_OP_lit6:
24249 case DW_OP_lit7:
24250 case DW_OP_lit8:
24251 case DW_OP_lit9:
24252 case DW_OP_lit10:
24253 case DW_OP_lit11:
24254 case DW_OP_lit12:
24255 case DW_OP_lit13:
24256 case DW_OP_lit14:
24257 case DW_OP_lit15:
24258 case DW_OP_lit16:
24259 case DW_OP_lit17:
24260 case DW_OP_lit18:
24261 case DW_OP_lit19:
24262 case DW_OP_lit20:
24263 case DW_OP_lit21:
24264 case DW_OP_lit22:
24265 case DW_OP_lit23:
24266 case DW_OP_lit24:
24267 case DW_OP_lit25:
24268 case DW_OP_lit26:
24269 case DW_OP_lit27:
24270 case DW_OP_lit28:
24271 case DW_OP_lit29:
24272 case DW_OP_lit30:
24273 case DW_OP_lit31:
24274 stack[++stacki] = op - DW_OP_lit0;
24275 break;
24276
24277 case DW_OP_reg0:
24278 case DW_OP_reg1:
24279 case DW_OP_reg2:
24280 case DW_OP_reg3:
24281 case DW_OP_reg4:
24282 case DW_OP_reg5:
24283 case DW_OP_reg6:
24284 case DW_OP_reg7:
24285 case DW_OP_reg8:
24286 case DW_OP_reg9:
24287 case DW_OP_reg10:
24288 case DW_OP_reg11:
24289 case DW_OP_reg12:
24290 case DW_OP_reg13:
24291 case DW_OP_reg14:
24292 case DW_OP_reg15:
24293 case DW_OP_reg16:
24294 case DW_OP_reg17:
24295 case DW_OP_reg18:
24296 case DW_OP_reg19:
24297 case DW_OP_reg20:
24298 case DW_OP_reg21:
24299 case DW_OP_reg22:
24300 case DW_OP_reg23:
24301 case DW_OP_reg24:
24302 case DW_OP_reg25:
24303 case DW_OP_reg26:
24304 case DW_OP_reg27:
24305 case DW_OP_reg28:
24306 case DW_OP_reg29:
24307 case DW_OP_reg30:
24308 case DW_OP_reg31:
24309 stack[++stacki] = op - DW_OP_reg0;
24310 if (i < size)
24311 {
24312 if (computed == nullptr)
24313 dwarf2_complex_location_expr_complaint ();
24314 else
24315 return 0;
24316 }
24317 break;
24318
24319 case DW_OP_regx:
24320 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24321 i += bytes_read;
24322 stack[++stacki] = unsnd;
24323 if (i < size)
24324 {
24325 if (computed == nullptr)
24326 dwarf2_complex_location_expr_complaint ();
24327 else
24328 return 0;
24329 }
24330 break;
24331
24332 case DW_OP_addr:
24333 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24334 &bytes_read);
24335 i += bytes_read;
24336 break;
24337
24338 case DW_OP_const1u:
24339 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24340 i += 1;
24341 break;
24342
24343 case DW_OP_const1s:
24344 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24345 i += 1;
24346 break;
24347
24348 case DW_OP_const2u:
24349 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24350 i += 2;
24351 break;
24352
24353 case DW_OP_const2s:
24354 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24355 i += 2;
24356 break;
24357
24358 case DW_OP_const4u:
24359 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24360 i += 4;
24361 break;
24362
24363 case DW_OP_const4s:
24364 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24365 i += 4;
24366 break;
24367
24368 case DW_OP_const8u:
24369 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24370 i += 8;
24371 break;
24372
24373 case DW_OP_constu:
24374 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24375 &bytes_read);
24376 i += bytes_read;
24377 break;
24378
24379 case DW_OP_consts:
24380 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24381 i += bytes_read;
24382 break;
24383
24384 case DW_OP_dup:
24385 stack[stacki + 1] = stack[stacki];
24386 stacki++;
24387 break;
24388
24389 case DW_OP_plus:
24390 stack[stacki - 1] += stack[stacki];
24391 stacki--;
24392 break;
24393
24394 case DW_OP_plus_uconst:
24395 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24396 &bytes_read);
24397 i += bytes_read;
24398 break;
24399
24400 case DW_OP_minus:
24401 stack[stacki - 1] -= stack[stacki];
24402 stacki--;
24403 break;
24404
24405 case DW_OP_deref:
24406 /* If we're not the last op, then we definitely can't encode
24407 this using GDB's address_class enum. This is valid for partial
24408 global symbols, although the variable's address will be bogus
24409 in the psymtab. */
24410 if (i < size)
24411 {
24412 if (computed == nullptr)
24413 dwarf2_complex_location_expr_complaint ();
24414 else
24415 return 0;
24416 }
24417 break;
24418
24419 case DW_OP_GNU_push_tls_address:
24420 case DW_OP_form_tls_address:
24421 /* The top of the stack has the offset from the beginning
24422 of the thread control block at which the variable is located. */
24423 /* Nothing should follow this operator, so the top of stack would
24424 be returned. */
24425 /* This is valid for partial global symbols, but the variable's
24426 address will be bogus in the psymtab. Make it always at least
24427 non-zero to not look as a variable garbage collected by linker
24428 which have DW_OP_addr 0. */
24429 if (i < size)
24430 {
24431 if (computed == nullptr)
24432 dwarf2_complex_location_expr_complaint ();
24433 else
24434 return 0;
24435 }
24436 stack[stacki]++;
24437 break;
24438
24439 case DW_OP_GNU_uninit:
24440 if (computed != nullptr)
24441 return 0;
24442 break;
24443
24444 case DW_OP_addrx:
24445 case DW_OP_GNU_addr_index:
24446 case DW_OP_GNU_const_index:
24447 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24448 &bytes_read);
24449 i += bytes_read;
24450 break;
24451
24452 default:
24453 if (computed == nullptr)
24454 {
24455 const char *name = get_DW_OP_name (op);
24456
24457 if (name)
24458 complaint (_("unsupported stack op: '%s'"),
24459 name);
24460 else
24461 complaint (_("unsupported stack op: '%02x'"),
24462 op);
24463 }
24464
24465 return (stack[stacki]);
24466 }
24467
24468 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24469 outside of the allocated space. Also enforce minimum>0. */
24470 if (stacki >= ARRAY_SIZE (stack) - 1)
24471 {
24472 if (computed == nullptr)
24473 complaint (_("location description stack overflow"));
24474 return 0;
24475 }
24476
24477 if (stacki <= 0)
24478 {
24479 if (computed == nullptr)
24480 complaint (_("location description stack underflow"));
24481 return 0;
24482 }
24483 }
24484
24485 if (computed != nullptr)
24486 *computed = true;
24487 return (stack[stacki]);
24488 }
24489
24490 /* memory allocation interface */
24491
24492 static struct dwarf_block *
24493 dwarf_alloc_block (struct dwarf2_cu *cu)
24494 {
24495 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24496 }
24497
24498 static struct die_info *
24499 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24500 {
24501 struct die_info *die;
24502 size_t size = sizeof (struct die_info);
24503
24504 if (num_attrs > 1)
24505 size += (num_attrs - 1) * sizeof (struct attribute);
24506
24507 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24508 memset (die, 0, sizeof (struct die_info));
24509 return (die);
24510 }
24511
24512 \f
24513
24514 /* Macro support. */
24515
24516 /* An overload of dwarf_decode_macros that finds the correct section
24517 and ensures it is read in before calling the other overload. */
24518
24519 static void
24520 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24521 int section_is_gnu)
24522 {
24523 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24524 struct objfile *objfile = per_objfile->objfile;
24525 const struct line_header *lh = cu->line_header;
24526 unsigned int offset_size = cu->header.offset_size;
24527 struct dwarf2_section_info *section;
24528 const char *section_name;
24529
24530 if (cu->dwo_unit != nullptr)
24531 {
24532 if (section_is_gnu)
24533 {
24534 section = &cu->dwo_unit->dwo_file->sections.macro;
24535 section_name = ".debug_macro.dwo";
24536 }
24537 else
24538 {
24539 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24540 section_name = ".debug_macinfo.dwo";
24541 }
24542 }
24543 else
24544 {
24545 if (section_is_gnu)
24546 {
24547 section = &per_objfile->per_bfd->macro;
24548 section_name = ".debug_macro";
24549 }
24550 else
24551 {
24552 section = &per_objfile->per_bfd->macinfo;
24553 section_name = ".debug_macinfo";
24554 }
24555 }
24556
24557 section->read (objfile);
24558 if (section->buffer == nullptr)
24559 {
24560 complaint (_("missing %s section"), section_name);
24561 return;
24562 }
24563
24564 buildsym_compunit *builder = cu->get_builder ();
24565
24566 struct dwarf2_section_info *str_offsets_section;
24567 struct dwarf2_section_info *str_section;
24568 ULONGEST str_offsets_base;
24569
24570 if (cu->dwo_unit != nullptr)
24571 {
24572 str_offsets_section = &cu->dwo_unit->dwo_file
24573 ->sections.str_offsets;
24574 str_section = &cu->dwo_unit->dwo_file->sections.str;
24575 str_offsets_base = cu->header.addr_size;
24576 }
24577 else
24578 {
24579 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24580 str_section = &per_objfile->per_bfd->str;
24581 str_offsets_base = *cu->str_offsets_base;
24582 }
24583
24584 dwarf_decode_macros (per_objfile, builder, section, lh,
24585 offset_size, offset, str_section, str_offsets_section,
24586 str_offsets_base, section_is_gnu);
24587 }
24588
24589 /* Return the .debug_loc section to use for CU.
24590 For DWO files use .debug_loc.dwo. */
24591
24592 static struct dwarf2_section_info *
24593 cu_debug_loc_section (struct dwarf2_cu *cu)
24594 {
24595 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24596
24597 if (cu->dwo_unit)
24598 {
24599 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24600
24601 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24602 }
24603 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24604 : &per_objfile->per_bfd->loc);
24605 }
24606
24607 /* Return the .debug_rnglists section to use for CU. */
24608 static struct dwarf2_section_info *
24609 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24610 {
24611 if (cu->header.version < 5)
24612 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24613 cu->header.version);
24614 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24615
24616 /* Make sure we read the .debug_rnglists section from the file that
24617 contains the DW_AT_ranges attribute we are reading. Normally that
24618 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24619 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24620 program. */
24621 if (cu->dwo_unit != nullptr
24622 && tag != DW_TAG_compile_unit
24623 && tag != DW_TAG_skeleton_unit)
24624 {
24625 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24626
24627 if (sections->rnglists.size > 0)
24628 return &sections->rnglists;
24629 else
24630 error (_(".debug_rnglists section is missing from .dwo file."));
24631 }
24632 return &dwarf2_per_objfile->per_bfd->rnglists;
24633 }
24634
24635 /* A helper function that fills in a dwarf2_loclist_baton. */
24636
24637 static void
24638 fill_in_loclist_baton (struct dwarf2_cu *cu,
24639 struct dwarf2_loclist_baton *baton,
24640 const struct attribute *attr)
24641 {
24642 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24643 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24644
24645 section->read (per_objfile->objfile);
24646
24647 baton->per_objfile = per_objfile;
24648 baton->per_cu = cu->per_cu;
24649 gdb_assert (baton->per_cu);
24650 /* We don't know how long the location list is, but make sure we
24651 don't run off the edge of the section. */
24652 baton->size = section->size - attr->as_unsigned ();
24653 baton->data = section->buffer + attr->as_unsigned ();
24654 if (cu->base_address.has_value ())
24655 baton->base_address = *cu->base_address;
24656 else
24657 baton->base_address = 0;
24658 baton->from_dwo = cu->dwo_unit != NULL;
24659 }
24660
24661 static void
24662 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24663 struct dwarf2_cu *cu, int is_block)
24664 {
24665 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24666 struct objfile *objfile = per_objfile->objfile;
24667 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24668
24669 if (attr->form_is_section_offset ()
24670 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24671 the section. If so, fall through to the complaint in the
24672 other branch. */
24673 && attr->as_unsigned () < section->get_size (objfile))
24674 {
24675 struct dwarf2_loclist_baton *baton;
24676
24677 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24678
24679 fill_in_loclist_baton (cu, baton, attr);
24680
24681 if (!cu->base_address.has_value ())
24682 complaint (_("Location list used without "
24683 "specifying the CU base address."));
24684
24685 SYMBOL_ACLASS_INDEX (sym) = (is_block
24686 ? dwarf2_loclist_block_index
24687 : dwarf2_loclist_index);
24688 SYMBOL_LOCATION_BATON (sym) = baton;
24689 }
24690 else
24691 {
24692 struct dwarf2_locexpr_baton *baton;
24693
24694 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24695 baton->per_objfile = per_objfile;
24696 baton->per_cu = cu->per_cu;
24697 gdb_assert (baton->per_cu);
24698
24699 if (attr->form_is_block ())
24700 {
24701 /* Note that we're just copying the block's data pointer
24702 here, not the actual data. We're still pointing into the
24703 info_buffer for SYM's objfile; right now we never release
24704 that buffer, but when we do clean up properly this may
24705 need to change. */
24706 struct dwarf_block *block = attr->as_block ();
24707 baton->size = block->size;
24708 baton->data = block->data;
24709 }
24710 else
24711 {
24712 dwarf2_invalid_attrib_class_complaint ("location description",
24713 sym->natural_name ());
24714 baton->size = 0;
24715 }
24716
24717 SYMBOL_ACLASS_INDEX (sym) = (is_block
24718 ? dwarf2_locexpr_block_index
24719 : dwarf2_locexpr_index);
24720 SYMBOL_LOCATION_BATON (sym) = baton;
24721 }
24722 }
24723
24724 /* See read.h. */
24725
24726 const comp_unit_head *
24727 dwarf2_per_cu_data::get_header () const
24728 {
24729 if (!m_header_read_in)
24730 {
24731 const gdb_byte *info_ptr
24732 = this->section->buffer + to_underlying (this->sect_off);
24733
24734 memset (&m_header, 0, sizeof (m_header));
24735
24736 read_comp_unit_head (&m_header, info_ptr, this->section,
24737 rcuh_kind::COMPILE);
24738 }
24739
24740 return &m_header;
24741 }
24742
24743 /* See read.h. */
24744
24745 int
24746 dwarf2_per_cu_data::addr_size () const
24747 {
24748 return this->get_header ()->addr_size;
24749 }
24750
24751 /* See read.h. */
24752
24753 int
24754 dwarf2_per_cu_data::offset_size () const
24755 {
24756 return this->get_header ()->offset_size;
24757 }
24758
24759 /* See read.h. */
24760
24761 int
24762 dwarf2_per_cu_data::ref_addr_size () const
24763 {
24764 const comp_unit_head *header = this->get_header ();
24765
24766 if (header->version == 2)
24767 return header->addr_size;
24768 else
24769 return header->offset_size;
24770 }
24771
24772 /* See read.h. */
24773
24774 struct type *
24775 dwarf2_cu::addr_type () const
24776 {
24777 struct objfile *objfile = this->per_objfile->objfile;
24778 struct type *void_type = objfile_type (objfile)->builtin_void;
24779 struct type *addr_type = lookup_pointer_type (void_type);
24780 int addr_size = this->per_cu->addr_size ();
24781
24782 if (TYPE_LENGTH (addr_type) == addr_size)
24783 return addr_type;
24784
24785 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
24786 return addr_type;
24787 }
24788
24789 /* A helper function for dwarf2_find_containing_comp_unit that returns
24790 the index of the result, and that searches a vector. It will
24791 return a result even if the offset in question does not actually
24792 occur in any CU. This is separate so that it can be unit
24793 tested. */
24794
24795 static int
24796 dwarf2_find_containing_comp_unit
24797 (sect_offset sect_off,
24798 unsigned int offset_in_dwz,
24799 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
24800 {
24801 int low, high;
24802
24803 low = 0;
24804 high = all_comp_units.size () - 1;
24805 while (high > low)
24806 {
24807 struct dwarf2_per_cu_data *mid_cu;
24808 int mid = low + (high - low) / 2;
24809
24810 mid_cu = all_comp_units[mid];
24811 if (mid_cu->is_dwz > offset_in_dwz
24812 || (mid_cu->is_dwz == offset_in_dwz
24813 && mid_cu->sect_off + mid_cu->length > sect_off))
24814 high = mid;
24815 else
24816 low = mid + 1;
24817 }
24818 gdb_assert (low == high);
24819 return low;
24820 }
24821
24822 /* Locate the .debug_info compilation unit from CU's objfile which contains
24823 the DIE at OFFSET. Raises an error on failure. */
24824
24825 static struct dwarf2_per_cu_data *
24826 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24827 unsigned int offset_in_dwz,
24828 dwarf2_per_objfile *per_objfile)
24829 {
24830 int low = dwarf2_find_containing_comp_unit
24831 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24832 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
24833
24834 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24835 {
24836 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24837 error (_("Dwarf Error: could not find partial DIE containing "
24838 "offset %s [in module %s]"),
24839 sect_offset_str (sect_off),
24840 bfd_get_filename (per_objfile->objfile->obfd));
24841
24842 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
24843 <= sect_off);
24844 return per_objfile->per_bfd->all_comp_units[low-1];
24845 }
24846 else
24847 {
24848 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
24849 && sect_off >= this_cu->sect_off + this_cu->length)
24850 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24851 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24852 return this_cu;
24853 }
24854 }
24855
24856 #if GDB_SELF_TEST
24857
24858 namespace selftests {
24859 namespace find_containing_comp_unit {
24860
24861 static void
24862 run_test ()
24863 {
24864 struct dwarf2_per_cu_data one {};
24865 struct dwarf2_per_cu_data two {};
24866 struct dwarf2_per_cu_data three {};
24867 struct dwarf2_per_cu_data four {};
24868
24869 one.length = 5;
24870 two.sect_off = sect_offset (one.length);
24871 two.length = 7;
24872
24873 three.length = 5;
24874 three.is_dwz = 1;
24875 four.sect_off = sect_offset (three.length);
24876 four.length = 7;
24877 four.is_dwz = 1;
24878
24879 std::vector<dwarf2_per_cu_data *> units;
24880 units.push_back (&one);
24881 units.push_back (&two);
24882 units.push_back (&three);
24883 units.push_back (&four);
24884
24885 int result;
24886
24887 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24888 SELF_CHECK (units[result] == &one);
24889 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24890 SELF_CHECK (units[result] == &one);
24891 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24892 SELF_CHECK (units[result] == &two);
24893
24894 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24895 SELF_CHECK (units[result] == &three);
24896 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24897 SELF_CHECK (units[result] == &three);
24898 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24899 SELF_CHECK (units[result] == &four);
24900 }
24901
24902 }
24903 }
24904
24905 #endif /* GDB_SELF_TEST */
24906
24907 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
24908
24909 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
24910 dwarf2_per_objfile *per_objfile)
24911 : per_cu (per_cu),
24912 per_objfile (per_objfile),
24913 mark (false),
24914 has_loclist (false),
24915 checked_producer (false),
24916 producer_is_gxx_lt_4_6 (false),
24917 producer_is_gcc_lt_4_3 (false),
24918 producer_is_icc (false),
24919 producer_is_icc_lt_14 (false),
24920 producer_is_codewarrior (false),
24921 processing_has_namespace_info (false)
24922 {
24923 }
24924
24925 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24926
24927 static void
24928 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24929 enum language pretend_language)
24930 {
24931 struct attribute *attr;
24932
24933 /* Set the language we're debugging. */
24934 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24935 if (attr != nullptr)
24936 set_cu_language (attr->constant_value (0), cu);
24937 else
24938 {
24939 cu->language = pretend_language;
24940 cu->language_defn = language_def (cu->language);
24941 }
24942
24943 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24944 }
24945
24946 /* See read.h. */
24947
24948 dwarf2_cu *
24949 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
24950 {
24951 auto it = m_dwarf2_cus.find (per_cu);
24952 if (it == m_dwarf2_cus.end ())
24953 return nullptr;
24954
24955 return it->second;
24956 }
24957
24958 /* See read.h. */
24959
24960 void
24961 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24962 {
24963 gdb_assert (this->get_cu (per_cu) == nullptr);
24964
24965 m_dwarf2_cus[per_cu] = cu;
24966 }
24967
24968 /* See read.h. */
24969
24970 void
24971 dwarf2_per_objfile::age_comp_units ()
24972 {
24973 /* Start by clearing all marks. */
24974 for (auto pair : m_dwarf2_cus)
24975 pair.second->mark = false;
24976
24977 /* Traverse all CUs, mark them and their dependencies if used recently
24978 enough. */
24979 for (auto pair : m_dwarf2_cus)
24980 {
24981 dwarf2_cu *cu = pair.second;
24982
24983 cu->last_used++;
24984 if (cu->last_used <= dwarf_max_cache_age)
24985 dwarf2_mark (cu);
24986 }
24987
24988 /* Delete all CUs still not marked. */
24989 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
24990 {
24991 dwarf2_cu *cu = it->second;
24992
24993 if (!cu->mark)
24994 {
24995 delete cu;
24996 it = m_dwarf2_cus.erase (it);
24997 }
24998 else
24999 it++;
25000 }
25001 }
25002
25003 /* See read.h. */
25004
25005 void
25006 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
25007 {
25008 auto it = m_dwarf2_cus.find (per_cu);
25009 if (it == m_dwarf2_cus.end ())
25010 return;
25011
25012 delete it->second;
25013
25014 m_dwarf2_cus.erase (it);
25015 }
25016
25017 dwarf2_per_objfile::~dwarf2_per_objfile ()
25018 {
25019 remove_all_cus ();
25020 }
25021
25022 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25023 We store these in a hash table separate from the DIEs, and preserve them
25024 when the DIEs are flushed out of cache.
25025
25026 The CU "per_cu" pointer is needed because offset alone is not enough to
25027 uniquely identify the type. A file may have multiple .debug_types sections,
25028 or the type may come from a DWO file. Furthermore, while it's more logical
25029 to use per_cu->section+offset, with Fission the section with the data is in
25030 the DWO file but we don't know that section at the point we need it.
25031 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25032 because we can enter the lookup routine, get_die_type_at_offset, from
25033 outside this file, and thus won't necessarily have PER_CU->cu.
25034 Fortunately, PER_CU is stable for the life of the objfile. */
25035
25036 struct dwarf2_per_cu_offset_and_type
25037 {
25038 const struct dwarf2_per_cu_data *per_cu;
25039 sect_offset sect_off;
25040 struct type *type;
25041 };
25042
25043 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25044
25045 static hashval_t
25046 per_cu_offset_and_type_hash (const void *item)
25047 {
25048 const struct dwarf2_per_cu_offset_and_type *ofs
25049 = (const struct dwarf2_per_cu_offset_and_type *) item;
25050
25051 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25052 }
25053
25054 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25055
25056 static int
25057 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25058 {
25059 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25060 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25061 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25062 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25063
25064 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25065 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25066 }
25067
25068 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25069 table if necessary. For convenience, return TYPE.
25070
25071 The DIEs reading must have careful ordering to:
25072 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25073 reading current DIE.
25074 * Not trying to dereference contents of still incompletely read in types
25075 while reading in other DIEs.
25076 * Enable referencing still incompletely read in types just by a pointer to
25077 the type without accessing its fields.
25078
25079 Therefore caller should follow these rules:
25080 * Try to fetch any prerequisite types we may need to build this DIE type
25081 before building the type and calling set_die_type.
25082 * After building type call set_die_type for current DIE as soon as
25083 possible before fetching more types to complete the current type.
25084 * Make the type as complete as possible before fetching more types. */
25085
25086 static struct type *
25087 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
25088 bool skip_data_location)
25089 {
25090 dwarf2_per_objfile *per_objfile = cu->per_objfile;
25091 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25092 struct objfile *objfile = per_objfile->objfile;
25093 struct attribute *attr;
25094 struct dynamic_prop prop;
25095
25096 /* For Ada types, make sure that the gnat-specific data is always
25097 initialized (if not already set). There are a few types where
25098 we should not be doing so, because the type-specific area is
25099 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25100 where the type-specific area is used to store the floatformat).
25101 But this is not a problem, because the gnat-specific information
25102 is actually not needed for these types. */
25103 if (need_gnat_info (cu)
25104 && type->code () != TYPE_CODE_FUNC
25105 && type->code () != TYPE_CODE_FLT
25106 && type->code () != TYPE_CODE_METHODPTR
25107 && type->code () != TYPE_CODE_MEMBERPTR
25108 && type->code () != TYPE_CODE_METHOD
25109 && type->code () != TYPE_CODE_FIXED_POINT
25110 && !HAVE_GNAT_AUX_INFO (type))
25111 INIT_GNAT_SPECIFIC (type);
25112
25113 /* Read DW_AT_allocated and set in type. */
25114 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25115 if (attr != NULL)
25116 {
25117 struct type *prop_type = cu->addr_sized_int_type (false);
25118 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25119 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
25120 }
25121
25122 /* Read DW_AT_associated and set in type. */
25123 attr = dwarf2_attr (die, DW_AT_associated, cu);
25124 if (attr != NULL)
25125 {
25126 struct type *prop_type = cu->addr_sized_int_type (false);
25127 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25128 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
25129 }
25130
25131 /* Read DW_AT_data_location and set in type. */
25132 if (!skip_data_location)
25133 {
25134 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25135 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
25136 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
25137 }
25138
25139 if (per_objfile->die_type_hash == NULL)
25140 per_objfile->die_type_hash
25141 = htab_up (htab_create_alloc (127,
25142 per_cu_offset_and_type_hash,
25143 per_cu_offset_and_type_eq,
25144 NULL, xcalloc, xfree));
25145
25146 ofs.per_cu = cu->per_cu;
25147 ofs.sect_off = die->sect_off;
25148 ofs.type = type;
25149 slot = (struct dwarf2_per_cu_offset_and_type **)
25150 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
25151 if (*slot)
25152 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25153 sect_offset_str (die->sect_off));
25154 *slot = XOBNEW (&objfile->objfile_obstack,
25155 struct dwarf2_per_cu_offset_and_type);
25156 **slot = ofs;
25157 return type;
25158 }
25159
25160 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25161 or return NULL if the die does not have a saved type. */
25162
25163 static struct type *
25164 get_die_type_at_offset (sect_offset sect_off,
25165 dwarf2_per_cu_data *per_cu,
25166 dwarf2_per_objfile *per_objfile)
25167 {
25168 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25169
25170 if (per_objfile->die_type_hash == NULL)
25171 return NULL;
25172
25173 ofs.per_cu = per_cu;
25174 ofs.sect_off = sect_off;
25175 slot = ((struct dwarf2_per_cu_offset_and_type *)
25176 htab_find (per_objfile->die_type_hash.get (), &ofs));
25177 if (slot)
25178 return slot->type;
25179 else
25180 return NULL;
25181 }
25182
25183 /* Look up the type for DIE in CU in die_type_hash,
25184 or return NULL if DIE does not have a saved type. */
25185
25186 static struct type *
25187 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25188 {
25189 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
25190 }
25191
25192 /* Add a dependence relationship from CU to REF_PER_CU. */
25193
25194 static void
25195 dwarf2_add_dependence (struct dwarf2_cu *cu,
25196 struct dwarf2_per_cu_data *ref_per_cu)
25197 {
25198 void **slot;
25199
25200 if (cu->dependencies == NULL)
25201 cu->dependencies
25202 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25203 NULL, &cu->comp_unit_obstack,
25204 hashtab_obstack_allocate,
25205 dummy_obstack_deallocate);
25206
25207 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25208 if (*slot == NULL)
25209 *slot = ref_per_cu;
25210 }
25211
25212 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25213 Set the mark field in every compilation unit in the
25214 cache that we must keep because we are keeping CU.
25215
25216 DATA is the dwarf2_per_objfile object in which to look up CUs. */
25217
25218 static int
25219 dwarf2_mark_helper (void **slot, void *data)
25220 {
25221 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25222 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25223 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
25224
25225 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25226 reading of the chain. As such dependencies remain valid it is not much
25227 useful to track and undo them during QUIT cleanups. */
25228 if (cu == nullptr)
25229 return 1;
25230
25231 if (cu->mark)
25232 return 1;
25233
25234 cu->mark = true;
25235
25236 if (cu->dependencies != nullptr)
25237 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
25238
25239 return 1;
25240 }
25241
25242 /* Set the mark field in CU and in every other compilation unit in the
25243 cache that we must keep because we are keeping CU. */
25244
25245 static void
25246 dwarf2_mark (struct dwarf2_cu *cu)
25247 {
25248 if (cu->mark)
25249 return;
25250
25251 cu->mark = true;
25252
25253 if (cu->dependencies != nullptr)
25254 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
25255 }
25256
25257 /* Trivial hash function for partial_die_info: the hash value of a DIE
25258 is its offset in .debug_info for this objfile. */
25259
25260 static hashval_t
25261 partial_die_hash (const void *item)
25262 {
25263 const struct partial_die_info *part_die
25264 = (const struct partial_die_info *) item;
25265
25266 return to_underlying (part_die->sect_off);
25267 }
25268
25269 /* Trivial comparison function for partial_die_info structures: two DIEs
25270 are equal if they have the same offset. */
25271
25272 static int
25273 partial_die_eq (const void *item_lhs, const void *item_rhs)
25274 {
25275 const struct partial_die_info *part_die_lhs
25276 = (const struct partial_die_info *) item_lhs;
25277 const struct partial_die_info *part_die_rhs
25278 = (const struct partial_die_info *) item_rhs;
25279
25280 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25281 }
25282
25283 struct cmd_list_element *set_dwarf_cmdlist;
25284 struct cmd_list_element *show_dwarf_cmdlist;
25285
25286 static void
25287 show_check_physname (struct ui_file *file, int from_tty,
25288 struct cmd_list_element *c, const char *value)
25289 {
25290 fprintf_filtered (file,
25291 _("Whether to check \"physname\" is %s.\n"),
25292 value);
25293 }
25294
25295 void _initialize_dwarf2_read ();
25296 void
25297 _initialize_dwarf2_read ()
25298 {
25299 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
25300 Set DWARF specific variables.\n\
25301 Configure DWARF variables such as the cache size."),
25302 &set_dwarf_cmdlist, "maintenance set dwarf ",
25303 0/*allow-unknown*/, &maintenance_set_cmdlist);
25304
25305 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
25306 Show DWARF specific variables.\n\
25307 Show DWARF variables such as the cache size."),
25308 &show_dwarf_cmdlist, "maintenance show dwarf ",
25309 0/*allow-unknown*/, &maintenance_show_cmdlist);
25310
25311 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25312 &dwarf_max_cache_age, _("\
25313 Set the upper bound on the age of cached DWARF compilation units."), _("\
25314 Show the upper bound on the age of cached DWARF compilation units."), _("\
25315 A higher limit means that cached compilation units will be stored\n\
25316 in memory longer, and more total memory will be used. Zero disables\n\
25317 caching, which can slow down startup."),
25318 NULL,
25319 show_dwarf_max_cache_age,
25320 &set_dwarf_cmdlist,
25321 &show_dwarf_cmdlist);
25322
25323 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25324 Set debugging of the DWARF reader."), _("\
25325 Show debugging of the DWARF reader."), _("\
25326 When enabled (non-zero), debugging messages are printed during DWARF\n\
25327 reading and symtab expansion. A value of 1 (one) provides basic\n\
25328 information. A value greater than 1 provides more verbose information."),
25329 NULL,
25330 NULL,
25331 &setdebuglist, &showdebuglist);
25332
25333 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25334 Set debugging of the DWARF DIE reader."), _("\
25335 Show debugging of the DWARF DIE reader."), _("\
25336 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25337 The value is the maximum depth to print."),
25338 NULL,
25339 NULL,
25340 &setdebuglist, &showdebuglist);
25341
25342 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25343 Set debugging of the dwarf line reader."), _("\
25344 Show debugging of the dwarf line reader."), _("\
25345 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25346 A value of 1 (one) provides basic information.\n\
25347 A value greater than 1 provides more verbose information."),
25348 NULL,
25349 NULL,
25350 &setdebuglist, &showdebuglist);
25351
25352 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25353 Set cross-checking of \"physname\" code against demangler."), _("\
25354 Show cross-checking of \"physname\" code against demangler."), _("\
25355 When enabled, GDB's internal \"physname\" code is checked against\n\
25356 the demangler."),
25357 NULL, show_check_physname,
25358 &setdebuglist, &showdebuglist);
25359
25360 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25361 no_class, &use_deprecated_index_sections, _("\
25362 Set whether to use deprecated gdb_index sections."), _("\
25363 Show whether to use deprecated gdb_index sections."), _("\
25364 When enabled, deprecated .gdb_index sections are used anyway.\n\
25365 Normally they are ignored either because of a missing feature or\n\
25366 performance issue.\n\
25367 Warning: This option must be enabled before gdb reads the file."),
25368 NULL,
25369 NULL,
25370 &setlist, &showlist);
25371
25372 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25373 &dwarf2_locexpr_funcs);
25374 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25375 &dwarf2_loclist_funcs);
25376
25377 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25378 &dwarf2_block_frame_base_locexpr_funcs);
25379 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25380 &dwarf2_block_frame_base_loclist_funcs);
25381
25382 #if GDB_SELF_TEST
25383 selftests::register_test ("dw2_expand_symtabs_matching",
25384 selftests::dw2_expand_symtabs_matching::run_test);
25385 selftests::register_test ("dwarf2_find_containing_comp_unit",
25386 selftests::find_containing_comp_unit::run_test);
25387 #endif
25388 }