Attribute method inlining
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
b811d2c2 3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
c906108c
SS
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
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
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
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
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.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
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
c906108c 31#include "defs.h"
82ca8957 32#include "dwarf2/read.h"
3054dd54 33#include "dwarf2/abbrev.h"
162dce55 34#include "dwarf2/attribute.h"
4057dfde 35#include "dwarf2/comp-unit.h"
82ca8957
TT
36#include "dwarf2/index-cache.h"
37#include "dwarf2/index-common.h"
f4382c45 38#include "dwarf2/leb.h"
8fdd972c 39#include "dwarf2/line-header.h"
9fda78b6 40#include "dwarf2/dwz.h"
c90ec28a 41#include "dwarf2/macro.h"
c2d50fd0 42#include "dwarf2/die.h"
2b2558bf 43#include "dwarf2/stringify.h"
4de283e4
TT
44#include "bfd.h"
45#include "elf-bfd.h"
46#include "symtab.h"
47#include "gdbtypes.h"
48#include "objfiles.h"
d55e5aa6 49#include "dwarf2.h"
4de283e4
TT
50#include "buildsym.h"
51#include "demangle.h"
52#include "gdb-demangle.h"
4de283e4 53#include "filenames.h" /* for DOSish file names */
4de283e4
TT
54#include "language.h"
55#include "complaints.h"
82ca8957
TT
56#include "dwarf2/expr.h"
57#include "dwarf2/loc.h"
4de283e4
TT
58#include "cp-support.h"
59#include "hashtab.h"
60#include "command.h"
d55e5aa6 61#include "gdbcmd.h"
4de283e4
TT
62#include "block.h"
63#include "addrmap.h"
64#include "typeprint.h"
65#include "psympriv.h"
4de283e4 66#include "c-lang.h"
d55e5aa6 67#include "go-lang.h"
4de283e4
TT
68#include "valprint.h"
69#include "gdbcore.h" /* for gnutarget */
70#include "gdb/gdb-index.h"
4de283e4
TT
71#include "gdb_bfd.h"
72#include "f-lang.h"
73#include "source.h"
4de283e4 74#include "build-id.h"
d55e5aa6 75#include "namespace.h"
268a13a5
TT
76#include "gdbsupport/function-view.h"
77#include "gdbsupport/gdb_optional.h"
78#include "gdbsupport/underlying.h"
268a13a5 79#include "gdbsupport/hash_enum.h"
4de283e4 80#include "filename-seen-cache.h"
b32b108a 81#include "producer.h"
4de283e4 82#include <fcntl.h>
4de283e4 83#include <algorithm>
4de283e4 84#include <unordered_map>
268a13a5 85#include "gdbsupport/selftest.h"
c9317f21 86#include "rust-lang.h"
268a13a5 87#include "gdbsupport/pathstuff.h"
edd45eb0 88#include "count-one-bits.h"
0d79cdc4 89#include "debuginfod-support.h"
437afbb8 90
73be47f5
DE
91/* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
b4f54984
DE
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94static unsigned int dwarf_read_debug = 0;
45cfd468 95
d97bc12b 96/* When non-zero, dump DIEs after they are read in. */
b4f54984 97static unsigned int dwarf_die_debug = 0;
d97bc12b 98
27e0867f 99/* When non-zero, dump line number entries as they are read in. */
8fdd972c 100unsigned int dwarf_line_debug = 0;
27e0867f 101
491144b5
CB
102/* When true, cross-check physname against demangler. */
103static bool check_physname = false;
900e11f9 104
491144b5
CB
105/* When true, do not reject deprecated .gdb_index sections. */
106static bool use_deprecated_index_sections = false;
481860b3 107
17ee85fc
TT
108/* This is used to store the data that is always per objfile. */
109static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
110
111/* These are used to store the dwarf2_per_bfd objects.
112
113 objfiles having the same BFD, which doesn't require relocations, are going to
114 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
115
116 Other objfiles are not going to share a dwarf2_per_bfd with any other
117 objfiles, so they'll have their own version kept in the _objfile_data_key
118 version. */
119static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
120static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
6502dd73 121
f1e6e072
TT
122/* The "aclass" indices for various kinds of computed DWARF symbols. */
123
124static int dwarf2_locexpr_index;
125static int dwarf2_loclist_index;
126static int dwarf2_locexpr_block_index;
127static int dwarf2_loclist_block_index;
128
41144253 129/* Size of .debug_loclists section header for 32-bit DWARF format. */
130#define LOCLIST_HEADER_SIZE32 12
131
132/* Size of .debug_loclists section header for 64-bit DWARF format. */
133#define LOCLIST_HEADER_SIZE64 20
134
3f563c84
PA
135/* An index into a (C++) symbol name component in a symbol name as
136 recorded in the mapped_index's symbol table. For each C++ symbol
137 in the symbol table, we record one entry for the start of each
138 component in the symbol in a table of name components, and then
139 sort the table, in order to be able to binary search symbol names,
140 ignoring leading namespaces, both completion and regular look up.
141 For example, for symbol "A::B::C", we'll have an entry that points
142 to "A::B::C", another that points to "B::C", and another for "C".
143 Note that function symbols in GDB index have no parameter
144 information, just the function/method names. You can convert a
145 name_component to a "const char *" using the
146 'mapped_index::symbol_name_at(offset_type)' method. */
147
148struct name_component
149{
150 /* Offset in the symbol name where the component starts. Stored as
151 a (32-bit) offset instead of a pointer to save memory and improve
152 locality on 64-bit architectures. */
153 offset_type name_offset;
154
155 /* The symbol's index in the symbol and constant pool tables of a
156 mapped_index. */
157 offset_type idx;
158};
159
44ed8f3e
PA
160/* Base class containing bits shared by both .gdb_index and
161 .debug_name indexes. */
162
163struct mapped_index_base
164{
22ca247e
TT
165 mapped_index_base () = default;
166 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
167
44ed8f3e
PA
168 /* The name_component table (a sorted vector). See name_component's
169 description above. */
170 std::vector<name_component> name_components;
171
172 /* How NAME_COMPONENTS is sorted. */
173 enum case_sensitivity name_components_casing;
174
175 /* Return the number of names in the symbol table. */
176 virtual size_t symbol_name_count () const = 0;
177
178 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
179 virtual const char *symbol_name_at
180 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
44ed8f3e
PA
181
182 /* Return whether the name at IDX in the symbol table should be
183 ignored. */
184 virtual bool symbol_name_slot_invalid (offset_type idx) const
185 {
186 return false;
187 }
188
189 /* Build the symbol name component sorted vector, if we haven't
190 yet. */
fcf23d5b 191 void build_name_components (dwarf2_per_objfile *per_objfile);
44ed8f3e
PA
192
193 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
194 possible matches for LN_NO_PARAMS in the name component
195 vector. */
196 std::pair<std::vector<name_component>::const_iterator,
197 std::vector<name_component>::const_iterator>
3b00ef10 198 find_name_components_bounds (const lookup_name_info &ln_no_params,
fcf23d5b
SM
199 enum language lang,
200 dwarf2_per_objfile *per_objfile) const;
44ed8f3e
PA
201
202 /* Prevent deleting/destroying via a base class pointer. */
203protected:
204 ~mapped_index_base() = default;
205};
206
9291a0cd
TT
207/* A description of the mapped index. The file format is described in
208 a comment by the code that writes the index. */
fc898b42 209struct mapped_index final : public mapped_index_base
9291a0cd 210{
f00a2de2
PA
211 /* A slot/bucket in the symbol table hash. */
212 struct symbol_table_slot
213 {
214 const offset_type name;
215 const offset_type vec;
216 };
217
559a7a62 218 /* Index data format version. */
3063847f 219 int version = 0;
559a7a62 220
f00a2de2
PA
221 /* The address table data. */
222 gdb::array_view<const gdb_byte> address_table;
b11b1f88 223
3876f04e 224 /* The symbol table, implemented as a hash table. */
f00a2de2 225 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 226
9291a0cd 227 /* A pointer to the constant pool. */
3063847f 228 const char *constant_pool = nullptr;
3f563c84 229
44ed8f3e
PA
230 bool symbol_name_slot_invalid (offset_type idx) const override
231 {
232 const auto &bucket = this->symbol_table[idx];
9ab08412 233 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 234 }
5c58de74 235
3f563c84
PA
236 /* Convenience method to get at the name of the symbol at IDX in the
237 symbol table. */
fcf23d5b
SM
238 const char *symbol_name_at
239 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
f00a2de2 240 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 241
44ed8f3e
PA
242 size_t symbol_name_count () const override
243 { return this->symbol_table.size (); }
9291a0cd
TT
244};
245
927aa2e7
JK
246/* A description of the mapped .debug_names.
247 Uninitialized map has CU_COUNT 0. */
fc898b42 248struct mapped_debug_names final : public mapped_index_base
927aa2e7
JK
249{
250 bfd_endian dwarf5_byte_order;
251 bool dwarf5_is_dwarf64;
252 bool augmentation_is_gdb;
253 uint8_t offset_size;
254 uint32_t cu_count = 0;
255 uint32_t tu_count, bucket_count, name_count;
256 const gdb_byte *cu_table_reordered, *tu_table_reordered;
257 const uint32_t *bucket_table_reordered, *hash_table_reordered;
258 const gdb_byte *name_table_string_offs_reordered;
259 const gdb_byte *name_table_entry_offs_reordered;
260 const gdb_byte *entry_pool;
261
262 struct index_val
263 {
264 ULONGEST dwarf_tag;
265 struct attr
266 {
267 /* Attribute name DW_IDX_*. */
268 ULONGEST dw_idx;
269
270 /* Attribute form DW_FORM_*. */
271 ULONGEST form;
272
273 /* Value if FORM is DW_FORM_implicit_const. */
274 LONGEST implicit_const;
275 };
276 std::vector<attr> attr_vec;
277 };
278
279 std::unordered_map<ULONGEST, index_val> abbrev_map;
280
fcf23d5b
SM
281 const char *namei_to_name
282 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
44ed8f3e
PA
283
284 /* Implementation of the mapped_index_base virtual interface, for
285 the name_components cache. */
286
fcf23d5b
SM
287 const char *symbol_name_at
288 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
289 { return namei_to_name (idx, per_objfile); }
44ed8f3e
PA
290
291 size_t symbol_name_count () const override
292 { return this->name_count; }
927aa2e7
JK
293};
294
cd4fb1b2 295/* See dwarf2read.h. */
ed2dc618 296
cd4fb1b2 297dwarf2_per_objfile *
ed2dc618
SM
298get_dwarf2_per_objfile (struct objfile *objfile)
299{
5bfd760d 300 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 301}
c906108c 302
251d32d9 303/* Default names of the debugging sections. */
c906108c 304
233a11ab
CS
305/* Note that if the debugging section has been compressed, it might
306 have a name like .zdebug_info. */
307
9cdd5dbd
DE
308static const struct dwarf2_debug_sections dwarf2_elf_names =
309{
251d32d9
TG
310 { ".debug_info", ".zdebug_info" },
311 { ".debug_abbrev", ".zdebug_abbrev" },
312 { ".debug_line", ".zdebug_line" },
313 { ".debug_loc", ".zdebug_loc" },
43988095 314 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 315 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 316 { ".debug_macro", ".zdebug_macro" },
251d32d9 317 { ".debug_str", ".zdebug_str" },
18a8505e 318 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 319 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 320 { ".debug_ranges", ".zdebug_ranges" },
43988095 321 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 322 { ".debug_types", ".zdebug_types" },
3019eac3 323 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
324 { ".debug_frame", ".zdebug_frame" },
325 { ".eh_frame", NULL },
24d3216f 326 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
327 { ".debug_names", ".zdebug_names" },
328 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 329 23
251d32d9 330};
c906108c 331
80626a55 332/* List of DWO/DWP sections. */
3019eac3 333
80626a55 334static const struct dwop_section_names
3019eac3
DE
335{
336 struct dwarf2_section_names abbrev_dwo;
337 struct dwarf2_section_names info_dwo;
338 struct dwarf2_section_names line_dwo;
339 struct dwarf2_section_names loc_dwo;
43988095 340 struct dwarf2_section_names loclists_dwo;
09262596
DE
341 struct dwarf2_section_names macinfo_dwo;
342 struct dwarf2_section_names macro_dwo;
3019eac3
DE
343 struct dwarf2_section_names str_dwo;
344 struct dwarf2_section_names str_offsets_dwo;
345 struct dwarf2_section_names types_dwo;
80626a55
DE
346 struct dwarf2_section_names cu_index;
347 struct dwarf2_section_names tu_index;
3019eac3 348}
80626a55 349dwop_section_names =
3019eac3
DE
350{
351 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
352 { ".debug_info.dwo", ".zdebug_info.dwo" },
353 { ".debug_line.dwo", ".zdebug_line.dwo" },
354 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 355 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
356 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
357 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
358 { ".debug_str.dwo", ".zdebug_str.dwo" },
359 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
360 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
361 { ".debug_cu_index", ".zdebug_cu_index" },
362 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
363};
364
c906108c
SS
365/* local data types */
366
41144253 367/* The location list section (.debug_loclists) begins with a header,
368 which contains the following information. */
369struct loclist_header
370{
371 /* A 4-byte or 12-byte length containing the length of the
372 set of entries for this compilation unit, not including the
373 length field itself. */
374 unsigned int length;
375
376 /* A 2-byte version identifier. */
377 short version;
378
379 /* A 1-byte unsigned integer containing the size in bytes of an address on
380 the target system. */
381 unsigned char addr_size;
382
383 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
384 on the target system. */
385 unsigned char segment_collector_size;
386
387 /* A 4-byte count of the number of offsets that follow the header. */
388 unsigned int offset_entry_count;
389};
390
3da10d80
KS
391/* Type used for delaying computation of method physnames.
392 See comments for compute_delayed_physnames. */
393struct delayed_method_info
394{
395 /* The type to which the method is attached, i.e., its parent class. */
396 struct type *type;
397
398 /* The index of the method in the type's function fieldlists. */
399 int fnfield_index;
400
401 /* The index of the method in the fieldlist. */
402 int index;
403
404 /* The name of the DIE. */
405 const char *name;
406
407 /* The DIE associated with this method. */
408 struct die_info *die;
409};
410
e7c27a73
DJ
411/* Internal state when decoding a particular compilation unit. */
412struct dwarf2_cu
413{
9e021579
SM
414 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
415 dwarf2_per_objfile *per_objfile);
fcd3b13d
SM
416
417 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
418
c24bdb02
KS
419 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
420 Create the set of symtabs used by this TU, or if this TU is sharing
421 symtabs with another TU and the symtabs have already been created
422 then restore those symtabs in the line header.
423 We don't need the pc/line-number mapping for type units. */
424 void setup_type_unit_groups (struct die_info *die);
425
426 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
427 buildsym_compunit constructor. */
428 struct compunit_symtab *start_symtab (const char *name,
429 const char *comp_dir,
430 CORE_ADDR low_pc);
431
432 /* Reset the builder. */
433 void reset_builder () { m_builder.reset (); }
434
293e7e51
SM
435 /* Return a type that is a generic pointer type, the size of which
436 matches the address size given in the compilation unit header for
437 this CU. */
438 struct type *addr_type () const;
439
440 /* Find an integer type the same size as the address size given in
441 the compilation unit header for this CU. UNSIGNED_P controls if
442 the integer is unsigned or not. */
443 struct type *addr_sized_int_type (bool unsigned_p) const;
444
d00adf39 445 /* The header of the compilation unit. */
fcd3b13d 446 struct comp_unit_head header {};
e142c38c 447
d00adf39 448 /* Base address of this compilation unit. */
2b24b6e4 449 gdb::optional<CORE_ADDR> base_address;
d00adf39 450
e142c38c 451 /* The language we are debugging. */
fcd3b13d
SM
452 enum language language = language_unknown;
453 const struct language_defn *language_defn = nullptr;
e142c38c 454
fcd3b13d 455 const char *producer = nullptr;
b0f35d58 456
c24bdb02 457private:
804d2729
TT
458 /* The symtab builder for this CU. This is only non-NULL when full
459 symbols are being read. */
c24bdb02 460 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 461
c24bdb02 462public:
e142c38c
DJ
463 /* The generic symbol table building routines have separate lists for
464 file scope symbols and all all other scopes (local scopes). So
465 we need to select the right one to pass to add_symbol_to_list().
466 We do it by keeping a pointer to the correct list in list_in_scope.
467
468 FIXME: The original dwarf code just treated the file scope as the
469 first local scope, and all other local scopes as nested local
470 scopes, and worked fine. Check to see if we really need to
471 distinguish these in buildsym.c. */
fcd3b13d 472 struct pending **list_in_scope = nullptr;
e142c38c 473
b64f50a1
JK
474 /* Hash table holding all the loaded partial DIEs
475 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 476 htab_t partial_dies = nullptr;
72bf9492
DJ
477
478 /* Storage for things with the same lifetime as this read-in compilation
479 unit, including partial DIEs. */
fcd3b13d 480 auto_obstack comp_unit_obstack;
72bf9492 481
69d751e3 482 /* Backlink to our per_cu entry. */
ae038cb0
DJ
483 struct dwarf2_per_cu_data *per_cu;
484
9e021579
SM
485 /* The dwarf2_per_objfile that owns this. */
486 struct dwarf2_per_objfile *per_objfile;
487
ae038cb0 488 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 489 int last_used = 0;
ae038cb0 490
b64f50a1
JK
491 /* A hash table of DIE cu_offset for following references with
492 die_info->offset.sect_off as hash. */
fcd3b13d 493 htab_t die_hash = nullptr;
10b3939b
DJ
494
495 /* Full DIEs if read in. */
fcd3b13d 496 struct die_info *dies = nullptr;
10b3939b
DJ
497
498 /* A set of pointers to dwarf2_per_cu_data objects for compilation
499 units referenced by this one. Only set during full symbol processing;
500 partial symbol tables do not have dependencies. */
fcd3b13d 501 htab_t dependencies = nullptr;
10b3939b 502
cb1df416 503 /* Header data from the line table, during full symbol processing. */
fcd3b13d 504 struct line_header *line_header = nullptr;
4c8aa72d 505 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
5989a64e 506 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
4c8aa72d
PA
507 this is the DW_TAG_compile_unit die for this CU. We'll hold on
508 to the line header as long as this DIE is being processed. See
509 process_die_scope. */
fcd3b13d 510 die_info *line_header_die_owner = nullptr;
cb1df416 511
3da10d80
KS
512 /* A list of methods which need to have physnames computed
513 after all type information has been read. */
c89b44cd 514 std::vector<delayed_method_info> method_list;
3da10d80 515
96408a79 516 /* To be copied to symtab->call_site_htab. */
fcd3b13d 517 htab_t call_site_htab = nullptr;
96408a79 518
034e5797
DE
519 /* Non-NULL if this CU came from a DWO file.
520 There is an invariant here that is important to remember:
521 Except for attributes copied from the top level DIE in the "main"
522 (or "stub") file in preparation for reading the DWO file
18a8505e 523 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
524 Either there isn't a DWO file (in which case this is NULL and the point
525 is moot), or there is and either we're not going to read it (in which
526 case this is NULL) or there is and we are reading it (in which case this
527 is non-NULL). */
fcd3b13d 528 struct dwo_unit *dwo_unit = nullptr;
3019eac3 529
18a8505e 530 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 531 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 532 gdb::optional<ULONGEST> addr_base;
3019eac3 533
18a8505e 534 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 535 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 536 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
537 be used without needing to know whether DWO files are in use or not.
538 N.B. This does not apply to DW_AT_ranges appearing in
539 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
540 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 541 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 542 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 543 ULONGEST ranges_base = 0;
2e3cf129 544
41144253 545 /* The DW_AT_loclists_base attribute if present. */
546 ULONGEST loclist_base = 0;
547
c9317f21
TT
548 /* When reading debug info generated by older versions of rustc, we
549 have to rewrite some union types to be struct types with a
550 variant part. This rewriting must be done after the CU is fully
551 read in, because otherwise at the point of rewriting some struct
552 type might not have been fully processed. So, we keep a list of
553 all such types here and process them after expansion. */
554 std::vector<struct type *> rust_unions;
555
18a8505e
AT
556 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
557 files, the value is implicitly zero. For DWARF 5 version DWO files, the
558 value is often implicit and is the size of the header of
559 .debug_str_offsets section (8 or 4, depending on the address size). */
560 gdb::optional<ULONGEST> str_offsets_base;
561
ae038cb0 562 /* Mark used when releasing cached dies. */
9068261f 563 bool mark : 1;
ae038cb0 564
8be455d7
JK
565 /* This CU references .debug_loc. See the symtab->locations_valid field.
566 This test is imperfect as there may exist optimized debug code not using
567 any location list and still facing inlining issues if handled as
568 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 569 bool has_loclist : 1;
ba919b58 570
9068261f 571 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
572 if all the producer_is_* fields are valid. This information is cached
573 because profiling CU expansion showed excessive time spent in
574 producer_is_gxx_lt_4_6. */
9068261f
AB
575 bool checked_producer : 1;
576 bool producer_is_gxx_lt_4_6 : 1;
577 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 578 bool producer_is_icc : 1;
9068261f 579 bool producer_is_icc_lt_14 : 1;
c258c396 580 bool producer_is_codewarrior : 1;
4d4ec4e5 581
9068261f 582 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
583 debugging info for C++ namespaces. GCC 3.3.x did not produce
584 this information, but later versions do. */
585
9068261f 586 bool processing_has_namespace_info : 1;
d590ff25
YQ
587
588 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
589
590 /* If this CU was inherited by another CU (via specification,
591 abstract_origin, etc), this is the ancestor CU. */
592 dwarf2_cu *ancestor;
593
594 /* Get the buildsym_compunit for this CU. */
595 buildsym_compunit *get_builder ()
596 {
597 /* If this CU has a builder associated with it, use that. */
598 if (m_builder != nullptr)
599 return m_builder.get ();
600
601 /* Otherwise, search ancestors for a valid builder. */
602 if (ancestor != nullptr)
603 return ancestor->get_builder ();
604
605 return nullptr;
606 }
e7c27a73
DJ
607};
608
094b34ac
DE
609/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
610 This includes type_unit_group and quick_file_names. */
611
612struct stmt_list_hash
613{
614 /* The DWO unit this table is from or NULL if there is none. */
615 struct dwo_unit *dwo_unit;
616
617 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 618 sect_offset line_sect_off;
094b34ac
DE
619};
620
5989a64e 621/* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
8adb8487
TT
622 an object of this type. This contains elements of type unit groups
623 that can be shared across objfiles. The non-shareable parts are in
624 type_unit_group_unshareable. */
f4dc4d17
DE
625
626struct type_unit_group
627{
0186c6a7 628 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
629 To simplify things we create an artificial CU that "includes" all the
630 type units using this stmt_list so that the rest of the code still has
197400e8 631 a "per_cu" handle on the symtab. */
094b34ac
DE
632 struct dwarf2_per_cu_data per_cu;
633
0186c6a7
DE
634 /* The TUs that share this DW_AT_stmt_list entry.
635 This is added to while parsing type units to build partial symtabs,
636 and is deleted afterwards and not used again. */
a8b3b8e9 637 std::vector<signatured_type *> *tus;
f4dc4d17 638
094b34ac
DE
639 /* The data used to construct the hash key. */
640 struct stmt_list_hash hash;
f4dc4d17
DE
641};
642
73869dc2 643/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
644
645struct dwo_sections
646{
647 struct dwarf2_section_info abbrev;
3019eac3
DE
648 struct dwarf2_section_info line;
649 struct dwarf2_section_info loc;
43988095 650 struct dwarf2_section_info loclists;
09262596
DE
651 struct dwarf2_section_info macinfo;
652 struct dwarf2_section_info macro;
3019eac3
DE
653 struct dwarf2_section_info str;
654 struct dwarf2_section_info str_offsets;
80626a55
DE
655 /* In the case of a virtual DWO file, these two are unused. */
656 struct dwarf2_section_info info;
fd5866f6 657 std::vector<dwarf2_section_info> types;
3019eac3
DE
658};
659
c88ee1f0 660/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
661
662struct dwo_unit
663{
664 /* Backlink to the containing struct dwo_file. */
665 struct dwo_file *dwo_file;
666
667 /* The "id" that distinguishes this CU/TU.
668 .debug_info calls this "dwo_id", .debug_types calls this "signature".
669 Since signatures came first, we stick with it for consistency. */
670 ULONGEST signature;
671
672 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 673 struct dwarf2_section_info *section;
3019eac3 674
9c541725
PA
675 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
676 sect_offset sect_off;
3019eac3
DE
677 unsigned int length;
678
679 /* For types, offset in the type's DIE of the type defined by this TU. */
680 cu_offset type_offset_in_tu;
681};
682
73869dc2
DE
683/* include/dwarf2.h defines the DWP section codes.
684 It defines a max value but it doesn't define a min value, which we
685 use for error checking, so provide one. */
686
687enum dwp_v2_section_ids
688{
689 DW_SECT_MIN = 1
690};
691
80626a55 692/* Data for one DWO file.
57d63ce2
DE
693
694 This includes virtual DWO files (a virtual DWO file is a DWO file as it
695 appears in a DWP file). DWP files don't really have DWO files per se -
696 comdat folding of types "loses" the DWO file they came from, and from
697 a high level view DWP files appear to contain a mass of random types.
698 However, to maintain consistency with the non-DWP case we pretend DWP
699 files contain virtual DWO files, and we assign each TU with one virtual
700 DWO file (generally based on the line and abbrev section offsets -
701 a heuristic that seems to work in practice). */
3019eac3
DE
702
703struct dwo_file
704{
51ac9db5
SM
705 dwo_file () = default;
706 DISABLE_COPY_AND_ASSIGN (dwo_file);
707
18a8505e 708 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
709 For virtual DWO files the name is constructed from the section offsets
710 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
711 from related CU+TUs. */
51ac9db5 712 const char *dwo_name = nullptr;
0ac5b59e
DE
713
714 /* The DW_AT_comp_dir attribute. */
51ac9db5 715 const char *comp_dir = nullptr;
3019eac3 716
80626a55
DE
717 /* The bfd, when the file is open. Otherwise this is NULL.
718 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 719 gdb_bfd_ref_ptr dbfd;
3019eac3 720
73869dc2
DE
721 /* The sections that make up this DWO file.
722 Remember that for virtual DWO files in DWP V2, these are virtual
723 sections (for lack of a better name). */
51ac9db5 724 struct dwo_sections sections {};
3019eac3 725
33c5cd75
DB
726 /* The CUs in the file.
727 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
728 an extension to handle LLVM's Link Time Optimization output (where
729 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 730 htab_up cus;
3019eac3
DE
731
732 /* Table of TUs in the file.
733 Each element is a struct dwo_unit. */
b0b6a987 734 htab_up tus;
3019eac3
DE
735};
736
80626a55
DE
737/* These sections are what may appear in a DWP file. */
738
739struct dwp_sections
740{
73869dc2 741 /* These are used by both DWP version 1 and 2. */
80626a55
DE
742 struct dwarf2_section_info str;
743 struct dwarf2_section_info cu_index;
744 struct dwarf2_section_info tu_index;
73869dc2
DE
745
746 /* These are only used by DWP version 2 files.
747 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
748 sections are referenced by section number, and are not recorded here.
749 In DWP version 2 there is at most one copy of all these sections, each
750 section being (effectively) comprised of the concatenation of all of the
751 individual sections that exist in the version 1 format.
752 To keep the code simple we treat each of these concatenated pieces as a
753 section itself (a virtual section?). */
754 struct dwarf2_section_info abbrev;
755 struct dwarf2_section_info info;
756 struct dwarf2_section_info line;
757 struct dwarf2_section_info loc;
758 struct dwarf2_section_info macinfo;
759 struct dwarf2_section_info macro;
760 struct dwarf2_section_info str_offsets;
761 struct dwarf2_section_info types;
80626a55
DE
762};
763
73869dc2
DE
764/* These sections are what may appear in a virtual DWO file in DWP version 1.
765 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 766
73869dc2 767struct virtual_v1_dwo_sections
80626a55
DE
768{
769 struct dwarf2_section_info abbrev;
770 struct dwarf2_section_info line;
771 struct dwarf2_section_info loc;
772 struct dwarf2_section_info macinfo;
773 struct dwarf2_section_info macro;
774 struct dwarf2_section_info str_offsets;
775 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 776 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
777 struct dwarf2_section_info info_or_types;
778};
779
73869dc2
DE
780/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
781 In version 2, the sections of the DWO files are concatenated together
782 and stored in one section of that name. Thus each ELF section contains
783 several "virtual" sections. */
784
785struct virtual_v2_dwo_sections
786{
787 bfd_size_type abbrev_offset;
788 bfd_size_type abbrev_size;
789
790 bfd_size_type line_offset;
791 bfd_size_type line_size;
792
793 bfd_size_type loc_offset;
794 bfd_size_type loc_size;
795
796 bfd_size_type macinfo_offset;
797 bfd_size_type macinfo_size;
798
799 bfd_size_type macro_offset;
800 bfd_size_type macro_size;
801
802 bfd_size_type str_offsets_offset;
803 bfd_size_type str_offsets_size;
804
805 /* Each DWP hash table entry records one CU or one TU.
806 That is recorded here, and copied to dwo_unit.section. */
807 bfd_size_type info_or_types_offset;
808 bfd_size_type info_or_types_size;
809};
810
80626a55
DE
811/* Contents of DWP hash tables. */
812
813struct dwp_hash_table
814{
73869dc2 815 uint32_t version, nr_columns;
80626a55 816 uint32_t nr_units, nr_slots;
73869dc2
DE
817 const gdb_byte *hash_table, *unit_table;
818 union
819 {
820 struct
821 {
822 const gdb_byte *indices;
823 } v1;
824 struct
825 {
826 /* This is indexed by column number and gives the id of the section
827 in that column. */
828#define MAX_NR_V2_DWO_SECTIONS \
829 (1 /* .debug_info or .debug_types */ \
830 + 1 /* .debug_abbrev */ \
831 + 1 /* .debug_line */ \
832 + 1 /* .debug_loc */ \
833 + 1 /* .debug_str_offsets */ \
834 + 1 /* .debug_macro or .debug_macinfo */)
835 int section_ids[MAX_NR_V2_DWO_SECTIONS];
836 const gdb_byte *offsets;
837 const gdb_byte *sizes;
838 } v2;
839 } section_pool;
80626a55
DE
840};
841
842/* Data for one DWP file. */
843
844struct dwp_file
845{
400174b1
TT
846 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
847 : name (name_),
848 dbfd (std::move (abfd))
849 {
850 }
851
80626a55
DE
852 /* Name of the file. */
853 const char *name;
854
73869dc2 855 /* File format version. */
400174b1 856 int version = 0;
73869dc2 857
93417882 858 /* The bfd. */
400174b1 859 gdb_bfd_ref_ptr dbfd;
80626a55
DE
860
861 /* Section info for this file. */
400174b1 862 struct dwp_sections sections {};
80626a55 863
57d63ce2 864 /* Table of CUs in the file. */
400174b1 865 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
866
867 /* Table of TUs in the file. */
400174b1 868 const struct dwp_hash_table *tus = nullptr;
80626a55 869
19ac8c2e 870 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
871 htab_up loaded_cus;
872 htab_up loaded_tus;
80626a55 873
73869dc2
DE
874 /* Table to map ELF section numbers to their sections.
875 This is only needed for the DWP V1 file format. */
400174b1
TT
876 unsigned int num_sections = 0;
877 asection **elf_sections = nullptr;
80626a55
DE
878};
879
0963b4bd
MS
880/* Struct used to pass misc. parameters to read_die_and_children, et
881 al. which are used for both .debug_info and .debug_types dies.
882 All parameters here are unchanging for the life of the call. This
dee91e82 883 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
884
885struct die_reader_specs
886{
a32a8923 887 /* The bfd of die_section. */
93311388
DE
888 bfd* abfd;
889
890 /* The CU of the DIE we are parsing. */
891 struct dwarf2_cu *cu;
892
80626a55 893 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
894 struct dwo_file *dwo_file;
895
dee91e82 896 /* The section the die comes from.
3019eac3 897 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
898 struct dwarf2_section_info *die_section;
899
900 /* die_section->buffer. */
d521ce57 901 const gdb_byte *buffer;
f664829e
DE
902
903 /* The end of the buffer. */
904 const gdb_byte *buffer_end;
a2ce51a0 905
685af9cd
TT
906 /* The abbreviation table to use when reading the DIEs. */
907 struct abbrev_table *abbrev_table;
93311388
DE
908};
909
c0ab21c2
TT
910/* A subclass of die_reader_specs that holds storage and has complex
911 constructor and destructor behavior. */
912
913class cutu_reader : public die_reader_specs
914{
915public:
916
ab432490
SM
917 cutu_reader (dwarf2_per_cu_data *this_cu,
918 dwarf2_per_objfile *per_objfile,
c0ab21c2 919 struct abbrev_table *abbrev_table,
2e671100 920 dwarf2_cu *existing_cu,
c0ab21c2
TT
921 bool skip_partial);
922
923 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
ab432490 924 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
925 struct dwarf2_cu *parent_cu = nullptr,
926 struct dwo_file *dwo_file = nullptr);
927
c0ab21c2
TT
928 DISABLE_COPY_AND_ASSIGN (cutu_reader);
929
930 const gdb_byte *info_ptr = nullptr;
931 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
932 bool dummy_p = false;
933
6751ebae
TT
934 /* Release the new CU, putting it on the chain. This cannot be done
935 for dummy CUs. */
936 void keep ();
937
c0ab21c2 938private:
9e021579
SM
939 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
940 dwarf2_per_objfile *per_objfile,
2e671100 941 dwarf2_cu *existing_cu);
c0ab21c2
TT
942
943 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
944 std::unique_ptr<dwarf2_cu> m_new_cu;
945
946 /* The ordinary abbreviation table. */
947 abbrev_table_up m_abbrev_table_holder;
948
949 /* The DWO abbreviation table. */
950 abbrev_table_up m_dwo_abbrev_table;
951};
dee91e82 952
c906108c 953/* When we construct a partial symbol table entry we only
0963b4bd 954 need this much information. */
6f06d47b 955struct partial_die_info : public allocate_on_obstack
c906108c 956 {
6f06d47b
YQ
957 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
958
959 /* Disable assign but still keep copy ctor, which is needed
960 load_partial_dies. */
961 partial_die_info& operator=(const partial_die_info& rhs) = delete;
962
52356b79
YQ
963 /* Adjust the partial die before generating a symbol for it. This
964 function may set the is_external flag or change the DIE's
965 name. */
966 void fixup (struct dwarf2_cu *cu);
967
48fbe735
YQ
968 /* Read a minimal amount of information into the minimal die
969 structure. */
970 const gdb_byte *read (const struct die_reader_specs *reader,
971 const struct abbrev_info &abbrev,
972 const gdb_byte *info_ptr);
973
72bf9492 974 /* Offset of this DIE. */
6f06d47b 975 const sect_offset sect_off;
72bf9492
DJ
976
977 /* DWARF-2 tag for this DIE. */
6f06d47b 978 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 979
72bf9492 980 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
981 const unsigned int has_children : 1;
982
72bf9492
DJ
983 unsigned int is_external : 1;
984 unsigned int is_declaration : 1;
985 unsigned int has_type : 1;
986 unsigned int has_specification : 1;
987 unsigned int has_pc_info : 1;
481860b3 988 unsigned int may_be_inlined : 1;
72bf9492 989
0c1b455e
TT
990 /* This DIE has been marked DW_AT_main_subprogram. */
991 unsigned int main_subprogram : 1;
992
72bf9492
DJ
993 /* Flag set if the SCOPE field of this structure has been
994 computed. */
995 unsigned int scope_set : 1;
996
fa4028e9
JB
997 /* Flag set if the DIE has a byte_size attribute. */
998 unsigned int has_byte_size : 1;
999
ff908ebf
AW
1000 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1001 unsigned int has_const_value : 1;
1002
98bfdba5
PA
1003 /* Flag set if any of the DIE's children are template arguments. */
1004 unsigned int has_template_arguments : 1;
1005
52356b79 1006 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1007 unsigned int fixup_called : 1;
1008
36586728
TT
1009 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1010 unsigned int is_dwz : 1;
1011
1012 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1013 unsigned int spec_is_dwz : 1;
1014
72bf9492 1015 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1016 sometimes a default name for unnamed DIEs. */
6f06d47b 1017 const char *name = nullptr;
72bf9492 1018
abc72ce4 1019 /* The linkage name, if present. */
6f06d47b 1020 const char *linkage_name = nullptr;
abc72ce4 1021
72bf9492
DJ
1022 /* The scope to prepend to our children. This is generally
1023 allocated on the comp_unit_obstack, so will disappear
1024 when this compilation unit leaves the cache. */
6f06d47b 1025 const char *scope = nullptr;
72bf9492 1026
95554aad
TT
1027 /* Some data associated with the partial DIE. The tag determines
1028 which field is live. */
1029 union
1030 {
1031 /* The location description associated with this DIE, if any. */
1032 struct dwarf_block *locdesc;
1033 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1034 sect_offset sect_off;
6f06d47b 1035 } d {};
72bf9492
DJ
1036
1037 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1038 CORE_ADDR lowpc = 0;
1039 CORE_ADDR highpc = 0;
72bf9492 1040
93311388 1041 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1042 DW_AT_sibling, if any. */
48fbe735
YQ
1043 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1044 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1045 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1046
1047 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1048 DW_AT_specification (or DW_AT_abstract_origin or
1049 DW_AT_extension). */
6f06d47b 1050 sect_offset spec_offset {};
72bf9492
DJ
1051
1052 /* Pointers to this DIE's parent, first child, and next sibling,
1053 if any. */
6f06d47b
YQ
1054 struct partial_die_info *die_parent = nullptr;
1055 struct partial_die_info *die_child = nullptr;
1056 struct partial_die_info *die_sibling = nullptr;
1057
1058 friend struct partial_die_info *
1059 dwarf2_cu::find_partial_die (sect_offset sect_off);
1060
1061 private:
1062 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1063 partial_die_info (sect_offset sect_off)
1064 : partial_die_info (sect_off, DW_TAG_padding, 0)
1065 {
1066 }
1067
1068 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1069 int has_children_)
1070 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1071 {
1072 is_external = 0;
1073 is_declaration = 0;
1074 has_type = 0;
1075 has_specification = 0;
1076 has_pc_info = 0;
1077 may_be_inlined = 0;
1078 main_subprogram = 0;
1079 scope_set = 0;
1080 has_byte_size = 0;
1081 has_const_value = 0;
1082 has_template_arguments = 0;
1083 fixup_called = 0;
1084 is_dwz = 0;
1085 spec_is_dwz = 0;
1086 }
c906108c
SS
1087 };
1088
c906108c
SS
1089/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1090 but this would require a corresponding change in unpack_field_as_long
1091 and friends. */
1092static int bits_per_byte = 8;
1093
9c6a1327
TT
1094struct variant_part_builder;
1095
1096/* When reading a variant, we track a bit more information about the
1097 field, and store it in an object of this type. */
2ddeaf8a
TT
1098
1099struct variant_field
1100{
9c6a1327
TT
1101 int first_field = -1;
1102 int last_field = -1;
1103
1104 /* A variant can contain other variant parts. */
1105 std::vector<variant_part_builder> variant_parts;
1106
2ddeaf8a
TT
1107 /* If we see a DW_TAG_variant, then this will be set if this is the
1108 default branch. */
9c6a1327
TT
1109 bool default_branch = false;
1110 /* If we see a DW_AT_discr_value, then this will be the discriminant
1111 value. */
1112 ULONGEST discriminant_value = 0;
1113 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1114 data. */
1115 struct dwarf_block *discr_list_data = nullptr;
1116};
1117
1118/* This represents a DW_TAG_variant_part. */
1119
1120struct variant_part_builder
1121{
1122 /* The offset of the discriminant field. */
1123 sect_offset discriminant_offset {};
1124
1125 /* Variants that are direct children of this variant part. */
1126 std::vector<variant_field> variants;
1127
1128 /* True if we're currently reading a variant. */
1129 bool processing_variant = false;
2ddeaf8a
TT
1130};
1131
52059ffd
TT
1132struct nextfield
1133{
be2daae6
TT
1134 int accessibility = 0;
1135 int virtuality = 0;
9c6a1327
TT
1136 /* Variant parts need to find the discriminant, which is a DIE
1137 reference. We track the section offset of each field to make
1138 this link. */
1139 sect_offset offset;
be2daae6 1140 struct field field {};
52059ffd
TT
1141};
1142
1143struct fnfieldlist
1144{
be2daae6
TT
1145 const char *name = nullptr;
1146 std::vector<struct fn_field> fnfields;
52059ffd
TT
1147};
1148
c906108c
SS
1149/* The routines that read and process dies for a C struct or C++ class
1150 pass lists of data member fields and lists of member function fields
1151 in an instance of a field_info structure, as defined below. */
1152struct field_info
c5aa993b 1153 {
0963b4bd 1154 /* List of data member and baseclasses fields. */
be2daae6
TT
1155 std::vector<struct nextfield> fields;
1156 std::vector<struct nextfield> baseclasses;
c906108c 1157
85102364 1158 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1159 int non_public_fields = 0;
c906108c 1160
c5aa993b
JM
1161 /* Member function fieldlist array, contains name of possibly overloaded
1162 member function, number of overloaded member functions and a pointer
1163 to the head of the member function field chain. */
be2daae6 1164 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1165
1166 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1167 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1168 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1169
1170 /* Nested types defined by this class and the number of elements in this
1171 list. */
be2daae6 1172 std::vector<struct decl_field> nested_types_list;
317f7127 1173
9c6a1327
TT
1174 /* If non-null, this is the variant part we are currently
1175 reading. */
1176 variant_part_builder *current_variant_part = nullptr;
1177 /* This holds all the top-level variant parts attached to the type
1178 we're reading. */
1179 std::vector<variant_part_builder> variant_parts;
1180
317f7127
TT
1181 /* Return the total number of fields (including baseclasses). */
1182 int nfields () const
1183 {
1184 return fields.size () + baseclasses.size ();
1185 }
c5aa993b 1186 };
c906108c 1187
ae038cb0
DJ
1188/* Loaded secondary compilation units are kept in memory until they
1189 have not been referenced for the processing of this many
1190 compilation units. Set this to zero to disable caching. Cache
1191 sizes of up to at least twenty will improve startup time for
1192 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1193static int dwarf_max_cache_age = 5;
920d2a44 1194static void
b4f54984
DE
1195show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1196 struct cmd_list_element *c, const char *value)
920d2a44 1197{
3e43a32a 1198 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1199 "DWARF compilation units is %s.\n"),
920d2a44
AC
1200 value);
1201}
4390d890 1202\f
c906108c
SS
1203/* local function prototypes */
1204
918dd910
JK
1205static void dwarf2_find_base_address (struct die_info *die,
1206 struct dwarf2_cu *cu);
1207
891813be 1208static dwarf2_psymtab *create_partial_symtab
7aa104c4
SM
1209 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1210 const char *name);
0018ea6f 1211
f1902523
JK
1212static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1213 const gdb_byte *info_ptr,
3e225074 1214 struct die_info *type_unit_die);
f1902523 1215
ed2dc618
SM
1216static void dwarf2_build_psymtabs_hard
1217 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1218
72bf9492
DJ
1219static void scan_partial_symbols (struct partial_die_info *,
1220 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1221 int, struct dwarf2_cu *);
c906108c 1222
72bf9492
DJ
1223static void add_partial_symbol (struct partial_die_info *,
1224 struct dwarf2_cu *);
63d06c5c 1225
72bf9492
DJ
1226static void add_partial_namespace (struct partial_die_info *pdi,
1227 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1228 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1229
5d7cb8df 1230static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1231 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1232 struct dwarf2_cu *cu);
1233
72bf9492
DJ
1234static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1235 struct dwarf2_cu *cu);
91c24f0a 1236
bc30ff58
JB
1237static void add_partial_subprogram (struct partial_die_info *pdi,
1238 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1239 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1240
d521ce57 1241static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1242
dee91e82 1243static struct partial_die_info *load_partial_dies
d521ce57 1244 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1245
fb816e8b
TV
1246/* A pair of partial_die_info and compilation unit. */
1247struct cu_partial_die_info
1248{
1249 /* The compilation unit of the partial_die_info. */
1250 struct dwarf2_cu *cu;
1251 /* A partial_die_info. */
1252 struct partial_die_info *pdi;
122cf0f2
AB
1253
1254 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1255 : cu (cu),
1256 pdi (pdi)
405feb71 1257 { /* Nothing. */ }
122cf0f2
AB
1258
1259private:
1260 cu_partial_die_info () = delete;
fb816e8b
TV
1261};
1262
122cf0f2
AB
1263static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1264 struct dwarf2_cu *);
72bf9492 1265
d521ce57
TT
1266static const gdb_byte *read_attribute (const struct die_reader_specs *,
1267 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1268 const gdb_byte *, bool *need_reprocess);
1269
1270static void read_attribute_reprocess (const struct die_reader_specs *reader,
1271 struct attribute *attr);
1272
1273static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1274
ed2dc618
SM
1275static sect_offset read_abbrev_offset
1276 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1277 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1278
ed2dc618
SM
1279static const char *read_indirect_string
1280 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1281 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1282
ed2dc618 1283static const char *read_indirect_string_at_offset
4f44ae6c 1284 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
927aa2e7 1285
d521ce57
TT
1286static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1287 const gdb_byte *,
3019eac3
DE
1288 unsigned int *);
1289
18a8505e
AT
1290static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1291 ULONGEST str_index);
1292
1293static const char *read_stub_str_index (struct dwarf2_cu *cu,
1294 ULONGEST str_index);
3019eac3 1295
e142c38c 1296static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1297
e142c38c
DJ
1298static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1299 struct dwarf2_cu *);
c906108c 1300
7d45c7c3
KB
1301static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1302 struct dwarf2_cu *cu);
1303
a084a2a6
AT
1304static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1305
05cf31d1
JB
1306static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1307 struct dwarf2_cu *cu);
1308
e142c38c 1309static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1310
e142c38c 1311static struct die_info *die_specification (struct die_info *die,
f2f0e013 1312 struct dwarf2_cu **);
63d06c5c 1313
9c541725 1314static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1315 struct dwarf2_cu *cu);
debd256d 1316
f3f5162e 1317static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1318 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1319 CORE_ADDR, int decode_mapping);
c906108c 1320
804d2729
TT
1321static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1322 const char *);
c906108c 1323
a14ed312 1324static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1325 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1326
ff39bb5e 1327static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1328 struct dwarf2_cu *);
c906108c 1329
ff39bb5e 1330static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1331 struct type *type,
1332 const char *name,
1333 struct obstack *obstack,
12df843f 1334 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1335 const gdb_byte **bytes,
98bfdba5 1336 struct dwarf2_locexpr_baton **baton);
2df3850c 1337
e7c27a73 1338static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1339
b4ba55a1
JB
1340static int need_gnat_info (struct dwarf2_cu *);
1341
3e43a32a
MS
1342static struct type *die_descriptive_type (struct die_info *,
1343 struct dwarf2_cu *);
b4ba55a1
JB
1344
1345static void set_descriptive_type (struct type *, struct die_info *,
1346 struct dwarf2_cu *);
1347
e7c27a73
DJ
1348static struct type *die_containing_type (struct die_info *,
1349 struct dwarf2_cu *);
c906108c 1350
ff39bb5e 1351static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1352 struct dwarf2_cu *);
c906108c 1353
f792889a 1354static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1355
673bfd45
DE
1356static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1357
0d5cff50 1358static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1359
6e70227d 1360static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1361 const char *suffix, int physname,
1362 struct dwarf2_cu *cu);
63d06c5c 1363
e7c27a73 1364static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1365
348e048f
DE
1366static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1367
e7c27a73 1368static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1369
e7c27a73 1370static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1371
96408a79
SA
1372static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1373
71a3c369
TT
1374static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1375
ff013f42 1376static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1377 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1378
41144253 1379/* Return the .debug_loclists section to use for cu. */
1380static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1381
3a2b436a 1382/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1383 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1384enum pc_bounds_kind
1385{
e385593e 1386 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1387 PC_BOUNDS_NOT_PRESENT,
1388
e385593e
JK
1389 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1390 were present but they do not form a valid range of PC addresses. */
1391 PC_BOUNDS_INVALID,
1392
3a2b436a
JK
1393 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1394 PC_BOUNDS_RANGES,
1395
1396 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1397 PC_BOUNDS_HIGH_LOW,
1398};
1399
1400static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1401 CORE_ADDR *, CORE_ADDR *,
1402 struct dwarf2_cu *,
891813be 1403 dwarf2_psymtab *);
c906108c 1404
fae299cd
DC
1405static void get_scope_pc_bounds (struct die_info *,
1406 CORE_ADDR *, CORE_ADDR *,
1407 struct dwarf2_cu *);
1408
801e3a5b
JB
1409static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1410 CORE_ADDR, struct dwarf2_cu *);
1411
a14ed312 1412static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1413 struct dwarf2_cu *);
c906108c 1414
a14ed312 1415static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1416 struct type *, struct dwarf2_cu *);
c906108c 1417
a14ed312 1418static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1419 struct die_info *, struct type *,
e7c27a73 1420 struct dwarf2_cu *);
c906108c 1421
a14ed312 1422static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1423 struct type *,
1424 struct dwarf2_cu *);
c906108c 1425
134d01f1 1426static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1427
e7c27a73 1428static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1429
e7c27a73 1430static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1431
5d7cb8df
JK
1432static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1433
804d2729 1434static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1435
27aa8d6a
SW
1436static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1437
74921315
KS
1438static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1439
f55ee35c
JK
1440static struct type *read_module_type (struct die_info *die,
1441 struct dwarf2_cu *cu);
1442
38d518c9 1443static const char *namespace_name (struct die_info *die,
e142c38c 1444 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1445
134d01f1 1446static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1447
7d79de9a
TT
1448static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1449 bool * = nullptr);
c906108c 1450
6e70227d 1451static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1452 struct dwarf2_cu *);
1453
bf6af496 1454static struct die_info *read_die_and_siblings_1
d521ce57 1455 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1456 struct die_info *);
639d11d3 1457
dee91e82 1458static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1459 const gdb_byte *info_ptr,
1460 const gdb_byte **new_info_ptr,
639d11d3
DC
1461 struct die_info *parent);
1462
d521ce57
TT
1463static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1464 struct die_info **, const gdb_byte *,
3e225074 1465 int);
3019eac3 1466
d521ce57 1467static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1468 struct die_info **, const gdb_byte *);
93311388 1469
e7c27a73 1470static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1471
15d034d0 1472static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1473 struct objfile *);
71c25dea 1474
15d034d0 1475static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1476
15d034d0 1477static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1478 struct die_info *die,
1479 struct dwarf2_cu *cu);
1480
ca69b9e6
DE
1481static const char *dwarf2_physname (const char *name, struct die_info *die,
1482 struct dwarf2_cu *cu);
1483
e142c38c 1484static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1485 struct dwarf2_cu **);
9219021c 1486
d97bc12b
DE
1487static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1488
1489static void dump_die_for_error (struct die_info *);
1490
1491static void dump_die_1 (struct ui_file *, int level, int max_level,
1492 struct die_info *);
c906108c 1493
d97bc12b 1494/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1495
51545339 1496static void store_in_ref_table (struct die_info *,
10b3939b 1497 struct dwarf2_cu *);
c906108c 1498
348e048f 1499static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1500 const struct attribute *,
348e048f
DE
1501 struct dwarf2_cu **);
1502
10b3939b 1503static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1504 const struct attribute *,
f2f0e013 1505 struct dwarf2_cu **);
c906108c 1506
348e048f 1507static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1508 const struct attribute *,
348e048f
DE
1509 struct dwarf2_cu **);
1510
ac9ec31b
DE
1511static struct type *get_signatured_type (struct die_info *, ULONGEST,
1512 struct dwarf2_cu *);
1513
1514static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1515 const struct attribute *,
ac9ec31b
DE
1516 struct dwarf2_cu *);
1517
ab432490
SM
1518static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1519 dwarf2_per_objfile *per_objfile);
348e048f 1520
ab432490
SM
1521static void read_signatured_type (signatured_type *sig_type,
1522 dwarf2_per_objfile *per_objfile);
348e048f 1523
63e43d3a
PMR
1524static int attr_to_dynamic_prop (const struct attribute *attr,
1525 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1526 struct dynamic_prop *prop, struct type *type);
63e43d3a 1527
c906108c
SS
1528/* memory allocation interface */
1529
7b5a2f43 1530static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1531
b60c80d6 1532static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1533
43f3e411 1534static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1535
8cf6f0b1
TT
1536static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1537 struct dwarf2_loclist_baton *baton,
ff39bb5e 1538 const struct attribute *attr);
8cf6f0b1 1539
ff39bb5e 1540static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1541 struct symbol *sym,
f1e6e072
TT
1542 struct dwarf2_cu *cu,
1543 int is_block);
4c2df51b 1544
d521ce57
TT
1545static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1546 const gdb_byte *info_ptr,
1547 struct abbrev_info *abbrev);
4bb7a0a7 1548
72bf9492
DJ
1549static hashval_t partial_die_hash (const void *item);
1550
1551static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1552
ae038cb0 1553static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1554 (sect_offset sect_off, unsigned int offset_in_dwz,
1555 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1556
9816fde3 1557static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1558 struct die_info *comp_unit_die,
1559 enum language pretend_language);
93311388 1560
f792889a
DJ
1561static struct type *set_die_type (struct die_info *, struct type *,
1562 struct dwarf2_cu *);
1c379e20 1563
ed2dc618 1564static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1565
ed2dc618 1566static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1567
ab432490
SM
1568static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1569 dwarf2_per_objfile *per_objfile,
1570 bool skip_partial,
1571 enum language pretend_language);
10b3939b 1572
8fc0b21d 1573static void process_full_comp_unit (dwarf2_cu *cu,
47b14e86 1574 enum language pretend_language);
10b3939b 1575
8fc0b21d 1576static void process_full_type_unit (dwarf2_cu *cu,
47b14e86 1577 enum language pretend_language);
f4dc4d17 1578
10b3939b
DJ
1579static void dwarf2_add_dependence (struct dwarf2_cu *,
1580 struct dwarf2_per_cu_data *);
1581
ae038cb0
DJ
1582static void dwarf2_mark (struct dwarf2_cu *);
1583
b64f50a1 1584static struct type *get_die_type_at_offset (sect_offset,
aa66c379
SM
1585 dwarf2_per_cu_data *per_cu,
1586 dwarf2_per_objfile *per_objfile);
673bfd45 1587
f792889a 1588static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1589
120ce1b5
SM
1590static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1591 dwarf2_per_objfile *per_objfile,
95554aad
TT
1592 enum language pretend_language);
1593
ed2dc618 1594static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1595
b303c6f6
AB
1596/* Class, the destructor of which frees all allocated queue entries. This
1597 will only have work to do if an error was thrown while processing the
1598 dwarf. If no error was thrown then the queue entries should have all
1599 been processed, and freed, as we went along. */
1600
1601class dwarf2_queue_guard
1602{
1603public:
39856def
TT
1604 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1605 : m_per_objfile (per_objfile)
1606 {
1607 }
b303c6f6
AB
1608
1609 /* Free any entries remaining on the queue. There should only be
1610 entries left if we hit an error while processing the dwarf. */
1611 ~dwarf2_queue_guard ()
1612 {
39856def
TT
1613 /* Ensure that no memory is allocated by the queue. */
1614 std::queue<dwarf2_queue_item> empty;
5989a64e 1615 std::swap (m_per_objfile->per_bfd->queue, empty);
39856def 1616 }
b303c6f6 1617
39856def 1618 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1619
39856def
TT
1620private:
1621 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1622};
1623
39856def
TT
1624dwarf2_queue_item::~dwarf2_queue_item ()
1625{
1626 /* Anything still marked queued is likely to be in an
1627 inconsistent state, so discard it. */
1628 if (per_cu->queued)
1629 {
7188ed02 1630 per_objfile->remove_cu (per_cu);
39856def
TT
1631 per_cu->queued = 0;
1632 }
1633}
1634
d721ba37
PA
1635/* The return type of find_file_and_directory. Note, the enclosed
1636 string pointers are only valid while this object is valid. */
1637
1638struct file_and_directory
1639{
1640 /* The filename. This is never NULL. */
1641 const char *name;
1642
1643 /* The compilation directory. NULL if not known. If we needed to
1644 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1645 points directly to the DW_AT_comp_dir string attribute owned by
1646 the obstack that owns the DIE. */
1647 const char *comp_dir;
1648
1649 /* If we needed to build a new string for comp_dir, this is what
1650 owns the storage. */
1651 std::string comp_dir_storage;
1652};
1653
1654static file_and_directory find_file_and_directory (struct die_info *die,
1655 struct dwarf2_cu *cu);
9291a0cd 1656
298e9637 1657static htab_up allocate_signatured_type_table ();
1fd400ff 1658
298e9637 1659static htab_up allocate_dwo_unit_table ();
3019eac3 1660
57d63ce2 1661static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1662 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1663 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1664 ULONGEST signature, int is_debug_types);
a2ce51a0 1665
ed2dc618
SM
1666static struct dwp_file *get_dwp_file
1667 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1668
3019eac3 1669static struct dwo_unit *lookup_dwo_comp_unit
4ab09049
SM
1670 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1671 ULONGEST signature);
3019eac3
DE
1672
1673static struct dwo_unit *lookup_dwo_type_unit
4ab09049 1674 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
3019eac3 1675
1b555f17 1676static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
89e63ee4 1677
263db9a1
TT
1678/* A unique pointer to a dwo_file. */
1679
51ac9db5 1680typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1681
ed2dc618 1682static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1683
1b80a9fa 1684static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1685
1686static void free_line_header_voidp (void *arg);
4390d890
DE
1687\f
1688/* Various complaints about symbol reading that don't abort the process. */
1689
4390d890
DE
1690static void
1691dwarf2_debug_line_missing_file_complaint (void)
1692{
b98664d3 1693 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1694}
1695
1696static void
1697dwarf2_debug_line_missing_end_sequence_complaint (void)
1698{
b98664d3 1699 complaint (_(".debug_line section has line "
4390d890
DE
1700 "program sequence without an end"));
1701}
1702
1703static void
1704dwarf2_complex_location_expr_complaint (void)
1705{
b98664d3 1706 complaint (_("location expression too complex"));
4390d890
DE
1707}
1708
1709static void
1710dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1711 int arg3)
1712{
b98664d3 1713 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1714 arg1, arg2, arg3);
1715}
1716
4390d890
DE
1717static void
1718dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1719{
b98664d3 1720 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1721 arg1, arg2);
1722}
527f3840
JK
1723
1724/* Hash function for line_header_hash. */
1725
1726static hashval_t
1727line_header_hash (const struct line_header *ofs)
1728{
9c541725 1729 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1730}
1731
1732/* Hash function for htab_create_alloc_ex for line_header_hash. */
1733
1734static hashval_t
1735line_header_hash_voidp (const void *item)
1736{
9a3c8263 1737 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1738
1739 return line_header_hash (ofs);
1740}
1741
1742/* Equality function for line_header_hash. */
1743
1744static int
1745line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1746{
9a3c8263
SM
1747 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1748 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1749
9c541725 1750 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1751 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1752}
1753
4390d890 1754\f
9291a0cd 1755
330cdd98
PA
1756/* See declaration. */
1757
5989a64e
SM
1758dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1759 bool can_copy_)
c3699833
SM
1760 : obfd (obfd),
1761 can_copy (can_copy_)
330cdd98
PA
1762{
1763 if (names == NULL)
1764 names = &dwarf2_elf_names;
1765
330cdd98
PA
1766 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1767 locate_sections (obfd, sec, *names);
1768}
1769
5989a64e 1770dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98 1771{
b76e467d 1772 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1773 per_cu->imported_symtabs_free ();
fc8e7e75 1774
b2bdb8cf 1775 for (signatured_type *sig_type : all_type_units)
ae640021 1776 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1777
5989a64e 1778 /* Everything else should be on this->obstack. */
330cdd98
PA
1779}
1780
7188ed02 1781/* See read.h. */
330cdd98
PA
1782
1783void
7188ed02 1784dwarf2_per_objfile::remove_all_cus ()
330cdd98 1785{
7188ed02
SM
1786 for (auto pair : m_dwarf2_cus)
1787 delete pair.second;
330cdd98 1788
7188ed02 1789 m_dwarf2_cus.clear ();
330cdd98
PA
1790}
1791
11ed8cad
TT
1792/* A helper class that calls free_cached_comp_units on
1793 destruction. */
1794
1795class free_cached_comp_units
1796{
1797public:
1798
1799 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1800 : m_per_objfile (per_objfile)
1801 {
1802 }
1803
1804 ~free_cached_comp_units ()
1805 {
7188ed02 1806 m_per_objfile->remove_all_cus ();
11ed8cad
TT
1807 }
1808
1809 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1810
1811private:
1812
1813 dwarf2_per_objfile *m_per_objfile;
1814};
1815
af758d11
SM
1816/* See read.h. */
1817
1818bool
1819dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1820{
1821 gdb_assert (per_cu->index < this->m_symtabs.size ());
1822
1823 return this->m_symtabs[per_cu->index] != nullptr;
1824}
1825
1826/* See read.h. */
1827
1828compunit_symtab *
1829dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1830{
1831 gdb_assert (per_cu->index < this->m_symtabs.size ());
1832
1833 return this->m_symtabs[per_cu->index];
1834}
1835
1836/* See read.h. */
1837
1838void
1839dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1840 compunit_symtab *symtab)
1841{
1842 gdb_assert (per_cu->index < this->m_symtabs.size ());
1843 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1844
1845 this->m_symtabs[per_cu->index] = symtab;
1846}
1847
c906108c 1848/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1849 information and return true if we have enough to do something.
1850 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1851 ELF names are used. CAN_COPY is true for formats where symbol
1852 interposition is possible and so symbol values must follow copy
1853 relocation rules. */
c906108c
SS
1854
1855int
251d32d9 1856dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1857 const struct dwarf2_debug_sections *names,
1858 bool can_copy)
c906108c 1859{
97cbe998
SDJ
1860 if (objfile->flags & OBJF_READNEVER)
1861 return 0;
1862
ed2dc618
SM
1863 struct dwarf2_per_objfile *dwarf2_per_objfile
1864 = get_dwarf2_per_objfile (objfile);
1865
1866 if (dwarf2_per_objfile == NULL)
5989a64e 1867 {
17ee85fc
TT
1868 dwarf2_per_bfd *per_bfd;
1869
1870 /* We can share a "dwarf2_per_bfd" with other objfiles if the BFD
1871 doesn't require relocations and if there aren't partial symbols
1872 from some other reader. */
1873 if (!objfile_has_partial_symbols (objfile)
1874 && !gdb_bfd_requires_relocations (objfile->obfd))
1875 {
1876 /* See if one has been created for this BFD yet. */
1877 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1878
1879 if (per_bfd == nullptr)
1880 {
1881 /* No, create it now. */
1882 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1883 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1884 }
1885 }
1886 else
1887 {
1888 /* No sharing possible, create one specifically for this objfile. */
1889 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1890 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1891 }
5989a64e
SM
1892
1893 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1894 }
5bfd760d 1895
5989a64e
SM
1896 return (!dwarf2_per_objfile->per_bfd->info.is_virtual
1897 && dwarf2_per_objfile->per_bfd->info.s.section != NULL
1898 && !dwarf2_per_objfile->per_bfd->abbrev.is_virtual
1899 && dwarf2_per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1900}
1901
251d32d9
TG
1902/* When loading sections, we look either for uncompressed section or for
1903 compressed section names. */
233a11ab
CS
1904
1905static int
251d32d9
TG
1906section_is_p (const char *section_name,
1907 const struct dwarf2_section_names *names)
233a11ab 1908{
251d32d9
TG
1909 if (names->normal != NULL
1910 && strcmp (section_name, names->normal) == 0)
1911 return 1;
1912 if (names->compressed != NULL
1913 && strcmp (section_name, names->compressed) == 0)
1914 return 1;
1915 return 0;
233a11ab
CS
1916}
1917
330cdd98 1918/* See declaration. */
c906108c 1919
330cdd98 1920void
5989a64e
SM
1921dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1922 const dwarf2_debug_sections &names)
c906108c 1923{
fd361982 1924 flagword aflag = bfd_section_flags (sectp);
251d32d9 1925
dc7650b8
JK
1926 if ((aflag & SEC_HAS_CONTENTS) == 0)
1927 {
1928 }
950b7495
KS
1929 else if (elf_section_data (sectp)->this_hdr.sh_size
1930 > bfd_get_file_size (abfd))
1931 {
1932 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1933 warning (_("Discarding section %s which has a section size (%s"
1934 ") larger than the file size [in module %s]"),
1935 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1936 bfd_get_filename (abfd));
1937 }
330cdd98 1938 else if (section_is_p (sectp->name, &names.info))
c906108c 1939 {
330cdd98 1940 this->info.s.section = sectp;
fd361982 1941 this->info.size = bfd_section_size (sectp);
c906108c 1942 }
330cdd98 1943 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1944 {
330cdd98 1945 this->abbrev.s.section = sectp;
fd361982 1946 this->abbrev.size = bfd_section_size (sectp);
c906108c 1947 }
330cdd98 1948 else if (section_is_p (sectp->name, &names.line))
c906108c 1949 {
330cdd98 1950 this->line.s.section = sectp;
fd361982 1951 this->line.size = bfd_section_size (sectp);
c906108c 1952 }
330cdd98 1953 else if (section_is_p (sectp->name, &names.loc))
c906108c 1954 {
330cdd98 1955 this->loc.s.section = sectp;
fd361982 1956 this->loc.size = bfd_section_size (sectp);
c906108c 1957 }
330cdd98 1958 else if (section_is_p (sectp->name, &names.loclists))
43988095 1959 {
330cdd98 1960 this->loclists.s.section = sectp;
fd361982 1961 this->loclists.size = bfd_section_size (sectp);
43988095 1962 }
330cdd98 1963 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1964 {
330cdd98 1965 this->macinfo.s.section = sectp;
fd361982 1966 this->macinfo.size = bfd_section_size (sectp);
c906108c 1967 }
330cdd98 1968 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1969 {
330cdd98 1970 this->macro.s.section = sectp;
fd361982 1971 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1972 }
330cdd98 1973 else if (section_is_p (sectp->name, &names.str))
c906108c 1974 {
330cdd98 1975 this->str.s.section = sectp;
fd361982 1976 this->str.size = bfd_section_size (sectp);
c906108c 1977 }
18a8505e
AT
1978 else if (section_is_p (sectp->name, &names.str_offsets))
1979 {
1980 this->str_offsets.s.section = sectp;
1981 this->str_offsets.size = bfd_section_size (sectp);
1982 }
330cdd98 1983 else if (section_is_p (sectp->name, &names.line_str))
43988095 1984 {
330cdd98 1985 this->line_str.s.section = sectp;
fd361982 1986 this->line_str.size = bfd_section_size (sectp);
43988095 1987 }
330cdd98 1988 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1989 {
330cdd98 1990 this->addr.s.section = sectp;
fd361982 1991 this->addr.size = bfd_section_size (sectp);
3019eac3 1992 }
330cdd98 1993 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1994 {
330cdd98 1995 this->frame.s.section = sectp;
fd361982 1996 this->frame.size = bfd_section_size (sectp);
b6af0555 1997 }
330cdd98 1998 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 1999 {
330cdd98 2000 this->eh_frame.s.section = sectp;
fd361982 2001 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2002 }
330cdd98 2003 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2004 {
330cdd98 2005 this->ranges.s.section = sectp;
fd361982 2006 this->ranges.size = bfd_section_size (sectp);
af34e669 2007 }
330cdd98 2008 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2009 {
330cdd98 2010 this->rnglists.s.section = sectp;
fd361982 2011 this->rnglists.size = bfd_section_size (sectp);
43988095 2012 }
330cdd98 2013 else if (section_is_p (sectp->name, &names.types))
348e048f 2014 {
8b70b953
TT
2015 struct dwarf2_section_info type_section;
2016
2017 memset (&type_section, 0, sizeof (type_section));
049412e3 2018 type_section.s.section = sectp;
fd361982 2019 type_section.size = bfd_section_size (sectp);
8b70b953 2020
fd5866f6 2021 this->types.push_back (type_section);
348e048f 2022 }
330cdd98 2023 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2024 {
330cdd98 2025 this->gdb_index.s.section = sectp;
fd361982 2026 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2027 }
927aa2e7
JK
2028 else if (section_is_p (sectp->name, &names.debug_names))
2029 {
2030 this->debug_names.s.section = sectp;
fd361982 2031 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2032 }
2033 else if (section_is_p (sectp->name, &names.debug_aranges))
2034 {
2035 this->debug_aranges.s.section = sectp;
fd361982 2036 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2037 }
dce234bc 2038
fd361982
AM
2039 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2040 && bfd_section_vma (sectp) == 0)
330cdd98 2041 this->has_section_at_zero = true;
c906108c
SS
2042}
2043
dce234bc 2044/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2045 SECTION_NAME. */
af34e669 2046
dce234bc 2047void
3017a003
TG
2048dwarf2_get_section_info (struct objfile *objfile,
2049 enum dwarf2_section_enum sect,
d521ce57 2050 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2051 bfd_size_type *sizep)
2052{
17ee85fc 2053 struct dwarf2_per_objfile *data = get_dwarf2_per_objfile (objfile);
dce234bc 2054 struct dwarf2_section_info *info;
a3b2a86b
TT
2055
2056 /* We may see an objfile without any DWARF, in which case we just
2057 return nothing. */
2058 if (data == NULL)
2059 {
2060 *sectp = NULL;
2061 *bufp = NULL;
2062 *sizep = 0;
2063 return;
2064 }
3017a003
TG
2065 switch (sect)
2066 {
2067 case DWARF2_DEBUG_FRAME:
5989a64e 2068 info = &data->per_bfd->frame;
3017a003
TG
2069 break;
2070 case DWARF2_EH_FRAME:
5989a64e 2071 info = &data->per_bfd->eh_frame;
3017a003
TG
2072 break;
2073 default:
2074 gdb_assert_not_reached ("unexpected section");
2075 }
dce234bc 2076
96b79293 2077 info->read (objfile);
dce234bc 2078
96b79293 2079 *sectp = info->get_bfd_section ();
dce234bc
PP
2080 *bufp = info->buffer;
2081 *sizep = info->size;
2082}
2083
36586728
TT
2084/* A helper function to find the sections for a .dwz file. */
2085
2086static void
2087locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2088{
9a3c8263 2089 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2090
2091 /* Note that we only support the standard ELF names, because .dwz
2092 is ELF-only (at the time of writing). */
2093 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2094 {
049412e3 2095 dwz_file->abbrev.s.section = sectp;
fd361982 2096 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2097 }
2098 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2099 {
049412e3 2100 dwz_file->info.s.section = sectp;
fd361982 2101 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2102 }
2103 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2104 {
049412e3 2105 dwz_file->str.s.section = sectp;
fd361982 2106 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2107 }
2108 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2109 {
049412e3 2110 dwz_file->line.s.section = sectp;
fd361982 2111 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2112 }
2113 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2114 {
049412e3 2115 dwz_file->macro.s.section = sectp;
fd361982 2116 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2117 }
2ec9a5e0
TT
2118 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2119 {
049412e3 2120 dwz_file->gdb_index.s.section = sectp;
fd361982 2121 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2122 }
927aa2e7
JK
2123 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2124 {
2125 dwz_file->debug_names.s.section = sectp;
fd361982 2126 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2127 }
36586728
TT
2128}
2129
c4973306 2130/* See dwarf2read.h. */
36586728 2131
c4973306 2132struct dwz_file *
c3699833 2133dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
36586728 2134{
36586728 2135 const char *filename;
acd13123 2136 bfd_size_type buildid_len_arg;
dc294be5
TT
2137 size_t buildid_len;
2138 bfd_byte *buildid;
36586728 2139
c3699833
SM
2140 if (per_bfd->dwz_file != NULL)
2141 return per_bfd->dwz_file.get ();
36586728 2142
4db1a1dc 2143 bfd_set_error (bfd_error_no_error);
791afaa2 2144 gdb::unique_xmalloc_ptr<char> data
c3699833 2145 (bfd_get_alt_debug_link_info (per_bfd->obfd,
791afaa2 2146 &buildid_len_arg, &buildid));
4db1a1dc
TT
2147 if (data == NULL)
2148 {
2149 if (bfd_get_error () == bfd_error_no_error)
2150 return NULL;
2151 error (_("could not read '.gnu_debugaltlink' section: %s"),
2152 bfd_errmsg (bfd_get_error ()));
2153 }
791afaa2
TT
2154
2155 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2156
acd13123
TT
2157 buildid_len = (size_t) buildid_len_arg;
2158
791afaa2 2159 filename = data.get ();
d721ba37
PA
2160
2161 std::string abs_storage;
36586728
TT
2162 if (!IS_ABSOLUTE_PATH (filename))
2163 {
14278e1f 2164 gdb::unique_xmalloc_ptr<char> abs
c3699833 2165 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
36586728 2166
14278e1f 2167 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2168 filename = abs_storage.c_str ();
36586728
TT
2169 }
2170
dc294be5
TT
2171 /* First try the file name given in the section. If that doesn't
2172 work, try to use the build-id instead. */
ad80db5b 2173 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
dc294be5 2174 if (dwz_bfd != NULL)
36586728 2175 {
192b62ce 2176 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2177 dwz_bfd.reset (nullptr);
36586728
TT
2178 }
2179
dc294be5
TT
2180 if (dwz_bfd == NULL)
2181 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2182
0d79cdc4
AM
2183 if (dwz_bfd == nullptr)
2184 {
2185 gdb::unique_xmalloc_ptr<char> alt_filename;
c3699833 2186 const char *origname = bfd_get_filename (per_bfd->obfd);
0d79cdc4
AM
2187
2188 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2189 buildid_len,
2190 origname,
2191 &alt_filename));
2192
2193 if (fd.get () >= 0)
2194 {
2195 /* File successfully retrieved from server. */
ad80db5b 2196 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
0d79cdc4
AM
2197
2198 if (dwz_bfd == nullptr)
2199 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2200 alt_filename.get ());
2201 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2202 dwz_bfd.reset (nullptr);
2203 }
2204 }
2205
dc294be5
TT
2206 if (dwz_bfd == NULL)
2207 error (_("could not find '.gnu_debugaltlink' file for %s"),
c3699833 2208 bfd_get_filename (per_bfd->obfd));
dc294be5 2209
7ff8cb8c
TT
2210 std::unique_ptr<struct dwz_file> result
2211 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2212
7ff8cb8c
TT
2213 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2214 result.get ());
36586728 2215
c3699833
SM
2216 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2217 per_bfd->dwz_file = std::move (result);
2218 return per_bfd->dwz_file.get ();
36586728 2219}
9291a0cd 2220\f
7b9f3c50
DE
2221/* DWARF quick_symbols_functions support. */
2222
2223/* TUs can share .debug_line entries, and there can be a lot more TUs than
2224 unique line tables, so we maintain a separate table of all .debug_line
2225 derived entries to support the sharing.
2226 All the quick functions need is the list of file names. We discard the
2227 line_header when we're done and don't need to record it here. */
2228struct quick_file_names
2229{
094b34ac
DE
2230 /* The data used to construct the hash key. */
2231 struct stmt_list_hash hash;
7b9f3c50
DE
2232
2233 /* The number of entries in file_names, real_names. */
2234 unsigned int num_file_names;
2235
2236 /* The file names from the line table, after being run through
2237 file_full_name. */
2238 const char **file_names;
2239
2240 /* The file names from the line table after being run through
2241 gdb_realpath. These are computed lazily. */
2242 const char **real_names;
2243};
2244
2245/* When using the index (and thus not using psymtabs), each CU has an
2246 object of this type. This is used to hold information needed by
2247 the various "quick" methods. */
2248struct dwarf2_per_cu_quick_data
2249{
2250 /* The file table. This can be NULL if there was no file table
2251 or it's currently not read in.
5989a64e 2252 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2253 struct quick_file_names *file_names;
2254
7b9f3c50
DE
2255 /* A temporary mark bit used when iterating over all CUs in
2256 expand_symtabs_matching. */
2257 unsigned int mark : 1;
2258
2259 /* True if we've tried to read the file table and found there isn't one.
2260 There will be no point in trying to read it again next time. */
2261 unsigned int no_file_data : 1;
2262};
2263
094b34ac
DE
2264/* Utility hash function for a stmt_list_hash. */
2265
2266static hashval_t
2267hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2268{
2269 hashval_t v = 0;
2270
2271 if (stmt_list_hash->dwo_unit != NULL)
2272 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2273 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2274 return v;
2275}
2276
2277/* Utility equality function for a stmt_list_hash. */
2278
2279static int
2280eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2281 const struct stmt_list_hash *rhs)
2282{
2283 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2284 return 0;
2285 if (lhs->dwo_unit != NULL
2286 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2287 return 0;
2288
9c541725 2289 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2290}
2291
7b9f3c50
DE
2292/* Hash function for a quick_file_names. */
2293
2294static hashval_t
2295hash_file_name_entry (const void *e)
2296{
9a3c8263
SM
2297 const struct quick_file_names *file_data
2298 = (const struct quick_file_names *) e;
7b9f3c50 2299
094b34ac 2300 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2301}
2302
2303/* Equality function for a quick_file_names. */
2304
2305static int
2306eq_file_name_entry (const void *a, const void *b)
2307{
9a3c8263
SM
2308 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2309 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2310
094b34ac 2311 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2312}
2313
2314/* Delete function for a quick_file_names. */
2315
2316static void
2317delete_file_name_entry (void *e)
2318{
9a3c8263 2319 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2320 int i;
2321
2322 for (i = 0; i < file_data->num_file_names; ++i)
2323 {
2324 xfree ((void*) file_data->file_names[i]);
2325 if (file_data->real_names)
2326 xfree ((void*) file_data->real_names[i]);
2327 }
2328
45940949
TT
2329 /* The space for the struct itself lives on the obstack, so we don't
2330 free it here. */
7b9f3c50
DE
2331}
2332
2333/* Create a quick_file_names hash table. */
2334
5895093f 2335static htab_up
7b9f3c50
DE
2336create_quick_file_names_table (unsigned int nr_initial_entries)
2337{
5895093f
TT
2338 return htab_up (htab_create_alloc (nr_initial_entries,
2339 hash_file_name_entry, eq_file_name_entry,
2340 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2341}
9291a0cd 2342
ab432490
SM
2343/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2344 function is unrelated to symtabs, symtab would have to be created afterwards.
2345 You should call age_cached_comp_units after processing the CU. */
918dd910 2346
1b555f17 2347static dwarf2_cu *
ab432490
SM
2348load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2349 bool skip_partial)
918dd910 2350{
3019eac3 2351 if (per_cu->is_debug_types)
ab432490 2352 load_full_type_unit (per_cu, per_objfile);
918dd910 2353 else
ab432490 2354 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
918dd910 2355
7188ed02
SM
2356 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2357 if (cu == nullptr)
1b555f17 2358 return nullptr; /* Dummy CU. */
2dc860c0 2359
7188ed02 2360 dwarf2_find_base_address (cu->dies, cu);
1b555f17 2361
7188ed02 2362 return cu;
918dd910
JK
2363}
2364
97a1449a 2365/* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2fdf6df6 2366
9291a0cd 2367static void
97a1449a
SM
2368dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2369 dwarf2_per_objfile *dwarf2_per_objfile,
2370 bool skip_partial)
9291a0cd 2371{
f4dc4d17
DE
2372 /* Skip type_unit_groups, reading the type units they contain
2373 is handled elsewhere. */
197400e8 2374 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2375 return;
2376
b303c6f6
AB
2377 /* The destructor of dwarf2_queue_guard frees any entries left on
2378 the queue. After this point we're guaranteed to leave this function
2379 with the dwarf queue empty. */
39856def 2380 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2381
af758d11 2382 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
95554aad 2383 {
120ce1b5 2384 queue_comp_unit (per_cu, dwarf2_per_objfile, language_minimal);
1b555f17 2385 dwarf2_cu *cu = load_cu (per_cu, dwarf2_per_objfile, skip_partial);
89e63ee4
DE
2386
2387 /* If we just loaded a CU from a DWO, and we're working with an index
2388 that may badly handle TUs, load all the TUs in that DWO as well.
2389 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2390 if (!per_cu->is_debug_types
1b555f17
SM
2391 && cu != NULL
2392 && cu->dwo_unit != NULL
5989a64e
SM
2393 && dwarf2_per_objfile->per_bfd->index_table != NULL
2394 && dwarf2_per_objfile->per_bfd->index_table->version <= 7
89e63ee4 2395 /* DWP files aren't supported yet. */
ed2dc618 2396 && get_dwp_file (dwarf2_per_objfile) == NULL)
1b555f17 2397 queue_and_load_all_dwo_tus (cu);
95554aad 2398 }
9291a0cd 2399
ed2dc618 2400 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2401
2402 /* Age the cache, releasing compilation units that have not
2403 been used recently. */
7188ed02 2404 dwarf2_per_objfile->age_comp_units ();
9291a0cd
TT
2405}
2406
97a1449a
SM
2407/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2408 the per-objfile for which this symtab is instantiated.
2409
2410 Returns the resulting symbol table. */
2fdf6df6 2411
43f3e411 2412static struct compunit_symtab *
97a1449a
SM
2413dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2414 dwarf2_per_objfile *dwarf2_per_objfile,
2415 bool skip_partial)
9291a0cd 2416{
5989a64e 2417 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
af758d11
SM
2418
2419 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd 2420 {
11ed8cad 2421 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2422 scoped_restore decrementer = increment_reading_symtab ();
97a1449a 2423 dw2_do_instantiate_symtab (per_cu, dwarf2_per_objfile, skip_partial);
ed2dc618 2424 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2425 }
f194fefb 2426
af758d11 2427 return dwarf2_per_objfile->get_symtab (per_cu);
9291a0cd
TT
2428}
2429
ff4c9fec 2430/* See declaration. */
f4dc4d17 2431
ff4c9fec 2432dwarf2_per_cu_data *
5989a64e 2433dwarf2_per_bfd::get_cutu (int index)
ff4c9fec 2434{
b76e467d 2435 if (index >= this->all_comp_units.size ())
ff4c9fec 2436 {
b76e467d 2437 index -= this->all_comp_units.size ();
b2bdb8cf 2438 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2439 return &this->all_type_units[index]->per_cu;
2440 }
f4dc4d17 2441
ff4c9fec
SM
2442 return this->all_comp_units[index];
2443}
f4dc4d17 2444
ff4c9fec 2445/* See declaration. */
2fdf6df6 2446
ff4c9fec 2447dwarf2_per_cu_data *
5989a64e 2448dwarf2_per_bfd::get_cu (int index)
1fd400ff 2449{
b76e467d 2450 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2451
ff4c9fec 2452 return this->all_comp_units[index];
f4dc4d17
DE
2453}
2454
ff4c9fec 2455/* See declaration. */
f4dc4d17 2456
ff4c9fec 2457signatured_type *
5989a64e 2458dwarf2_per_bfd::get_tu (int index)
f4dc4d17 2459{
b2bdb8cf 2460 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2461
ff4c9fec 2462 return this->all_type_units[index];
1fd400ff
TT
2463}
2464
d3473f0c
TT
2465/* See read.h. */
2466
2467dwarf2_per_cu_data *
5989a64e 2468dwarf2_per_bfd::allocate_per_cu ()
d3473f0c
TT
2469{
2470 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
1859c670 2471 result->per_bfd = this;
d3473f0c
TT
2472 result->index = m_num_psymtabs++;
2473 return result;
2474}
2475
2476/* See read.h. */
2477
2478signatured_type *
5989a64e 2479dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c
TT
2480{
2481 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
1859c670 2482 result->per_cu.per_bfd = this;
d3473f0c
TT
2483 result->per_cu.index = m_num_psymtabs++;
2484 return result;
2485}
2486
168c9250 2487/* Return a new dwarf2_per_cu_data allocated on the per-bfd
45940949 2488 obstack, and constructed with the specified field values. */
4b514bc8
JK
2489
2490static dwarf2_per_cu_data *
168c9250
SM
2491create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2492 struct dwarf2_section_info *section,
2493 int is_dwz,
2494 sect_offset sect_off, ULONGEST length)
4b514bc8 2495{
168c9250 2496 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
4b514bc8
JK
2497 the_cu->sect_off = sect_off;
2498 the_cu->length = length;
4b514bc8 2499 the_cu->section = section;
168c9250 2500 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 2501 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2502 the_cu->is_dwz = is_dwz;
2503 return the_cu;
2504}
2505
2ec9a5e0
TT
2506/* A helper for create_cus_from_index that handles a given list of
2507 CUs. */
2fdf6df6 2508
74a0d9f6 2509static void
168c9250 2510create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2511 const gdb_byte *cu_list, offset_type n_elements,
2512 struct dwarf2_section_info *section,
b76e467d 2513 int is_dwz)
9291a0cd 2514{
12359b5e 2515 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2516 {
74a0d9f6 2517 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2518
2519 sect_offset sect_off
2520 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2521 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2522 cu_list += 2 * 8;
2523
b76e467d 2524 dwarf2_per_cu_data *per_cu
168c9250
SM
2525 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2526 length);
2527 per_bfd->all_comp_units.push_back (per_cu);
9291a0cd 2528 }
9291a0cd
TT
2529}
2530
2ec9a5e0 2531/* Read the CU list from the mapped index, and use it to create all
168c9250 2532 the CU objects for PER_BFD. */
2ec9a5e0 2533
74a0d9f6 2534static void
168c9250 2535create_cus_from_index (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2536 const gdb_byte *cu_list, offset_type cu_list_elements,
2537 const gdb_byte *dwz_list, offset_type dwz_elements)
2538{
168c9250
SM
2539 gdb_assert (per_bfd->all_comp_units.empty ());
2540 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2541
168c9250
SM
2542 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2543 &per_bfd->info, 0);
2ec9a5e0
TT
2544
2545 if (dwz_elements == 0)
74a0d9f6 2546 return;
2ec9a5e0 2547
168c9250
SM
2548 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2549 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
b76e467d 2550 &dwz->info, 1);
2ec9a5e0
TT
2551}
2552
1fd400ff 2553/* Create the signatured type hash table from the index. */
673bfd45 2554
74a0d9f6 2555static void
12359b5e 2556create_signatured_type_table_from_index
168c9250
SM
2557 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2558 const gdb_byte *bytes, offset_type elements)
1fd400ff 2559{
168c9250
SM
2560 gdb_assert (per_bfd->all_type_units.empty ());
2561 per_bfd->all_type_units.reserve (elements / 3);
1fd400ff 2562
298e9637 2563 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2564
12359b5e 2565 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2566 {
52dc124a 2567 struct signatured_type *sig_type;
9c541725 2568 ULONGEST signature;
1fd400ff 2569 void **slot;
9c541725 2570 cu_offset type_offset_in_tu;
1fd400ff 2571
74a0d9f6 2572 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2573 sect_offset sect_off
2574 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2575 type_offset_in_tu
2576 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2577 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2578 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2579 bytes += 3 * 8;
2580
168c9250 2581 sig_type = per_bfd->allocate_signatured_type ();
52dc124a 2582 sig_type->signature = signature;
9c541725 2583 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2584 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2585 sig_type->per_cu.section = section;
9c541725 2586 sig_type->per_cu.sect_off = sect_off;
52dc124a 2587 sig_type->per_cu.v.quick
168c9250 2588 = OBSTACK_ZALLOC (&per_bfd->obstack,
1fd400ff
TT
2589 struct dwarf2_per_cu_quick_data);
2590
b0b6a987 2591 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2592 *slot = sig_type;
1fd400ff 2593
168c9250 2594 per_bfd->all_type_units.push_back (sig_type);
1fd400ff
TT
2595 }
2596
168c9250 2597 per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2598}
2599
927aa2e7
JK
2600/* Create the signatured type hash table from .debug_names. */
2601
2602static void
2603create_signatured_type_table_from_debug_names
ed2dc618 2604 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2605 const mapped_debug_names &map,
2606 struct dwarf2_section_info *section,
2607 struct dwarf2_section_info *abbrev_section)
2608{
ed2dc618
SM
2609 struct objfile *objfile = dwarf2_per_objfile->objfile;
2610
96b79293
TT
2611 section->read (objfile);
2612 abbrev_section->read (objfile);
927aa2e7 2613
5989a64e
SM
2614 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2615 dwarf2_per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
927aa2e7 2616
298e9637 2617 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2618
2619 for (uint32_t i = 0; i < map.tu_count; ++i)
2620 {
2621 struct signatured_type *sig_type;
927aa2e7 2622 void **slot;
927aa2e7
JK
2623
2624 sect_offset sect_off
2625 = (sect_offset) (extract_unsigned_integer
2626 (map.tu_table_reordered + i * map.offset_size,
2627 map.offset_size,
2628 map.dwarf5_byte_order));
2629
2630 comp_unit_head cu_header;
ed2dc618
SM
2631 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2632 abbrev_section,
927aa2e7
JK
2633 section->buffer + to_underlying (sect_off),
2634 rcuh_kind::TYPE);
2635
5989a64e 2636 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2637 sig_type->signature = cu_header.signature;
2638 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2639 sig_type->per_cu.is_debug_types = 1;
2640 sig_type->per_cu.section = section;
2641 sig_type->per_cu.sect_off = sect_off;
927aa2e7 2642 sig_type->per_cu.v.quick
5989a64e 2643 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
927aa2e7
JK
2644 struct dwarf2_per_cu_quick_data);
2645
b0b6a987 2646 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2647 *slot = sig_type;
2648
5989a64e 2649 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
927aa2e7
JK
2650 }
2651
5989a64e 2652 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2653}
2654
9291a0cd
TT
2655/* Read the address map data from the mapped index, and use it to
2656 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2657
9291a0cd 2658static void
ed2dc618
SM
2659create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2660 struct mapped_index *index)
9291a0cd 2661{
ed2dc618 2662 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 2663 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2664 const gdb_byte *iter, *end;
9291a0cd 2665 struct addrmap *mutable_map;
9291a0cd
TT
2666 CORE_ADDR baseaddr;
2667
8268c778
PA
2668 auto_obstack temp_obstack;
2669
9291a0cd
TT
2670 mutable_map = addrmap_create_mutable (&temp_obstack);
2671
f00a2de2
PA
2672 iter = index->address_table.data ();
2673 end = iter + index->address_table.size ();
9291a0cd 2674
b3b3bada 2675 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2676
2677 while (iter < end)
2678 {
2679 ULONGEST hi, lo, cu_index;
2680 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2681 iter += 8;
2682 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2683 iter += 8;
2684 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2685 iter += 4;
f652bce2 2686
24a55014 2687 if (lo > hi)
f652bce2 2688 {
b98664d3 2689 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2690 hex_string (lo), hex_string (hi));
24a55014 2691 continue;
f652bce2 2692 }
24a55014 2693
5989a64e 2694 if (cu_index >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
f652bce2 2695 {
b98664d3 2696 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2697 (unsigned) cu_index);
24a55014 2698 continue;
f652bce2 2699 }
24a55014 2700
79748972
TT
2701 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2702 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2703 addrmap_set_empty (mutable_map, lo, hi - 1,
5989a64e 2704 dwarf2_per_objfile->per_bfd->get_cu (cu_index));
9291a0cd
TT
2705 }
2706
d320c2b5 2707 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2708 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2709}
2710
927aa2e7
JK
2711/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2712 populate the objfile's psymtabs_addrmap. */
2713
2714static void
ed2dc618 2715create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2716 struct dwarf2_section_info *section)
2717{
ed2dc618 2718 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7 2719 bfd *abfd = objfile->obfd;
08feed99 2720 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2721 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2722
2723 auto_obstack temp_obstack;
2724 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2725
2726 std::unordered_map<sect_offset,
2727 dwarf2_per_cu_data *,
2728 gdb::hash_enum<sect_offset>>
2729 debug_info_offset_to_per_cu;
5989a64e 2730 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 2731 {
927aa2e7
JK
2732 const auto insertpair
2733 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2734 if (!insertpair.second)
2735 {
2736 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2737 "debug_info_offset %s, ignoring .debug_aranges."),
2738 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2739 return;
2740 }
2741 }
2742
96b79293 2743 section->read (objfile);
927aa2e7
JK
2744
2745 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2746
2747 const gdb_byte *addr = section->buffer;
2748
2749 while (addr < section->buffer + section->size)
2750 {
2751 const gdb_byte *const entry_addr = addr;
2752 unsigned int bytes_read;
2753
2754 const LONGEST entry_length = read_initial_length (abfd, addr,
2755 &bytes_read);
2756 addr += bytes_read;
2757
2758 const gdb_byte *const entry_end = addr + entry_length;
2759 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2760 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2761 if (addr + entry_length > section->buffer + section->size)
2762 {
47e3f474 2763 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2764 "length %s exceeds section length %s, "
2765 "ignoring .debug_aranges."),
47e3f474
TV
2766 objfile_name (objfile),
2767 plongest (entry_addr - section->buffer),
927aa2e7
JK
2768 plongest (bytes_read + entry_length),
2769 pulongest (section->size));
2770 return;
2771 }
2772
2773 /* The version number. */
2774 const uint16_t version = read_2_bytes (abfd, addr);
2775 addr += 2;
2776 if (version != 2)
2777 {
47e3f474 2778 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2779 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2780 objfile_name (objfile),
2781 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2782 return;
2783 }
2784
2785 const uint64_t debug_info_offset
2786 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2787 addr += offset_size;
2788 const auto per_cu_it
2789 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2790 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2791 {
47e3f474 2792 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2793 "debug_info_offset %s does not exists, "
2794 "ignoring .debug_aranges."),
47e3f474
TV
2795 objfile_name (objfile),
2796 plongest (entry_addr - section->buffer),
927aa2e7
JK
2797 pulongest (debug_info_offset));
2798 return;
2799 }
2800 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2801
2802 const uint8_t address_size = *addr++;
2803 if (address_size < 1 || address_size > 8)
2804 {
47e3f474 2805 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2806 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2807 objfile_name (objfile),
2808 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2809 return;
2810 }
2811
2812 const uint8_t segment_selector_size = *addr++;
2813 if (segment_selector_size != 0)
2814 {
47e3f474 2815 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2816 "segment_selector_size %u is not supported, "
2817 "ignoring .debug_aranges."),
47e3f474
TV
2818 objfile_name (objfile),
2819 plongest (entry_addr - section->buffer),
927aa2e7
JK
2820 segment_selector_size);
2821 return;
2822 }
2823
2824 /* Must pad to an alignment boundary that is twice the address
2825 size. It is undocumented by the DWARF standard but GCC does
2826 use it. */
2827 for (size_t padding = ((-(addr - section->buffer))
2828 & (2 * address_size - 1));
2829 padding > 0; padding--)
2830 if (*addr++ != 0)
2831 {
47e3f474 2832 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2833 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2834 objfile_name (objfile),
2835 plongest (entry_addr - section->buffer));
927aa2e7
JK
2836 return;
2837 }
2838
2839 for (;;)
2840 {
2841 if (addr + 2 * address_size > entry_end)
2842 {
47e3f474 2843 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2844 "address list is not properly terminated, "
2845 "ignoring .debug_aranges."),
47e3f474
TV
2846 objfile_name (objfile),
2847 plongest (entry_addr - section->buffer));
927aa2e7
JK
2848 return;
2849 }
2850 ULONGEST start = extract_unsigned_integer (addr, address_size,
2851 dwarf5_byte_order);
2852 addr += address_size;
2853 ULONGEST length = extract_unsigned_integer (addr, address_size,
2854 dwarf5_byte_order);
2855 addr += address_size;
2856 if (start == 0 && length == 0)
2857 break;
5989a64e 2858 if (start == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
927aa2e7
JK
2859 {
2860 /* Symbol was eliminated due to a COMDAT group. */
2861 continue;
2862 }
2863 ULONGEST end = start + length;
79748972
TT
2864 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2865 - baseaddr);
2866 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2867 - baseaddr);
927aa2e7
JK
2868 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2869 }
2870 }
2871
d320c2b5 2872 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2873 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2874}
2875
9291a0cd
TT
2876/* Find a slot in the mapped index INDEX for the object named NAME.
2877 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2878 constant pool and return true. If NAME cannot be found, return
2879 false. */
2fdf6df6 2880
109483d9 2881static bool
9291a0cd
TT
2882find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2883 offset_type **vec_out)
2884{
0cf03b49 2885 offset_type hash;
9291a0cd 2886 offset_type slot, step;
559a7a62 2887 int (*cmp) (const char *, const char *);
9291a0cd 2888
791afaa2 2889 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2890 if (current_language->la_language == language_cplus
45280282
IB
2891 || current_language->la_language == language_fortran
2892 || current_language->la_language == language_d)
0cf03b49
JK
2893 {
2894 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2895 not contain any. */
a8719064 2896
72998fb3 2897 if (strchr (name, '(') != NULL)
0cf03b49 2898 {
109483d9 2899 without_params = cp_remove_params (name);
0cf03b49 2900
72998fb3 2901 if (without_params != NULL)
791afaa2 2902 name = without_params.get ();
0cf03b49
JK
2903 }
2904 }
2905
559a7a62 2906 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2907 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2908 simulate our NAME being searched is also lowercased. */
2909 hash = mapped_index_string_hash ((index->version == 4
2910 && case_sensitivity == case_sensitive_off
2911 ? 5 : index->version),
2912 name);
2913
f00a2de2
PA
2914 slot = hash & (index->symbol_table.size () - 1);
2915 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2916 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2917
2918 for (;;)
2919 {
9291a0cd 2920 const char *str;
f00a2de2
PA
2921
2922 const auto &bucket = index->symbol_table[slot];
2923 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2924 return false;
9291a0cd 2925
f00a2de2 2926 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2927 if (!cmp (name, str))
9291a0cd
TT
2928 {
2929 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2930 + MAYBE_SWAP (bucket.vec));
109483d9 2931 return true;
9291a0cd
TT
2932 }
2933
f00a2de2 2934 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2935 }
2936}
2937
4485a1c1
SM
2938/* A helper function that reads the .gdb_index from BUFFER and fills
2939 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2940 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2941 ok to use deprecated sections.
2942
2943 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2944 out parameters that are filled in with information about the CU and
2945 TU lists in the section.
2946
4485a1c1 2947 Returns true if all went well, false otherwise. */
2fdf6df6 2948
d33bc52e 2949static bool
3810f182 2950read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2951 bool deprecated_ok,
2952 gdb::array_view<const gdb_byte> buffer,
2953 struct mapped_index *map,
2954 const gdb_byte **cu_list,
2955 offset_type *cu_list_elements,
2956 const gdb_byte **types_list,
2957 offset_type *types_list_elements)
2958{
2959 const gdb_byte *addr = &buffer[0];
82430852 2960
9291a0cd 2961 /* Version check. */
4485a1c1 2962 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2963 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2964 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2965 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2966 indices. */
831adc1f 2967 if (version < 4)
481860b3
GB
2968 {
2969 static int warning_printed = 0;
2970 if (!warning_printed)
2971 {
2972 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2973 filename);
481860b3
GB
2974 warning_printed = 1;
2975 }
2976 return 0;
2977 }
2978 /* Index version 4 uses a different hash function than index version
2979 5 and later.
2980
2981 Versions earlier than 6 did not emit psymbols for inlined
2982 functions. Using these files will cause GDB not to be able to
2983 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2984 indices unless the user has done
2985 "set use-deprecated-index-sections on". */
2ec9a5e0 2986 if (version < 6 && !deprecated_ok)
481860b3
GB
2987 {
2988 static int warning_printed = 0;
2989 if (!warning_printed)
2990 {
e615022a
DE
2991 warning (_("\
2992Skipping deprecated .gdb_index section in %s.\n\
2993Do \"set use-deprecated-index-sections on\" before the file is read\n\
2994to use the section anyway."),
2ec9a5e0 2995 filename);
481860b3
GB
2996 warning_printed = 1;
2997 }
2998 return 0;
2999 }
796a7ff8 3000 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3001 of the TU (for symbols coming from TUs),
3002 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3003 Plus gold-generated indices can have duplicate entries for global symbols,
3004 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3005 These are just performance bugs, and we can't distinguish gdb-generated
3006 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3007
481860b3 3008 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3009 longer backward compatible. */
796a7ff8 3010 if (version > 8)
594e8718 3011 return 0;
9291a0cd 3012
559a7a62 3013 map->version = version;
9291a0cd 3014
4485a1c1 3015 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3016
4485a1c1 3017 int i = 0;
2ec9a5e0
TT
3018 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3019 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3020 / 8);
1fd400ff
TT
3021 ++i;
3022
2ec9a5e0
TT
3023 *types_list = addr + MAYBE_SWAP (metadata[i]);
3024 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3025 - MAYBE_SWAP (metadata[i]))
3026 / 8);
987d643c 3027 ++i;
1fd400ff 3028
f00a2de2
PA
3029 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3030 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3031 map->address_table
3032 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3033 ++i;
3034
f00a2de2
PA
3035 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3036 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3037 map->symbol_table
3038 = gdb::array_view<mapped_index::symbol_table_slot>
3039 ((mapped_index::symbol_table_slot *) symbol_table,
3040 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3041
f00a2de2 3042 ++i;
f9d83a0b 3043 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3044
2ec9a5e0
TT
3045 return 1;
3046}
3047
4485a1c1
SM
3048/* Callback types for dwarf2_read_gdb_index. */
3049
3050typedef gdb::function_view
5989a64e 3051 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
3052 get_gdb_index_contents_ftype;
3053typedef gdb::function_view
3054 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3055 get_gdb_index_contents_dwz_ftype;
3056
927aa2e7 3057/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3058 elements of all the CUs and return 1. Otherwise, return 0. */
3059
3060static int
4485a1c1
SM
3061dwarf2_read_gdb_index
3062 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3063 get_gdb_index_contents_ftype get_gdb_index_contents,
3064 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3065{
2ec9a5e0
TT
3066 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3067 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3068 struct dwz_file *dwz;
12359b5e 3069 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3070
4485a1c1 3071 gdb::array_view<const gdb_byte> main_index_contents
5989a64e 3072 = get_gdb_index_contents (objfile, dwarf2_per_objfile->per_bfd);
4485a1c1
SM
3073
3074 if (main_index_contents.empty ())
3075 return 0;
3076
3063847f 3077 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3078 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3079 use_deprecated_index_sections,
3080 main_index_contents, map.get (), &cu_list,
3081 &cu_list_elements, &types_list,
3082 &types_list_elements))
2ec9a5e0
TT
3083 return 0;
3084
0fefef59 3085 /* Don't use the index if it's empty. */
3063847f 3086 if (map->symbol_table.empty ())
0fefef59
DE
3087 return 0;
3088
2ec9a5e0
TT
3089 /* If there is a .dwz file, read it so we can get its CU list as
3090 well. */
c3699833 3091 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
4db1a1dc 3092 if (dwz != NULL)
2ec9a5e0 3093 {
2ec9a5e0
TT
3094 struct mapped_index dwz_map;
3095 const gdb_byte *dwz_types_ignore;
3096 offset_type dwz_types_elements_ignore;
3097
4485a1c1
SM
3098 gdb::array_view<const gdb_byte> dwz_index_content
3099 = get_gdb_index_contents_dwz (objfile, dwz);
3100
3101 if (dwz_index_content.empty ())
3102 return 0;
3103
3810f182 3104 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3105 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3106 &dwz_list, &dwz_list_elements,
3107 &dwz_types_ignore,
3108 &dwz_types_elements_ignore))
2ec9a5e0
TT
3109 {
3110 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3111 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3112 return 0;
3113 }
3114 }
3115
168c9250 3116 create_cus_from_index (dwarf2_per_objfile->per_bfd, cu_list, cu_list_elements,
12359b5e 3117 dwz_list, dwz_list_elements);
1fd400ff 3118
8b70b953
TT
3119 if (types_list_elements)
3120 {
8b70b953
TT
3121 /* We can only handle a single .debug_types when we have an
3122 index. */
5989a64e 3123 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
8b70b953
TT
3124 return 0;
3125
5989a64e 3126 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
8b70b953 3127
168c9250
SM
3128 create_signatured_type_table_from_index (dwarf2_per_objfile->per_bfd,
3129 section, types_list,
3130 types_list_elements);
8b70b953 3131 }
9291a0cd 3132
3063847f 3133 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3134
5989a64e
SM
3135 dwarf2_per_objfile->per_bfd->index_table = std::move (map);
3136 dwarf2_per_objfile->per_bfd->using_index = 1;
3137 dwarf2_per_objfile->per_bfd->quick_file_names_table =
3138 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
9291a0cd
TT
3139
3140 return 1;
3141}
3142
dee91e82 3143/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3144
dee91e82
DE
3145static void
3146dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3147 const gdb_byte *info_ptr,
3e225074 3148 struct die_info *comp_unit_die)
9291a0cd 3149{
dee91e82 3150 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3151 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
5e22e966 3152 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
094b34ac 3153 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3154 struct attribute *attr;
7b9f3c50
DE
3155 void **slot;
3156 struct quick_file_names *qfn;
9291a0cd 3157
0186c6a7
DE
3158 gdb_assert (! this_cu->is_debug_types);
3159
07261596
TT
3160 /* Our callers never want to match partial units -- instead they
3161 will match the enclosing full CU. */
3162 if (comp_unit_die->tag == DW_TAG_partial_unit)
3163 {
3164 this_cu->v.quick->no_file_data = 1;
3165 return;
3166 }
3167
0186c6a7 3168 lh_cu = this_cu;
7b9f3c50 3169 slot = NULL;
dee91e82 3170
fff8551c 3171 line_header_up lh;
9c541725 3172 sect_offset line_offset {};
fff8551c 3173
dee91e82 3174 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3175 if (attr != nullptr)
9291a0cd 3176 {
7b9f3c50
DE
3177 struct quick_file_names find_entry;
3178
9c541725 3179 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3180
3181 /* We may have already read in this line header (TU line header sharing).
3182 If we have we're done. */
094b34ac 3183 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3184 find_entry.hash.line_sect_off = line_offset;
5989a64e 3185 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3186 &find_entry, INSERT);
3187 if (*slot != NULL)
3188 {
9a3c8263 3189 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3190 return;
7b9f3c50
DE
3191 }
3192
3019eac3 3193 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3194 }
3195 if (lh == NULL)
3196 {
094b34ac 3197 lh_cu->v.quick->no_file_data = 1;
dee91e82 3198 return;
9291a0cd
TT
3199 }
3200
5989a64e 3201 qfn = XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3202 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3203 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3204 gdb_assert (slot != NULL);
3205 *slot = qfn;
9291a0cd 3206
d721ba37 3207 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3208
aa391654
TT
3209 int offset = 0;
3210 if (strcmp (fnd.name, "<unknown>") != 0)
3211 ++offset;
3212
7ba99d21 3213 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3214 qfn->file_names =
5989a64e 3215 XOBNEWVEC (&dwarf2_per_objfile->per_bfd->obstack, const char *,
45940949 3216 qfn->num_file_names);
aa391654
TT
3217 if (offset != 0)
3218 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3219 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3220 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3221 fnd.comp_dir).release ();
7b9f3c50 3222 qfn->real_names = NULL;
9291a0cd 3223
094b34ac 3224 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3225}
3226
3227/* A helper for the "quick" functions which attempts to read the line
3228 table for THIS_CU. */
3229
3230static struct quick_file_names *
ab432490
SM
3231dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3232 dwarf2_per_objfile *per_objfile)
dee91e82 3233{
0186c6a7
DE
3234 /* This should never be called for TUs. */
3235 gdb_assert (! this_cu->is_debug_types);
3236 /* Nor type unit groups. */
197400e8 3237 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3238
dee91e82
DE
3239 if (this_cu->v.quick->file_names != NULL)
3240 return this_cu->v.quick->file_names;
3241 /* If we know there is no line data, no point in looking again. */
3242 if (this_cu->v.quick->no_file_data)
3243 return NULL;
3244
ab432490 3245 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3246 if (!reader.dummy_p)
3e225074 3247 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3248
3249 if (this_cu->v.quick->no_file_data)
3250 return NULL;
3251 return this_cu->v.quick->file_names;
9291a0cd
TT
3252}
3253
3254/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3255 real path for a given file name from the line table. */
2fdf6df6 3256
9291a0cd 3257static const char *
45940949 3258dw2_get_real_path (struct dwarf2_per_objfile *dwarf2_per_objfile,
7b9f3c50 3259 struct quick_file_names *qfn, int index)
9291a0cd 3260{
7b9f3c50 3261 if (qfn->real_names == NULL)
5989a64e 3262 qfn->real_names = OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
26f2dc30 3263 qfn->num_file_names, const char *);
9291a0cd 3264
7b9f3c50 3265 if (qfn->real_names[index] == NULL)
14278e1f 3266 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3267
7b9f3c50 3268 return qfn->real_names[index];
9291a0cd
TT
3269}
3270
3271static struct symtab *
3272dw2_find_last_source_symtab (struct objfile *objfile)
3273{
ed2dc618
SM
3274 struct dwarf2_per_objfile *dwarf2_per_objfile
3275 = get_dwarf2_per_objfile (objfile);
5989a64e 3276 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->per_bfd->all_comp_units.back ();
97a1449a
SM
3277 compunit_symtab *cust
3278 = dw2_instantiate_symtab (dwarf_cu, dwarf2_per_objfile, false);
ae2de4f8 3279
43f3e411
DE
3280 if (cust == NULL)
3281 return NULL;
ed2dc618 3282
43f3e411 3283 return compunit_primary_filetab (cust);
9291a0cd
TT
3284}
3285
7b9f3c50
DE
3286/* Traversal function for dw2_forget_cached_source_info. */
3287
3288static int
3289dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3290{
7b9f3c50 3291 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3292
7b9f3c50 3293 if (file_data->real_names)
9291a0cd 3294 {
7b9f3c50 3295 int i;
9291a0cd 3296
7b9f3c50 3297 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3298 {
7b9f3c50
DE
3299 xfree ((void*) file_data->real_names[i]);
3300 file_data->real_names[i] = NULL;
9291a0cd
TT
3301 }
3302 }
7b9f3c50
DE
3303
3304 return 1;
3305}
3306
3307static void
3308dw2_forget_cached_source_info (struct objfile *objfile)
3309{
ed2dc618
SM
3310 struct dwarf2_per_objfile *dwarf2_per_objfile
3311 = get_dwarf2_per_objfile (objfile);
7b9f3c50 3312
5989a64e 3313 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3314 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3315}
3316
f8eba3c6
TT
3317/* Helper function for dw2_map_symtabs_matching_filename that expands
3318 the symtabs and calls the iterator. */
3319
3320static int
3321dw2_map_expand_apply (struct objfile *objfile,
3322 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3323 const char *name, const char *real_path,
14bc53a8 3324 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3325{
43f3e411 3326 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3327
3328 /* Don't visit already-expanded CUs. */
af758d11
SM
3329 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3330 if (per_objfile->symtab_set_p (per_cu))
f8eba3c6
TT
3331 return 0;
3332
3333 /* This may expand more than one symtab, and we want to iterate over
3334 all of them. */
97a1449a 3335 dw2_instantiate_symtab (per_cu, per_objfile, false);
f8eba3c6 3336
14bc53a8
PA
3337 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3338 last_made, callback);
f8eba3c6
TT
3339}
3340
3341/* Implementation of the map_symtabs_matching_filename method. */
3342
14bc53a8
PA
3343static bool
3344dw2_map_symtabs_matching_filename
3345 (struct objfile *objfile, const char *name, const char *real_path,
3346 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3347{
c011a4f4 3348 const char *name_basename = lbasename (name);
ed2dc618
SM
3349 struct dwarf2_per_objfile *dwarf2_per_objfile
3350 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3351
848e3e78
DE
3352 /* The rule is CUs specify all the files, including those used by
3353 any TU, so there's no need to scan TUs here. */
f4dc4d17 3354
5989a64e 3355 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
9291a0cd 3356 {
3d7bb9d9 3357 /* We only need to look at symtabs not already expanded. */
af758d11 3358 if (dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3359 continue;
3360
ab432490
SM
3361 quick_file_names *file_data
3362 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
7b9f3c50 3363 if (file_data == NULL)
9291a0cd
TT
3364 continue;
3365
b76e467d 3366 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3367 {
7b9f3c50 3368 const char *this_name = file_data->file_names[j];
da235a7c 3369 const char *this_real_name;
9291a0cd 3370
af529f8f 3371 if (compare_filenames_for_search (this_name, name))
9291a0cd 3372 {
f5b95b50 3373 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3374 callback))
3375 return true;
288e77a7 3376 continue;
4aac40c8 3377 }
9291a0cd 3378
c011a4f4
DE
3379 /* Before we invoke realpath, which can get expensive when many
3380 files are involved, do a quick comparison of the basenames. */
3381 if (! basenames_may_differ
3382 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3383 continue;
3384
45940949
TT
3385 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
3386 file_data, j);
da235a7c 3387 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3388 {
da235a7c 3389 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3390 callback))
3391 return true;
288e77a7 3392 continue;
da235a7c 3393 }
9291a0cd 3394
da235a7c
JK
3395 if (real_path != NULL)
3396 {
af529f8f
JK
3397 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3398 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3399 if (this_real_name != NULL
af529f8f 3400 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3401 {
f5b95b50 3402 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3403 callback))
3404 return true;
288e77a7 3405 continue;
9291a0cd
TT
3406 }
3407 }
3408 }
3409 }
3410
14bc53a8 3411 return false;
9291a0cd
TT
3412}
3413
da51c347
DE
3414/* Struct used to manage iterating over all CUs looking for a symbol. */
3415
3416struct dw2_symtab_iterator
9291a0cd 3417{
ed2dc618
SM
3418 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3419 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3420 /* If set, only look for symbols that match that block. Valid values are
3421 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3422 gdb::optional<block_enum> block_index;
da51c347
DE
3423 /* The kind of symbol we're looking for. */
3424 domain_enum domain;
3425 /* The list of CUs from the index entry of the symbol,
3426 or NULL if not found. */
3427 offset_type *vec;
3428 /* The next element in VEC to look at. */
3429 int next;
3430 /* The number of elements in VEC, or zero if there is no match. */
3431 int length;
8943b874
DE
3432 /* Have we seen a global version of the symbol?
3433 If so we can ignore all further global instances.
3434 This is to work around gold/15646, inefficient gold-generated
3435 indices. */
3436 int global_seen;
da51c347 3437};
9291a0cd 3438
2b79f376 3439/* Initialize the index symtab iterator ITER. */
2fdf6df6 3440
9291a0cd 3441static void
da51c347 3442dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3443 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3444 gdb::optional<block_enum> block_index,
da51c347
DE
3445 domain_enum domain,
3446 const char *name)
3447{
ed2dc618 3448 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3449 iter->block_index = block_index;
3450 iter->domain = domain;
3451 iter->next = 0;
8943b874 3452 iter->global_seen = 0;
da51c347 3453
5989a64e 3454 mapped_index *index = dwarf2_per_objfile->per_bfd->index_table.get ();
ed2dc618
SM
3455
3456 /* index is NULL if OBJF_READNOW. */
3457 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3458 iter->length = MAYBE_SWAP (*iter->vec);
3459 else
3460 {
3461 iter->vec = NULL;
3462 iter->length = 0;
3463 }
3464}
3465
3466/* Return the next matching CU or NULL if there are no more. */
3467
3468static struct dwarf2_per_cu_data *
3469dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3470{
ed2dc618
SM
3471 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3472
da51c347
DE
3473 for ( ; iter->next < iter->length; ++iter->next)
3474 {
3475 offset_type cu_index_and_attrs =
3476 MAYBE_SWAP (iter->vec[iter->next + 1]);
3477 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3478 gdb_index_symbol_kind symbol_kind =
3479 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3480 /* Only check the symbol attributes if they're present.
3481 Indices prior to version 7 don't record them,
3482 and indices >= 7 may elide them for certain symbols
3483 (gold does this). */
3484 int attrs_valid =
5989a64e 3485 (dwarf2_per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3486 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3487
3190f0c6 3488 /* Don't crash on bad data. */
5989a64e
SM
3489 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3490 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3491 {
b98664d3 3492 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3493 " [in module %s]"),
3494 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3495 continue;
3496 }
3497
5989a64e 3498 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3499
da51c347 3500 /* Skip if already read in. */
af758d11 3501 if (dwarf2_per_objfile->symtab_set_p (per_cu))
da51c347
DE
3502 continue;
3503
8943b874
DE
3504 /* Check static vs global. */
3505 if (attrs_valid)
3506 {
2b79f376
SM
3507 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3508
3509 if (iter->block_index.has_value ())
3510 {
3511 bool want_static = *iter->block_index == STATIC_BLOCK;
3512
3513 if (is_static != want_static)
3514 continue;
3515 }
3516
8943b874
DE
3517 /* Work around gold/15646. */
3518 if (!is_static && iter->global_seen)
3519 continue;
3520 if (!is_static)
3521 iter->global_seen = 1;
3522 }
da51c347
DE
3523
3524 /* Only check the symbol's kind if it has one. */
3525 if (attrs_valid)
3526 {
3527 switch (iter->domain)
3528 {
3529 case VAR_DOMAIN:
3530 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3531 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3532 /* Some types are also in VAR_DOMAIN. */
3533 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3534 continue;
3535 break;
3536 case STRUCT_DOMAIN:
3537 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3538 continue;
3539 break;
3540 case LABEL_DOMAIN:
3541 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3542 continue;
3543 break;
59c35742
AB
3544 case MODULE_DOMAIN:
3545 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3546 continue;
3547 break;
da51c347
DE
3548 default:
3549 break;
3550 }
3551 }
3552
3553 ++iter->next;
3554 return per_cu;
3555 }
3556
3557 return NULL;
3558}
3559
43f3e411 3560static struct compunit_symtab *
c7f839cb 3561dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3562 const char *name, domain_enum domain)
9291a0cd 3563{
43f3e411 3564 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3565 struct dwarf2_per_objfile *dwarf2_per_objfile
3566 = get_dwarf2_per_objfile (objfile);
9291a0cd 3567
b5ec771e
PA
3568 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3569
ed2dc618
SM
3570 struct dw2_symtab_iterator iter;
3571 struct dwarf2_per_cu_data *per_cu;
da51c347 3572
2b79f376 3573 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3574
ed2dc618
SM
3575 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3576 {
3577 struct symbol *sym, *with_opaque = NULL;
97a1449a
SM
3578 struct compunit_symtab *stab
3579 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
ed2dc618 3580 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3581 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3582
ed2dc618
SM
3583 sym = block_find_symbol (block, name, domain,
3584 block_find_non_opaque_type_preferred,
3585 &with_opaque);
b2e2f908 3586
ed2dc618
SM
3587 /* Some caution must be observed with overloaded functions
3588 and methods, since the index will not contain any overload
3589 information (but NAME might contain it). */
da51c347 3590
ed2dc618
SM
3591 if (sym != NULL
3592 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3593 return stab;
3594 if (with_opaque != NULL
3595 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3596 stab_best = stab;
da51c347 3597
ed2dc618 3598 /* Keep looking through other CUs. */
9291a0cd 3599 }
9291a0cd 3600
da51c347 3601 return stab_best;
9291a0cd
TT
3602}
3603
3604static void
3605dw2_print_stats (struct objfile *objfile)
3606{
ed2dc618
SM
3607 struct dwarf2_per_objfile *dwarf2_per_objfile
3608 = get_dwarf2_per_objfile (objfile);
5989a64e
SM
3609 int total = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3610 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3611 int count = 0;
9291a0cd 3612
ed2dc618 3613 for (int i = 0; i < total; ++i)
9291a0cd 3614 {
5989a64e 3615 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 3616
af758d11 3617 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3618 ++count;
3619 }
e4a48d9d 3620 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3621 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3622}
3623
779bd270
DE
3624/* This dumps minimal information about the index.
3625 It is called via "mt print objfiles".
3626 One use is to verify .gdb_index has been loaded by the
3627 gdb.dwarf2/gdb-index.exp testcase. */
3628
9291a0cd
TT
3629static void
3630dw2_dump (struct objfile *objfile)
3631{
ed2dc618
SM
3632 struct dwarf2_per_objfile *dwarf2_per_objfile
3633 = get_dwarf2_per_objfile (objfile);
3634
5989a64e 3635 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
779bd270 3636 printf_filtered (".gdb_index:");
5989a64e 3637 if (dwarf2_per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3638 {
3639 printf_filtered (" version %d\n",
5989a64e 3640 dwarf2_per_objfile->per_bfd->index_table->version);
779bd270
DE
3641 }
3642 else
3643 printf_filtered (" faked for \"readnow\"\n");
3644 printf_filtered ("\n");
9291a0cd
TT
3645}
3646
9291a0cd
TT
3647static void
3648dw2_expand_symtabs_for_function (struct objfile *objfile,
3649 const char *func_name)
3650{
ed2dc618
SM
3651 struct dwarf2_per_objfile *dwarf2_per_objfile
3652 = get_dwarf2_per_objfile (objfile);
da51c347 3653
ed2dc618
SM
3654 struct dw2_symtab_iterator iter;
3655 struct dwarf2_per_cu_data *per_cu;
da51c347 3656
2b79f376 3657 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3658
ed2dc618 3659 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
97a1449a 3660 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
da51c347 3661
9291a0cd
TT
3662}
3663
3664static void
3665dw2_expand_all_symtabs (struct objfile *objfile)
3666{
ed2dc618
SM
3667 struct dwarf2_per_objfile *dwarf2_per_objfile
3668 = get_dwarf2_per_objfile (objfile);
5989a64e
SM
3669 int total_units = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3670 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3671
ed2dc618 3672 for (int i = 0; i < total_units; ++i)
9291a0cd 3673 {
5989a64e 3674 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 3675
58f0c718
TT
3676 /* We don't want to directly expand a partial CU, because if we
3677 read it with the wrong language, then assertion failures can
3678 be triggered later on. See PR symtab/23010. So, tell
3679 dw2_instantiate_symtab to skip partial CUs -- any important
3680 partial CU will be read via DW_TAG_imported_unit anyway. */
97a1449a 3681 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, true);
9291a0cd
TT
3682 }
3683}
3684
3685static void
652a8996
JK
3686dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3687 const char *fullname)
9291a0cd 3688{
ed2dc618
SM
3689 struct dwarf2_per_objfile *dwarf2_per_objfile
3690 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3691
3692 /* We don't need to consider type units here.
3693 This is only called for examining code, e.g. expand_line_sal.
3694 There can be an order of magnitude (or more) more type units
3695 than comp units, and we avoid them if we can. */
3696
5989a64e 3697 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
9291a0cd 3698 {
3d7bb9d9 3699 /* We only need to look at symtabs not already expanded. */
af758d11 3700 if (dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3701 continue;
3702
ab432490
SM
3703 quick_file_names *file_data
3704 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
7b9f3c50 3705 if (file_data == NULL)
9291a0cd
TT
3706 continue;
3707
b76e467d 3708 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3709 {
652a8996
JK
3710 const char *this_fullname = file_data->file_names[j];
3711
3712 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3713 {
97a1449a 3714 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
9291a0cd
TT
3715 break;
3716 }
3717 }
3718 }
3719}
3720
9a0bacfb
TV
3721static void
3722dw2_expand_symtabs_matching_symbol
3723 (mapped_index_base &index,
3724 const lookup_name_info &lookup_name_in,
3725 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3726 enum search_domain kind,
fcf23d5b
SM
3727 gdb::function_view<bool (offset_type)> match_callback,
3728 dwarf2_per_objfile *per_objfile);
9a0bacfb
TV
3729
3730static void
3731dw2_expand_symtabs_matching_one
97a1449a
SM
3732 (dwarf2_per_cu_data *per_cu,
3733 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3734 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3735 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3736
9291a0cd 3737static void
199b4314
TT
3738dw2_map_matching_symbols
3739 (struct objfile *objfile,
b054970d 3740 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3741 int global,
3742 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3743 symbol_compare_ftype *ordered_compare)
9291a0cd 3744{
1aa98955
TV
3745 /* Used for Ada. */
3746 struct dwarf2_per_objfile *dwarf2_per_objfile
3747 = get_dwarf2_per_objfile (objfile);
3748
9a0bacfb
TV
3749 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3750
5989a64e 3751 if (dwarf2_per_objfile->per_bfd->index_table != nullptr)
1aa98955
TV
3752 {
3753 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3754 here though if the current language is Ada for a non-Ada objfile
9a0bacfb 3755 using GNU index. */
5989a64e 3756 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
1aa98955 3757
9a0bacfb
TV
3758 const char *match_name = name.ada ().lookup_name ().c_str ();
3759 auto matcher = [&] (const char *symname)
3760 {
3761 if (ordered_compare == nullptr)
3762 return true;
3763 return ordered_compare (symname, match_name) == 0;
3764 };
3765
3766 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3767 [&] (offset_type namei)
3768 {
3769 struct dw2_symtab_iterator iter;
3770 struct dwarf2_per_cu_data *per_cu;
3771
3772 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3773 match_name);
3774 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
97a1449a
SM
3775 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
3776 nullptr);
9a0bacfb 3777 return true;
fcf23d5b 3778 }, dwarf2_per_objfile);
9a0bacfb
TV
3779 }
3780 else
3781 {
3782 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3783 proceed assuming all symtabs have been read in. */
3784 }
1aa98955
TV
3785
3786 for (compunit_symtab *cust : objfile->compunits ())
3787 {
3788 const struct block *block;
3789
3790 if (cust == NULL)
3791 continue;
3792 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3793 if (!iterate_over_symbols_terminated (block, name,
3794 domain, callback))
3795 return;
3796 }
9291a0cd
TT
3797}
3798
e1ef7d7a
PA
3799/* Starting from a search name, return the string that finds the upper
3800 bound of all strings that start with SEARCH_NAME in a sorted name
3801 list. Returns the empty string to indicate that the upper bound is
3802 the end of the list. */
3803
3804static std::string
3805make_sort_after_prefix_name (const char *search_name)
3806{
3807 /* When looking to complete "func", we find the upper bound of all
3808 symbols that start with "func" by looking for where we'd insert
3809 the closest string that would follow "func" in lexicographical
3810 order. Usually, that's "func"-with-last-character-incremented,
3811 i.e. "fund". Mind non-ASCII characters, though. Usually those
3812 will be UTF-8 multi-byte sequences, but we can't be certain.
3813 Especially mind the 0xff character, which is a valid character in
3814 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3815 rule out compilers allowing it in identifiers. Note that
3816 conveniently, strcmp/strcasecmp are specified to compare
3817 characters interpreted as unsigned char. So what we do is treat
3818 the whole string as a base 256 number composed of a sequence of
3819 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3820 to 0, and carries 1 to the following more-significant position.
3821 If the very first character in SEARCH_NAME ends up incremented
3822 and carries/overflows, then the upper bound is the end of the
3823 list. The string after the empty string is also the empty
3824 string.
3825
3826 Some examples of this operation:
3827
3828 SEARCH_NAME => "+1" RESULT
3829
3830 "abc" => "abd"
3831 "ab\xff" => "ac"
3832 "\xff" "a" "\xff" => "\xff" "b"
3833 "\xff" => ""
3834 "\xff\xff" => ""
3835 "" => ""
3836
3837 Then, with these symbols for example:
3838
3839 func
3840 func1
3841 fund
3842
3843 completing "func" looks for symbols between "func" and
3844 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3845 which finds "func" and "func1", but not "fund".
3846
3847 And with:
3848
3849 funcÿ (Latin1 'ÿ' [0xff])
3850 funcÿ1
3851 fund
3852
3853 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3854 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3855
3856 And with:
3857
3858 ÿÿ (Latin1 'ÿ' [0xff])
3859 ÿÿ1
3860
3861 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3862 the end of the list.
3863 */
3864 std::string after = search_name;
3865 while (!after.empty () && (unsigned char) after.back () == 0xff)
3866 after.pop_back ();
3867 if (!after.empty ())
3868 after.back () = (unsigned char) after.back () + 1;
3869 return after;
3870}
3871
5c58de74 3872/* See declaration. */
61d96d7e 3873
5c58de74
PA
3874std::pair<std::vector<name_component>::const_iterator,
3875 std::vector<name_component>::const_iterator>
44ed8f3e 3876mapped_index_base::find_name_components_bounds
fcf23d5b
SM
3877 (const lookup_name_info &lookup_name_without_params, language lang,
3878 dwarf2_per_objfile *per_objfile) const
3f563c84 3879{
5c58de74
PA
3880 auto *name_cmp
3881 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3882
3b00ef10 3883 const char *lang_name
e0802d59 3884 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3885
3f563c84
PA
3886 /* Comparison function object for lower_bound that matches against a
3887 given symbol name. */
3888 auto lookup_compare_lower = [&] (const name_component &elem,
3889 const char *name)
3890 {
fcf23d5b 3891 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3892 const char *elem_name = elem_qualified + elem.name_offset;
3893 return name_cmp (elem_name, name) < 0;
3894 };
3895
3896 /* Comparison function object for upper_bound that matches against a
3897 given symbol name. */
3898 auto lookup_compare_upper = [&] (const char *name,
3899 const name_component &elem)
3900 {
fcf23d5b 3901 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3902 const char *elem_name = elem_qualified + elem.name_offset;
3903 return name_cmp (name, elem_name) < 0;
3904 };
3905
5c58de74
PA
3906 auto begin = this->name_components.begin ();
3907 auto end = this->name_components.end ();
3f563c84
PA
3908
3909 /* Find the lower bound. */
3910 auto lower = [&] ()
3911 {
3b00ef10 3912 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3913 return begin;
3914 else
3b00ef10 3915 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3916 } ();
3917
3918 /* Find the upper bound. */
3919 auto upper = [&] ()
3920 {
5c58de74 3921 if (lookup_name_without_params.completion_mode ())
3f563c84 3922 {
e1ef7d7a
PA
3923 /* In completion mode, we want UPPER to point past all
3924 symbols names that have the same prefix. I.e., with
3925 these symbols, and completing "func":
3926
3927 function << lower bound
3928 function1
3929 other_function << upper bound
3930
3931 We find the upper bound by looking for the insertion
3932 point of "func"-with-last-character-incremented,
3933 i.e. "fund". */
3b00ef10 3934 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3935 if (after.empty ())
3f563c84 3936 return end;
e6b2f5ef
PA
3937 return std::lower_bound (lower, end, after.c_str (),
3938 lookup_compare_lower);
3f563c84
PA
3939 }
3940 else
3b00ef10 3941 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3942 } ();
3943
5c58de74
PA
3944 return {lower, upper};
3945}
3946
3947/* See declaration. */
3948
3949void
fcf23d5b 3950mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
5c58de74
PA
3951{
3952 if (!this->name_components.empty ())
3953 return;
3954
3955 this->name_components_casing = case_sensitivity;
3956 auto *name_cmp
3957 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3958
3959 /* The code below only knows how to break apart components of C++
3960 symbol names (and other languages that use '::' as
3b00ef10 3961 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3962 auto count = this->symbol_name_count ();
3963 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3964 {
44ed8f3e 3965 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3966 continue;
3967
fcf23d5b 3968 const char *name = this->symbol_name_at (idx, per_objfile);
5c58de74
PA
3969
3970 /* Add each name component to the name component table. */
3971 unsigned int previous_len = 0;
3b00ef10
TT
3972
3973 if (strstr (name, "::") != nullptr)
3974 {
3975 for (unsigned int current_len = cp_find_first_component (name);
3976 name[current_len] != '\0';
3977 current_len += cp_find_first_component (name + current_len))
3978 {
3979 gdb_assert (name[current_len] == ':');
3980 this->name_components.push_back ({previous_len, idx});
3981 /* Skip the '::'. */
3982 current_len += 2;
3983 previous_len = current_len;
3984 }
3985 }
3986 else
5c58de74 3987 {
3b00ef10
TT
3988 /* Handle the Ada encoded (aka mangled) form here. */
3989 for (const char *iter = strstr (name, "__");
3990 iter != nullptr;
3991 iter = strstr (iter, "__"))
3992 {
3993 this->name_components.push_back ({previous_len, idx});
3994 iter += 2;
3995 previous_len = iter - name;
3996 }
5c58de74 3997 }
3b00ef10 3998
5c58de74
PA
3999 this->name_components.push_back ({previous_len, idx});
4000 }
4001
4002 /* Sort name_components elements by name. */
4003 auto name_comp_compare = [&] (const name_component &left,
4004 const name_component &right)
4005 {
fcf23d5b
SM
4006 const char *left_qualified
4007 = this->symbol_name_at (left.idx, per_objfile);
4008 const char *right_qualified
4009 = this->symbol_name_at (right.idx, per_objfile);
5c58de74
PA
4010
4011 const char *left_name = left_qualified + left.name_offset;
4012 const char *right_name = right_qualified + right.name_offset;
4013
4014 return name_cmp (left_name, right_name) < 0;
4015 };
4016
4017 std::sort (this->name_components.begin (),
4018 this->name_components.end (),
4019 name_comp_compare);
4020}
4021
4022/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4023 mapped_index_base instead of the containing objfile. This is split
4024 to a separate function in order to be able to unit test the
4025 name_components matching using a mock mapped_index_base. For each
5c58de74 4026 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4027 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4028
4029static void
4030dw2_expand_symtabs_matching_symbol
44ed8f3e 4031 (mapped_index_base &index,
5c58de74
PA
4032 const lookup_name_info &lookup_name_in,
4033 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4034 enum search_domain kind,
fcf23d5b
SM
4035 gdb::function_view<bool (offset_type)> match_callback,
4036 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4037{
4038 lookup_name_info lookup_name_without_params
4039 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4040
4041 /* Build the symbol name component sorted vector, if we haven't
4042 yet. */
fcf23d5b 4043 index.build_name_components (per_objfile);
5c58de74 4044
3f563c84
PA
4045 /* The same symbol may appear more than once in the range though.
4046 E.g., if we're looking for symbols that complete "w", and we have
4047 a symbol named "w1::w2", we'll find the two name components for
4048 that same symbol in the range. To be sure we only call the
4049 callback once per symbol, we first collect the symbol name
4050 indexes that matched in a temporary vector and ignore
4051 duplicates. */
4052 std::vector<offset_type> matches;
3f563c84 4053
3b00ef10
TT
4054 struct name_and_matcher
4055 {
4056 symbol_name_matcher_ftype *matcher;
ecc6c606 4057 const char *name;
3b00ef10
TT
4058
4059 bool operator== (const name_and_matcher &other) const
3f563c84 4060 {
ecc6c606 4061 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
4062 }
4063 };
4064
4065 /* A vector holding all the different symbol name matchers, for all
4066 languages. */
4067 std::vector<name_and_matcher> matchers;
4068
4069 for (int i = 0; i < nr_languages; i++)
4070 {
4071 enum language lang_e = (enum language) i;
4072
4073 const language_defn *lang = language_def (lang_e);
4074 symbol_name_matcher_ftype *name_matcher
4075 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4076
3b00ef10
TT
4077 name_and_matcher key {
4078 name_matcher,
4079 lookup_name_without_params.language_lookup_name (lang_e)
4080 };
4081
4082 /* Don't insert the same comparison routine more than once.
4083 Note that we do this linear walk. This is not a problem in
4084 practice because the number of supported languages is
4085 low. */
4086 if (std::find (matchers.begin (), matchers.end (), key)
4087 != matchers.end ())
9291a0cd 4088 continue;
3b00ef10
TT
4089 matchers.push_back (std::move (key));
4090
4091 auto bounds
4092 = index.find_name_components_bounds (lookup_name_without_params,
fcf23d5b 4093 lang_e, per_objfile);
3b00ef10
TT
4094
4095 /* Now for each symbol name in range, check to see if we have a name
4096 match, and if so, call the MATCH_CALLBACK callback. */
4097
4098 for (; bounds.first != bounds.second; ++bounds.first)
4099 {
fcf23d5b
SM
4100 const char *qualified
4101 = index.symbol_name_at (bounds.first->idx, per_objfile);
3b00ef10
TT
4102
4103 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4104 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4105 continue;
9291a0cd 4106
3b00ef10
TT
4107 matches.push_back (bounds.first->idx);
4108 }
3f563c84
PA
4109 }
4110
4111 std::sort (matches.begin (), matches.end ());
4112
4113 /* Finally call the callback, once per match. */
4114 ULONGEST prev = -1;
4115 for (offset_type idx : matches)
4116 {
4117 if (prev != idx)
4118 {
3b00ef10
TT
4119 if (!match_callback (idx))
4120 break;
3f563c84
PA
4121 prev = idx;
4122 }
4123 }
4124
4125 /* Above we use a type wider than idx's for 'prev', since 0 and
4126 (offset_type)-1 are both possible values. */
4127 static_assert (sizeof (prev) > sizeof (offset_type), "");
4128}
4129
c62446b1
PA
4130#if GDB_SELF_TEST
4131
4132namespace selftests { namespace dw2_expand_symtabs_matching {
4133
a3c5fafd
PA
4134/* A mock .gdb_index/.debug_names-like name index table, enough to
4135 exercise dw2_expand_symtabs_matching_symbol, which works with the
4136 mapped_index_base interface. Builds an index from the symbol list
4137 passed as parameter to the constructor. */
4138class mock_mapped_index : public mapped_index_base
c62446b1
PA
4139{
4140public:
a3c5fafd
PA
4141 mock_mapped_index (gdb::array_view<const char *> symbols)
4142 : m_symbol_table (symbols)
c62446b1
PA
4143 {}
4144
a3c5fafd 4145 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4146
a3c5fafd 4147 /* Return the number of names in the symbol table. */
632e107b 4148 size_t symbol_name_count () const override
c62446b1 4149 {
a3c5fafd 4150 return m_symbol_table.size ();
c62446b1
PA
4151 }
4152
a3c5fafd 4153 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
4154 const char *symbol_name_at
4155 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
a3c5fafd
PA
4156 {
4157 return m_symbol_table[idx];
4158 }
c62446b1 4159
a3c5fafd
PA
4160private:
4161 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4162};
4163
4164/* Convenience function that converts a NULL pointer to a "<null>"
4165 string, to pass to print routines. */
4166
4167static const char *
4168string_or_null (const char *str)
4169{
4170 return str != NULL ? str : "<null>";
4171}
4172
4173/* Check if a lookup_name_info built from
4174 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4175 index. EXPECTED_LIST is the list of expected matches, in expected
4176 matching order. If no match expected, then an empty list is
4177 specified. Returns true on success. On failure prints a warning
4178 indicating the file:line that failed, and returns false. */
4179
4180static bool
4181check_match (const char *file, int line,
4182 mock_mapped_index &mock_index,
4183 const char *name, symbol_name_match_type match_type,
4184 bool completion_mode,
fcf23d5b
SM
4185 std::initializer_list<const char *> expected_list,
4186 dwarf2_per_objfile *per_objfile)
c62446b1
PA
4187{
4188 lookup_name_info lookup_name (name, match_type, completion_mode);
4189
4190 bool matched = true;
4191
4192 auto mismatch = [&] (const char *expected_str,
4193 const char *got)
4194 {
4195 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4196 "expected=\"%s\", got=\"%s\"\n"),
4197 file, line,
4198 (match_type == symbol_name_match_type::FULL
4199 ? "FULL" : "WILD"),
4200 name, string_or_null (expected_str), string_or_null (got));
4201 matched = false;
4202 };
4203
4204 auto expected_it = expected_list.begin ();
4205 auto expected_end = expected_list.end ();
4206
a3c5fafd 4207 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4208 NULL, ALL_DOMAIN,
4209 [&] (offset_type idx)
4210 {
fcf23d5b 4211 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
c62446b1
PA
4212 const char *expected_str
4213 = expected_it == expected_end ? NULL : *expected_it++;
4214
4215 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4216 mismatch (expected_str, matched_name);
3b00ef10 4217 return true;
fcf23d5b 4218 }, per_objfile);
c62446b1
PA
4219
4220 const char *expected_str
4221 = expected_it == expected_end ? NULL : *expected_it++;
4222 if (expected_str != NULL)
4223 mismatch (expected_str, NULL);
4224
4225 return matched;
4226}
4227
4228/* The symbols added to the mock mapped_index for testing (in
4229 canonical form). */
4230static const char *test_symbols[] = {
4231 "function",
4232 "std::bar",
4233 "std::zfunction",
4234 "std::zfunction2",
4235 "w1::w2",
4236 "ns::foo<char*>",
4237 "ns::foo<int>",
4238 "ns::foo<long>",
a20714ff
PA
4239 "ns2::tmpl<int>::foo2",
4240 "(anonymous namespace)::A::B::C",
c62446b1 4241
e1ef7d7a
PA
4242 /* These are used to check that the increment-last-char in the
4243 matching algorithm for completion doesn't match "t1_fund" when
4244 completing "t1_func". */
4245 "t1_func",
4246 "t1_func1",
4247 "t1_fund",
4248 "t1_fund1",
4249
4250 /* A UTF-8 name with multi-byte sequences to make sure that
4251 cp-name-parser understands this as a single identifier ("função"
4252 is "function" in PT). */
4253 u8"u8função",
4254
4255 /* \377 (0xff) is Latin1 'ÿ'. */
4256 "yfunc\377",
4257
4258 /* \377 (0xff) is Latin1 'ÿ'. */
4259 "\377",
4260 "\377\377123",
4261
c62446b1
PA
4262 /* A name with all sorts of complications. Starts with "z" to make
4263 it easier for the completion tests below. */
4264#define Z_SYM_NAME \
4265 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4266 "::tuple<(anonymous namespace)::ui*, " \
4267 "std::default_delete<(anonymous namespace)::ui>, void>"
4268
4269 Z_SYM_NAME
4270};
4271
a3c5fafd
PA
4272/* Returns true if the mapped_index_base::find_name_component_bounds
4273 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4274 in completion mode. */
5c58de74
PA
4275
4276static bool
a3c5fafd 4277check_find_bounds_finds (mapped_index_base &index,
5c58de74 4278 const char *search_name,
fcf23d5b
SM
4279 gdb::array_view<const char *> expected_syms,
4280 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4281{
4282 lookup_name_info lookup_name (search_name,
4283 symbol_name_match_type::FULL, true);
4284
3b00ef10 4285 auto bounds = index.find_name_components_bounds (lookup_name,
fcf23d5b
SM
4286 language_cplus,
4287 per_objfile);
5c58de74
PA
4288
4289 size_t distance = std::distance (bounds.first, bounds.second);
4290 if (distance != expected_syms.size ())
4291 return false;
4292
4293 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4294 {
4295 auto nc_elem = bounds.first + exp_elem;
fcf23d5b 4296 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
5c58de74
PA
4297 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4298 return false;
4299 }
4300
4301 return true;
4302}
4303
4304/* Test the lower-level mapped_index::find_name_component_bounds
4305 method. */
4306
c62446b1 4307static void
5c58de74
PA
4308test_mapped_index_find_name_component_bounds ()
4309{
4310 mock_mapped_index mock_index (test_symbols);
4311
fcf23d5b 4312 mock_index.build_name_components (NULL /* per_objfile */);
5c58de74
PA
4313
4314 /* Test the lower-level mapped_index::find_name_component_bounds
4315 method in completion mode. */
4316 {
4317 static const char *expected_syms[] = {
4318 "t1_func",
4319 "t1_func1",
5c58de74
PA
4320 };
4321
fcf23d5b
SM
4322 SELF_CHECK (check_find_bounds_finds
4323 (mock_index, "t1_func", expected_syms,
4324 NULL /* per_objfile */));
5c58de74
PA
4325 }
4326
4327 /* Check that the increment-last-char in the name matching algorithm
4328 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4329 {
4330 static const char *expected_syms1[] = {
4331 "\377",
4332 "\377\377123",
4333 };
fcf23d5b
SM
4334 SELF_CHECK (check_find_bounds_finds
4335 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
5c58de74
PA
4336
4337 static const char *expected_syms2[] = {
4338 "\377\377123",
4339 };
fcf23d5b
SM
4340 SELF_CHECK (check_find_bounds_finds
4341 (mock_index, "\377\377", expected_syms2,
4342 NULL /* per_objfile */));
5c58de74
PA
4343 }
4344}
4345
4346/* Test dw2_expand_symtabs_matching_symbol. */
4347
4348static void
4349test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4350{
4351 mock_mapped_index mock_index (test_symbols);
4352
4353 /* We let all tests run until the end even if some fails, for debug
4354 convenience. */
4355 bool any_mismatch = false;
4356
4357 /* Create the expected symbols list (an initializer_list). Needed
4358 because lists have commas, and we need to pass them to CHECK,
4359 which is a macro. */
4360#define EXPECT(...) { __VA_ARGS__ }
4361
4362 /* Wrapper for check_match that passes down the current
4363 __FILE__/__LINE__. */
4364#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4365 any_mismatch |= !check_match (__FILE__, __LINE__, \
4366 mock_index, \
4367 NAME, MATCH_TYPE, COMPLETION_MODE, \
fcf23d5b 4368 EXPECTED_LIST, NULL)
c62446b1
PA
4369
4370 /* Identity checks. */
4371 for (const char *sym : test_symbols)
4372 {
4373 /* Should be able to match all existing symbols. */
4374 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4375 EXPECT (sym));
4376
4377 /* Should be able to match all existing symbols with
4378 parameters. */
4379 std::string with_params = std::string (sym) + "(int)";
4380 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4381 EXPECT (sym));
4382
4383 /* Should be able to match all existing symbols with
4384 parameters and qualifiers. */
4385 with_params = std::string (sym) + " ( int ) const";
4386 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4387 EXPECT (sym));
4388
4389 /* This should really find sym, but cp-name-parser.y doesn't
4390 know about lvalue/rvalue qualifiers yet. */
4391 with_params = std::string (sym) + " ( int ) &&";
4392 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4393 {});
4394 }
4395
e1ef7d7a
PA
4396 /* Check that the name matching algorithm for completion doesn't get
4397 confused with Latin1 'ÿ' / 0xff. */
4398 {
4399 static const char str[] = "\377";
4400 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4401 EXPECT ("\377", "\377\377123"));
4402 }
4403
4404 /* Check that the increment-last-char in the matching algorithm for
4405 completion doesn't match "t1_fund" when completing "t1_func". */
4406 {
4407 static const char str[] = "t1_func";
4408 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4409 EXPECT ("t1_func", "t1_func1"));
4410 }
4411
c62446b1
PA
4412 /* Check that completion mode works at each prefix of the expected
4413 symbol name. */
4414 {
4415 static const char str[] = "function(int)";
4416 size_t len = strlen (str);
4417 std::string lookup;
4418
4419 for (size_t i = 1; i < len; i++)
4420 {
4421 lookup.assign (str, i);
4422 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4423 EXPECT ("function"));
4424 }
4425 }
4426
4427 /* While "w" is a prefix of both components, the match function
4428 should still only be called once. */
4429 {
4430 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4431 EXPECT ("w1::w2"));
a20714ff
PA
4432 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4433 EXPECT ("w1::w2"));
c62446b1
PA
4434 }
4435
4436 /* Same, with a "complicated" symbol. */
4437 {
4438 static const char str[] = Z_SYM_NAME;
4439 size_t len = strlen (str);
4440 std::string lookup;
4441
4442 for (size_t i = 1; i < len; i++)
4443 {
4444 lookup.assign (str, i);
4445 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4446 EXPECT (Z_SYM_NAME));
4447 }
4448 }
4449
4450 /* In FULL mode, an incomplete symbol doesn't match. */
4451 {
4452 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4453 {});
4454 }
4455
4456 /* A complete symbol with parameters matches any overload, since the
4457 index has no overload info. */
4458 {
4459 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4460 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4461 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4462 EXPECT ("std::zfunction", "std::zfunction2"));
4463 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4464 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4465 }
4466
4467 /* Check that whitespace is ignored appropriately. A symbol with a
4468 template argument list. */
4469 {
4470 static const char expected[] = "ns::foo<int>";
4471 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4472 EXPECT (expected));
a20714ff
PA
4473 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4474 EXPECT (expected));
c62446b1
PA
4475 }
4476
4477 /* Check that whitespace is ignored appropriately. A symbol with a
4478 template argument list that includes a pointer. */
4479 {
4480 static const char expected[] = "ns::foo<char*>";
4481 /* Try both completion and non-completion modes. */
4482 static const bool completion_mode[2] = {false, true};
4483 for (size_t i = 0; i < 2; i++)
4484 {
4485 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4486 completion_mode[i], EXPECT (expected));
a20714ff
PA
4487 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4488 completion_mode[i], EXPECT (expected));
c62446b1
PA
4489
4490 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4491 completion_mode[i], EXPECT (expected));
a20714ff
PA
4492 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4493 completion_mode[i], EXPECT (expected));
c62446b1
PA
4494 }
4495 }
4496
4497 {
4498 /* Check method qualifiers are ignored. */
4499 static const char expected[] = "ns::foo<char*>";
4500 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4501 symbol_name_match_type::FULL, true, EXPECT (expected));
4502 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4503 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4504 CHECK_MATCH ("foo < char * > ( int ) const",
4505 symbol_name_match_type::WILD, true, EXPECT (expected));
4506 CHECK_MATCH ("foo < char * > ( int ) &&",
4507 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4508 }
4509
4510 /* Test lookup names that don't match anything. */
4511 {
a20714ff
PA
4512 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4513 {});
4514
c62446b1
PA
4515 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4516 {});
4517 }
4518
a20714ff
PA
4519 /* Some wild matching tests, exercising "(anonymous namespace)",
4520 which should not be confused with a parameter list. */
4521 {
4522 static const char *syms[] = {
4523 "A::B::C",
4524 "B::C",
4525 "C",
4526 "A :: B :: C ( int )",
4527 "B :: C ( int )",
4528 "C ( int )",
4529 };
4530
4531 for (const char *s : syms)
4532 {
4533 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4534 EXPECT ("(anonymous namespace)::A::B::C"));
4535 }
4536 }
4537
4538 {
4539 static const char expected[] = "ns2::tmpl<int>::foo2";
4540 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4541 EXPECT (expected));
4542 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4543 EXPECT (expected));
4544 }
4545
c62446b1
PA
4546 SELF_CHECK (!any_mismatch);
4547
4548#undef EXPECT
4549#undef CHECK_MATCH
4550}
4551
5c58de74
PA
4552static void
4553run_test ()
4554{
4555 test_mapped_index_find_name_component_bounds ();
4556 test_dw2_expand_symtabs_matching_symbol ();
4557}
4558
c62446b1
PA
4559}} // namespace selftests::dw2_expand_symtabs_matching
4560
4561#endif /* GDB_SELF_TEST */
4562
4b514bc8
JK
4563/* If FILE_MATCHER is NULL or if PER_CU has
4564 dwarf2_per_cu_quick_data::MARK set (see
4565 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4566 EXPANSION_NOTIFY on it. */
4567
4568static void
4569dw2_expand_symtabs_matching_one
97a1449a
SM
4570 (dwarf2_per_cu_data *per_cu,
4571 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4572 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4573 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4574{
4575 if (file_matcher == NULL || per_cu->v.quick->mark)
4576 {
af758d11 4577 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4578
97a1449a
SM
4579 compunit_symtab *symtab
4580 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4581 gdb_assert (symtab != nullptr);
4b514bc8 4582
af758d11
SM
4583 if (expansion_notify != NULL && symtab_was_null)
4584 expansion_notify (symtab);
4b514bc8
JK
4585 }
4586}
4587
3f563c84
PA
4588/* Helper for dw2_expand_matching symtabs. Called on each symbol
4589 matched, to expand corresponding CUs that were marked. IDX is the
4590 index of the symbol name that matched. */
4591
4592static void
4593dw2_expand_marked_cus
97a1449a 4594 (dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4595 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4596 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4597 search_domain kind)
4598{
3f563c84
PA
4599 offset_type *vec, vec_len, vec_idx;
4600 bool global_seen = false;
5989a64e 4601 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
3f563c84 4602
61920122 4603 vec = (offset_type *) (index.constant_pool
f00a2de2 4604 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4605 vec_len = MAYBE_SWAP (vec[0]);
4606 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4607 {
61920122
PA
4608 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4609 /* This value is only valid for index versions >= 7. */
4610 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4611 gdb_index_symbol_kind symbol_kind =
4612 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4613 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4614 /* Only check the symbol attributes if they're present.
4615 Indices prior to version 7 don't record them,
4616 and indices >= 7 may elide them for certain symbols
4617 (gold does this). */
4618 int attrs_valid =
4619 (index.version >= 7
4620 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4621
4622 /* Work around gold/15646. */
4623 if (attrs_valid)
9291a0cd 4624 {
61920122
PA
4625 if (!is_static && global_seen)
4626 continue;
4627 if (!is_static)
4628 global_seen = true;
4629 }
3190f0c6 4630
61920122
PA
4631 /* Only check the symbol's kind if it has one. */
4632 if (attrs_valid)
4633 {
4634 switch (kind)
8943b874 4635 {
61920122
PA
4636 case VARIABLES_DOMAIN:
4637 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4638 continue;
4639 break;
4640 case FUNCTIONS_DOMAIN:
4641 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4642 continue;
61920122
PA
4643 break;
4644 case TYPES_DOMAIN:
4645 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4646 continue;
4647 break;
59c35742
AB
4648 case MODULES_DOMAIN:
4649 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4650 continue;
4651 break;
61920122
PA
4652 default:
4653 break;
8943b874 4654 }
61920122 4655 }
8943b874 4656
61920122 4657 /* Don't crash on bad data. */
5989a64e
SM
4658 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
4659 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
61920122 4660 {
b98664d3 4661 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4662 " [in module %s]"),
4663 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4664 continue;
4665 }
4666
5989a64e 4667 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
97a1449a 4668 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, file_matcher,
4b514bc8 4669 expansion_notify);
61920122
PA
4670 }
4671}
4672
4b514bc8
JK
4673/* If FILE_MATCHER is non-NULL, set all the
4674 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4675 that match FILE_MATCHER. */
4676
61920122 4677static void
4b514bc8 4678dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4679 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4680 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4681{
4b514bc8 4682 if (file_matcher == NULL)
61920122
PA
4683 return;
4684
4b514bc8
JK
4685 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4686 htab_eq_pointer,
4687 NULL, xcalloc, xfree));
4688 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4689 htab_eq_pointer,
4690 NULL, xcalloc, xfree));
61920122 4691
4b514bc8
JK
4692 /* The rule is CUs specify all the files, including those used by
4693 any TU, so there's no need to scan TUs here. */
61920122 4694
5989a64e 4695 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4696 {
927aa2e7
JK
4697 QUIT;
4698
4699 per_cu->v.quick->mark = 0;
4700
4701 /* We only need to look at symtabs not already expanded. */
af758d11 4702 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4703 continue;
4704
ab432490
SM
4705 quick_file_names *file_data
4706 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
927aa2e7
JK
4707 if (file_data == NULL)
4708 continue;
4709
4710 if (htab_find (visited_not_found.get (), file_data) != NULL)
4711 continue;
4712 else if (htab_find (visited_found.get (), file_data) != NULL)
4713 {
4714 per_cu->v.quick->mark = 1;
4715 continue;
4716 }
4717
b76e467d 4718 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4719 {
4720 const char *this_real_name;
4721
4722 if (file_matcher (file_data->file_names[j], false))
4723 {
4724 per_cu->v.quick->mark = 1;
4725 break;
4726 }
4727
4728 /* Before we invoke realpath, which can get expensive when many
4729 files are involved, do a quick comparison of the basenames. */
4730 if (!basenames_may_differ
4731 && !file_matcher (lbasename (file_data->file_names[j]),
4732 true))
4733 continue;
4734
45940949
TT
4735 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
4736 file_data, j);
927aa2e7
JK
4737 if (file_matcher (this_real_name, false))
4738 {
4739 per_cu->v.quick->mark = 1;
4740 break;
4741 }
4742 }
4743
b76e467d
SM
4744 void **slot = htab_find_slot (per_cu->v.quick->mark
4745 ? visited_found.get ()
4746 : visited_not_found.get (),
4747 file_data, INSERT);
927aa2e7
JK
4748 *slot = file_data;
4749 }
4750}
4751
4752static void
4753dw2_expand_symtabs_matching
4754 (struct objfile *objfile,
4755 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4756 const lookup_name_info *lookup_name,
927aa2e7
JK
4757 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4758 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4759 enum search_domain kind)
4760{
ed2dc618
SM
4761 struct dwarf2_per_objfile *dwarf2_per_objfile
4762 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4763
4764 /* index_table is NULL if OBJF_READNOW. */
5989a64e 4765 if (!dwarf2_per_objfile->per_bfd->index_table)
927aa2e7
JK
4766 return;
4767
ed2dc618 4768 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7 4769
c1a66c06
TV
4770 if (symbol_matcher == NULL && lookup_name == NULL)
4771 {
5989a64e 4772 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4773 {
4774 QUIT;
4775
97a1449a
SM
4776 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
4777 file_matcher, expansion_notify);
c1a66c06
TV
4778 }
4779 return;
4780 }
4781
5989a64e 4782 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
927aa2e7 4783
c1a66c06 4784 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4785 symbol_matcher,
4786 kind, [&] (offset_type idx)
4787 {
ed2dc618 4788 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4789 expansion_notify, kind);
3b00ef10 4790 return true;
fcf23d5b 4791 }, dwarf2_per_objfile);
927aa2e7
JK
4792}
4793
4794/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4795 symtab. */
4796
4797static struct compunit_symtab *
4798recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4799 CORE_ADDR pc)
4800{
4801 int i;
4802
4803 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4804 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4805 return cust;
4806
4807 if (cust->includes == NULL)
4808 return NULL;
4809
4810 for (i = 0; cust->includes[i]; ++i)
4811 {
4812 struct compunit_symtab *s = cust->includes[i];
4813
4814 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4815 if (s != NULL)
4816 return s;
4817 }
4818
4819 return NULL;
4820}
4821
4822static struct compunit_symtab *
4823dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4824 struct bound_minimal_symbol msymbol,
4825 CORE_ADDR pc,
4826 struct obj_section *section,
4827 int warn_if_readin)
4828{
4829 struct dwarf2_per_cu_data *data;
4830 struct compunit_symtab *result;
4831
d320c2b5 4832 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4833 return NULL;
4834
b3b3bada 4835 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4836 data = (struct dwarf2_per_cu_data *) addrmap_find
4837 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4838 if (!data)
4839 return NULL;
4840
af758d11
SM
4841 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4842 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4843 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4844 paddress (objfile->arch (), pc));
927aa2e7 4845
97a1449a
SM
4846 result = recursively_find_pc_sect_compunit_symtab
4847 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4848
927aa2e7
JK
4849 gdb_assert (result != NULL);
4850 return result;
4851}
4852
4853static void
4854dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4855 void *data, int need_fullname)
4856{
ed2dc618
SM
4857 struct dwarf2_per_objfile *dwarf2_per_objfile
4858 = get_dwarf2_per_objfile (objfile);
927aa2e7 4859
5989a64e 4860 if (!dwarf2_per_objfile->per_bfd->filenames_cache)
927aa2e7 4861 {
5989a64e 4862 dwarf2_per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
4863
4864 htab_up visited (htab_create_alloc (10,
4865 htab_hash_pointer, htab_eq_pointer,
4866 NULL, xcalloc, xfree));
4867
4868 /* The rule is CUs specify all the files, including those used
4869 by any TU, so there's no need to scan TUs here. We can
4870 ignore file names coming from already-expanded CUs. */
4871
5989a64e 4872 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4873 {
af758d11 4874 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4875 {
4876 void **slot = htab_find_slot (visited.get (),
4877 per_cu->v.quick->file_names,
4878 INSERT);
4879
4880 *slot = per_cu->v.quick->file_names;
4881 }
4882 }
4883
5989a64e 4884 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4885 {
927aa2e7 4886 /* We only need to look at symtabs not already expanded. */
af758d11 4887 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4888 continue;
4889
ab432490
SM
4890 quick_file_names *file_data
4891 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
927aa2e7
JK
4892 if (file_data == NULL)
4893 continue;
4894
b76e467d 4895 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4896 if (*slot)
4897 {
4898 /* Already visited. */
4899 continue;
4900 }
4901 *slot = file_data;
4902
4903 for (int j = 0; j < file_data->num_file_names; ++j)
4904 {
4905 const char *filename = file_data->file_names[j];
5989a64e 4906 dwarf2_per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
4907 }
4908 }
4909 }
4910
5989a64e 4911 dwarf2_per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
4912 {
4913 gdb::unique_xmalloc_ptr<char> this_real_name;
4914
4915 if (need_fullname)
4916 this_real_name = gdb_realpath (filename);
4917 (*fun) (filename, this_real_name.get (), data);
4918 });
4919}
4920
4921static int
4922dw2_has_symbols (struct objfile *objfile)
4923{
4924 return 1;
4925}
4926
4927const struct quick_symbol_functions dwarf2_gdb_index_functions =
4928{
4929 dw2_has_symbols,
4930 dw2_find_last_source_symtab,
4931 dw2_forget_cached_source_info,
4932 dw2_map_symtabs_matching_filename,
4933 dw2_lookup_symbol,
d3214198 4934 NULL,
927aa2e7
JK
4935 dw2_print_stats,
4936 dw2_dump,
927aa2e7
JK
4937 dw2_expand_symtabs_for_function,
4938 dw2_expand_all_symtabs,
4939 dw2_expand_symtabs_with_fullname,
4940 dw2_map_matching_symbols,
4941 dw2_expand_symtabs_matching,
4942 dw2_find_pc_sect_compunit_symtab,
4943 NULL,
4944 dw2_map_symbol_filenames
4945};
4946
4947/* DWARF-5 debug_names reader. */
4948
4949/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4950static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4951
4952/* A helper function that reads the .debug_names section in SECTION
4953 and fills in MAP. FILENAME is the name of the file containing the
4954 section; it is used for error reporting.
4955
4956 Returns true if all went well, false otherwise. */
4957
4958static bool
4959read_debug_names_from_section (struct objfile *objfile,
4960 const char *filename,
4961 struct dwarf2_section_info *section,
4962 mapped_debug_names &map)
4963{
96b79293 4964 if (section->empty ())
927aa2e7
JK
4965 return false;
4966
4967 /* Older elfutils strip versions could keep the section in the main
4968 executable while splitting it for the separate debug info file. */
96b79293 4969 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4970 return false;
4971
96b79293 4972 section->read (objfile);
927aa2e7 4973
08feed99 4974 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4975
4976 const gdb_byte *addr = section->buffer;
4977
96b79293 4978 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4979
4980 unsigned int bytes_read;
4981 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4982 addr += bytes_read;
4983
4984 map.dwarf5_is_dwarf64 = bytes_read != 4;
4985 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4986 if (bytes_read + length != section->size)
4987 {
4988 /* There may be multiple per-CU indices. */
4989 warning (_("Section .debug_names in %s length %s does not match "
4990 "section length %s, ignoring .debug_names."),
4991 filename, plongest (bytes_read + length),
4992 pulongest (section->size));
4993 return false;
4994 }
4995
4996 /* The version number. */
4997 uint16_t version = read_2_bytes (abfd, addr);
4998 addr += 2;
4999 if (version != 5)
5000 {
5001 warning (_("Section .debug_names in %s has unsupported version %d, "
5002 "ignoring .debug_names."),
5003 filename, version);
5004 return false;
5005 }
5006
5007 /* Padding. */
5008 uint16_t padding = read_2_bytes (abfd, addr);
5009 addr += 2;
5010 if (padding != 0)
5011 {
5012 warning (_("Section .debug_names in %s has unsupported padding %d, "
5013 "ignoring .debug_names."),
5014 filename, padding);
5015 return false;
5016 }
5017
5018 /* comp_unit_count - The number of CUs in the CU list. */
5019 map.cu_count = read_4_bytes (abfd, addr);
5020 addr += 4;
5021
5022 /* local_type_unit_count - The number of TUs in the local TU
5023 list. */
5024 map.tu_count = read_4_bytes (abfd, addr);
5025 addr += 4;
5026
5027 /* foreign_type_unit_count - The number of TUs in the foreign TU
5028 list. */
5029 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5030 addr += 4;
5031 if (foreign_tu_count != 0)
5032 {
5033 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5034 "ignoring .debug_names."),
5035 filename, static_cast<unsigned long> (foreign_tu_count));
5036 return false;
5037 }
5038
5039 /* bucket_count - The number of hash buckets in the hash lookup
5040 table. */
5041 map.bucket_count = read_4_bytes (abfd, addr);
5042 addr += 4;
5043
5044 /* name_count - The number of unique names in the index. */
5045 map.name_count = read_4_bytes (abfd, addr);
5046 addr += 4;
5047
5048 /* abbrev_table_size - The size in bytes of the abbreviations
5049 table. */
5050 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5051 addr += 4;
5052
5053 /* augmentation_string_size - The size in bytes of the augmentation
5054 string. This value is rounded up to a multiple of 4. */
5055 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5056 addr += 4;
5057 map.augmentation_is_gdb = ((augmentation_string_size
5058 == sizeof (dwarf5_augmentation))
5059 && memcmp (addr, dwarf5_augmentation,
5060 sizeof (dwarf5_augmentation)) == 0);
5061 augmentation_string_size += (-augmentation_string_size) & 3;
5062 addr += augmentation_string_size;
5063
5064 /* List of CUs */
5065 map.cu_table_reordered = addr;
5066 addr += map.cu_count * map.offset_size;
5067
5068 /* List of Local TUs */
5069 map.tu_table_reordered = addr;
5070 addr += map.tu_count * map.offset_size;
5071
5072 /* Hash Lookup Table */
5073 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5074 addr += map.bucket_count * 4;
5075 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5076 addr += map.name_count * 4;
5077
5078 /* Name Table */
5079 map.name_table_string_offs_reordered = addr;
5080 addr += map.name_count * map.offset_size;
5081 map.name_table_entry_offs_reordered = addr;
5082 addr += map.name_count * map.offset_size;
5083
5084 const gdb_byte *abbrev_table_start = addr;
5085 for (;;)
5086 {
927aa2e7
JK
5087 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5088 addr += bytes_read;
5089 if (index_num == 0)
5090 break;
5091
5092 const auto insertpair
5093 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5094 if (!insertpair.second)
5095 {
5096 warning (_("Section .debug_names in %s has duplicate index %s, "
5097 "ignoring .debug_names."),
5098 filename, pulongest (index_num));
5099 return false;
5100 }
5101 mapped_debug_names::index_val &indexval = insertpair.first->second;
5102 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5103 addr += bytes_read;
5104
5105 for (;;)
5106 {
5107 mapped_debug_names::index_val::attr attr;
5108 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5109 addr += bytes_read;
5110 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5111 addr += bytes_read;
5112 if (attr.form == DW_FORM_implicit_const)
5113 {
5114 attr.implicit_const = read_signed_leb128 (abfd, addr,
5115 &bytes_read);
5116 addr += bytes_read;
5117 }
5118 if (attr.dw_idx == 0 && attr.form == 0)
5119 break;
5120 indexval.attr_vec.push_back (std::move (attr));
5121 }
5122 }
5123 if (addr != abbrev_table_start + abbrev_table_size)
5124 {
5125 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5126 "of size %s vs. written as %u, ignoring .debug_names."),
5127 filename, plongest (addr - abbrev_table_start),
5128 abbrev_table_size);
927aa2e7
JK
5129 return false;
5130 }
5131 map.entry_pool = addr;
5132
5133 return true;
5134}
5135
5136/* A helper for create_cus_from_debug_names that handles the MAP's CU
5137 list. */
5138
5139static void
168c9250 5140create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5141 const mapped_debug_names &map,
5142 dwarf2_section_info &section,
b76e467d 5143 bool is_dwz)
927aa2e7 5144{
3ee6bb11
TV
5145 if (!map.augmentation_is_gdb)
5146 {
5147 for (uint32_t i = 0; i < map.cu_count; ++i)
5148 {
5149 sect_offset sect_off
5150 = (sect_offset) (extract_unsigned_integer
5151 (map.cu_table_reordered + i * map.offset_size,
5152 map.offset_size,
5153 map.dwarf5_byte_order));
5154 /* We don't know the length of the CU, because the CU list in a
5155 .debug_names index can be incomplete, so we can't use the start of
5156 the next CU as end of this CU. We create the CUs here with length 0,
5157 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5158 dwarf2_per_cu_data *per_cu
168c9250
SM
5159 = create_cu_from_index_list (per_bfd, &section, is_dwz, sect_off, 0);
5160 per_bfd->all_comp_units.push_back (per_cu);
3ee6bb11
TV
5161 }
5162 }
5163
927aa2e7
JK
5164 sect_offset sect_off_prev;
5165 for (uint32_t i = 0; i <= map.cu_count; ++i)
5166 {
5167 sect_offset sect_off_next;
5168 if (i < map.cu_count)
5169 {
5170 sect_off_next
5171 = (sect_offset) (extract_unsigned_integer
5172 (map.cu_table_reordered + i * map.offset_size,
5173 map.offset_size,
5174 map.dwarf5_byte_order));
5175 }
5176 else
5177 sect_off_next = (sect_offset) section.size;
5178 if (i >= 1)
5179 {
5180 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5181 dwarf2_per_cu_data *per_cu
168c9250 5182 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 5183 sect_off_prev, length);
168c9250 5184 per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5185 }
5186 sect_off_prev = sect_off_next;
5187 }
5188}
5189
5190/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5191 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5192
5193static void
168c9250 5194create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5195 const mapped_debug_names &map,
5196 const mapped_debug_names &dwz_map)
5197{
168c9250
SM
5198 gdb_assert (per_bfd->all_comp_units.empty ());
5199 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5200
168c9250 5201 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 5202 false /* is_dwz */);
927aa2e7
JK
5203
5204 if (dwz_map.cu_count == 0)
5205 return;
5206
168c9250
SM
5207 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5208 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 5209 true /* is_dwz */);
927aa2e7
JK
5210}
5211
5212/* Read .debug_names. If everything went ok, initialize the "quick"
5213 elements of all the CUs and return true. Otherwise, return false. */
5214
5215static bool
ed2dc618 5216dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5217{
fcf23d5b
SM
5218 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5219 mapped_debug_names dwz_map;
ed2dc618 5220 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5221
5222 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5989a64e 5223 &dwarf2_per_objfile->per_bfd->debug_names,
22ca247e 5224 *map))
927aa2e7
JK
5225 return false;
5226
5227 /* Don't use the index if it's empty. */
22ca247e 5228 if (map->name_count == 0)
927aa2e7
JK
5229 return false;
5230
5231 /* If there is a .dwz file, read it so we can get its CU list as
5232 well. */
c3699833 5233 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
927aa2e7
JK
5234 if (dwz != NULL)
5235 {
5236 if (!read_debug_names_from_section (objfile,
00f93c44 5237 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5238 &dwz->debug_names, dwz_map))
5239 {
5240 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5241 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5242 return false;
5243 }
5244 }
5245
168c9250 5246 create_cus_from_debug_names (dwarf2_per_objfile->per_bfd, *map, dwz_map);
927aa2e7 5247
22ca247e 5248 if (map->tu_count != 0)
927aa2e7
JK
5249 {
5250 /* We can only handle a single .debug_types when we have an
5251 index. */
5989a64e 5252 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
927aa2e7
JK
5253 return false;
5254
5989a64e 5255 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
927aa2e7
JK
5256
5257 create_signatured_type_table_from_debug_names
5989a64e 5258 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->per_bfd->abbrev);
927aa2e7
JK
5259 }
5260
ed2dc618 5261 create_addrmap_from_aranges (dwarf2_per_objfile,
5989a64e 5262 &dwarf2_per_objfile->per_bfd->debug_aranges);
927aa2e7 5263
5989a64e
SM
5264 dwarf2_per_objfile->per_bfd->debug_names_table = std::move (map);
5265 dwarf2_per_objfile->per_bfd->using_index = 1;
5266 dwarf2_per_objfile->per_bfd->quick_file_names_table =
5267 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
927aa2e7
JK
5268
5269 return true;
5270}
5271
927aa2e7
JK
5272/* Type used to manage iterating over all CUs looking for a symbol for
5273 .debug_names. */
5274
5275class dw2_debug_names_iterator
5276{
5277public:
927aa2e7 5278 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5279 gdb::optional<block_enum> block_index,
5280 domain_enum domain,
fcf23d5b 5281 const char *name, dwarf2_per_objfile *per_objfile)
2b79f376 5282 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5283 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5284 m_per_objfile (per_objfile)
927aa2e7
JK
5285 {}
5286
5287 dw2_debug_names_iterator (const mapped_debug_names &map,
fcf23d5b 5288 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5289 : m_map (map),
5290 m_search (search),
fcf23d5b
SM
5291 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5292 m_per_objfile (per_objfile)
927aa2e7
JK
5293 {}
5294
3b00ef10
TT
5295 dw2_debug_names_iterator (const mapped_debug_names &map,
5296 block_enum block_index, domain_enum domain,
fcf23d5b 5297 uint32_t namei, dwarf2_per_objfile *per_objfile)
3b00ef10 5298 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5299 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5300 m_per_objfile (per_objfile)
3b00ef10
TT
5301 {}
5302
927aa2e7
JK
5303 /* Return the next matching CU or NULL if there are no more. */
5304 dwarf2_per_cu_data *next ();
5305
5306private:
5307 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
fcf23d5b 5308 const char *name, dwarf2_per_objfile *per_objfile);
927aa2e7 5309 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
fcf23d5b 5310 uint32_t namei, dwarf2_per_objfile *per_objfile);
927aa2e7
JK
5311
5312 /* The internalized form of .debug_names. */
5313 const mapped_debug_names &m_map;
5314
2b79f376
SM
5315 /* If set, only look for symbols that match that block. Valid values are
5316 GLOBAL_BLOCK and STATIC_BLOCK. */
5317 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5318
5319 /* The kind of symbol we're looking for. */
5320 const domain_enum m_domain = UNDEF_DOMAIN;
5321 const search_domain m_search = ALL_DOMAIN;
5322
5323 /* The list of CUs from the index entry of the symbol, or NULL if
5324 not found. */
5325 const gdb_byte *m_addr;
fcf23d5b
SM
5326
5327 dwarf2_per_objfile *m_per_objfile;
927aa2e7
JK
5328};
5329
5330const char *
fcf23d5b
SM
5331mapped_debug_names::namei_to_name
5332 (uint32_t namei, dwarf2_per_objfile *dwarf2_per_objfile) const
927aa2e7
JK
5333{
5334 const ULONGEST namei_string_offs
5335 = extract_unsigned_integer ((name_table_string_offs_reordered
5336 + namei * offset_size),
5337 offset_size,
5338 dwarf5_byte_order);
4f44ae6c
TT
5339 return read_indirect_string_at_offset (dwarf2_per_objfile,
5340 namei_string_offs);
927aa2e7
JK
5341}
5342
5343/* Find a slot in .debug_names for the object named NAME. If NAME is
5344 found, return pointer to its pool data. If NAME cannot be found,
5345 return NULL. */
5346
5347const gdb_byte *
5348dw2_debug_names_iterator::find_vec_in_debug_names
fcf23d5b 5349 (const mapped_debug_names &map, const char *name, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5350{
5351 int (*cmp) (const char *, const char *);
5352
54ee4252 5353 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5354 if (current_language->la_language == language_cplus
5355 || current_language->la_language == language_fortran
5356 || current_language->la_language == language_d)
5357 {
5358 /* NAME is already canonical. Drop any qualifiers as
5359 .debug_names does not contain any. */
5360
5361 if (strchr (name, '(') != NULL)
5362 {
54ee4252 5363 without_params = cp_remove_params (name);
927aa2e7 5364 if (without_params != NULL)
54ee4252 5365 name = without_params.get ();
927aa2e7
JK
5366 }
5367 }
5368
5369 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5370
5371 const uint32_t full_hash = dwarf5_djb_hash (name);
5372 uint32_t namei
5373 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5374 (map.bucket_table_reordered
5375 + (full_hash % map.bucket_count)), 4,
5376 map.dwarf5_byte_order);
5377 if (namei == 0)
5378 return NULL;
5379 --namei;
5380 if (namei >= map.name_count)
5381 {
b98664d3 5382 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5383 "[in module %s]"),
5384 namei, map.name_count,
fcf23d5b 5385 objfile_name (per_objfile->objfile));
927aa2e7
JK
5386 return NULL;
5387 }
5388
5389 for (;;)
5390 {
5391 const uint32_t namei_full_hash
5392 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5393 (map.hash_table_reordered + namei), 4,
5394 map.dwarf5_byte_order);
5395 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5396 return NULL;
5397
5398 if (full_hash == namei_full_hash)
5399 {
fcf23d5b 5400 const char *const namei_string = map.namei_to_name (namei, per_objfile);
927aa2e7
JK
5401
5402#if 0 /* An expensive sanity check. */
5403 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5404 {
b98664d3 5405 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5406 "[in module %s]"),
5407 namei, objfile_name (dwarf2_per_objfile->objfile));
5408 return NULL;
5409 }
5410#endif
5411
5412 if (cmp (namei_string, name) == 0)
5413 {
5414 const ULONGEST namei_entry_offs
5415 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5416 + namei * map.offset_size),
5417 map.offset_size, map.dwarf5_byte_order);
5418 return map.entry_pool + namei_entry_offs;
5419 }
5420 }
5421
5422 ++namei;
5423 if (namei >= map.name_count)
5424 return NULL;
5425 }
5426}
5427
5428const gdb_byte *
5429dw2_debug_names_iterator::find_vec_in_debug_names
fcf23d5b 5430 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5431{
5432 if (namei >= map.name_count)
5433 {
b98664d3 5434 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5435 "[in module %s]"),
5436 namei, map.name_count,
fcf23d5b 5437 objfile_name (per_objfile->objfile));
927aa2e7
JK
5438 return NULL;
5439 }
5440
5441 const ULONGEST namei_entry_offs
5442 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5443 + namei * map.offset_size),
5444 map.offset_size, map.dwarf5_byte_order);
5445 return map.entry_pool + namei_entry_offs;
5446}
5447
5448/* See dw2_debug_names_iterator. */
5449
5450dwarf2_per_cu_data *
5451dw2_debug_names_iterator::next ()
5452{
5453 if (m_addr == NULL)
5454 return NULL;
5455
fcf23d5b
SM
5456 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5457 struct objfile *objfile = m_per_objfile->objfile;
ed2dc618 5458 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5459
5460 again:
5461
5462 unsigned int bytes_read;
5463 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5464 m_addr += bytes_read;
5465 if (abbrev == 0)
5466 return NULL;
5467
5468 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5469 if (indexval_it == m_map.abbrev_map.cend ())
5470 {
b98664d3 5471 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5472 "[in module %s]"),
ed2dc618 5473 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5474 return NULL;
5475 }
5476 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5477 enum class symbol_linkage {
5478 unknown,
5479 static_,
5480 extern_,
23c13d42 5481 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5482 dwarf2_per_cu_data *per_cu = NULL;
5483 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5484 {
5485 ULONGEST ull;
5486 switch (attr.form)
5487 {
5488 case DW_FORM_implicit_const:
5489 ull = attr.implicit_const;
5490 break;
5491 case DW_FORM_flag_present:
5492 ull = 1;
5493 break;
5494 case DW_FORM_udata:
5495 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5496 m_addr += bytes_read;
5497 break;
6dc55ce9 5498 case DW_FORM_ref4:
5499 ull = read_4_bytes (abfd, m_addr);
5500 m_addr += 4;
5501 break;
5502 case DW_FORM_ref8:
5503 ull = read_8_bytes (abfd, m_addr);
5504 m_addr += 8;
5505 break;
5506 case DW_FORM_ref_sig8:
5507 ull = read_8_bytes (abfd, m_addr);
5508 m_addr += 8;
5509 break;
927aa2e7 5510 default:
b98664d3 5511 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5512 dwarf_form_name (attr.form),
ed2dc618 5513 objfile_name (objfile));
927aa2e7
JK
5514 return NULL;
5515 }
5516 switch (attr.dw_idx)
5517 {
5518 case DW_IDX_compile_unit:
5519 /* Don't crash on bad data. */
fcf23d5b 5520 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
927aa2e7 5521 {
b98664d3 5522 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5523 " [in module %s]"),
5524 pulongest (ull),
fcf23d5b 5525 objfile_name (objfile));
927aa2e7
JK
5526 continue;
5527 }
fcf23d5b 5528 per_cu = per_bfd->get_cutu (ull);
927aa2e7 5529 break;
8af5c486
JK
5530 case DW_IDX_type_unit:
5531 /* Don't crash on bad data. */
fcf23d5b 5532 if (ull >= per_bfd->all_type_units.size ())
8af5c486 5533 {
b98664d3 5534 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5535 " [in module %s]"),
5536 pulongest (ull),
fcf23d5b 5537 objfile_name (objfile));
8af5c486
JK
5538 continue;
5539 }
fcf23d5b 5540 per_cu = &per_bfd->get_tu (ull)->per_cu;
8af5c486 5541 break;
6dc55ce9 5542 case DW_IDX_die_offset:
5543 /* In a per-CU index (as opposed to a per-module index), index
5544 entries without CU attribute implicitly refer to the single CU. */
5545 if (per_cu == NULL)
fcf23d5b 5546 per_cu = per_bfd->get_cu (0);
6dc55ce9 5547 break;
927aa2e7
JK
5548 case DW_IDX_GNU_internal:
5549 if (!m_map.augmentation_is_gdb)
5550 break;
23c13d42 5551 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5552 break;
5553 case DW_IDX_GNU_external:
5554 if (!m_map.augmentation_is_gdb)
5555 break;
23c13d42 5556 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5557 break;
5558 }
5559 }
5560
5561 /* Skip if already read in. */
fcf23d5b 5562 if (m_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5563 goto again;
5564
5565 /* Check static vs global. */
23c13d42 5566 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5567 {
2b79f376 5568 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5569 const bool symbol_is_static =
5570 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5571 if (want_static != symbol_is_static)
2b79f376 5572 goto again;
927aa2e7
JK
5573 }
5574
5575 /* Match dw2_symtab_iter_next, symbol_kind
5576 and debug_names::psymbol_tag. */
5577 switch (m_domain)
5578 {
5579 case VAR_DOMAIN:
5580 switch (indexval.dwarf_tag)
5581 {
5582 case DW_TAG_variable:
5583 case DW_TAG_subprogram:
5584 /* Some types are also in VAR_DOMAIN. */
5585 case DW_TAG_typedef:
5586 case DW_TAG_structure_type:
5587 break;
5588 default:
5589 goto again;
5590 }
5591 break;
5592 case STRUCT_DOMAIN:
5593 switch (indexval.dwarf_tag)
5594 {
5595 case DW_TAG_typedef:
5596 case DW_TAG_structure_type:
5597 break;
5598 default:
5599 goto again;
5600 }
5601 break;
5602 case LABEL_DOMAIN:
5603 switch (indexval.dwarf_tag)
5604 {
5605 case 0:
5606 case DW_TAG_variable:
5607 break;
5608 default:
5609 goto again;
5610 }
5611 break;
59c35742
AB
5612 case MODULE_DOMAIN:
5613 switch (indexval.dwarf_tag)
5614 {
5615 case DW_TAG_module:
5616 break;
5617 default:
5618 goto again;
5619 }
5620 break;
927aa2e7
JK
5621 default:
5622 break;
5623 }
5624
5625 /* Match dw2_expand_symtabs_matching, symbol_kind and
5626 debug_names::psymbol_tag. */
5627 switch (m_search)
4b514bc8 5628 {
927aa2e7
JK
5629 case VARIABLES_DOMAIN:
5630 switch (indexval.dwarf_tag)
4b514bc8 5631 {
927aa2e7
JK
5632 case DW_TAG_variable:
5633 break;
5634 default:
5635 goto again;
4b514bc8 5636 }
927aa2e7
JK
5637 break;
5638 case FUNCTIONS_DOMAIN:
5639 switch (indexval.dwarf_tag)
4b514bc8 5640 {
927aa2e7
JK
5641 case DW_TAG_subprogram:
5642 break;
5643 default:
5644 goto again;
4b514bc8 5645 }
927aa2e7
JK
5646 break;
5647 case TYPES_DOMAIN:
5648 switch (indexval.dwarf_tag)
5649 {
5650 case DW_TAG_typedef:
5651 case DW_TAG_structure_type:
5652 break;
5653 default:
5654 goto again;
5655 }
5656 break;
59c35742
AB
5657 case MODULES_DOMAIN:
5658 switch (indexval.dwarf_tag)
5659 {
5660 case DW_TAG_module:
5661 break;
5662 default:
5663 goto again;
5664 }
927aa2e7
JK
5665 default:
5666 break;
4b514bc8 5667 }
927aa2e7
JK
5668
5669 return per_cu;
4b514bc8 5670}
61920122 5671
927aa2e7 5672static struct compunit_symtab *
c7f839cb 5673dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5674 const char *name, domain_enum domain)
4b514bc8 5675{
ed2dc618
SM
5676 struct dwarf2_per_objfile *dwarf2_per_objfile
5677 = get_dwarf2_per_objfile (objfile);
61920122 5678
5989a64e 5679 const auto &mapp = dwarf2_per_objfile->per_bfd->debug_names_table;
927aa2e7 5680 if (!mapp)
61920122 5681 {
927aa2e7
JK
5682 /* index is NULL if OBJF_READNOW. */
5683 return NULL;
5684 }
5685 const auto &map = *mapp;
9291a0cd 5686
fcf23d5b
SM
5687 dw2_debug_names_iterator iter (map, block_index, domain, name,
5688 dwarf2_per_objfile);
9703b513 5689
927aa2e7
JK
5690 struct compunit_symtab *stab_best = NULL;
5691 struct dwarf2_per_cu_data *per_cu;
5692 while ((per_cu = iter.next ()) != NULL)
5693 {
5694 struct symbol *sym, *with_opaque = NULL;
97a1449a
SM
5695 compunit_symtab *stab
5696 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
927aa2e7 5697 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5698 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5699
927aa2e7
JK
5700 sym = block_find_symbol (block, name, domain,
5701 block_find_non_opaque_type_preferred,
5702 &with_opaque);
9703b513 5703
927aa2e7
JK
5704 /* Some caution must be observed with overloaded functions and
5705 methods, since the index will not contain any overload
5706 information (but NAME might contain it). */
a3ec0bb1 5707
927aa2e7 5708 if (sym != NULL
987012b8 5709 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5710 return stab;
5711 if (with_opaque != NULL
987012b8 5712 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5713 stab_best = stab;
9703b513 5714
927aa2e7 5715 /* Keep looking through other CUs. */
9703b513
TT
5716 }
5717
927aa2e7 5718 return stab_best;
9703b513
TT
5719}
5720
927aa2e7
JK
5721/* This dumps minimal information about .debug_names. It is called
5722 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5723 uses this to verify that .debug_names has been loaded. */
9291a0cd 5724
927aa2e7
JK
5725static void
5726dw2_debug_names_dump (struct objfile *objfile)
5727{
ed2dc618
SM
5728 struct dwarf2_per_objfile *dwarf2_per_objfile
5729 = get_dwarf2_per_objfile (objfile);
5730
5989a64e 5731 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
927aa2e7 5732 printf_filtered (".debug_names:");
5989a64e 5733 if (dwarf2_per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5734 printf_filtered (" exists\n");
5735 else
5736 printf_filtered (" faked for \"readnow\"\n");
5737 printf_filtered ("\n");
9291a0cd
TT
5738}
5739
9291a0cd 5740static void
927aa2e7
JK
5741dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5742 const char *func_name)
9291a0cd 5743{
ed2dc618
SM
5744 struct dwarf2_per_objfile *dwarf2_per_objfile
5745 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5746
5989a64e
SM
5747 /* dwarf2_per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5748 if (dwarf2_per_objfile->per_bfd->debug_names_table)
24c79950 5749 {
5989a64e 5750 const mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
24c79950 5751
fcf23d5b
SM
5752 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
5753 dwarf2_per_objfile);
24c79950 5754
927aa2e7
JK
5755 struct dwarf2_per_cu_data *per_cu;
5756 while ((per_cu = iter.next ()) != NULL)
97a1449a 5757 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
927aa2e7
JK
5758 }
5759}
24c79950 5760
3b00ef10
TT
5761static void
5762dw2_debug_names_map_matching_symbols
5763 (struct objfile *objfile,
5764 const lookup_name_info &name, domain_enum domain,
5765 int global,
5766 gdb::function_view<symbol_found_callback_ftype> callback,
5767 symbol_compare_ftype *ordered_compare)
5768{
5769 struct dwarf2_per_objfile *dwarf2_per_objfile
5770 = get_dwarf2_per_objfile (objfile);
5771
5772 /* debug_names_table is NULL if OBJF_READNOW. */
5989a64e 5773 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5774 return;
5775
5989a64e 5776 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
3b00ef10
TT
5777 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5778
5779 const char *match_name = name.ada ().lookup_name ().c_str ();
5780 auto matcher = [&] (const char *symname)
5781 {
5782 if (ordered_compare == nullptr)
5783 return true;
5784 return ordered_compare (symname, match_name) == 0;
5785 };
5786
5787 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5788 [&] (offset_type namei)
5789 {
5790 /* The name was matched, now expand corresponding CUs that were
5791 marked. */
fcf23d5b
SM
5792 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
5793 dwarf2_per_objfile);
3b00ef10
TT
5794
5795 struct dwarf2_per_cu_data *per_cu;
5796 while ((per_cu = iter.next ()) != NULL)
97a1449a
SM
5797 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
5798 nullptr);
3b00ef10 5799 return true;
fcf23d5b 5800 }, dwarf2_per_objfile);
3b00ef10
TT
5801
5802 /* It's a shame we couldn't do this inside the
5803 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5804 that have already been expanded. Instead, this loop matches what
5805 the psymtab code does. */
5989a64e 5806 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3b00ef10 5807 {
af758d11
SM
5808 compunit_symtab *symtab = dwarf2_per_objfile->get_symtab (per_cu);
5809 if (symtab != nullptr)
3b00ef10
TT
5810 {
5811 const struct block *block
af758d11 5812 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
3b00ef10
TT
5813 if (!iterate_over_symbols_terminated (block, name,
5814 domain, callback))
5815 break;
5816 }
5817 }
5818}
5819
927aa2e7
JK
5820static void
5821dw2_debug_names_expand_symtabs_matching
5822 (struct objfile *objfile,
5823 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5824 const lookup_name_info *lookup_name,
927aa2e7
JK
5825 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5826 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5827 enum search_domain kind)
5828{
ed2dc618
SM
5829 struct dwarf2_per_objfile *dwarf2_per_objfile
5830 = get_dwarf2_per_objfile (objfile);
9291a0cd 5831
927aa2e7 5832 /* debug_names_table is NULL if OBJF_READNOW. */
5989a64e 5833 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
927aa2e7 5834 return;
9291a0cd 5835
ed2dc618 5836 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5837
c1a66c06
TV
5838 if (symbol_matcher == NULL && lookup_name == NULL)
5839 {
5989a64e 5840 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5841 {
5842 QUIT;
5843
97a1449a
SM
5844 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5845 file_matcher, expansion_notify);
c1a66c06
TV
5846 }
5847 return;
5848 }
5849
5989a64e 5850 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
bbf2f4df 5851
c1a66c06 5852 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
5853 symbol_matcher,
5854 kind, [&] (offset_type namei)
927aa2e7 5855 {
927aa2e7
JK
5856 /* The name was matched, now expand corresponding CUs that were
5857 marked. */
fcf23d5b 5858 dw2_debug_names_iterator iter (map, kind, namei, dwarf2_per_objfile);
bbf2f4df 5859
927aa2e7
JK
5860 struct dwarf2_per_cu_data *per_cu;
5861 while ((per_cu = iter.next ()) != NULL)
97a1449a
SM
5862 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5863 file_matcher, expansion_notify);
3b00ef10 5864 return true;
fcf23d5b 5865 }, dwarf2_per_objfile);
9291a0cd
TT
5866}
5867
927aa2e7 5868const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5869{
5870 dw2_has_symbols,
5871 dw2_find_last_source_symtab,
5872 dw2_forget_cached_source_info,
f8eba3c6 5873 dw2_map_symtabs_matching_filename,
927aa2e7 5874 dw2_debug_names_lookup_symbol,
d3214198 5875 NULL,
9291a0cd 5876 dw2_print_stats,
927aa2e7 5877 dw2_debug_names_dump,
927aa2e7 5878 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5879 dw2_expand_all_symtabs,
652a8996 5880 dw2_expand_symtabs_with_fullname,
3b00ef10 5881 dw2_debug_names_map_matching_symbols,
927aa2e7 5882 dw2_debug_names_expand_symtabs_matching,
43f3e411 5883 dw2_find_pc_sect_compunit_symtab,
71a3c369 5884 NULL,
9291a0cd
TT
5885 dw2_map_symbol_filenames
5886};
5887
4485a1c1 5888/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5889 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5890
5891template <typename T>
5892static gdb::array_view<const gdb_byte>
5893get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5894{
5895 dwarf2_section_info *section = &section_owner->gdb_index;
5896
96b79293 5897 if (section->empty ())
4485a1c1
SM
5898 return {};
5899
5900 /* Older elfutils strip versions could keep the section in the main
5901 executable while splitting it for the separate debug info file. */
96b79293 5902 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5903 return {};
5904
96b79293 5905 section->read (obj);
4485a1c1 5906
8bebfcda
PA
5907 /* dwarf2_section_info::size is a bfd_size_type, while
5908 gdb::array_view works with size_t. On 32-bit hosts, with
5909 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5910 is 32-bit. So we need an explicit narrowing conversion here.
5911 This is fine, because it's impossible to allocate or mmap an
5912 array/buffer larger than what size_t can represent. */
5913 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5914}
5915
87d6a7aa
SM
5916/* Lookup the index cache for the contents of the index associated to
5917 DWARF2_OBJ. */
5918
5919static gdb::array_view<const gdb_byte>
5989a64e 5920get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5921{
5922 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5923 if (build_id == nullptr)
5924 return {};
5925
5926 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5927 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5928}
5929
5930/* Same as the above, but for DWZ. */
5931
5932static gdb::array_view<const gdb_byte>
5933get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5934{
5935 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5936 if (build_id == nullptr)
5937 return {};
5938
5939 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5940}
5941
3c0aa29a 5942/* See symfile.h. */
9291a0cd 5943
3c0aa29a
PA
5944bool
5945dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5946{
ed2dc618
SM
5947 struct dwarf2_per_objfile *dwarf2_per_objfile
5948 = get_dwarf2_per_objfile (objfile);
17ee85fc 5949 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
ed2dc618 5950
9291a0cd
TT
5951 /* If we're about to read full symbols, don't bother with the
5952 indices. In this case we also don't care if some other debug
5953 format is making psymtabs, because they are all about to be
5954 expanded anyway. */
5955 if ((objfile->flags & OBJF_READNOW))
5956 {
17ee85fc
TT
5957 /* When using READNOW, the using_index flag (set below) indicates that
5958 PER_BFD was already initialized, when we loaded some other objfile. */
5959 if (per_bfd->using_index)
5960 {
5961 *index_kind = dw_index_kind::GDB_INDEX;
5962 dwarf2_per_objfile->resize_symtabs ();
5963 return true;
5964 }
5965
5966 per_bfd->using_index = 1;
ed2dc618
SM
5967 create_all_comp_units (dwarf2_per_objfile);
5968 create_all_type_units (dwarf2_per_objfile);
17ee85fc
TT
5969 per_bfd->quick_file_names_table
5970 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
af758d11 5971 dwarf2_per_objfile->resize_symtabs ();
9291a0cd 5972
17ee85fc
TT
5973 for (int i = 0; i < (per_bfd->all_comp_units.size ()
5974 + per_bfd->all_type_units.size ()); ++i)
9291a0cd 5975 {
17ee85fc 5976 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
9291a0cd 5977
17ee85fc 5978 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
e254ef6a 5979 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5980 }
5981
5982 /* Return 1 so that gdb sees the "quick" functions. However,
5983 these functions will be no-ops because we will have expanded
5984 all symtabs. */
3c0aa29a
PA
5985 *index_kind = dw_index_kind::GDB_INDEX;
5986 return true;
9291a0cd
TT
5987 }
5988
17ee85fc
TT
5989 /* Was a debug names index already read when we processed an objfile sharing
5990 PER_BFD? */
5991 if (per_bfd->debug_names_table != nullptr)
5992 {
5993 *index_kind = dw_index_kind::DEBUG_NAMES;
5994 dwarf2_per_objfile->resize_symtabs ();
5995 return true;
5996 }
5997
5998 /* Was a GDB index already read when we processed an objfile sharing
5999 PER_BFD? */
6000 if (per_bfd->index_table != nullptr)
6001 {
6002 *index_kind = dw_index_kind::GDB_INDEX;
6003 dwarf2_per_objfile->resize_symtabs ();
6004 return true;
6005 }
6006
ed2dc618 6007 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6008 {
6009 *index_kind = dw_index_kind::DEBUG_NAMES;
af758d11 6010 dwarf2_per_objfile->resize_symtabs ();
3c0aa29a
PA
6011 return true;
6012 }
927aa2e7 6013
4485a1c1 6014 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5989a64e 6015 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 6016 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6017 {
6018 *index_kind = dw_index_kind::GDB_INDEX;
af758d11 6019 dwarf2_per_objfile->resize_symtabs ();
3c0aa29a
PA
6020 return true;
6021 }
9291a0cd 6022
87d6a7aa
SM
6023 /* ... otherwise, try to find the index in the index cache. */
6024 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6025 get_gdb_index_contents_from_cache,
6026 get_gdb_index_contents_from_cache_dwz))
6027 {
6028 global_index_cache.hit ();
6029 *index_kind = dw_index_kind::GDB_INDEX;
af758d11 6030 dwarf2_per_objfile->resize_symtabs ();
87d6a7aa
SM
6031 return true;
6032 }
6033
6034 global_index_cache.miss ();
3c0aa29a 6035 return false;
9291a0cd
TT
6036}
6037
6038\f
6039
dce234bc
PP
6040/* Build a partial symbol table. */
6041
6042void
f29dff0a 6043dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6044{
ed2dc618
SM
6045 struct dwarf2_per_objfile *dwarf2_per_objfile
6046 = get_dwarf2_per_objfile (objfile);
17ee85fc
TT
6047 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
6048
6049 if (per_bfd->partial_symtabs != nullptr)
6050 {
6051 /* Partial symbols were already read, so now we can simply
6052 attach them. */
6053 objfile->partial_symtabs = per_bfd->partial_symtabs;
6054 dwarf2_per_objfile->resize_symtabs ();
6055 return;
6056 }
c9bf0622 6057
6eee24ce 6058 init_psymbol_list (objfile, 1024);
c906108c 6059
a70b8144 6060 try
c9bf0622
TT
6061 {
6062 /* This isn't really ideal: all the data we allocate on the
6063 objfile's obstack is still uselessly kept around. However,
6064 freeing it seems unsafe. */
906768f9 6065 psymtab_discarder psymtabs (objfile);
ed2dc618 6066 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6067 psymtabs.keep ();
87d6a7aa 6068
af758d11
SM
6069 dwarf2_per_objfile->resize_symtabs ();
6070
87d6a7aa
SM
6071 /* (maybe) store an index in the cache. */
6072 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 6073 }
230d2906 6074 catch (const gdb_exception_error &except)
492d29ea
PA
6075 {
6076 exception_print (gdb_stderr, except);
6077 }
17ee85fc
TT
6078
6079 /* Finish by setting the local reference to partial symtabs, so that
6080 we don't try to read them again if reading another objfile with the same
6081 BFD. If we can't in fact share, this won't make a difference anyway as
6082 the dwarf2_per_bfd object won't be shared. */
6083 per_bfd->partial_symtabs = objfile->partial_symtabs;
c906108c 6084}
c906108c 6085
3b80fe9b
DE
6086/* Find the base address of the compilation unit for range lists and
6087 location lists. It will normally be specified by DW_AT_low_pc.
6088 In DWARF-3 draft 4, the base address could be overridden by
6089 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6090 compilation units with discontinuous ranges. */
6091
6092static void
6093dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6094{
6095 struct attribute *attr;
6096
2b24b6e4 6097 cu->base_address.reset ();
3b80fe9b
DE
6098
6099 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6100 if (attr != nullptr)
2b24b6e4 6101 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6102 else
6103 {
6104 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6105 if (attr != nullptr)
2b24b6e4 6106 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6107 }
6108}
6109
36586728
TT
6110/* Helper function that returns the proper abbrev section for
6111 THIS_CU. */
6112
6113static struct dwarf2_section_info *
6114get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6115{
6116 struct dwarf2_section_info *abbrev;
c3699833 6117 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
6118
6119 if (this_cu->is_dwz)
c3699833 6120 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
36586728 6121 else
c3699833 6122 abbrev = &per_bfd->abbrev;
36586728
TT
6123
6124 return abbrev;
6125}
6126
f4dc4d17
DE
6127/* Fetch the abbreviation table offset from a comp or type unit header. */
6128
6129static sect_offset
ed2dc618
SM
6130read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6131 struct dwarf2_section_info *section,
9c541725 6132 sect_offset sect_off)
f4dc4d17 6133{
96b79293 6134 bfd *abfd = section->get_bfd_owner ();
d521ce57 6135 const gdb_byte *info_ptr;
ac298888 6136 unsigned int initial_length_size, offset_size;
43988095 6137 uint16_t version;
f4dc4d17 6138
96b79293 6139 section->read (dwarf2_per_objfile->objfile);
9c541725 6140 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6141 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6142 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6143 info_ptr += initial_length_size;
6144
6145 version = read_2_bytes (abfd, info_ptr);
6146 info_ptr += 2;
6147 if (version >= 5)
6148 {
6149 /* Skip unit type and address size. */
6150 info_ptr += 2;
6151 }
6152
24aa364d 6153 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6154}
6155
b83470bf
TT
6156/* A partial symtab that is used only for include files. */
6157struct dwarf2_include_psymtab : public partial_symtab
6158{
6159 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6160 : partial_symtab (filename, objfile)
6161 {
6162 }
6163
6164 void read_symtab (struct objfile *objfile) override
6165 {
194d088f
TV
6166 /* It's an include file, no symbols to read for it.
6167 Everything is in the includer symtab. */
6168
6169 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6170 expansion of the includer psymtab. We use the dependencies[0] field to
6171 model the includer. But if we go the regular route of calling
6172 expand_psymtab here, and having expand_psymtab call expand_dependencies
6173 to expand the includer, we'll only use expand_psymtab on the includer
6174 (making it a non-toplevel psymtab), while if we expand the includer via
6175 another path, we'll use read_symtab (making it a toplevel psymtab).
6176 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6177 psymtab, and trigger read_symtab on the includer here directly. */
6178 includer ()->read_symtab (objfile);
b83470bf
TT
6179 }
6180
6181 void expand_psymtab (struct objfile *objfile) override
6182 {
194d088f
TV
6183 /* This is not called by read_symtab, and should not be called by any
6184 expand_dependencies. */
6185 gdb_assert (false);
b83470bf
TT
6186 }
6187
5717c425 6188 bool readin_p (struct objfile *objfile) const override
b83470bf 6189 {
5717c425 6190 return includer ()->readin_p (objfile);
b83470bf
TT
6191 }
6192
5717c425 6193 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6194 {
6195 return nullptr;
6196 }
6197
6198private:
194d088f
TV
6199 partial_symtab *includer () const
6200 {
6201 /* An include psymtab has exactly one dependency: the psymtab that
6202 includes it. */
6203 gdb_assert (this->number_of_dependencies == 1);
6204 return this->dependencies[0];
6205 }
b83470bf
TT
6206};
6207
aaa75496
JB
6208/* Allocate a new partial symtab for file named NAME and mark this new
6209 partial symtab as being an include of PST. */
6210
6211static void
891813be 6212dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
6213 struct objfile *objfile)
6214{
b83470bf 6215 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6216
fbd9ab74 6217 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6218 subpst->dirname = pst->dirname;
fbd9ab74 6219
a9342b62 6220 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6221 subpst->dependencies[0] = pst;
6222 subpst->number_of_dependencies = 1;
aaa75496
JB
6223}
6224
6225/* Read the Line Number Program data and extract the list of files
6226 included by the source file represented by PST. Build an include
d85a05f0 6227 partial symtab for each of these included files. */
aaa75496
JB
6228
6229static void
6230dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6231 struct die_info *die,
891813be 6232 dwarf2_psymtab *pst)
aaa75496 6233{
fff8551c 6234 line_header_up lh;
d85a05f0 6235 struct attribute *attr;
aaa75496 6236
d85a05f0 6237 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6238 if (attr != nullptr)
9c541725 6239 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6240 if (lh == NULL)
6241 return; /* No linetable, so no includes. */
6242
79748972
TT
6243 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6244 that we pass in the raw text_low here; that is ok because we're
6245 only decoding the line table to make include partial symtabs, and
6246 so the addresses aren't really used. */
4ae976d1 6247 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6248 pst->raw_text_low (), 1);
aaa75496
JB
6249}
6250
348e048f 6251static hashval_t
52dc124a 6252hash_signatured_type (const void *item)
348e048f 6253{
9a3c8263
SM
6254 const struct signatured_type *sig_type
6255 = (const struct signatured_type *) item;
9a619af0 6256
348e048f 6257 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6258 return sig_type->signature;
348e048f
DE
6259}
6260
6261static int
52dc124a 6262eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6263{
9a3c8263
SM
6264 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6265 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6266
348e048f
DE
6267 return lhs->signature == rhs->signature;
6268}
6269
1fd400ff
TT
6270/* Allocate a hash table for signatured types. */
6271
b0b6a987 6272static htab_up
298e9637 6273allocate_signatured_type_table ()
1fd400ff 6274{
b0b6a987
TT
6275 return htab_up (htab_create_alloc (41,
6276 hash_signatured_type,
6277 eq_signatured_type,
6278 NULL, xcalloc, xfree));
1fd400ff
TT
6279}
6280
d467dd73 6281/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6282
6283static int
d467dd73 6284add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6285{
9a3c8263 6286 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6287 std::vector<signatured_type *> *all_type_units
6288 = (std::vector<signatured_type *> *) datum;
1fd400ff 6289
b2bdb8cf 6290 all_type_units->push_back (sigt);
1fd400ff
TT
6291
6292 return 1;
6293}
6294
78d4d2c5 6295/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6296 and fill them into TYPES_HTAB. It will process only type units,
6297 therefore DW_UT_type. */
c88ee1f0 6298
78d4d2c5 6299static void
ed2dc618
SM
6300create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6301 struct dwo_file *dwo_file,
b0b6a987 6302 dwarf2_section_info *section, htab_up &types_htab,
43988095 6303 rcuh_kind section_kind)
348e048f 6304{
3019eac3 6305 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6306 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6307 bfd *abfd;
6308 const gdb_byte *info_ptr, *end_ptr;
348e048f 6309
4bdcc0c1
DE
6310 abbrev_section = (dwo_file != NULL
6311 ? &dwo_file->sections.abbrev
5989a64e 6312 : &dwarf2_per_objfile->per_bfd->abbrev);
4bdcc0c1 6313
b4f54984 6314 if (dwarf_read_debug)
43988095 6315 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6316 section->get_name (),
6317 abbrev_section->get_file_name ());
09406207 6318
96b79293 6319 section->read (objfile);
78d4d2c5 6320 info_ptr = section->buffer;
348e048f 6321
78d4d2c5
JK
6322 if (info_ptr == NULL)
6323 return;
348e048f 6324
78d4d2c5
JK
6325 /* We can't set abfd until now because the section may be empty or
6326 not present, in which case the bfd is unknown. */
96b79293 6327 abfd = section->get_bfd_owner ();
348e048f 6328
c0ab21c2
TT
6329 /* We don't use cutu_reader here because we don't need to read
6330 any dies: the signature is in the header. */
3019eac3 6331
78d4d2c5
JK
6332 end_ptr = info_ptr + section->size;
6333 while (info_ptr < end_ptr)
6334 {
78d4d2c5
JK
6335 struct signatured_type *sig_type;
6336 struct dwo_unit *dwo_tu;
6337 void **slot;
6338 const gdb_byte *ptr = info_ptr;
6339 struct comp_unit_head header;
6340 unsigned int length;
8b70b953 6341
9c541725 6342 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6343
a49dd8dd
JK
6344 /* Initialize it due to a false compiler warning. */
6345 header.signature = -1;
9c541725 6346 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6347
78d4d2c5
JK
6348 /* We need to read the type's signature in order to build the hash
6349 table, but we don't need anything else just yet. */
348e048f 6350
ed2dc618 6351 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6352 abbrev_section, ptr, section_kind);
348e048f 6353
4057dfde 6354 length = header.get_length ();
6caca83c 6355
78d4d2c5
JK
6356 /* Skip dummy type units. */
6357 if (ptr >= info_ptr + length
43988095
JK
6358 || peek_abbrev_code (abfd, ptr) == 0
6359 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6360 {
6361 info_ptr += length;
6362 continue;
6363 }
dee91e82 6364
78d4d2c5
JK
6365 if (types_htab == NULL)
6366 {
6367 if (dwo_file)
298e9637 6368 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6369 else
298e9637 6370 types_htab = allocate_signatured_type_table ();
78d4d2c5 6371 }
8b70b953 6372
78d4d2c5
JK
6373 if (dwo_file)
6374 {
6375 sig_type = NULL;
5989a64e 6376 dwo_tu = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
78d4d2c5
JK
6377 struct dwo_unit);
6378 dwo_tu->dwo_file = dwo_file;
43988095 6379 dwo_tu->signature = header.signature;
9c541725 6380 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6381 dwo_tu->section = section;
9c541725 6382 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6383 dwo_tu->length = length;
6384 }
6385 else
6386 {
6387 /* N.B.: type_offset is not usable if this type uses a DWO file.
6388 The real type_offset is in the DWO file. */
6389 dwo_tu = NULL;
5989a64e 6390 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
43988095 6391 sig_type->signature = header.signature;
9c541725 6392 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
6393 sig_type->per_cu.is_debug_types = 1;
6394 sig_type->per_cu.section = section;
9c541725 6395 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6396 sig_type->per_cu.length = length;
6397 }
6398
b0b6a987 6399 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6400 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6401 INSERT);
6402 gdb_assert (slot != NULL);
6403 if (*slot != NULL)
6404 {
9c541725 6405 sect_offset dup_sect_off;
0349ea22 6406
3019eac3
DE
6407 if (dwo_file)
6408 {
78d4d2c5
JK
6409 const struct dwo_unit *dup_tu
6410 = (const struct dwo_unit *) *slot;
6411
9c541725 6412 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6413 }
6414 else
6415 {
78d4d2c5
JK
6416 const struct signatured_type *dup_tu
6417 = (const struct signatured_type *) *slot;
6418
9c541725 6419 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6420 }
8b70b953 6421
b98664d3 6422 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6423 " the entry at offset %s, signature %s"),
6424 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6425 hex_string (header.signature));
78d4d2c5
JK
6426 }
6427 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6428
78d4d2c5 6429 if (dwarf_read_debug > 1)
9d8780f0
SM
6430 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6431 sect_offset_str (sect_off),
43988095 6432 hex_string (header.signature));
3019eac3 6433
78d4d2c5
JK
6434 info_ptr += length;
6435 }
6436}
3019eac3 6437
78d4d2c5
JK
6438/* Create the hash table of all entries in the .debug_types
6439 (or .debug_types.dwo) section(s).
6440 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6441 otherwise it is NULL.
b3c8eb43 6442
78d4d2c5 6443 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6444
78d4d2c5 6445 Note: This function processes DWO files only, not DWP files. */
348e048f 6446
78d4d2c5 6447static void
ed2dc618
SM
6448create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6449 struct dwo_file *dwo_file,
fd5866f6 6450 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6451 htab_up &types_htab)
78d4d2c5 6452{
fd5866f6
SM
6453 for (dwarf2_section_info &section : type_sections)
6454 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6455 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6456}
6457
6458/* Create the hash table of all entries in the .debug_types section,
6459 and initialize all_type_units.
6460 The result is zero if there is an error (e.g. missing .debug_types section),
6461 otherwise non-zero. */
6462
6463static int
ed2dc618 6464create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6465{
b0b6a987 6466 htab_up types_htab;
3019eac3 6467
ed2dc618 6468 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
5989a64e 6469 &dwarf2_per_objfile->per_bfd->info, types_htab,
43988095 6470 rcuh_kind::COMPILE);
ed2dc618 6471 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
5989a64e 6472 dwarf2_per_objfile->per_bfd->types, types_htab);
3019eac3
DE
6473 if (types_htab == NULL)
6474 {
5989a64e 6475 dwarf2_per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6476 return 0;
6477 }
6478
5989a64e 6479 dwarf2_per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6480
5989a64e
SM
6481 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
6482 dwarf2_per_objfile->per_bfd->all_type_units.reserve
6483 (htab_elements (dwarf2_per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6484
5989a64e 6485 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6486 add_signatured_type_cu_to_table,
5989a64e 6487 &dwarf2_per_objfile->per_bfd->all_type_units);
1fd400ff 6488
348e048f
DE
6489 return 1;
6490}
6491
5989a64e 6492/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6493 If SLOT is non-NULL, it is the entry to use in the hash table.
6494 Otherwise we find one. */
6495
6496static struct signatured_type *
ed2dc618
SM
6497add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6498 void **slot)
6aa5f3a6 6499{
5989a64e
SM
6500 if (dwarf2_per_objfile->per_bfd->all_type_units.size ()
6501 == dwarf2_per_objfile->per_bfd->all_type_units.capacity ())
6502 ++dwarf2_per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6503
5989a64e 6504 signatured_type *sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6505
af758d11
SM
6506 dwarf2_per_objfile->resize_symtabs ();
6507
5989a64e 6508 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6509 sig_type->signature = sig;
6510 sig_type->per_cu.is_debug_types = 1;
5989a64e 6511 if (dwarf2_per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6512 {
6513 sig_type->per_cu.v.quick =
5989a64e 6514 OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6515 struct dwarf2_per_cu_quick_data);
6516 }
6517
6518 if (slot == NULL)
6519 {
5989a64e 6520 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6521 sig_type, INSERT);
6522 }
6523 gdb_assert (*slot == NULL);
6524 *slot = sig_type;
6525 /* The rest of sig_type must be filled in by the caller. */
6526 return sig_type;
6527}
6528
a2ce51a0
DE
6529/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6530 Fill in SIG_ENTRY with DWO_ENTRY. */
6531
6532static void
ed2dc618 6533fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6534 struct signatured_type *sig_entry,
6535 struct dwo_unit *dwo_entry)
6536{
1859c670
SM
6537 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
6538
7ee85ab1 6539 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0 6540 gdb_assert (! sig_entry->per_cu.queued);
7188ed02 6541 gdb_assert (dwarf2_per_objfile->get_cu (&sig_entry->per_cu) == NULL);
1859c670 6542 if (per_bfd->using_index)
6aa5f3a6
DE
6543 {
6544 gdb_assert (sig_entry->per_cu.v.quick != NULL);
af758d11 6545 gdb_assert (!dwarf2_per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6546 }
6547 else
6548 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6549 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6550 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6551 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6552 gdb_assert (sig_entry->dwo_unit == NULL);
6553
6554 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6555 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6556 sig_entry->per_cu.length = dwo_entry->length;
6557 sig_entry->per_cu.reading_dwo_directly = 1;
1859c670 6558 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6559 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6560 sig_entry->dwo_unit = dwo_entry;
6561}
6562
6563/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6564 If we haven't read the TU yet, create the signatured_type data structure
6565 for a TU to be read in directly from a DWO file, bypassing the stub.
6566 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6567 using .gdb_index, then when reading a CU we want to stay in the DWO file
6568 containing that CU. Otherwise we could end up reading several other DWO
6569 files (due to comdat folding) to process the transitive closure of all the
6570 mentioned TUs, and that can be slow. The current DWO file will have every
6571 type signature that it needs.
a2ce51a0
DE
6572 We only do this for .gdb_index because in the psymtab case we already have
6573 to read all the DWOs to build the type unit groups. */
6574
6575static struct signatured_type *
6576lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6577{
5e22e966 6578 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
a2ce51a0
DE
6579 struct dwo_file *dwo_file;
6580 struct dwo_unit find_dwo_entry, *dwo_entry;
6581 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6582 void **slot;
a2ce51a0 6583
5989a64e 6584 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
a2ce51a0 6585
6aa5f3a6
DE
6586 /* If TU skeletons have been removed then we may not have read in any
6587 TUs yet. */
5989a64e
SM
6588 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6589 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6590
6591 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6592 Use the global signatured_types array to do our own comdat-folding
6593 of types. If this is the first time we're reading this TU, and
6594 the TU has an entry in .gdb_index, replace the recorded data from
6595 .gdb_index with this TU. */
a2ce51a0 6596
a2ce51a0 6597 find_sig_entry.signature = sig;
5989a64e 6598 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6599 &find_sig_entry, INSERT);
9a3c8263 6600 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6601
6602 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6603 read. Don't reassign the global entry to point to this DWO if that's
6604 the case. Also note that if the TU is already being read, it may not
6605 have come from a DWO, the program may be a mix of Fission-compiled
6606 code and non-Fission-compiled code. */
6607
6608 /* Have we already tried to read this TU?
6609 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6610 needn't exist in the global table yet). */
6611 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6612 return sig_entry;
6613
6aa5f3a6
DE
6614 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6615 dwo_unit of the TU itself. */
6616 dwo_file = cu->dwo_unit->dwo_file;
6617
a2ce51a0
DE
6618 /* Ok, this is the first time we're reading this TU. */
6619 if (dwo_file->tus == NULL)
6620 return NULL;
6621 find_dwo_entry.signature = sig;
b0b6a987
TT
6622 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6623 &find_dwo_entry);
a2ce51a0
DE
6624 if (dwo_entry == NULL)
6625 return NULL;
6626
6aa5f3a6
DE
6627 /* If the global table doesn't have an entry for this TU, add one. */
6628 if (sig_entry == NULL)
ed2dc618 6629 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6630
ed2dc618 6631 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6632 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6633 return sig_entry;
6634}
6635
a2ce51a0
DE
6636/* Subroutine of lookup_signatured_type.
6637 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6638 then try the DWP file. If the TU stub (skeleton) has been removed then
6639 it won't be in .gdb_index. */
a2ce51a0
DE
6640
6641static struct signatured_type *
6642lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6643{
5e22e966 6644 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 6645 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6646 struct dwo_unit *dwo_entry;
6647 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6648 void **slot;
a2ce51a0 6649
5989a64e 6650 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
a2ce51a0
DE
6651 gdb_assert (dwp_file != NULL);
6652
6aa5f3a6
DE
6653 /* If TU skeletons have been removed then we may not have read in any
6654 TUs yet. */
5989a64e
SM
6655 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6656 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6657
6aa5f3a6 6658 find_sig_entry.signature = sig;
5989a64e 6659 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6660 &find_sig_entry, INSERT);
9a3c8263 6661 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6662
6663 /* Have we already tried to read this TU?
6664 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6665 needn't exist in the global table yet). */
6666 if (sig_entry != NULL)
6667 return sig_entry;
6668
a2ce51a0
DE
6669 if (dwp_file->tus == NULL)
6670 return NULL;
ed2dc618 6671 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6672 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6673 if (dwo_entry == NULL)
6674 return NULL;
6675
ed2dc618
SM
6676 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6677 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6678
a2ce51a0
DE
6679 return sig_entry;
6680}
6681
380bca97 6682/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6683 Returns NULL if signature SIG is not present in the table.
6684 It is up to the caller to complain about this. */
348e048f
DE
6685
6686static struct signatured_type *
a2ce51a0 6687lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6688{
5e22e966 6689 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 6690
a2ce51a0 6691 if (cu->dwo_unit
5989a64e 6692 && dwarf2_per_objfile->per_bfd->using_index)
a2ce51a0
DE
6693 {
6694 /* We're in a DWO/DWP file, and we're using .gdb_index.
6695 These cases require special processing. */
ed2dc618 6696 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6697 return lookup_dwo_signatured_type (cu, sig);
6698 else
6699 return lookup_dwp_signatured_type (cu, sig);
6700 }
6701 else
6702 {
6703 struct signatured_type find_entry, *entry;
348e048f 6704
5989a64e 6705 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6706 return NULL;
6707 find_entry.signature = sig;
9a3c8263 6708 entry = ((struct signatured_type *)
5989a64e 6709 htab_find (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6710 &find_entry));
a2ce51a0
DE
6711 return entry;
6712 }
348e048f 6713}
18a8505e 6714
42e7ad6c 6715/* Low level DIE reading support. */
348e048f 6716
d85a05f0
DJ
6717/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6718
6719static void
6720init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6721 struct dwarf2_cu *cu,
3019eac3 6722 struct dwarf2_section_info *section,
685af9cd
TT
6723 struct dwo_file *dwo_file,
6724 struct abbrev_table *abbrev_table)
d85a05f0 6725{
fceca515 6726 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6727 reader->abfd = section->get_bfd_owner ();
d85a05f0 6728 reader->cu = cu;
3019eac3 6729 reader->dwo_file = dwo_file;
dee91e82
DE
6730 reader->die_section = section;
6731 reader->buffer = section->buffer;
f664829e 6732 reader->buffer_end = section->buffer + section->size;
685af9cd 6733 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6734}
6735
c0ab21c2 6736/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6737 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6738 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6739 already.
6740
6741 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6742 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6743 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6744 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6745 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6746 STUB_COMP_DIR may be non-NULL.
3e225074 6747 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6748 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6749 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6750 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6751 kept around for at least as long as *RESULT_READER.
6752
b0c7bfa9
DE
6753 The result is non-zero if a valid (non-dummy) DIE was found. */
6754
6755static int
4ab09049 6756read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6757 struct dwo_unit *dwo_unit,
b0c7bfa9 6758 struct die_info *stub_comp_unit_die,
a2ce51a0 6759 const char *stub_comp_dir,
b0c7bfa9 6760 struct die_reader_specs *result_reader,
d521ce57 6761 const gdb_byte **result_info_ptr,
b0c7bfa9 6762 struct die_info **result_comp_unit_die,
685af9cd 6763 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6764{
4ab09049
SM
6765 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6766 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6767 struct objfile *objfile = dwarf2_per_objfile->objfile;
b0c7bfa9 6768 bfd *abfd;
d521ce57 6769 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6770 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6771 int i,num_extra_attrs;
6772 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6773 struct die_info *comp_unit_die;
6774
b0aeadb3
DE
6775 /* At most one of these may be provided. */
6776 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6777
b0c7bfa9
DE
6778 /* These attributes aren't processed until later:
6779 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6780 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6781 referenced later. However, these attributes are found in the stub
6782 which we won't have later. In order to not impose this complication
6783 on the rest of the code, we read them here and copy them to the
6784 DWO CU/TU die. */
b0c7bfa9
DE
6785
6786 stmt_list = NULL;
6787 low_pc = NULL;
6788 high_pc = NULL;
6789 ranges = NULL;
6790 comp_dir = NULL;
6791
6792 if (stub_comp_unit_die != NULL)
6793 {
6794 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6795 DWO file. */
4ab09049 6796 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6797 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6798 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6799 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6800 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6801 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6802
a39fdb41 6803 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6804
18a8505e
AT
6805 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6806 here (if needed). We need the value before we can process
6807 DW_AT_ranges. */
a39fdb41 6808 cu->ranges_base = stub_comp_unit_die->ranges_base ();
b0c7bfa9 6809 }
a2ce51a0
DE
6810 else if (stub_comp_dir != NULL)
6811 {
6812 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6813 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6814 comp_dir->name = DW_AT_comp_dir;
6815 comp_dir->form = DW_FORM_string;
6816 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6817 DW_STRING (comp_dir) = stub_comp_dir;
6818 }
b0c7bfa9
DE
6819
6820 /* Set up for reading the DWO CU/TU. */
6821 cu->dwo_unit = dwo_unit;
685af9cd 6822 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6823 section->read (objfile);
6824 abfd = section->get_bfd_owner ();
9c541725
PA
6825 begin_info_ptr = info_ptr = (section->buffer
6826 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6827 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6828
4ab09049 6829 if (per_cu->is_debug_types)
b0c7bfa9 6830 {
4ab09049 6831 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6832
ed2dc618
SM
6833 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6834 &cu->header, section,
b0c7bfa9 6835 dwo_abbrev_section,
43988095 6836 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6837 /* This is not an assert because it can be caused by bad debug info. */
43988095 6838 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6839 {
6840 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6841 " TU at offset %s [in module %s]"),
a2ce51a0 6842 hex_string (sig_type->signature),
43988095 6843 hex_string (cu->header.signature),
9d8780f0 6844 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6845 bfd_get_filename (abfd));
6846 }
9c541725 6847 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6848 /* For DWOs coming from DWP files, we don't know the CU length
6849 nor the type's offset in the TU until now. */
4057dfde 6850 dwo_unit->length = cu->header.get_length ();
9c541725 6851 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6852
6853 /* Establish the type offset that can be used to lookup the type.
6854 For DWO files, we don't know it until now. */
9c541725
PA
6855 sig_type->type_offset_in_section
6856 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6857 }
6858 else
6859 {
ed2dc618
SM
6860 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6861 &cu->header, section,
b0c7bfa9 6862 dwo_abbrev_section,
43988095 6863 info_ptr, rcuh_kind::COMPILE);
9c541725 6864 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6865 /* For DWOs coming from DWP files, we don't know the CU length
6866 until now. */
4057dfde 6867 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6868 }
6869
685af9cd 6870 *result_dwo_abbrev_table
86de1d91
TT
6871 = abbrev_table::read (objfile, dwo_abbrev_section,
6872 cu->header.abbrev_sect_off);
685af9cd
TT
6873 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6874 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6875
6876 /* Read in the die, but leave space to copy over the attributes
6877 from the stub. This has the benefit of simplifying the rest of
6878 the code - all the work to maintain the illusion of a single
6879 DW_TAG_{compile,type}_unit DIE is done here. */
6880 num_extra_attrs = ((stmt_list != NULL)
6881 + (low_pc != NULL)
6882 + (high_pc != NULL)
6883 + (ranges != NULL)
6884 + (comp_dir != NULL));
6885 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6886 num_extra_attrs);
b0c7bfa9
DE
6887
6888 /* Copy over the attributes from the stub to the DIE we just read in. */
6889 comp_unit_die = *result_comp_unit_die;
6890 i = comp_unit_die->num_attrs;
6891 if (stmt_list != NULL)
6892 comp_unit_die->attrs[i++] = *stmt_list;
6893 if (low_pc != NULL)
6894 comp_unit_die->attrs[i++] = *low_pc;
6895 if (high_pc != NULL)
6896 comp_unit_die->attrs[i++] = *high_pc;
6897 if (ranges != NULL)
6898 comp_unit_die->attrs[i++] = *ranges;
6899 if (comp_dir != NULL)
6900 comp_unit_die->attrs[i++] = *comp_dir;
6901 comp_unit_die->num_attrs += num_extra_attrs;
6902
b4f54984 6903 if (dwarf_die_debug)
bf6af496
DE
6904 {
6905 fprintf_unfiltered (gdb_stdlog,
6906 "Read die from %s@0x%x of %s:\n",
96b79293 6907 section->get_name (),
bf6af496
DE
6908 (unsigned) (begin_info_ptr - section->buffer),
6909 bfd_get_filename (abfd));
b4f54984 6910 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6911 }
6912
b0c7bfa9
DE
6913 /* Skip dummy compilation units. */
6914 if (info_ptr >= begin_info_ptr + dwo_unit->length
6915 || peek_abbrev_code (abfd, info_ptr) == 0)
6916 return 0;
6917
6918 *result_info_ptr = info_ptr;
6919 return 1;
6920}
6921
a084a2a6
AT
6922/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6923 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6924 signature is part of the header. */
6925static gdb::optional<ULONGEST>
6926lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6927{
6928 if (cu->header.version >= 5)
6929 return cu->header.signature;
6930 struct attribute *attr;
6931 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6932 if (attr == nullptr)
6933 return gdb::optional<ULONGEST> ();
6934 return DW_UNSND (attr);
6935}
6936
c0ab21c2 6937/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6938 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6939 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6940
6941static struct dwo_unit *
4ab09049 6942lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 6943{
4ab09049 6944 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6945 struct dwo_unit *dwo_unit;
c0ab21c2 6946 const char *comp_dir;
b0c7bfa9 6947
a2ce51a0
DE
6948 gdb_assert (cu != NULL);
6949
b0c7bfa9 6950 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6951 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6952 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 6953
4ab09049
SM
6954 if (per_cu->is_debug_types)
6955 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
6956 else
6957 {
a084a2a6 6958 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 6959
a084a2a6 6960 if (!signature.has_value ())
b0c7bfa9
DE
6961 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6962 " [in module %s]"),
4ab09049
SM
6963 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6964
6965 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
6966 }
6967
b0c7bfa9
DE
6968 return dwo_unit;
6969}
6970
c0ab21c2 6971/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6972 See it for a description of the parameters.
fcd3b13d 6973 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6974
c0ab21c2 6975void
9e021579
SM
6976cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6977 dwarf2_per_objfile *per_objfile,
2e671100 6978 dwarf2_cu *existing_cu)
a2ce51a0 6979{
a2ce51a0 6980 struct signatured_type *sig_type;
a2ce51a0
DE
6981
6982 /* Verify we can do the following downcast, and that we have the
6983 data we need. */
6984 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6985 sig_type = (struct signatured_type *) this_cu;
6986 gdb_assert (sig_type->dwo_unit != NULL);
6987
2e671100
SM
6988 dwarf2_cu *cu;
6989
6990 if (existing_cu != nullptr)
6aa5f3a6 6991 {
2e671100
SM
6992 cu = existing_cu;
6993 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6994 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6995 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6996 }
6997 else
6998 {
7188ed02
SM
6999 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7000 in per_objfile yet. */
7001 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
9e021579 7002 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
2e671100 7003 cu = m_new_cu.get ();
6aa5f3a6
DE
7004 }
7005
7006 /* A future optimization, if needed, would be to use an existing
7007 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7008 could share abbrev tables. */
a2ce51a0 7009
2e671100 7010 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
a2ce51a0
DE
7011 NULL /* stub_comp_unit_die */,
7012 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 7013 this, &info_ptr,
3e225074 7014 &comp_unit_die,
c0ab21c2 7015 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
7016 {
7017 /* Dummy die. */
c0ab21c2 7018 dummy_p = true;
a2ce51a0 7019 }
a2ce51a0
DE
7020}
7021
fd820528 7022/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7023 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7024
f4dc4d17
DE
7025 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7026 Otherwise the table specified in the comp unit header is read in and used.
7027 This is an optimization for when we already have the abbrev table.
7028
2e671100
SM
7029 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7030 allocated. */
aaa75496 7031
ab432490
SM
7032cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7033 dwarf2_per_objfile *dwarf2_per_objfile,
c0ab21c2 7034 struct abbrev_table *abbrev_table,
2e671100 7035 dwarf2_cu *existing_cu,
c0ab21c2
TT
7036 bool skip_partial)
7037 : die_reader_specs {},
6751ebae 7038 m_this_cu (this_cu)
c906108c 7039{
dee91e82 7040 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7041 struct dwarf2_section_info *section = this_cu->section;
96b79293 7042 bfd *abfd = section->get_bfd_owner ();
c0ab21c2 7043 const gdb_byte *begin_info_ptr;
dee91e82 7044 struct signatured_type *sig_type = NULL;
4bdcc0c1 7045 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7046 /* Non-zero if CU currently points to a DWO file and we need to
7047 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7048 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7049 int rereading_dwo_cu = 0;
c906108c 7050
b4f54984 7051 if (dwarf_die_debug)
9d8780f0 7052 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7053 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7054 sect_offset_str (this_cu->sect_off));
09406207 7055
a2ce51a0
DE
7056 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7057 file (instead of going through the stub), short-circuit all of this. */
7058 if (this_cu->reading_dwo_directly)
7059 {
7060 /* Narrow down the scope of possibilities to have to understand. */
7061 gdb_assert (this_cu->is_debug_types);
7062 gdb_assert (abbrev_table == NULL);
2e671100 7063 init_tu_and_read_dwo_dies (this_cu, dwarf2_per_objfile, existing_cu);
a2ce51a0
DE
7064 return;
7065 }
7066
dee91e82 7067 /* This is cheap if the section is already read in. */
96b79293 7068 section->read (objfile);
dee91e82 7069
9c541725 7070 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7071
7072 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7073
2e671100
SM
7074 dwarf2_cu *cu;
7075
7076 if (existing_cu != nullptr)
dee91e82 7077 {
2e671100 7078 cu = existing_cu;
42e7ad6c
DE
7079 /* If this CU is from a DWO file we need to start over, we need to
7080 refetch the attributes from the skeleton CU.
7081 This could be optimized by retrieving those attributes from when we
7082 were here the first time: the previous comp_unit_die was stored in
7083 comp_unit_obstack. But there's no data yet that we need this
7084 optimization. */
7085 if (cu->dwo_unit != NULL)
7086 rereading_dwo_cu = 1;
dee91e82
DE
7087 }
7088 else
7089 {
7188ed02
SM
7090 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7091 in per_objfile yet. */
7092 gdb_assert (dwarf2_per_objfile->get_cu (this_cu) == nullptr);
9e021579 7093 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
c0ab21c2 7094 cu = m_new_cu.get ();
42e7ad6c 7095 }
dee91e82 7096
b0c7bfa9 7097 /* Get the header. */
9c541725 7098 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7099 {
7100 /* We already have the header, there's no need to read it in again. */
9c541725 7101 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7102 }
7103 else
7104 {
3019eac3 7105 if (this_cu->is_debug_types)
dee91e82 7106 {
ed2dc618
SM
7107 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7108 &cu->header, section,
4bdcc0c1 7109 abbrev_section, info_ptr,
43988095 7110 rcuh_kind::TYPE);
dee91e82 7111
42e7ad6c
DE
7112 /* Since per_cu is the first member of struct signatured_type,
7113 we can go from a pointer to one to a pointer to the other. */
7114 sig_type = (struct signatured_type *) this_cu;
43988095 7115 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7116 gdb_assert (sig_type->type_offset_in_tu
7117 == cu->header.type_cu_offset_in_tu);
7118 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7119
42e7ad6c
DE
7120 /* LENGTH has not been set yet for type units if we're
7121 using .gdb_index. */
4057dfde 7122 this_cu->length = cu->header.get_length ();
3019eac3
DE
7123
7124 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7125 sig_type->type_offset_in_section =
7126 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7127
7128 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7129 }
7130 else
7131 {
ed2dc618
SM
7132 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7133 &cu->header, section,
4bdcc0c1 7134 abbrev_section,
43988095
JK
7135 info_ptr,
7136 rcuh_kind::COMPILE);
dee91e82 7137
9c541725 7138 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
7139 if (this_cu->length == 0)
7140 this_cu->length = cu->header.get_length ();
7141 else
7142 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7143 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7144 }
7145 }
10b3939b 7146
6caca83c 7147 /* Skip dummy compilation units. */
dee91e82 7148 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7149 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7150 {
7151 dummy_p = true;
7152 return;
7153 }
6caca83c 7154
433df2d4
DE
7155 /* If we don't have them yet, read the abbrevs for this compilation unit.
7156 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7157 done. */
f4dc4d17 7158 if (abbrev_table != NULL)
685af9cd
TT
7159 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7160 else
f4dc4d17 7161 {
c0ab21c2 7162 m_abbrev_table_holder
86de1d91
TT
7163 = abbrev_table::read (objfile, abbrev_section,
7164 cu->header.abbrev_sect_off);
c0ab21c2 7165 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7166 }
af703f96 7167
dee91e82 7168 /* Read the top level CU/TU die. */
c0ab21c2 7169 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7170 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7171
58f0c718 7172 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7173 {
7174 dummy_p = true;
7175 return;
7176 }
58f0c718 7177
b0c7bfa9 7178 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7179 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7180 table from the DWO file and pass the ownership over to us. It will be
7181 referenced from READER, so we must make sure to free it after we're done
7182 with READER.
7183
b0c7bfa9
DE
7184 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7185 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7186 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7187 if (dwo_name != nullptr)
3019eac3 7188 {
3019eac3 7189 struct dwo_unit *dwo_unit;
b0c7bfa9 7190 struct die_info *dwo_comp_unit_die;
3019eac3 7191
3e225074 7192 if (comp_unit_die->has_children)
6a506a2d 7193 {
b98664d3 7194 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7195 " has children (offset %s) [in module %s]"),
7196 sect_offset_str (this_cu->sect_off),
7197 bfd_get_filename (abfd));
6a506a2d 7198 }
4ab09049 7199 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 7200 if (dwo_unit != NULL)
3019eac3 7201 {
4ab09049 7202 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 7203 comp_unit_die, NULL,
c0ab21c2 7204 this, &info_ptr,
3e225074 7205 &dwo_comp_unit_die,
c0ab21c2 7206 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7207 {
7208 /* Dummy die. */
c0ab21c2 7209 dummy_p = true;
6a506a2d
DE
7210 return;
7211 }
7212 comp_unit_die = dwo_comp_unit_die;
7213 }
7214 else
7215 {
7216 /* Yikes, we couldn't find the rest of the DIE, we only have
7217 the stub. A complaint has already been logged. There's
7218 not much more we can do except pass on the stub DIE to
7219 die_reader_func. We don't want to throw an error on bad
7220 debug info. */
3019eac3
DE
7221 }
7222 }
c0ab21c2 7223}
3019eac3 7224
6751ebae
TT
7225void
7226cutu_reader::keep ()
c0ab21c2 7227{
b0c7bfa9 7228 /* Done, clean up. */
6751ebae
TT
7229 gdb_assert (!dummy_p);
7230 if (m_new_cu != NULL)
348e048f 7231 {
7188ed02
SM
7232 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7233 now. */
7234 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7235 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
348e048f 7236 }
dee91e82
DE
7237}
7238
18a8505e
AT
7239/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7240 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7241 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7242
7243 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7244 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7245
7246 We fill in THIS_CU->length.
7247
dee91e82 7248 THIS_CU->cu is always freed when done.
3019eac3 7249 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7250 to care whether it refers to the "main" CU or the DWO CU.
7251
7252 When parent_cu is passed, it is used to provide a default value for
7253 str_offsets_base and addr_base from the parent. */
dee91e82 7254
ab432490
SM
7255cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7256 dwarf2_per_objfile *dwarf2_per_objfile,
c0ab21c2
TT
7257 struct dwarf2_cu *parent_cu,
7258 struct dwo_file *dwo_file)
7259 : die_reader_specs {},
7260 m_this_cu (this_cu)
dee91e82
DE
7261{
7262 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7263 struct dwarf2_section_info *section = this_cu->section;
96b79293 7264 bfd *abfd = section->get_bfd_owner ();
33e80786 7265 struct dwarf2_section_info *abbrev_section;
d521ce57 7266 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7267
b4f54984 7268 if (dwarf_die_debug)
9d8780f0 7269 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7270 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7271 sect_offset_str (this_cu->sect_off));
09406207 7272
7188ed02 7273 gdb_assert (dwarf2_per_objfile->get_cu (this_cu) == nullptr);
dee91e82 7274
33e80786
DE
7275 abbrev_section = (dwo_file != NULL
7276 ? &dwo_file->sections.abbrev
7277 : get_abbrev_section_for_cu (this_cu));
7278
dee91e82 7279 /* This is cheap if the section is already read in. */
96b79293 7280 section->read (objfile);
dee91e82 7281
9e021579 7282 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
dee91e82 7283
9c541725 7284 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7285 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7286 &m_new_cu->header, section,
4bdcc0c1 7287 abbrev_section, info_ptr,
43988095
JK
7288 (this_cu->is_debug_types
7289 ? rcuh_kind::TYPE
7290 : rcuh_kind::COMPILE));
dee91e82 7291
18a8505e
AT
7292 if (parent_cu != nullptr)
7293 {
c0ab21c2
TT
7294 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7295 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7296 }
4057dfde 7297 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7298
7299 /* Skip dummy compilation units. */
7300 if (info_ptr >= begin_info_ptr + this_cu->length
7301 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7302 {
7303 dummy_p = true;
7304 return;
7305 }
72bf9492 7306
c0ab21c2 7307 m_abbrev_table_holder
86de1d91
TT
7308 = abbrev_table::read (objfile, abbrev_section,
7309 m_new_cu->header.abbrev_sect_off);
dee91e82 7310
c0ab21c2
TT
7311 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7312 m_abbrev_table_holder.get ());
3e225074 7313 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7314}
7315
0018ea6f
DE
7316\f
7317/* Type Unit Groups.
dee91e82 7318
0018ea6f
DE
7319 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7320 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7321 so that all types coming from the same compilation (.o file) are grouped
7322 together. A future step could be to put the types in the same symtab as
7323 the CU the types ultimately came from. */
ff013f42 7324
f4dc4d17
DE
7325static hashval_t
7326hash_type_unit_group (const void *item)
7327{
9a3c8263
SM
7328 const struct type_unit_group *tu_group
7329 = (const struct type_unit_group *) item;
f4dc4d17 7330
094b34ac 7331 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7332}
348e048f
DE
7333
7334static int
f4dc4d17 7335eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7336{
9a3c8263
SM
7337 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7338 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7339
094b34ac 7340 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7341}
348e048f 7342
f4dc4d17
DE
7343/* Allocate a hash table for type unit groups. */
7344
eaa5fa8b 7345static htab_up
298e9637 7346allocate_type_unit_groups_table ()
f4dc4d17 7347{
eaa5fa8b
TT
7348 return htab_up (htab_create_alloc (3,
7349 hash_type_unit_group,
7350 eq_type_unit_group,
7351 NULL, xcalloc, xfree));
f4dc4d17 7352}
dee91e82 7353
f4dc4d17
DE
7354/* Type units that don't have DW_AT_stmt_list are grouped into their own
7355 partial symtabs. We combine several TUs per psymtab to not let the size
7356 of any one psymtab grow too big. */
7357#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7358#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7359
094b34ac 7360/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7361 Create the type_unit_group object used to hold one or more TUs. */
7362
7363static struct type_unit_group *
094b34ac 7364create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7365{
5e22e966 7366 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
1859c670 7367 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
094b34ac 7368 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7369 struct type_unit_group *tu_group;
f4dc4d17 7370
5989a64e 7371 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
f4dc4d17 7372 struct type_unit_group);
094b34ac 7373 per_cu = &tu_group->per_cu;
1859c670 7374 per_cu->per_bfd = per_bfd;
f4dc4d17 7375
1859c670 7376 if (per_bfd->using_index)
094b34ac 7377 {
1859c670 7378 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7379 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7380 }
7381 else
7382 {
9c541725 7383 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7384 dwarf2_psymtab *pst;
528e1572 7385 std::string name;
094b34ac
DE
7386
7387 /* Give the symtab a useful name for debug purposes. */
7388 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7389 name = string_printf ("<type_units_%d>",
7390 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7391 else
528e1572 7392 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7393
7aa104c4 7394 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, name.c_str ());
6d94535f 7395 pst->anonymous = true;
094b34ac 7396 }
f4dc4d17 7397
094b34ac 7398 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7399 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7400
7401 return tu_group;
7402}
7403
094b34ac
DE
7404/* Look up the type_unit_group for type unit CU, and create it if necessary.
7405 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7406
7407static struct type_unit_group *
ff39bb5e 7408get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7409{
5e22e966 7410 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
5989a64e 7411 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7412 struct type_unit_group *tu_group;
7413 void **slot;
7414 unsigned int line_offset;
7415 struct type_unit_group type_unit_group_for_lookup;
7416
5989a64e
SM
7417 if (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL)
7418 dwarf2_per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7419
7420 /* Do we need to create a new group, or can we use an existing one? */
7421
7422 if (stmt_list)
7423 {
7424 line_offset = DW_UNSND (stmt_list);
7425 ++tu_stats->nr_symtab_sharers;
7426 }
7427 else
7428 {
7429 /* Ugh, no stmt_list. Rare, but we have to handle it.
7430 We can do various things here like create one group per TU or
7431 spread them over multiple groups to split up the expansion work.
7432 To avoid worst case scenarios (too many groups or too large groups)
7433 we, umm, group them in bunches. */
7434 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7435 | (tu_stats->nr_stmt_less_type_units
7436 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7437 ++tu_stats->nr_stmt_less_type_units;
7438 }
7439
094b34ac 7440 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7441 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
5989a64e 7442 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7443 &type_unit_group_for_lookup, INSERT);
7444 if (*slot != NULL)
7445 {
9a3c8263 7446 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7447 gdb_assert (tu_group != NULL);
7448 }
7449 else
7450 {
9c541725 7451 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7452 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7453 *slot = tu_group;
7454 ++tu_stats->nr_symtabs;
7455 }
7456
7457 return tu_group;
7458}
0018ea6f
DE
7459\f
7460/* Partial symbol tables. */
7461
7462/* Create a psymtab named NAME and assign it to PER_CU.
7463
7464 The caller must fill in the following details:
7465 dirname, textlow, texthigh. */
7466
891813be 7467static dwarf2_psymtab *
7aa104c4
SM
7468create_partial_symtab (dwarf2_per_cu_data *per_cu,
7469 dwarf2_per_objfile *per_objfile,
7470 const char *name)
0018ea6f 7471{
7aa104c4 7472 struct objfile *objfile = per_objfile->objfile;
891813be 7473 dwarf2_psymtab *pst;
0018ea6f 7474
9f4e76a4 7475 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7476
6d94535f 7477 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7478
7479 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7480 per_cu->v.psymtab = pst;
7481
7482 return pst;
7483}
7484
c0ab21c2 7485/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7486
7487static void
7488process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7489 const gdb_byte *info_ptr,
0018ea6f 7490 struct die_info *comp_unit_die,
c0ab21c2 7491 enum language pretend_language)
0018ea6f
DE
7492{
7493 struct dwarf2_cu *cu = reader->cu;
7aa104c4
SM
7494 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7495 struct objfile *objfile = per_objfile->objfile;
08feed99 7496 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7497 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7498 CORE_ADDR baseaddr;
7499 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7500 dwarf2_psymtab *pst;
3a2b436a 7501 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7502 const char *filename;
0018ea6f 7503
0018ea6f
DE
7504 gdb_assert (! per_cu->is_debug_types);
7505
c0ab21c2 7506 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7507
0018ea6f 7508 /* Allocate a new partial symbol table structure. */
2e927613
TV
7509 gdb::unique_xmalloc_ptr<char> debug_filename;
7510 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7511 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7512 if (filename == NULL)
0018ea6f 7513 filename = "";
2e927613
TV
7514 else if (strcmp (filename, artificial) == 0)
7515 {
7516 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7517 sect_offset_str (per_cu->sect_off),
7518 (char *) NULL));
2e927613
TV
7519 filename = debug_filename.get ();
7520 }
0018ea6f 7521
7aa104c4 7522 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7523
7524 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7525 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7526
b3b3bada 7527 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7528
7529 dwarf2_find_base_address (comp_unit_die, cu);
7530
7531 /* Possibly set the default values of LOWPC and HIGHPC from
7532 `DW_AT_ranges'. */
3a2b436a
JK
7533 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7534 &best_highpc, cu, pst);
7535 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7536 {
7537 CORE_ADDR low
7538 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7539 - baseaddr);
7540 CORE_ADDR high
7541 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7542 - baseaddr - 1);
7543 /* Store the contiguous range if it is not empty; it can be
7544 empty for CUs with no code. */
d320c2b5
TT
7545 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7546 low, high, pst);
79748972 7547 }
0018ea6f
DE
7548
7549 /* Check if comp unit has_children.
7550 If so, read the rest of the partial symbols from this comp unit.
7551 If not, there's no more debug_info for this comp unit. */
3e225074 7552 if (comp_unit_die->has_children)
0018ea6f
DE
7553 {
7554 struct partial_die_info *first_die;
7555 CORE_ADDR lowpc, highpc;
7556
7557 lowpc = ((CORE_ADDR) -1);
7558 highpc = ((CORE_ADDR) 0);
7559
7560 first_die = load_partial_dies (reader, info_ptr, 1);
7561
7562 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7563 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7564
7565 /* If we didn't find a lowpc, set it to highpc to avoid
7566 complaints from `maint check'. */
7567 if (lowpc == ((CORE_ADDR) -1))
7568 lowpc = highpc;
7569
7570 /* If the compilation unit didn't have an explicit address range,
7571 then use the information extracted from its child dies. */
e385593e 7572 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7573 {
7574 best_lowpc = lowpc;
7575 best_highpc = highpc;
7576 }
7577 }
4ae976d1 7578 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7579 best_lowpc + baseaddr)
7580 - baseaddr);
4ae976d1 7581 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7582 best_highpc + baseaddr)
7583 - baseaddr);
0018ea6f 7584
8763cede 7585 end_psymtab_common (objfile, pst);
0018ea6f 7586
ae640021 7587 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7588 {
7589 int i;
ae640021 7590 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7591
7592 /* Fill in 'dependencies' here; we fill in 'users' in a
7593 post-pass. */
7594 pst->number_of_dependencies = len;
a9342b62
TT
7595 pst->dependencies
7596 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7597 for (i = 0; i < len; ++i)
7598 {
7599 pst->dependencies[i]
7600 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7601 }
0018ea6f 7602
ae640021 7603 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7604 }
7605
7606 /* Get the list of files included in the current compilation unit,
7607 and build a psymtab for each of them. */
7608 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7609
b4f54984 7610 if (dwarf_read_debug)
b926417a
TT
7611 fprintf_unfiltered (gdb_stdlog,
7612 "Psymtab for %s unit @%s: %s - %s"
7613 ", %d global, %d static syms\n",
7614 per_cu->is_debug_types ? "type" : "comp",
7615 sect_offset_str (per_cu->sect_off),
7616 paddress (gdbarch, pst->text_low (objfile)),
7617 paddress (gdbarch, pst->text_high (objfile)),
7618 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7619}
7620
7621/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7622 Process compilation unit THIS_CU for a psymtab. */
7623
7624static void
ab432490
SM
7625process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7626 dwarf2_per_objfile *per_objfile,
135f5437 7627 bool want_partial_unit,
b93601f3 7628 enum language pretend_language)
0018ea6f
DE
7629{
7630 /* If this compilation unit was already read in, free the
7631 cached copy in order to read it in again. This is
7632 necessary because we skipped some symbols when we first
7633 read in the compilation unit (see load_partial_dies).
7634 This problem could be avoided, but the benefit is unclear. */
7188ed02 7635 per_objfile->remove_cu (this_cu);
0018ea6f 7636
2e671100 7637 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2 7638
58990295
TV
7639 switch (reader.comp_unit_die->tag)
7640 {
7641 case DW_TAG_compile_unit:
7642 this_cu->unit_type = DW_UT_compile;
7643 break;
7644 case DW_TAG_partial_unit:
7645 this_cu->unit_type = DW_UT_partial;
7646 break;
7647 default:
7648 abort ();
7649 }
7650
c0ab21c2 7651 if (reader.dummy_p)
f1902523 7652 {
c0ab21c2 7653 /* Nothing. */
f1902523 7654 }
c0ab21c2 7655 else if (this_cu->is_debug_types)
3e225074
TT
7656 build_type_psymtabs_reader (&reader, reader.info_ptr,
7657 reader.comp_unit_die);
135f5437
TT
7658 else if (want_partial_unit
7659 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7660 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7661 reader.comp_unit_die,
c0ab21c2 7662 pretend_language);
0018ea6f 7663
7188ed02 7664 this_cu->lang = reader.cu->language;
58990295 7665
0018ea6f 7666 /* Age out any secondary CUs. */
7188ed02 7667 per_objfile->age_comp_units ();
0018ea6f 7668}
f4dc4d17
DE
7669
7670/* Reader function for build_type_psymtabs. */
7671
7672static void
7673build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7674 const gdb_byte *info_ptr,
3e225074 7675 struct die_info *type_unit_die)
f4dc4d17 7676{
5e22e966 7677 struct dwarf2_per_objfile *dwarf2_per_objfile = reader->cu->per_objfile;
f4dc4d17
DE
7678 struct objfile *objfile = dwarf2_per_objfile->objfile;
7679 struct dwarf2_cu *cu = reader->cu;
7680 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7681 struct signatured_type *sig_type;
f4dc4d17
DE
7682 struct type_unit_group *tu_group;
7683 struct attribute *attr;
7684 struct partial_die_info *first_die;
7685 CORE_ADDR lowpc, highpc;
891813be 7686 dwarf2_psymtab *pst;
f4dc4d17 7687
0186c6a7
DE
7688 gdb_assert (per_cu->is_debug_types);
7689 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7690
3e225074 7691 if (! type_unit_die->has_children)
f4dc4d17
DE
7692 return;
7693
052c8bb8 7694 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7695 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7696
df07e2c7 7697 if (tu_group->tus == nullptr)
a8b3b8e9 7698 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7699 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7700
7701 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7aa104c4 7702 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, "");
6d94535f 7703 pst->anonymous = true;
f4dc4d17
DE
7704
7705 first_die = load_partial_dies (reader, info_ptr, 1);
7706
7707 lowpc = (CORE_ADDR) -1;
7708 highpc = (CORE_ADDR) 0;
7709 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7710
8763cede 7711 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7712}
7713
73051182
DE
7714/* Struct used to sort TUs by their abbreviation table offset. */
7715
7716struct tu_abbrev_offset
7717{
b2bdb8cf
SM
7718 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7719 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7720 {}
7721
7722 signatured_type *sig_type;
73051182
DE
7723 sect_offset abbrev_offset;
7724};
7725
484cf504 7726/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7727
484cf504
TT
7728static bool
7729sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7730 const struct tu_abbrev_offset &b)
73051182 7731{
484cf504 7732 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7733}
7734
7735/* Efficiently read all the type units.
7736 This does the bulk of the work for build_type_psymtabs.
7737
7738 The efficiency is because we sort TUs by the abbrev table they use and
7739 only read each abbrev table once. In one program there are 200K TUs
7740 sharing 8K abbrev tables.
7741
7742 The main purpose of this function is to support building the
5989a64e 7743 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7744 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7745 can collapse the search space by grouping them by stmt_list.
7746 The savings can be significant, in the same program from above the 200K TUs
7747 share 8K stmt_list tables.
7748
7749 FUNC is expected to call get_type_unit_group, which will create the
7750 struct type_unit_group if necessary and add it to
5989a64e 7751 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7752
7753static void
ed2dc618 7754build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7755{
5989a64e 7756 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
685af9cd 7757 abbrev_table_up abbrev_table;
73051182 7758 sect_offset abbrev_offset;
73051182
DE
7759
7760 /* It's up to the caller to not call us multiple times. */
5989a64e 7761 gdb_assert (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7762
5989a64e 7763 if (dwarf2_per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7764 return;
7765
7766 /* TUs typically share abbrev tables, and there can be way more TUs than
7767 abbrev tables. Sort by abbrev table to reduce the number of times we
7768 read each abbrev table in.
7769 Alternatives are to punt or to maintain a cache of abbrev tables.
7770 This is simpler and efficient enough for now.
7771
7772 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7773 symtab to use). Typically TUs with the same abbrev offset have the same
7774 stmt_list value too so in practice this should work well.
7775
7776 The basic algorithm here is:
7777
7778 sort TUs by abbrev table
7779 for each TU with same abbrev table:
7780 read abbrev table if first user
7781 read TU top level DIE
7782 [IWBN if DWO skeletons had DW_AT_stmt_list]
7783 call FUNC */
7784
b4f54984 7785 if (dwarf_read_debug)
73051182
DE
7786 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7787
7788 /* Sort in a separate table to maintain the order of all_type_units
7789 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7790 std::vector<tu_abbrev_offset> sorted_by_abbrev;
5989a64e 7791 sorted_by_abbrev.reserve (dwarf2_per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7792
5989a64e 7793 for (signatured_type *sig_type : dwarf2_per_objfile->per_bfd->all_type_units)
b2bdb8cf
SM
7794 sorted_by_abbrev.emplace_back
7795 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7796 sig_type->per_cu.section,
7797 sig_type->per_cu.sect_off));
73051182 7798
484cf504
TT
7799 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7800 sort_tu_by_abbrev_offset);
73051182 7801
9c541725 7802 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7803
b2bdb8cf 7804 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7805 {
73051182
DE
7806 /* Switch to the next abbrev table if necessary. */
7807 if (abbrev_table == NULL
b2bdb8cf 7808 || tu.abbrev_offset != abbrev_offset)
73051182 7809 {
b2bdb8cf 7810 abbrev_offset = tu.abbrev_offset;
73051182 7811 abbrev_table =
86de1d91 7812 abbrev_table::read (dwarf2_per_objfile->objfile,
5989a64e 7813 &dwarf2_per_objfile->per_bfd->abbrev,
86de1d91 7814 abbrev_offset);
73051182
DE
7815 ++tu_stats->nr_uniq_abbrev_tables;
7816 }
7817
ab432490 7818 cutu_reader reader (&tu.sig_type->per_cu, dwarf2_per_objfile,
2e671100 7819 abbrev_table.get (), nullptr, false);
c0ab21c2
TT
7820 if (!reader.dummy_p)
7821 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7822 reader.comp_unit_die);
73051182 7823 }
6aa5f3a6 7824}
73051182 7825
6aa5f3a6
DE
7826/* Print collected type unit statistics. */
7827
7828static void
ed2dc618 7829print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6 7830{
5989a64e 7831 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
6aa5f3a6
DE
7832
7833 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf 7834 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
5989a64e 7835 dwarf2_per_objfile->per_bfd->all_type_units.size ());
6aa5f3a6
DE
7836 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7837 tu_stats->nr_uniq_abbrev_tables);
7838 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7839 tu_stats->nr_symtabs);
7840 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7841 tu_stats->nr_symtab_sharers);
7842 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7843 tu_stats->nr_stmt_less_type_units);
7844 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7845 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7846}
7847
f4dc4d17
DE
7848/* Traversal function for build_type_psymtabs. */
7849
7850static int
7851build_type_psymtab_dependencies (void **slot, void *info)
7852{
ed2dc618
SM
7853 struct dwarf2_per_objfile *dwarf2_per_objfile
7854 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
7855 struct objfile *objfile = dwarf2_per_objfile->objfile;
7856 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7857 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7858 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7859 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7860 int i;
7861
7862 gdb_assert (len > 0);
197400e8 7863 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7864
7865 pst->number_of_dependencies = len;
a9342b62 7866 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7867 for (i = 0; i < len; ++i)
f4dc4d17 7868 {
df07e2c7 7869 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7870 gdb_assert (iter->per_cu.is_debug_types);
7871 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7872 iter->type_unit_group = tu_group;
f4dc4d17
DE
7873 }
7874
df07e2c7
AB
7875 delete tu_group->tus;
7876 tu_group->tus = nullptr;
348e048f
DE
7877
7878 return 1;
7879}
7880
7881/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7882 Build partial symbol tables for the .debug_types comp-units. */
7883
7884static void
ed2dc618 7885build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 7886{
ed2dc618 7887 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
7888 return;
7889
ed2dc618 7890 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 7891}
f4dc4d17 7892
6aa5f3a6
DE
7893/* Traversal function for process_skeletonless_type_unit.
7894 Read a TU in a DWO file and build partial symbols for it. */
7895
7896static int
7897process_skeletonless_type_unit (void **slot, void *info)
7898{
7899 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
7900 struct dwarf2_per_objfile *dwarf2_per_objfile
7901 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
7902 struct signatured_type find_entry, *entry;
7903
7904 /* If this TU doesn't exist in the global table, add it and read it in. */
7905
5989a64e
SM
7906 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
7907 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7908
7909 find_entry.signature = dwo_unit->signature;
5989a64e 7910 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7911 &find_entry, INSERT);
6aa5f3a6
DE
7912 /* If we've already seen this type there's nothing to do. What's happening
7913 is we're doing our own version of comdat-folding here. */
7914 if (*slot != NULL)
7915 return 1;
7916
7917 /* This does the job that create_all_type_units would have done for
7918 this TU. */
ed2dc618
SM
7919 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7920 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7921 *slot = entry;
7922
7923 /* This does the job that build_type_psymtabs_1 would have done. */
2e671100
SM
7924 cutu_reader reader (&entry->per_cu, dwarf2_per_objfile, nullptr, nullptr,
7925 false);
c0ab21c2
TT
7926 if (!reader.dummy_p)
7927 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7928 reader.comp_unit_die);
6aa5f3a6
DE
7929
7930 return 1;
7931}
7932
7933/* Traversal function for process_skeletonless_type_units. */
7934
7935static int
7936process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7937{
7938 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7939
7940 if (dwo_file->tus != NULL)
b0b6a987
TT
7941 htab_traverse_noresize (dwo_file->tus.get (),
7942 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7943
7944 return 1;
7945}
7946
7947/* Scan all TUs of DWO files, verifying we've processed them.
7948 This is needed in case a TU was emitted without its skeleton.
7949 Note: This can't be done until we know what all the DWO files are. */
7950
7951static void
ed2dc618 7952process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7953{
7954 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 7955 if (get_dwp_file (dwarf2_per_objfile) == NULL
5989a64e 7956 && dwarf2_per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 7957 {
5989a64e 7958 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 7959 process_dwo_file_for_skeletonless_type_units,
ed2dc618 7960 dwarf2_per_objfile);
6aa5f3a6 7961 }
348e048f
DE
7962}
7963
ed2dc618 7964/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7965
7966static void
ed2dc618 7967set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 7968{
5989a64e 7969 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
95554aad 7970 {
891813be 7971 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7972
36586728
TT
7973 if (pst == NULL)
7974 continue;
7975
b76e467d 7976 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7977 {
7978 /* Set the 'user' field only if it is not already set. */
7979 if (pst->dependencies[j]->user == NULL)
7980 pst->dependencies[j]->user = pst;
7981 }
7982 }
7983}
7984
93311388
DE
7985/* Build the partial symbol table by doing a quick pass through the
7986 .debug_info and .debug_abbrev sections. */
72bf9492 7987
93311388 7988static void
ed2dc618 7989dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 7990{
ed2dc618 7991 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 7992
b4f54984 7993 if (dwarf_read_debug)
45cfd468
DE
7994 {
7995 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7996 objfile_name (objfile));
45cfd468
DE
7997 }
7998
76935768 7999 scoped_restore restore_reading_psyms
5989a64e 8000 = make_scoped_restore (&dwarf2_per_objfile->per_bfd->reading_partial_symbols,
76935768 8001 true);
98bfdba5 8002
5989a64e 8003 dwarf2_per_objfile->per_bfd->info.read (objfile);
91c24f0a 8004
93311388
DE
8005 /* Any cached compilation units will be linked by the per-objfile
8006 read_in_chain. Make sure to free them when we're done. */
11ed8cad 8007 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 8008
ed2dc618 8009 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8010
ed2dc618 8011 create_all_comp_units (dwarf2_per_objfile);
c906108c 8012
60606b2c
TT
8013 /* Create a temporary address map on a temporary obstack. We later
8014 copy this to the final obstack. */
8268c778 8015 auto_obstack temp_obstack;
791afaa2
TT
8016
8017 scoped_restore save_psymtabs_addrmap
d320c2b5 8018 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8019 addrmap_create_mutable (&temp_obstack));
72bf9492 8020
5989a64e 8021 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3d5afab3
TV
8022 {
8023 if (per_cu->v.psymtab != NULL)
8024 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8025 continue;
ab432490
SM
8026 process_psymtab_comp_unit (per_cu, dwarf2_per_objfile, false,
8027 language_minimal);
3d5afab3 8028 }
ff013f42 8029
6aa5f3a6 8030 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8031 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8032
8033 /* Now that all TUs have been processed we can fill in the dependencies. */
5989a64e 8034 if (dwarf2_per_objfile->per_bfd->type_unit_groups != NULL)
6aa5f3a6 8035 {
5989a64e 8036 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
ed2dc618 8037 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8038 }
8039
b4f54984 8040 if (dwarf_read_debug)
ed2dc618 8041 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8042
ed2dc618 8043 set_partial_user (dwarf2_per_objfile);
95554aad 8044
d320c2b5
TT
8045 objfile->partial_symtabs->psymtabs_addrmap
8046 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8047 objfile->partial_symtabs->obstack ());
791afaa2
TT
8048 /* At this point we want to keep the address map. */
8049 save_psymtabs_addrmap.release ();
ff013f42 8050
b4f54984 8051 if (dwarf_read_debug)
45cfd468 8052 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8053 objfile_name (objfile));
ae038cb0
DJ
8054}
8055
dee91e82
DE
8056/* Load the partial DIEs for a secondary CU into memory.
8057 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8058
dee91e82 8059static void
ab432490 8060load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
2e671100
SM
8061 dwarf2_per_objfile *per_objfile,
8062 dwarf2_cu *existing_cu)
dee91e82 8063{
2e671100 8064 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
c0ab21c2
TT
8065
8066 if (!reader.dummy_p)
8067 {
8068 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8069 language_minimal);
8070
8071 /* Check if comp unit has_children.
8072 If so, read the rest of the partial symbols from this comp unit.
8073 If not, there's no more debug_info for this comp unit. */
3e225074 8074 if (reader.comp_unit_die->has_children)
c0ab21c2 8075 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
8076
8077 reader.keep ();
c0ab21c2 8078 }
ae038cb0
DJ
8079}
8080
ae038cb0 8081static void
ed2dc618 8082read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8083 struct dwarf2_section_info *section,
f1902523 8084 struct dwarf2_section_info *abbrev_section,
b76e467d 8085 unsigned int is_dwz)
ae038cb0 8086{
d521ce57 8087 const gdb_byte *info_ptr;
ed2dc618 8088 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8089
b4f54984 8090 if (dwarf_read_debug)
bf6af496 8091 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
8092 section->get_name (),
8093 section->get_file_name ());
bf6af496 8094
96b79293 8095 section->read (objfile);
ae038cb0 8096
36586728 8097 info_ptr = section->buffer;
6e70227d 8098
36586728 8099 while (info_ptr < section->buffer + section->size)
ae038cb0 8100 {
ae038cb0 8101 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8102
9c541725 8103 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8104
f1902523 8105 comp_unit_head cu_header;
ed2dc618
SM
8106 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8107 abbrev_section, info_ptr,
8108 rcuh_kind::COMPILE);
ae038cb0
DJ
8109
8110 /* Save the compilation unit for later lookup. */
f1902523 8111 if (cu_header.unit_type != DW_UT_type)
5989a64e 8112 this_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
8113 else
8114 {
5989a64e 8115 auto sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
8116 sig_type->signature = cu_header.signature;
8117 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8118 this_cu = &sig_type->per_cu;
8119 }
8120 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8121 this_cu->sect_off = sect_off;
f1902523 8122 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8123 this_cu->is_dwz = is_dwz;
8a0459fd 8124 this_cu->section = section;
ae038cb0 8125
5989a64e 8126 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8127
8128 info_ptr = info_ptr + this_cu->length;
8129 }
36586728
TT
8130}
8131
8132/* Create a list of all compilation units in OBJFILE.
8133 This is only done for -readnow and building partial symtabs. */
8134
8135static void
ed2dc618 8136create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8137{
5989a64e
SM
8138 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
8139 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->per_bfd->info,
8140 &dwarf2_per_objfile->per_bfd->abbrev, 0);
36586728 8141
c3699833 8142 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
4db1a1dc 8143 if (dwz != NULL)
ed2dc618 8144 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8145 1);
c906108c
SS
8146}
8147
5734ee8b 8148/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8149 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8150 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8151 DW_AT_ranges). See the comments of add_partial_subprogram on how
8152 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8153
72bf9492
DJ
8154static void
8155scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8156 CORE_ADDR *highpc, int set_addrmap,
8157 struct dwarf2_cu *cu)
c906108c 8158{
72bf9492 8159 struct partial_die_info *pdi;
c906108c 8160
91c24f0a
DC
8161 /* Now, march along the PDI's, descending into ones which have
8162 interesting children but skipping the children of the other ones,
8163 until we reach the end of the compilation unit. */
c906108c 8164
72bf9492 8165 pdi = first_die;
91c24f0a 8166
72bf9492
DJ
8167 while (pdi != NULL)
8168 {
52356b79 8169 pdi->fixup (cu);
c906108c 8170
f55ee35c 8171 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8172 children, so we need to look at them. Ditto for anonymous
8173 enums. */
933c6fe4 8174
72bf9492 8175 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8176 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8177 || pdi->tag == DW_TAG_imported_unit
8178 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8179 {
72bf9492 8180 switch (pdi->tag)
c906108c
SS
8181 {
8182 case DW_TAG_subprogram:
b1dc1806 8183 case DW_TAG_inlined_subroutine:
cdc07690 8184 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8185 break;
72929c62 8186 case DW_TAG_constant:
c906108c
SS
8187 case DW_TAG_variable:
8188 case DW_TAG_typedef:
91c24f0a 8189 case DW_TAG_union_type:
317d2668
TV
8190 if (!pdi->is_declaration
8191 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8192 {
72bf9492 8193 add_partial_symbol (pdi, cu);
63d06c5c
DC
8194 }
8195 break;
c906108c 8196 case DW_TAG_class_type:
680b30c7 8197 case DW_TAG_interface_type:
c906108c 8198 case DW_TAG_structure_type:
72bf9492 8199 if (!pdi->is_declaration)
c906108c 8200 {
72bf9492 8201 add_partial_symbol (pdi, cu);
c906108c 8202 }
b7fee5a3
KS
8203 if ((cu->language == language_rust
8204 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8205 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8206 set_addrmap, cu);
c906108c 8207 break;
91c24f0a 8208 case DW_TAG_enumeration_type:
72bf9492
DJ
8209 if (!pdi->is_declaration)
8210 add_partial_enumeration (pdi, cu);
c906108c
SS
8211 break;
8212 case DW_TAG_base_type:
a02abb62 8213 case DW_TAG_subrange_type:
c906108c 8214 /* File scope base type definitions are added to the partial
c5aa993b 8215 symbol table. */
72bf9492 8216 add_partial_symbol (pdi, cu);
c906108c 8217 break;
d9fa45fe 8218 case DW_TAG_namespace:
cdc07690 8219 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8220 break;
5d7cb8df 8221 case DW_TAG_module:
59c35742
AB
8222 if (!pdi->is_declaration)
8223 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8224 break;
95554aad
TT
8225 case DW_TAG_imported_unit:
8226 {
8227 struct dwarf2_per_cu_data *per_cu;
8228
f4dc4d17
DE
8229 /* For now we don't handle imported units in type units. */
8230 if (cu->per_cu->is_debug_types)
8231 {
8232 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8233 " supported in type units [in module %s]"),
5e22e966 8234 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
8235 }
8236
e3b94546 8237 per_cu = dwarf2_find_containing_comp_unit
5e22e966 8238 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
8239
8240 /* Go read the partial unit, if needed. */
8241 if (per_cu->v.psymtab == NULL)
ab432490
SM
8242 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8243 cu->language);
95554aad 8244
ae640021 8245 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8246 }
8247 break;
74921315
KS
8248 case DW_TAG_imported_declaration:
8249 add_partial_symbol (pdi, cu);
8250 break;
c906108c
SS
8251 default:
8252 break;
8253 }
8254 }
8255
72bf9492
DJ
8256 /* If the die has a sibling, skip to the sibling. */
8257
8258 pdi = pdi->die_sibling;
8259 }
8260}
8261
8262/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8263
72bf9492 8264 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8265 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8266 Enumerators are an exception; they use the scope of their parent
8267 enumeration type, i.e. the name of the enumeration type is not
8268 prepended to the enumerator.
91c24f0a 8269
72bf9492
DJ
8270 There are two complexities. One is DW_AT_specification; in this
8271 case "parent" means the parent of the target of the specification,
8272 instead of the direct parent of the DIE. The other is compilers
8273 which do not emit DW_TAG_namespace; in this case we try to guess
8274 the fully qualified name of structure types from their members'
8275 linkage names. This must be done using the DIE's children rather
8276 than the children of any DW_AT_specification target. We only need
8277 to do this for structures at the top level, i.e. if the target of
8278 any DW_AT_specification (if any; otherwise the DIE itself) does not
8279 have a parent. */
8280
8281/* Compute the scope prefix associated with PDI's parent, in
8282 compilation unit CU. The result will be allocated on CU's
8283 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8284 field. NULL is returned if no prefix is necessary. */
15d034d0 8285static const char *
72bf9492
DJ
8286partial_die_parent_scope (struct partial_die_info *pdi,
8287 struct dwarf2_cu *cu)
8288{
15d034d0 8289 const char *grandparent_scope;
72bf9492 8290 struct partial_die_info *parent, *real_pdi;
91c24f0a 8291
72bf9492
DJ
8292 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8293 then this means the parent of the specification DIE. */
8294
8295 real_pdi = pdi;
72bf9492 8296 while (real_pdi->has_specification)
fb816e8b 8297 {
122cf0f2
AB
8298 auto res = find_partial_die (real_pdi->spec_offset,
8299 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8300 real_pdi = res.pdi;
8301 cu = res.cu;
8302 }
72bf9492
DJ
8303
8304 parent = real_pdi->die_parent;
8305 if (parent == NULL)
8306 return NULL;
8307
8308 if (parent->scope_set)
8309 return parent->scope;
8310
52356b79 8311 parent->fixup (cu);
72bf9492 8312
10b3939b 8313 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8314
acebe513
UW
8315 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8316 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8317 Work around this problem here. */
8318 if (cu->language == language_cplus
6e70227d 8319 && parent->tag == DW_TAG_namespace
acebe513
UW
8320 && strcmp (parent->name, "::") == 0
8321 && grandparent_scope == NULL)
8322 {
8323 parent->scope = NULL;
8324 parent->scope_set = 1;
8325 return NULL;
8326 }
8327
0a4b0913 8328 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8329 if (pdi->tag == DW_TAG_enumerator)
8330 /* Enumerators should not get the name of the enumeration as a prefix. */
8331 parent->scope = grandparent_scope;
8332 else if (parent->tag == DW_TAG_namespace
f55ee35c 8333 || parent->tag == DW_TAG_module
72bf9492
DJ
8334 || parent->tag == DW_TAG_structure_type
8335 || parent->tag == DW_TAG_class_type
680b30c7 8336 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8337 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8338 || parent->tag == DW_TAG_enumeration_type
8339 || (cu->language == language_fortran
8340 && parent->tag == DW_TAG_subprogram
8341 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8342 {
8343 if (grandparent_scope == NULL)
8344 parent->scope = parent->name;
8345 else
3e43a32a
MS
8346 parent->scope = typename_concat (&cu->comp_unit_obstack,
8347 grandparent_scope,
f55ee35c 8348 parent->name, 0, cu);
72bf9492 8349 }
72bf9492
DJ
8350 else
8351 {
8352 /* FIXME drow/2004-04-01: What should we be doing with
8353 function-local names? For partial symbols, we should probably be
8354 ignoring them. */
fa9c3fa0
TT
8355 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8356 dwarf_tag_name (parent->tag),
8357 sect_offset_str (pdi->sect_off));
72bf9492 8358 parent->scope = grandparent_scope;
c906108c
SS
8359 }
8360
72bf9492
DJ
8361 parent->scope_set = 1;
8362 return parent->scope;
8363}
8364
8365/* Return the fully scoped name associated with PDI, from compilation unit
8366 CU. The result will be allocated with malloc. */
4568ecf9 8367
43816ebc 8368static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8369partial_die_full_name (struct partial_die_info *pdi,
8370 struct dwarf2_cu *cu)
8371{
15d034d0 8372 const char *parent_scope;
72bf9492 8373
98bfdba5
PA
8374 /* If this is a template instantiation, we can not work out the
8375 template arguments from partial DIEs. So, unfortunately, we have
8376 to go through the full DIEs. At least any work we do building
8377 types here will be reused if full symbols are loaded later. */
8378 if (pdi->has_template_arguments)
8379 {
52356b79 8380 pdi->fixup (cu);
98bfdba5
PA
8381
8382 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8383 {
8384 struct die_info *die;
8385 struct attribute attr;
8386 struct dwarf2_cu *ref_cu = cu;
8387
b64f50a1 8388 /* DW_FORM_ref_addr is using section offset. */
b4069958 8389 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8390 attr.form = DW_FORM_ref_addr;
9c541725 8391 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8392 die = follow_die_ref (NULL, &attr, &ref_cu);
8393
43816ebc 8394 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8395 }
8396 }
8397
72bf9492
DJ
8398 parent_scope = partial_die_parent_scope (pdi, cu);
8399 if (parent_scope == NULL)
8400 return NULL;
8401 else
43816ebc
TT
8402 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8403 pdi->name, 0, cu));
c906108c
SS
8404}
8405
8406static void
72bf9492 8407add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8408{
5e22e966 8409 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 8410 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 8411 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8412 CORE_ADDR addr = 0;
15d034d0 8413 const char *actual_name = NULL;
e142c38c
DJ
8414 CORE_ADDR baseaddr;
8415
b3b3bada 8416 baseaddr = objfile->text_section_offset ();
c906108c 8417
43816ebc
TT
8418 gdb::unique_xmalloc_ptr<char> built_actual_name
8419 = partial_die_full_name (pdi, cu);
15d034d0 8420 if (built_actual_name != NULL)
43816ebc 8421 actual_name = built_actual_name.get ();
63d06c5c 8422
72bf9492
DJ
8423 if (actual_name == NULL)
8424 actual_name = pdi->name;
8425
76e288d1
TT
8426 partial_symbol psymbol;
8427 memset (&psymbol, 0, sizeof (psymbol));
8428 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8429 psymbol.ginfo.section = -1;
8430
8431 /* The code below indicates that the psymbol should be installed by
8432 setting this. */
8433 gdb::optional<psymbol_placement> where;
8434
c906108c
SS
8435 switch (pdi->tag)
8436 {
b1dc1806 8437 case DW_TAG_inlined_subroutine:
c906108c 8438 case DW_TAG_subprogram:
79748972
TT
8439 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8440 - baseaddr);
0a4b0913
AB
8441 if (pdi->is_external
8442 || cu->language == language_ada
8443 || (cu->language == language_fortran
8444 && pdi->die_parent != NULL
8445 && pdi->die_parent->tag == DW_TAG_subprogram))
8446 {
8447 /* Normally, only "external" DIEs are part of the global scope.
8448 But in Ada and Fortran, we want to be able to access nested
8449 procedures globally. So all Ada and Fortran subprograms are
8450 stored in the global scope. */
76e288d1 8451 where = psymbol_placement::GLOBAL;
c906108c
SS
8452 }
8453 else
76e288d1
TT
8454 where = psymbol_placement::STATIC;
8455
8456 psymbol.domain = VAR_DOMAIN;
8457 psymbol.aclass = LOC_BLOCK;
8458 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8459 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8460
8461 if (pdi->main_subprogram && actual_name != NULL)
8462 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8463 break;
72929c62 8464 case DW_TAG_constant:
76e288d1
TT
8465 psymbol.domain = VAR_DOMAIN;
8466 psymbol.aclass = LOC_STATIC;
8467 where = (pdi->is_external
8468 ? psymbol_placement::GLOBAL
8469 : psymbol_placement::STATIC);
72929c62 8470 break;
c906108c 8471 case DW_TAG_variable:
95554aad
TT
8472 if (pdi->d.locdesc)
8473 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8474
95554aad 8475 if (pdi->d.locdesc
caac4577 8476 && addr == 0
5989a64e 8477 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8478 {
8479 /* A global or static variable may also have been stripped
8480 out by the linker if unused, in which case its address
8481 will be nullified; do not add such variables into partial
8482 symbol table then. */
8483 }
8484 else if (pdi->is_external)
c906108c
SS
8485 {
8486 /* Global Variable.
8487 Don't enter into the minimal symbol tables as there is
8488 a minimal symbol table entry from the ELF symbols already.
8489 Enter into partial symbol table if it has a location
8490 descriptor or a type.
8491 If the location descriptor is missing, new_symbol will create
8492 a LOC_UNRESOLVED symbol, the address of the variable will then
8493 be determined from the minimal symbol table whenever the variable
8494 is referenced.
8495 The address for the partial symbol table entry is not
8496 used by GDB, but it comes in handy for debugging partial symbol
8497 table building. */
8498
95554aad 8499 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8500 {
8501 psymbol.domain = VAR_DOMAIN;
8502 psymbol.aclass = LOC_STATIC;
8503 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8504 psymbol.ginfo.value.address = addr;
8505 where = psymbol_placement::GLOBAL;
8506 }
c906108c
SS
8507 }
8508 else
8509 {
ff908ebf
AW
8510 int has_loc = pdi->d.locdesc != NULL;
8511
8512 /* Static Variable. Skip symbols whose value we cannot know (those
8513 without location descriptors or constant values). */
8514 if (!has_loc && !pdi->has_const_value)
43816ebc 8515 return;
ff908ebf 8516
76e288d1
TT
8517 psymbol.domain = VAR_DOMAIN;
8518 psymbol.aclass = LOC_STATIC;
8519 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8520 if (has_loc)
8521 psymbol.ginfo.value.address = addr;
8522 where = psymbol_placement::STATIC;
c906108c
SS
8523 }
8524 break;
8525 case DW_TAG_typedef:
8526 case DW_TAG_base_type:
a02abb62 8527 case DW_TAG_subrange_type:
76e288d1
TT
8528 psymbol.domain = VAR_DOMAIN;
8529 psymbol.aclass = LOC_TYPEDEF;
8530 where = psymbol_placement::STATIC;
c906108c 8531 break;
74921315 8532 case DW_TAG_imported_declaration:
72bf9492 8533 case DW_TAG_namespace:
76e288d1
TT
8534 psymbol.domain = VAR_DOMAIN;
8535 psymbol.aclass = LOC_TYPEDEF;
8536 where = psymbol_placement::GLOBAL;
72bf9492 8537 break;
530e8392 8538 case DW_TAG_module:
a5fd13a9
BH
8539 /* With Fortran 77 there might be a "BLOCK DATA" module
8540 available without any name. If so, we skip the module as it
8541 doesn't bring any value. */
8542 if (actual_name != nullptr)
76e288d1
TT
8543 {
8544 psymbol.domain = MODULE_DOMAIN;
8545 psymbol.aclass = LOC_TYPEDEF;
8546 where = psymbol_placement::GLOBAL;
8547 }
530e8392 8548 break;
c906108c 8549 case DW_TAG_class_type:
680b30c7 8550 case DW_TAG_interface_type:
c906108c
SS
8551 case DW_TAG_structure_type:
8552 case DW_TAG_union_type:
8553 case DW_TAG_enumeration_type:
fa4028e9
JB
8554 /* Skip external references. The DWARF standard says in the section
8555 about "Structure, Union, and Class Type Entries": "An incomplete
8556 structure, union or class type is represented by a structure,
8557 union or class entry that does not have a byte size attribute
8558 and that has a DW_AT_declaration attribute." */
8559 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8560 return;
fa4028e9 8561
63d06c5c
DC
8562 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8563 static vs. global. */
76e288d1
TT
8564 psymbol.domain = STRUCT_DOMAIN;
8565 psymbol.aclass = LOC_TYPEDEF;
8566 where = (cu->language == language_cplus
8567 ? psymbol_placement::GLOBAL
8568 : psymbol_placement::STATIC);
c906108c
SS
8569 break;
8570 case DW_TAG_enumerator:
76e288d1
TT
8571 psymbol.domain = VAR_DOMAIN;
8572 psymbol.aclass = LOC_CONST;
8573 where = (cu->language == language_cplus
8574 ? psymbol_placement::GLOBAL
8575 : psymbol_placement::STATIC);
c906108c
SS
8576 break;
8577 default:
8578 break;
8579 }
76e288d1
TT
8580
8581 if (where.has_value ())
8582 {
f049a313
TT
8583 if (built_actual_name != nullptr)
8584 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8585 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8586 psymbol.ginfo.set_linkage_name (actual_name);
8587 else
8588 {
8589 psymbol.ginfo.set_demangled_name (actual_name,
8590 &objfile->objfile_obstack);
8591 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8592 }
76e288d1
TT
8593 add_psymbol_to_list (psymbol, *where, objfile);
8594 }
c906108c
SS
8595}
8596
5c4e30ca
DC
8597/* Read a partial die corresponding to a namespace; also, add a symbol
8598 corresponding to that namespace to the symbol table. NAMESPACE is
8599 the name of the enclosing namespace. */
91c24f0a 8600
72bf9492
DJ
8601static void
8602add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8603 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8604 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8605{
72bf9492 8606 /* Add a symbol for the namespace. */
e7c27a73 8607
72bf9492 8608 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8609
8610 /* Now scan partial symbols in that namespace. */
8611
91c24f0a 8612 if (pdi->has_children)
cdc07690 8613 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8614}
8615
5d7cb8df
JK
8616/* Read a partial die corresponding to a Fortran module. */
8617
8618static void
8619add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8620 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8621{
530e8392
KB
8622 /* Add a symbol for the namespace. */
8623
8624 add_partial_symbol (pdi, cu);
8625
f55ee35c 8626 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8627
8628 if (pdi->has_children)
cdc07690 8629 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8630}
8631
b1dc1806
XR
8632/* Read a partial die corresponding to a subprogram or an inlined
8633 subprogram and create a partial symbol for that subprogram.
8634 When the CU language allows it, this routine also defines a partial
8635 symbol for each nested subprogram that this subprogram contains.
8636 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8637 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8638
cdc07690
YQ
8639 PDI may also be a lexical block, in which case we simply search
8640 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8641 Again, this is only performed when the CU language allows this
8642 type of definitions. */
8643
8644static void
8645add_partial_subprogram (struct partial_die_info *pdi,
8646 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8647 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8648{
b1dc1806 8649 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8650 {
8651 if (pdi->has_pc_info)
8652 {
8653 if (pdi->lowpc < *lowpc)
8654 *lowpc = pdi->lowpc;
8655 if (pdi->highpc > *highpc)
8656 *highpc = pdi->highpc;
cdc07690 8657 if (set_addrmap)
5734ee8b 8658 {
5e22e966 8659 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 8660 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8661 CORE_ADDR baseaddr;
b926417a
TT
8662 CORE_ADDR this_highpc;
8663 CORE_ADDR this_lowpc;
5734ee8b 8664
b3b3bada 8665 baseaddr = objfile->text_section_offset ();
b926417a
TT
8666 this_lowpc
8667 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8668 pdi->lowpc + baseaddr)
8669 - baseaddr);
8670 this_highpc
8671 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8672 pdi->highpc + baseaddr)
8673 - baseaddr);
d320c2b5 8674 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8675 this_lowpc, this_highpc - 1,
9291a0cd 8676 cu->per_cu->v.psymtab);
5734ee8b 8677 }
481860b3
GB
8678 }
8679
8680 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8681 {
bc30ff58 8682 if (!pdi->is_declaration)
e8d05480
JB
8683 /* Ignore subprogram DIEs that do not have a name, they are
8684 illegal. Do not emit a complaint at this point, we will
8685 do so when we convert this psymtab into a symtab. */
8686 if (pdi->name)
8687 add_partial_symbol (pdi, cu);
bc30ff58
JB
8688 }
8689 }
6e70227d 8690
bc30ff58
JB
8691 if (! pdi->has_children)
8692 return;
8693
0a4b0913 8694 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8695 {
8696 pdi = pdi->die_child;
8697 while (pdi != NULL)
8698 {
52356b79 8699 pdi->fixup (cu);
bc30ff58 8700 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8701 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8702 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8703 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8704 pdi = pdi->die_sibling;
8705 }
8706 }
8707}
8708
91c24f0a
DC
8709/* Read a partial die corresponding to an enumeration type. */
8710
72bf9492
DJ
8711static void
8712add_partial_enumeration (struct partial_die_info *enum_pdi,
8713 struct dwarf2_cu *cu)
91c24f0a 8714{
72bf9492 8715 struct partial_die_info *pdi;
91c24f0a
DC
8716
8717 if (enum_pdi->name != NULL)
72bf9492
DJ
8718 add_partial_symbol (enum_pdi, cu);
8719
8720 pdi = enum_pdi->die_child;
8721 while (pdi)
91c24f0a 8722 {
72bf9492 8723 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8724 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8725 else
72bf9492
DJ
8726 add_partial_symbol (pdi, cu);
8727 pdi = pdi->die_sibling;
91c24f0a 8728 }
91c24f0a
DC
8729}
8730
6caca83c
CC
8731/* Return the initial uleb128 in the die at INFO_PTR. */
8732
8733static unsigned int
d521ce57 8734peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8735{
8736 unsigned int bytes_read;
8737
8738 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8739}
8740
685af9cd
TT
8741/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8742 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8743
4bb7a0a7
DJ
8744 Return the corresponding abbrev, or NULL if the number is zero (indicating
8745 an empty DIE). In either case *BYTES_READ will be set to the length of
8746 the initial number. */
8747
8748static struct abbrev_info *
685af9cd
TT
8749peek_die_abbrev (const die_reader_specs &reader,
8750 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8751{
685af9cd 8752 dwarf2_cu *cu = reader.cu;
5e22e966 8753 bfd *abfd = cu->per_objfile->objfile->obfd;
685af9cd
TT
8754 unsigned int abbrev_number
8755 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8756
8757 if (abbrev_number == 0)
8758 return NULL;
8759
685af9cd 8760 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8761 if (!abbrev)
8762 {
422b9917 8763 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8764 " at offset %s [in module %s]"),
422b9917 8765 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8766 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8767 }
8768
8769 return abbrev;
8770}
8771
93311388
DE
8772/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8773 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8774 DIE. Any children of the skipped DIEs will also be skipped. */
8775
d521ce57
TT
8776static const gdb_byte *
8777skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8778{
4bb7a0a7
DJ
8779 while (1)
8780 {
685af9cd
TT
8781 unsigned int bytes_read;
8782 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8783
4bb7a0a7
DJ
8784 if (abbrev == NULL)
8785 return info_ptr + bytes_read;
8786 else
dee91e82 8787 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8788 }
8789}
8790
93311388
DE
8791/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8792 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8793 abbrev corresponding to that skipped uleb128 should be passed in
8794 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8795 children. */
8796
d521ce57
TT
8797static const gdb_byte *
8798skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8799 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8800{
8801 unsigned int bytes_read;
8802 struct attribute attr;
dee91e82
DE
8803 bfd *abfd = reader->abfd;
8804 struct dwarf2_cu *cu = reader->cu;
d521ce57 8805 const gdb_byte *buffer = reader->buffer;
f664829e 8806 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8807 unsigned int form, i;
8808
8809 for (i = 0; i < abbrev->num_attrs; i++)
8810 {
8811 /* The only abbrev we care about is DW_AT_sibling. */
8812 if (abbrev->attrs[i].name == DW_AT_sibling)
8813 {
18a8505e
AT
8814 bool ignored;
8815 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8816 &ignored);
4bb7a0a7 8817 if (attr.form == DW_FORM_ref_addr)
b98664d3 8818 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8819 else
b9502d3f 8820 {
0826b30a 8821 sect_offset off = attr.get_ref_die_offset ();
9c541725 8822 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8823
8824 if (sibling_ptr < info_ptr)
b98664d3 8825 complaint (_("DW_AT_sibling points backwards"));
22869d73 8826 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8827 reader->die_section->overflow_complaint ();
b9502d3f
WN
8828 else
8829 return sibling_ptr;
8830 }
4bb7a0a7
DJ
8831 }
8832
8833 /* If it isn't DW_AT_sibling, skip this attribute. */
8834 form = abbrev->attrs[i].form;
8835 skip_attribute:
8836 switch (form)
8837 {
4bb7a0a7 8838 case DW_FORM_ref_addr:
ae411497
TT
8839 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8840 and later it is offset sized. */
8841 if (cu->header.version == 2)
8842 info_ptr += cu->header.addr_size;
8843 else
8844 info_ptr += cu->header.offset_size;
8845 break;
36586728
TT
8846 case DW_FORM_GNU_ref_alt:
8847 info_ptr += cu->header.offset_size;
8848 break;
ae411497 8849 case DW_FORM_addr:
4bb7a0a7
DJ
8850 info_ptr += cu->header.addr_size;
8851 break;
8852 case DW_FORM_data1:
8853 case DW_FORM_ref1:
8854 case DW_FORM_flag:
8fe0f950 8855 case DW_FORM_strx1:
4bb7a0a7
DJ
8856 info_ptr += 1;
8857 break;
2dc7f7b3 8858 case DW_FORM_flag_present:
43988095 8859 case DW_FORM_implicit_const:
2dc7f7b3 8860 break;
4bb7a0a7
DJ
8861 case DW_FORM_data2:
8862 case DW_FORM_ref2:
8fe0f950 8863 case DW_FORM_strx2:
4bb7a0a7
DJ
8864 info_ptr += 2;
8865 break;
8fe0f950
AT
8866 case DW_FORM_strx3:
8867 info_ptr += 3;
8868 break;
4bb7a0a7
DJ
8869 case DW_FORM_data4:
8870 case DW_FORM_ref4:
8fe0f950 8871 case DW_FORM_strx4:
4bb7a0a7
DJ
8872 info_ptr += 4;
8873 break;
8874 case DW_FORM_data8:
8875 case DW_FORM_ref8:
55f1336d 8876 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8877 info_ptr += 8;
8878 break;
0224619f
JK
8879 case DW_FORM_data16:
8880 info_ptr += 16;
8881 break;
4bb7a0a7 8882 case DW_FORM_string:
9b1c24c8 8883 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8884 info_ptr += bytes_read;
8885 break;
2dc7f7b3 8886 case DW_FORM_sec_offset:
4bb7a0a7 8887 case DW_FORM_strp:
36586728 8888 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8889 info_ptr += cu->header.offset_size;
8890 break;
2dc7f7b3 8891 case DW_FORM_exprloc:
4bb7a0a7
DJ
8892 case DW_FORM_block:
8893 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8894 info_ptr += bytes_read;
8895 break;
8896 case DW_FORM_block1:
8897 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8898 break;
8899 case DW_FORM_block2:
8900 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8901 break;
8902 case DW_FORM_block4:
8903 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8904 break;
336d760d 8905 case DW_FORM_addrx:
cf532bd1 8906 case DW_FORM_strx:
4bb7a0a7
DJ
8907 case DW_FORM_sdata:
8908 case DW_FORM_udata:
8909 case DW_FORM_ref_udata:
3019eac3
DE
8910 case DW_FORM_GNU_addr_index:
8911 case DW_FORM_GNU_str_index:
18a8505e 8912 case DW_FORM_rnglistx:
41144253 8913 case DW_FORM_loclistx:
d521ce57 8914 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8915 break;
8916 case DW_FORM_indirect:
8917 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8918 info_ptr += bytes_read;
8919 /* We need to continue parsing from here, so just go back to
8920 the top. */
8921 goto skip_attribute;
8922
8923 default:
3e43a32a
MS
8924 error (_("Dwarf Error: Cannot handle %s "
8925 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8926 dwarf_form_name (form),
8927 bfd_get_filename (abfd));
8928 }
8929 }
8930
8931 if (abbrev->has_children)
dee91e82 8932 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8933 else
8934 return info_ptr;
8935}
8936
93311388 8937/* Locate ORIG_PDI's sibling.
dee91e82 8938 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8939
d521ce57 8940static const gdb_byte *
dee91e82
DE
8941locate_pdi_sibling (const struct die_reader_specs *reader,
8942 struct partial_die_info *orig_pdi,
d521ce57 8943 const gdb_byte *info_ptr)
91c24f0a
DC
8944{
8945 /* Do we know the sibling already? */
72bf9492 8946
91c24f0a
DC
8947 if (orig_pdi->sibling)
8948 return orig_pdi->sibling;
8949
8950 /* Are there any children to deal with? */
8951
8952 if (!orig_pdi->has_children)
8953 return info_ptr;
8954
4bb7a0a7 8955 /* Skip the children the long way. */
91c24f0a 8956
dee91e82 8957 return skip_children (reader, info_ptr);
91c24f0a
DC
8958}
8959
257e7a09 8960/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8961 not NULL. */
c906108c 8962
891813be
TT
8963void
8964dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8965{
ed2dc618
SM
8966 struct dwarf2_per_objfile *dwarf2_per_objfile
8967 = get_dwarf2_per_objfile (objfile);
8968
af758d11
SM
8969 gdb_assert (!dwarf2_per_objfile->symtab_set_p (per_cu_data));
8970
077cbab2
TT
8971 /* If this psymtab is constructed from a debug-only objfile, the
8972 has_section_at_zero flag will not necessarily be correct. We
8973 can get the correct value for this flag by looking at the data
8974 associated with the (presumably stripped) associated objfile. */
8975 if (objfile->separate_debug_objfile_backlink)
c906108c 8976 {
077cbab2
TT
8977 struct dwarf2_per_objfile *dpo_backlink
8978 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8979
5989a64e
SM
8980 dwarf2_per_objfile->per_bfd->has_section_at_zero
8981 = dpo_backlink->per_bfd->has_section_at_zero;
077cbab2 8982 }
98bfdba5 8983
8566b89b 8984 expand_psymtab (objfile);
95554aad 8985
ed2dc618 8986 process_cu_includes (dwarf2_per_objfile);
c906108c 8987}
9cdd5dbd
DE
8988\f
8989/* Reading in full CUs. */
c906108c 8990
10b3939b
DJ
8991/* Add PER_CU to the queue. */
8992
8993static void
120ce1b5
SM
8994queue_comp_unit (dwarf2_per_cu_data *per_cu,
8995 dwarf2_per_objfile *per_objfile,
95554aad 8996 enum language pretend_language)
10b3939b 8997{
10b3939b 8998 per_cu->queued = 1;
120ce1b5 8999 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
9000}
9001
89e63ee4
DE
9002/* If PER_CU is not yet queued, add it to the queue.
9003 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9004 dependency.
0907af0c 9005 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9006 meaning either PER_CU is already queued or it is already loaded.
9007
9008 N.B. There is an invariant here that if a CU is queued then it is loaded.
9009 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9010
9011static int
89e63ee4 9012maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
9013 dwarf2_per_cu_data *per_cu,
9014 dwarf2_per_objfile *per_objfile,
0907af0c
DE
9015 enum language pretend_language)
9016{
9017 /* We may arrive here during partial symbol reading, if we need full
9018 DIEs to process an unusual case (e.g. template arguments). Do
9019 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 9020 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c 9021 {
7188ed02
SM
9022 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9023
9024 if (cu == NULL || cu->dies == NULL)
0907af0c
DE
9025 return 1;
9026 return 0;
9027 }
9028
9029 /* Mark the dependence relation so that we don't flush PER_CU
9030 too early. */
89e63ee4
DE
9031 if (dependent_cu != NULL)
9032 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9033
9034 /* If it's already on the queue, we have nothing to do. */
9035 if (per_cu->queued)
9036 return 0;
9037
9038 /* If the compilation unit is already loaded, just mark it as
9039 used. */
7188ed02
SM
9040 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9041 if (cu != nullptr)
0907af0c 9042 {
7188ed02 9043 cu->last_used = 0;
0907af0c
DE
9044 return 0;
9045 }
9046
9047 /* Add it to the queue. */
120ce1b5 9048 queue_comp_unit (per_cu, per_objfile, pretend_language);
0907af0c
DE
9049
9050 return 1;
9051}
9052
10b3939b
DJ
9053/* Process the queue. */
9054
9055static void
ed2dc618 9056process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b 9057{
b4f54984 9058 if (dwarf_read_debug)
45cfd468
DE
9059 {
9060 fprintf_unfiltered (gdb_stdlog,
9061 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9062 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9063 }
9064
03dd20cc
DJ
9065 /* The queue starts out with one item, but following a DIE reference
9066 may load a new CU, adding it to the end of the queue. */
5989a64e 9067 while (!dwarf2_per_objfile->per_bfd->queue.empty ())
10b3939b 9068 {
5989a64e 9069 dwarf2_queue_item &item = dwarf2_per_objfile->per_bfd->queue.front ();
7188ed02 9070 dwarf2_per_cu_data *per_cu = item.per_cu;
39856def 9071
7188ed02 9072 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
f4dc4d17 9073 {
7188ed02 9074 dwarf2_cu *cu = dwarf2_per_objfile->get_cu (per_cu);
f4dc4d17 9075
7188ed02
SM
9076 /* Skip dummy CUs. */
9077 if (cu != nullptr)
73be47f5 9078 {
7188ed02
SM
9079 unsigned int debug_print_threshold;
9080 char buf[100];
9081
9082 if (per_cu->is_debug_types)
9083 {
9084 struct signatured_type *sig_type =
9085 (struct signatured_type *) per_cu;
9086
9087 sprintf (buf, "TU %s at offset %s",
9088 hex_string (sig_type->signature),
9089 sect_offset_str (per_cu->sect_off));
9090 /* There can be 100s of TUs.
9091 Only print them in verbose mode. */
9092 debug_print_threshold = 2;
9093 }
9094 else
9095 {
9096 sprintf (buf, "CU at offset %s",
9097 sect_offset_str (per_cu->sect_off));
9098 debug_print_threshold = 1;
9099 }
247f5c4f 9100
7188ed02
SM
9101 if (dwarf_read_debug >= debug_print_threshold)
9102 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17 9103
7188ed02
SM
9104 if (per_cu->is_debug_types)
9105 process_full_type_unit (cu, item.pretend_language);
9106 else
9107 process_full_comp_unit (cu, item.pretend_language);
f4dc4d17 9108
7188ed02
SM
9109 if (dwarf_read_debug >= debug_print_threshold)
9110 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9111 }
f4dc4d17 9112 }
10b3939b 9113
7188ed02 9114 per_cu->queued = 0;
5989a64e 9115 dwarf2_per_objfile->per_bfd->queue.pop ();
10b3939b
DJ
9116 }
9117
b4f54984 9118 if (dwarf_read_debug)
45cfd468
DE
9119 {
9120 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9121 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9122 }
10b3939b
DJ
9123}
9124
10b3939b
DJ
9125/* Read in full symbols for PST, and anything it depends on. */
9126
8566b89b
TT
9127void
9128dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9129{
af758d11 9130 gdb_assert (!readin_p (objfile));
95554aad 9131
17ee85fc
TT
9132 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9133 free_cached_comp_units freer (per_objfile);
48993951 9134 expand_dependencies (objfile);
aaa75496 9135
97a1449a 9136 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 9137 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
9138}
9139
af758d11
SM
9140/* See psympriv.h. */
9141
9142bool
9143dwarf2_psymtab::readin_p (struct objfile *objfile) const
9144{
9145 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9146 return per_objfile->symtab_set_p (per_cu_data);
9147}
9148
9149/* See psympriv.h. */
9150
9151compunit_symtab *
9152dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9153{
9154 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9155 return per_objfile->get_symtab (per_cu_data);
9156}
9157
dee91e82
DE
9158/* Trivial hash function for die_info: the hash value of a DIE
9159 is its offset in .debug_info for this objfile. */
10b3939b 9160
dee91e82
DE
9161static hashval_t
9162die_hash (const void *item)
10b3939b 9163{
9a3c8263 9164 const struct die_info *die = (const struct die_info *) item;
6502dd73 9165
9c541725 9166 return to_underlying (die->sect_off);
dee91e82 9167}
63d06c5c 9168
dee91e82
DE
9169/* Trivial comparison function for die_info structures: two DIEs
9170 are equal if they have the same offset. */
98bfdba5 9171
dee91e82
DE
9172static int
9173die_eq (const void *item_lhs, const void *item_rhs)
9174{
9a3c8263
SM
9175 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9176 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9177
9c541725 9178 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9179}
c906108c 9180
c0ab21c2 9181/* Load the DIEs associated with PER_CU into memory. */
c906108c 9182
dee91e82 9183static void
ab432490
SM
9184load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9185 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
9186 bool skip_partial,
9187 enum language pretend_language)
dee91e82 9188{
c0ab21c2
TT
9189 gdb_assert (! this_cu->is_debug_types);
9190
7188ed02
SM
9191 dwarf2_cu *existing_cu = per_objfile->get_cu (this_cu);
9192 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
c0ab21c2
TT
9193 if (reader.dummy_p)
9194 return;
9195
9196 struct dwarf2_cu *cu = reader.cu;
9197 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9198
dee91e82
DE
9199 gdb_assert (cu->die_hash == NULL);
9200 cu->die_hash =
9201 htab_create_alloc_ex (cu->header.length / 12,
9202 die_hash,
9203 die_eq,
9204 NULL,
9205 &cu->comp_unit_obstack,
9206 hashtab_obstack_allocate,
9207 dummy_obstack_deallocate);
e142c38c 9208
3e225074 9209 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9210 reader.comp_unit_die->child
9211 = read_die_and_siblings (&reader, reader.info_ptr,
9212 &info_ptr, reader.comp_unit_die);
9213 cu->dies = reader.comp_unit_die;
dee91e82 9214 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9215
9216 /* We try not to read any attributes in this function, because not
9cdd5dbd 9217 all CUs needed for references have been loaded yet, and symbol
10b3939b 9218 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9219 or we won't be able to build types correctly.
9220 Similarly, if we do not read the producer, we can not apply
9221 producer-specific interpretation. */
c0ab21c2 9222 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9223
9224 reader.keep ();
10b3939b
DJ
9225}
9226
3da10d80
KS
9227/* Add a DIE to the delayed physname list. */
9228
9229static void
9230add_to_method_list (struct type *type, int fnfield_index, int index,
9231 const char *name, struct die_info *die,
9232 struct dwarf2_cu *cu)
9233{
9234 struct delayed_method_info mi;
9235 mi.type = type;
9236 mi.fnfield_index = fnfield_index;
9237 mi.index = index;
9238 mi.name = name;
9239 mi.die = die;
c89b44cd 9240 cu->method_list.push_back (mi);
3da10d80
KS
9241}
9242
3693fdb3
PA
9243/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9244 "const" / "volatile". If so, decrements LEN by the length of the
9245 modifier and return true. Otherwise return false. */
9246
9247template<size_t N>
9248static bool
9249check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9250{
9251 size_t mod_len = sizeof (mod) - 1;
9252 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9253 {
9254 len -= mod_len;
9255 return true;
9256 }
9257 return false;
9258}
9259
3da10d80
KS
9260/* Compute the physnames of any methods on the CU's method list.
9261
9262 The computation of method physnames is delayed in order to avoid the
9263 (bad) condition that one of the method's formal parameters is of an as yet
9264 incomplete type. */
9265
9266static void
9267compute_delayed_physnames (struct dwarf2_cu *cu)
9268{
3693fdb3 9269 /* Only C++ delays computing physnames. */
c89b44cd 9270 if (cu->method_list.empty ())
3693fdb3
PA
9271 return;
9272 gdb_assert (cu->language == language_cplus);
9273
52941706 9274 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9275 {
1d06ead6 9276 const char *physname;
3da10d80 9277 struct fn_fieldlist *fn_flp
c89b44cd
TT
9278 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9279 physname = dwarf2_physname (mi.name, mi.die, cu);
9280 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9281 = physname ? physname : "";
3693fdb3
PA
9282
9283 /* Since there's no tag to indicate whether a method is a
9284 const/volatile overload, extract that information out of the
9285 demangled name. */
9286 if (physname != NULL)
9287 {
9288 size_t len = strlen (physname);
9289
9290 while (1)
9291 {
9292 if (physname[len] == ')') /* shortcut */
9293 break;
9294 else if (check_modifier (physname, len, " const"))
c89b44cd 9295 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9296 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9297 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9298 else
9299 break;
9300 }
9301 }
3da10d80 9302 }
c89b44cd
TT
9303
9304 /* The list is no longer needed. */
9305 cu->method_list.clear ();
3da10d80
KS
9306}
9307
a766d390
DE
9308/* Go objects should be embedded in a DW_TAG_module DIE,
9309 and it's not clear if/how imported objects will appear.
9310 To keep Go support simple until that's worked out,
9311 go back through what we've read and create something usable.
9312 We could do this while processing each DIE, and feels kinda cleaner,
9313 but that way is more invasive.
9314 This is to, for example, allow the user to type "p var" or "b main"
9315 without having to specify the package name, and allow lookups
9316 of module.object to work in contexts that use the expression
9317 parser. */
9318
9319static void
9320fixup_go_packaging (struct dwarf2_cu *cu)
9321{
421d1616 9322 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9323 struct pending *list;
9324 int i;
9325
c24bdb02 9326 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9327 list != NULL;
9328 list = list->next)
a766d390
DE
9329 {
9330 for (i = 0; i < list->nsyms; ++i)
9331 {
9332 struct symbol *sym = list->symbol[i];
9333
c1b5c1eb 9334 if (sym->language () == language_go
a766d390
DE
9335 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9336 {
421d1616
TT
9337 gdb::unique_xmalloc_ptr<char> this_package_name
9338 (go_symbol_package_name (sym));
a766d390
DE
9339
9340 if (this_package_name == NULL)
9341 continue;
9342 if (package_name == NULL)
421d1616 9343 package_name = std::move (this_package_name);
a766d390
DE
9344 else
9345 {
5e22e966 9346 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9347 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9348 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9349 (symbol_symtab (sym) != NULL
9350 ? symtab_to_filename_for_display
9351 (symbol_symtab (sym))
e3b94546 9352 : objfile_name (objfile)),
421d1616 9353 this_package_name.get (), package_name.get ());
a766d390
DE
9354 }
9355 }
9356 }
9357 }
9358
9359 if (package_name != NULL)
9360 {
5e22e966 9361 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9362 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9363 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9364 saved_package_name);
a766d390
DE
9365 struct symbol *sym;
9366
8c14c3a3 9367 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9368 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9369 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9370 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9371 e.g., "main" finds the "main" module and not C's main(). */
9372 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9373 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9374 SYMBOL_TYPE (sym) = type;
9375
c24bdb02 9376 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9377 }
9378}
9379
c9317f21
TT
9380/* Allocate a fully-qualified name consisting of the two parts on the
9381 obstack. */
9382
9383static const char *
9384rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9385{
9386 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9387}
9388
9c6a1327
TT
9389/* A helper that allocates a variant part to attach to a Rust enum
9390 type. OBSTACK is where the results should be allocated. TYPE is
9391 the type we're processing. DISCRIMINANT_INDEX is the index of the
9392 discriminant. It must be the index of one of the fields of TYPE.
9393 DEFAULT_INDEX is the index of the default field; or -1 if there is
9394 no default. RANGES is indexed by "effective" field number (the
9395 field index, but omitting the discriminant and default fields) and
9396 must hold the discriminant values used by the variants. Note that
9397 RANGES must have a lifetime at least as long as OBSTACK -- either
9398 already allocated on it, or static. */
c9317f21 9399
9c6a1327
TT
9400static void
9401alloc_rust_variant (struct obstack *obstack, struct type *type,
9402 int discriminant_index, int default_index,
9403 gdb::array_view<discriminant_range> ranges)
9404{
9405 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9406 must be handled by the caller. */
9407 gdb_assert (discriminant_index >= 0
1f704f76 9408 && discriminant_index < type->num_fields ());
c9317f21 9409 gdb_assert (default_index == -1
1f704f76 9410 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9411
9c6a1327 9412 /* We have one variant for each non-discriminant field. */
1f704f76 9413 int n_variants = type->num_fields () - 1;
c9317f21 9414
9c6a1327
TT
9415 variant *variants = new (obstack) variant[n_variants];
9416 int var_idx = 0;
9417 int range_idx = 0;
1f704f76 9418 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9419 {
9420 if (i == discriminant_index)
9421 continue;
c9317f21 9422
9c6a1327
TT
9423 variants[var_idx].first_field = i;
9424 variants[var_idx].last_field = i + 1;
9425
9426 /* The default field does not need a range, but other fields do.
9427 We skipped the discriminant above. */
9428 if (i != default_index)
9429 {
9430 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9431 ++range_idx;
9432 }
c9317f21 9433
9c6a1327
TT
9434 ++var_idx;
9435 }
9436
9437 gdb_assert (range_idx == ranges.size ());
9438 gdb_assert (var_idx == n_variants);
9439
9440 variant_part *part = new (obstack) variant_part;
9441 part->discriminant_index = discriminant_index;
9442 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9443 discriminant_index));
9444 part->variants = gdb::array_view<variant> (variants, n_variants);
9445
9446 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9447 gdb::array_view<variant_part> *prop_value
9448 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9449
9c6a1327
TT
9450 struct dynamic_prop prop;
9451 prop.kind = PROP_VARIANT_PARTS;
9452 prop.data.variant_parts = prop_value;
9453
5c54719c 9454 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9455}
9456
9457/* Some versions of rustc emitted enums in an unusual way.
9458
9459 Ordinary enums were emitted as unions. The first element of each
9460 structure in the union was named "RUST$ENUM$DISR". This element
9461 held the discriminant.
9462
9463 These versions of Rust also implemented the "non-zero"
9464 optimization. When the enum had two values, and one is empty and
9465 the other holds a pointer that cannot be zero, the pointer is used
9466 as the discriminant, with a zero value meaning the empty variant.
9467 Here, the union's first member is of the form
9468 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9469 where the fieldnos are the indices of the fields that should be
9470 traversed in order to find the field (which may be several fields deep)
9471 and the variantname is the name of the variant of the case when the
9472 field is zero.
9473
9474 This function recognizes whether TYPE is of one of these forms,
9475 and, if so, smashes it to be a variant type. */
9476
9477static void
9478quirk_rust_enum (struct type *type, struct objfile *objfile)
9479{
78134374 9480 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9481
9482 /* We don't need to deal with empty enums. */
1f704f76 9483 if (type->num_fields () == 0)
c9317f21
TT
9484 return;
9485
9486#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9487 if (type->num_fields () == 1
c9317f21
TT
9488 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9489 {
9490 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9491
9492 /* Decode the field name to find the offset of the
9493 discriminant. */
9494 ULONGEST bit_offset = 0;
9495 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9496 while (name[0] >= '0' && name[0] <= '9')
9497 {
9498 char *tail;
9499 unsigned long index = strtoul (name, &tail, 10);
9500 name = tail;
9501 if (*name != '$'
1f704f76 9502 || index >= field_type->num_fields ()
c9317f21
TT
9503 || (TYPE_FIELD_LOC_KIND (field_type, index)
9504 != FIELD_LOC_KIND_BITPOS))
9505 {
b98664d3 9506 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9507 "[in module %s]"),
9508 TYPE_FIELD_NAME (type, 0),
9509 objfile_name (objfile));
9510 return;
9511 }
9512 ++name;
9513
9514 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9515 field_type = TYPE_FIELD_TYPE (field_type, index);
9516 }
9517
9c6a1327
TT
9518 /* Smash this type to be a structure type. We have to do this
9519 because the type has already been recorded. */
67607e24 9520 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9521 type->set_num_fields (3);
9c6a1327 9522 /* Save the field we care about. */
ceacbf6e 9523 struct field saved_field = type->field (0);
3cabb6b0
SM
9524 type->set_fields
9525 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9526
9c6a1327
TT
9527 /* Put the discriminant at index 0. */
9528 TYPE_FIELD_TYPE (type, 0) = field_type;
9529 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9530 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9531 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9532
9533 /* The order of fields doesn't really matter, so put the real
9534 field at index 1 and the data-less field at index 2. */
ceacbf6e 9535 type->field (1) = saved_field;
9c6a1327 9536 TYPE_FIELD_NAME (type, 1)
7d93a1e0 9537 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
d0e39ea2 9538 TYPE_FIELD_TYPE (type, 1)->set_name
7d93a1e0 9539 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9540 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9541
9542 const char *dataless_name
7d93a1e0 9543 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9544 name);
9545 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9546 dataless_name);
9c6a1327 9547 TYPE_FIELD_TYPE (type, 2) = dataless_type;
c9317f21
TT
9548 /* NAME points into the original discriminant name, which
9549 already has the correct lifetime. */
9c6a1327 9550 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9551 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9552
9c6a1327
TT
9553 /* Indicate that this is a variant type. */
9554 static discriminant_range ranges[1] = { { 0, 0 } };
9555 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9556 }
77c2dba3
TT
9557 /* A union with a single anonymous field is probably an old-style
9558 univariant enum. */
1f704f76 9559 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9560 {
c9317f21
TT
9561 /* Smash this type to be a structure type. We have to do this
9562 because the type has already been recorded. */
67607e24 9563 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9564
9c6a1327 9565 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
c9317f21 9566 const char *variant_name
7d93a1e0 9567 = rust_last_path_segment (field_type->name ());
9c6a1327 9568 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9569 field_type->set_name
9570 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9571 type->name (), variant_name));
c9317f21
TT
9572 }
9573 else
9574 {
9575 struct type *disr_type = nullptr;
1f704f76 9576 for (int i = 0; i < type->num_fields (); ++i)
c9317f21
TT
9577 {
9578 disr_type = TYPE_FIELD_TYPE (type, i);
9579
78134374 9580 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9581 {
9582 /* All fields of a true enum will be structs. */
9583 return;
9584 }
1f704f76 9585 else if (disr_type->num_fields () == 0)
c9317f21
TT
9586 {
9587 /* Could be data-less variant, so keep going. */
a037790e 9588 disr_type = nullptr;
c9317f21
TT
9589 }
9590 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9591 "RUST$ENUM$DISR") != 0)
9592 {
9593 /* Not a Rust enum. */
9594 return;
9595 }
9596 else
9597 {
9598 /* Found one. */
9599 break;
9600 }
9601 }
9602
9603 /* If we got here without a discriminant, then it's probably
9604 just a union. */
9605 if (disr_type == nullptr)
9606 return;
9607
9608 /* Smash this type to be a structure type. We have to do this
9609 because the type has already been recorded. */
67607e24 9610 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9611
9c6a1327 9612 /* Make space for the discriminant field. */
ceacbf6e 9613 struct field *disr_field = &disr_type->field (0);
9c6a1327 9614 field *new_fields
1f704f76 9615 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9616 * sizeof (struct field)));
80fc5e77 9617 memcpy (new_fields + 1, type->fields (),
1f704f76 9618 type->num_fields () * sizeof (struct field));
3cabb6b0 9619 type->set_fields (new_fields);
1f704f76 9620 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9621
9622 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9623 type->field (0) = *disr_field;
9c6a1327
TT
9624 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9625 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9626
9627 /* We need a way to find the correct discriminant given a
9628 variant name. For convenience we build a map here. */
9629 struct type *enum_type = FIELD_TYPE (*disr_field);
9630 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9631 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9632 {
9633 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9634 {
9635 const char *name
9636 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9637 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9638 }
9639 }
9640
1f704f76 9641 int n_fields = type->num_fields ();
9c6a1327
TT
9642 /* We don't need a range entry for the discriminant, but we do
9643 need one for every other field, as there is no default
9644 variant. */
9645 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9646 discriminant_range,
9647 n_fields - 1);
c9317f21
TT
9648 /* Skip the discriminant here. */
9649 for (int i = 1; i < n_fields; ++i)
9650 {
9651 /* Find the final word in the name of this variant's type.
9652 That name can be used to look up the correct
9653 discriminant. */
9654 const char *variant_name
7d93a1e0 9655 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
c9317f21
TT
9656
9657 auto iter = discriminant_map.find (variant_name);
9658 if (iter != discriminant_map.end ())
9c6a1327
TT
9659 {
9660 ranges[i].low = iter->second;
9661 ranges[i].high = iter->second;
9662 }
c9317f21 9663
bedda9ac 9664 /* Remove the discriminant field, if it exists. */
9c6a1327 9665 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
1f704f76 9666 if (sub_type->num_fields () > 0)
bedda9ac 9667 {
5e33d5f4 9668 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9669 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9670 }
9c6a1327 9671 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9672 sub_type->set_name
9673 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9674 type->name (), variant_name));
c9317f21 9675 }
9c6a1327
TT
9676
9677 /* Indicate that this is a variant type. */
9678 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9679 gdb::array_view<discriminant_range> (ranges,
9680 n_fields - 1));
c9317f21
TT
9681 }
9682}
9683
9684/* Rewrite some Rust unions to be structures with variants parts. */
9685
9686static void
9687rust_union_quirks (struct dwarf2_cu *cu)
9688{
9689 gdb_assert (cu->language == language_rust);
52941706 9690 for (type *type_ : cu->rust_unions)
5e22e966 9691 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9692 /* We don't need this any more. */
9693 cu->rust_unions.clear ();
c9317f21
TT
9694}
9695
8adb8487
TT
9696/* See read.h. */
9697
9698type_unit_group_unshareable *
9699dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9700{
9701 auto iter = this->m_type_units.find (tu_group);
9702 if (iter != this->m_type_units.end ())
9703 return iter->second.get ();
9704
9705 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9706 type_unit_group_unshareable *result = uniq.get ();
9707 this->m_type_units[tu_group] = std::move (uniq);
9708 return result;
9709}
9710
e286671b
TT
9711struct type *
9712dwarf2_per_objfile::get_type_for_signatured_type
9713 (signatured_type *sig_type) const
9714{
9715 auto iter = this->m_type_map.find (sig_type);
9716 if (iter == this->m_type_map.end ())
9717 return nullptr;
9718
9719 return iter->second;
9720}
9721
9722void dwarf2_per_objfile::set_type_for_signatured_type
9723 (signatured_type *sig_type, struct type *type)
9724{
9725 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9726
9727 this->m_type_map[sig_type] = type;
9728}
9729
95554aad
TT
9730/* A helper function for computing the list of all symbol tables
9731 included by PER_CU. */
9732
9733static void
4c39bc03 9734recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9735 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9736 dwarf2_per_cu_data *per_cu,
9737 dwarf2_per_objfile *per_objfile,
43f3e411 9738 struct compunit_symtab *immediate_parent)
95554aad 9739{
af758d11 9740 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9741 if (*slot != NULL)
9742 {
9743 /* This inclusion and its children have been processed. */
9744 return;
9745 }
9746
9747 *slot = per_cu;
af758d11 9748
95554aad 9749 /* Only add a CU if it has a symbol table. */
43182c09 9750 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9751 if (cust != NULL)
ec94af83
DE
9752 {
9753 /* If this is a type unit only add its symbol table if we haven't
9754 seen it yet (type unit per_cu's can share symtabs). */
9755 if (per_cu->is_debug_types)
9756 {
43f3e411 9757 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9758 if (*slot == NULL)
9759 {
43f3e411 9760 *slot = cust;
4c39bc03 9761 result->push_back (cust);
43f3e411
DE
9762 if (cust->user == NULL)
9763 cust->user = immediate_parent;
ec94af83
DE
9764 }
9765 }
9766 else
f9125b6c 9767 {
4c39bc03 9768 result->push_back (cust);
43f3e411
DE
9769 if (cust->user == NULL)
9770 cust->user = immediate_parent;
f9125b6c 9771 }
ec94af83 9772 }
95554aad 9773
ae640021
AB
9774 if (!per_cu->imported_symtabs_empty ())
9775 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9776 {
9777 recursively_compute_inclusions (result, all_children,
43182c09
SM
9778 all_type_symtabs, ptr, per_objfile,
9779 cust);
ae640021 9780 }
95554aad
TT
9781}
9782
43f3e411 9783/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9784 PER_CU. */
9785
9786static void
43182c09
SM
9787compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9788 dwarf2_per_objfile *per_objfile)
95554aad 9789{
f4dc4d17
DE
9790 gdb_assert (! per_cu->is_debug_types);
9791
ae640021 9792 if (!per_cu->imported_symtabs_empty ())
95554aad 9793 {
ae640021 9794 int len;
4c39bc03 9795 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9796 htab_t all_children, all_type_symtabs;
43182c09 9797 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9798
9799 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9800 if (cust == NULL)
95554aad
TT
9801 return;
9802
9803 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9804 NULL, xcalloc, xfree);
ec94af83
DE
9805 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9806 NULL, xcalloc, xfree);
95554aad 9807
ae640021 9808 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9809 {
9810 recursively_compute_inclusions (&result_symtabs, all_children,
43182c09
SM
9811 all_type_symtabs, ptr, per_objfile,
9812 cust);
ec94af83 9813 }
95554aad 9814
ec94af83 9815 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9816 len = result_symtabs.size ();
43f3e411 9817 cust->includes
f6e649dd 9818 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9819 struct compunit_symtab *, len + 1);
4c39bc03
TT
9820 memcpy (cust->includes, result_symtabs.data (),
9821 len * sizeof (compunit_symtab *));
43f3e411 9822 cust->includes[len] = NULL;
95554aad 9823
95554aad 9824 htab_delete (all_children);
ec94af83 9825 htab_delete (all_type_symtabs);
95554aad
TT
9826 }
9827}
9828
9829/* Compute the 'includes' field for the symtabs of all the CUs we just
9830 read. */
9831
9832static void
ed2dc618 9833process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9834{
5989a64e 9835 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9836 {
9837 if (! iter->is_debug_types)
43182c09 9838 compute_compunit_symtab_includes (iter, dwarf2_per_objfile);
f4dc4d17 9839 }
95554aad 9840
5989a64e 9841 dwarf2_per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9842}
9843
8fc0b21d 9844/* Generate full symbol information for CU, whose DIEs have
10b3939b
DJ
9845 already been loaded into memory. */
9846
9847static void
8fc0b21d 9848process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10b3939b 9849{
8fc0b21d 9850 dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 9851 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 9852 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9853 CORE_ADDR lowpc, highpc;
43f3e411 9854 struct compunit_symtab *cust;
10b3939b 9855 CORE_ADDR baseaddr;
4359dff1 9856 struct block *static_block;
3e29f34a 9857 CORE_ADDR addr;
10b3939b 9858
b3b3bada 9859 baseaddr = objfile->text_section_offset ();
10b3939b 9860
c89b44cd
TT
9861 /* Clear the list here in case something was left over. */
9862 cu->method_list.clear ();
10b3939b 9863
95554aad
TT
9864 cu->language = pretend_language;
9865 cu->language_defn = language_def (cu->language);
9866
c906108c 9867 /* Do line number decoding in read_file_scope () */
10b3939b 9868 process_die (cu->dies, cu);
c906108c 9869
a766d390
DE
9870 /* For now fudge the Go package. */
9871 if (cu->language == language_go)
9872 fixup_go_packaging (cu);
9873
5f48f8f3 9874 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9875 should be complete, and it should now be safe to compute all of the
9876 physnames. */
9877 compute_delayed_physnames (cu);
3da10d80 9878
c9317f21
TT
9879 if (cu->language == language_rust)
9880 rust_union_quirks (cu);
9881
fae299cd
DC
9882 /* Some compilers don't define a DW_AT_high_pc attribute for the
9883 compilation unit. If the DW_AT_high_pc is missing, synthesize
9884 it, by scanning the DIE's below the compilation unit. */
10b3939b 9885 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9886
3e29f34a 9887 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9888 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9889
9890 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9891 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9892 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9893 addrmap to help ensure it has an accurate map of pc values belonging to
9894 this comp unit. */
9895 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9896
c24bdb02 9897 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9898 SECT_OFF_TEXT (objfile),
9899 0);
c906108c 9900
43f3e411 9901 if (cust != NULL)
c906108c 9902 {
df15bd07 9903 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9904
8be455d7
JK
9905 /* Set symtab language to language from DW_AT_language. If the
9906 compilation is from a C file generated by language preprocessors, do
9907 not set the language if it was already deduced by start_subfile. */
43f3e411 9908 if (!(cu->language == language_c
40e3ad0e 9909 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9910 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9911
9912 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9913 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9914 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9915 there were bugs in prologue debug info, fixed later in GCC-4.5
9916 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9917
9918 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9919 needed, it would be wrong due to missing DW_AT_producer there.
9920
9921 Still one can confuse GDB by using non-standard GCC compilation
9922 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9923 */
ab260dad 9924 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9925 cust->locations_valid = 1;
e0d00bc7
JK
9926
9927 if (gcc_4_minor >= 5)
43f3e411 9928 cust->epilogue_unwind_valid = 1;
96408a79 9929
43f3e411 9930 cust->call_site_htab = cu->call_site_htab;
c906108c 9931 }
9291a0cd 9932
8fc0b21d 9933 dwarf2_per_objfile->set_symtab (cu->per_cu, cust);
c906108c 9934
95554aad 9935 /* Push it for inclusion processing later. */
8fc0b21d 9936 dwarf2_per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
804d2729
TT
9937
9938 /* Not needed any more. */
c24bdb02 9939 cu->reset_builder ();
f4dc4d17 9940}
45cfd468 9941
8fc0b21d 9942/* Generate full symbol information for type unit CU, whose DIEs have
f4dc4d17
DE
9943 already been loaded into memory. */
9944
9945static void
8fc0b21d 9946process_full_type_unit (dwarf2_cu *cu,
f4dc4d17
DE
9947 enum language pretend_language)
9948{
8fc0b21d 9949 dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 9950 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 9951 struct compunit_symtab *cust;
0186c6a7
DE
9952 struct signatured_type *sig_type;
9953
8fc0b21d
SM
9954 gdb_assert (cu->per_cu->is_debug_types);
9955 sig_type = (struct signatured_type *) cu->per_cu;
f4dc4d17 9956
c89b44cd
TT
9957 /* Clear the list here in case something was left over. */
9958 cu->method_list.clear ();
f4dc4d17 9959
f4dc4d17
DE
9960 cu->language = pretend_language;
9961 cu->language_defn = language_def (cu->language);
9962
9963 /* The symbol tables are set up in read_type_unit_scope. */
9964 process_die (cu->dies, cu);
9965
9966 /* For now fudge the Go package. */
9967 if (cu->language == language_go)
9968 fixup_go_packaging (cu);
9969
5f48f8f3 9970 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9971 should be complete, and it should now be safe to compute all of the
9972 physnames. */
9973 compute_delayed_physnames (cu);
f4dc4d17 9974
c9317f21
TT
9975 if (cu->language == language_rust)
9976 rust_union_quirks (cu);
9977
f4dc4d17
DE
9978 /* TUs share symbol tables.
9979 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9980 of it with end_expandable_symtab. Otherwise, complete the addition of
9981 this TU's symbols to the existing symtab. */
8adb8487
TT
9982 type_unit_group_unshareable *tug_unshare =
9983 dwarf2_per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9984 if (tug_unshare->compunit_symtab == NULL)
45cfd468 9985 {
c24bdb02
KS
9986 buildsym_compunit *builder = cu->get_builder ();
9987 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 9988 tug_unshare->compunit_symtab = cust;
f4dc4d17 9989
43f3e411 9990 if (cust != NULL)
f4dc4d17
DE
9991 {
9992 /* Set symtab language to language from DW_AT_language. If the
9993 compilation is from a C file generated by language preprocessors,
9994 do not set the language if it was already deduced by
9995 start_subfile. */
43f3e411
DE
9996 if (!(cu->language == language_c
9997 && COMPUNIT_FILETABS (cust)->language != language_c))
9998 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9999 }
10000 }
10001 else
10002 {
c24bdb02 10003 cu->get_builder ()->augment_type_symtab ();
8adb8487 10004 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
10005 }
10006
8fc0b21d 10007 dwarf2_per_objfile->set_symtab (cu->per_cu, cust);
804d2729
TT
10008
10009 /* Not needed any more. */
c24bdb02 10010 cu->reset_builder ();
c906108c
SS
10011}
10012
95554aad
TT
10013/* Process an imported unit DIE. */
10014
10015static void
10016process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10017{
10018 struct attribute *attr;
10019
f4dc4d17
DE
10020 /* For now we don't handle imported units in type units. */
10021 if (cu->per_cu->is_debug_types)
10022 {
10023 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10024 " supported in type units [in module %s]"),
5e22e966 10025 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
10026 }
10027
95554aad
TT
10028 attr = dwarf2_attr (die, DW_AT_import, cu);
10029 if (attr != NULL)
10030 {
0826b30a 10031 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 10032 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 10033 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 10034 dwarf2_per_cu_data *per_cu
ab432490 10035 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 10036
58990295
TV
10037 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10038 into another compilation unit, at root level. Regard this as a hint,
10039 and ignore it. */
10040 if (die->parent && die->parent->parent == NULL
10041 && per_cu->unit_type == DW_UT_compile
10042 && per_cu->lang == language_cplus)
10043 return;
10044
69d751e3 10045 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 10046 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
ab432490 10047 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
95554aad 10048
ae640021 10049 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10050 }
10051}
10052
4c8aa72d
PA
10053/* RAII object that represents a process_die scope: i.e.,
10054 starts/finishes processing a DIE. */
10055class process_die_scope
adde2bff 10056{
4c8aa72d
PA
10057public:
10058 process_die_scope (die_info *die, dwarf2_cu *cu)
10059 : m_die (die), m_cu (cu)
10060 {
10061 /* We should only be processing DIEs not already in process. */
10062 gdb_assert (!m_die->in_process);
10063 m_die->in_process = true;
10064 }
8c3cb9fa 10065
4c8aa72d
PA
10066 ~process_die_scope ()
10067 {
10068 m_die->in_process = false;
10069
10070 /* If we're done processing the DIE for the CU that owns the line
10071 header, we don't need the line header anymore. */
10072 if (m_cu->line_header_die_owner == m_die)
10073 {
10074 delete m_cu->line_header;
10075 m_cu->line_header = NULL;
10076 m_cu->line_header_die_owner = NULL;
10077 }
10078 }
10079
10080private:
10081 die_info *m_die;
10082 dwarf2_cu *m_cu;
10083};
adde2bff 10084
c906108c
SS
10085/* Process a die and its children. */
10086
10087static void
e7c27a73 10088process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10089{
4c8aa72d 10090 process_die_scope scope (die, cu);
adde2bff 10091
c906108c
SS
10092 switch (die->tag)
10093 {
10094 case DW_TAG_padding:
10095 break;
10096 case DW_TAG_compile_unit:
95554aad 10097 case DW_TAG_partial_unit:
e7c27a73 10098 read_file_scope (die, cu);
c906108c 10099 break;
348e048f
DE
10100 case DW_TAG_type_unit:
10101 read_type_unit_scope (die, cu);
10102 break;
c906108c 10103 case DW_TAG_subprogram:
0a4b0913
AB
10104 /* Nested subprograms in Fortran get a prefix. */
10105 if (cu->language == language_fortran
10106 && die->parent != NULL
10107 && die->parent->tag == DW_TAG_subprogram)
10108 cu->processing_has_namespace_info = true;
10109 /* Fall through. */
c906108c 10110 case DW_TAG_inlined_subroutine:
edb3359d 10111 read_func_scope (die, cu);
c906108c
SS
10112 break;
10113 case DW_TAG_lexical_block:
14898363
L
10114 case DW_TAG_try_block:
10115 case DW_TAG_catch_block:
e7c27a73 10116 read_lexical_block_scope (die, cu);
c906108c 10117 break;
216f72a1 10118 case DW_TAG_call_site:
96408a79
SA
10119 case DW_TAG_GNU_call_site:
10120 read_call_site_scope (die, cu);
10121 break;
c906108c 10122 case DW_TAG_class_type:
680b30c7 10123 case DW_TAG_interface_type:
c906108c
SS
10124 case DW_TAG_structure_type:
10125 case DW_TAG_union_type:
134d01f1 10126 process_structure_scope (die, cu);
c906108c
SS
10127 break;
10128 case DW_TAG_enumeration_type:
134d01f1 10129 process_enumeration_scope (die, cu);
c906108c 10130 break;
134d01f1 10131
f792889a
DJ
10132 /* These dies have a type, but processing them does not create
10133 a symbol or recurse to process the children. Therefore we can
10134 read them on-demand through read_type_die. */
c906108c 10135 case DW_TAG_subroutine_type:
72019c9c 10136 case DW_TAG_set_type:
c906108c 10137 case DW_TAG_array_type:
c906108c 10138 case DW_TAG_pointer_type:
c906108c 10139 case DW_TAG_ptr_to_member_type:
c906108c 10140 case DW_TAG_reference_type:
4297a3f0 10141 case DW_TAG_rvalue_reference_type:
c906108c 10142 case DW_TAG_string_type:
c906108c 10143 break;
134d01f1 10144
c906108c 10145 case DW_TAG_base_type:
a02abb62 10146 case DW_TAG_subrange_type:
cb249c71 10147 case DW_TAG_typedef:
134d01f1
DJ
10148 /* Add a typedef symbol for the type definition, if it has a
10149 DW_AT_name. */
f792889a 10150 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10151 break;
c906108c 10152 case DW_TAG_common_block:
e7c27a73 10153 read_common_block (die, cu);
c906108c
SS
10154 break;
10155 case DW_TAG_common_inclusion:
10156 break;
d9fa45fe 10157 case DW_TAG_namespace:
9068261f 10158 cu->processing_has_namespace_info = true;
e7c27a73 10159 read_namespace (die, cu);
d9fa45fe 10160 break;
5d7cb8df 10161 case DW_TAG_module:
9068261f 10162 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10163 read_module (die, cu);
10164 break;
d9fa45fe 10165 case DW_TAG_imported_declaration:
9068261f 10166 cu->processing_has_namespace_info = true;
74921315
KS
10167 if (read_namespace_alias (die, cu))
10168 break;
86a73007
TT
10169 /* The declaration is not a global namespace alias. */
10170 /* Fall through. */
d9fa45fe 10171 case DW_TAG_imported_module:
9068261f 10172 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10173 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10174 || cu->language != language_fortran))
b98664d3 10175 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10176 dwarf_tag_name (die->tag));
10177 read_import_statement (die, cu);
d9fa45fe 10178 break;
95554aad
TT
10179
10180 case DW_TAG_imported_unit:
10181 process_imported_unit_die (die, cu);
10182 break;
10183
71a3c369
TT
10184 case DW_TAG_variable:
10185 read_variable (die, cu);
10186 break;
10187
c906108c 10188 default:
e7c27a73 10189 new_symbol (die, NULL, cu);
c906108c
SS
10190 break;
10191 }
10192}
ca69b9e6
DE
10193\f
10194/* DWARF name computation. */
c906108c 10195
94af9270
KS
10196/* A helper function for dwarf2_compute_name which determines whether DIE
10197 needs to have the name of the scope prepended to the name listed in the
10198 die. */
10199
10200static int
10201die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10202{
1c809c68
TT
10203 struct attribute *attr;
10204
94af9270
KS
10205 switch (die->tag)
10206 {
10207 case DW_TAG_namespace:
10208 case DW_TAG_typedef:
10209 case DW_TAG_class_type:
10210 case DW_TAG_interface_type:
10211 case DW_TAG_structure_type:
10212 case DW_TAG_union_type:
10213 case DW_TAG_enumeration_type:
10214 case DW_TAG_enumerator:
10215 case DW_TAG_subprogram:
08a76f8a 10216 case DW_TAG_inlined_subroutine:
94af9270 10217 case DW_TAG_member:
74921315 10218 case DW_TAG_imported_declaration:
94af9270
KS
10219 return 1;
10220
10221 case DW_TAG_variable:
c2b0a229 10222 case DW_TAG_constant:
94af9270
KS
10223 /* We only need to prefix "globally" visible variables. These include
10224 any variable marked with DW_AT_external or any variable that
10225 lives in a namespace. [Variables in anonymous namespaces
10226 require prefixing, but they are not DW_AT_external.] */
10227
10228 if (dwarf2_attr (die, DW_AT_specification, cu))
10229 {
10230 struct dwarf2_cu *spec_cu = cu;
9a619af0 10231
94af9270
KS
10232 return die_needs_namespace (die_specification (die, &spec_cu),
10233 spec_cu);
10234 }
10235
1c809c68 10236 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10237 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10238 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10239 return 0;
10240 /* A variable in a lexical block of some kind does not need a
10241 namespace, even though in C++ such variables may be external
10242 and have a mangled name. */
10243 if (die->parent->tag == DW_TAG_lexical_block
10244 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10245 || die->parent->tag == DW_TAG_catch_block
10246 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10247 return 0;
10248 return 1;
94af9270
KS
10249
10250 default:
10251 return 0;
10252 }
10253}
10254
73b9be8b
KS
10255/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10256 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10257 defined for the given DIE. */
10258
10259static struct attribute *
10260dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10261{
10262 struct attribute *attr;
10263
10264 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10265 if (attr == NULL)
10266 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10267
10268 return attr;
10269}
10270
10271/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10272 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10273 defined for the given DIE. */
10274
10275static const char *
10276dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10277{
10278 const char *linkage_name;
10279
10280 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10281 if (linkage_name == NULL)
10282 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10283
787de330
TT
10284 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10285 See https://github.com/rust-lang/rust/issues/32925. */
10286 if (cu->language == language_rust && linkage_name != NULL
10287 && strchr (linkage_name, '{') != NULL)
10288 linkage_name = NULL;
10289
73b9be8b
KS
10290 return linkage_name;
10291}
10292
94af9270 10293/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10294 compute the physname for the object, which include a method's:
9c37b5ae 10295 - formal parameters (C++),
a766d390 10296 - receiver type (Go),
a766d390
DE
10297
10298 The term "physname" is a bit confusing.
10299 For C++, for example, it is the demangled name.
10300 For Go, for example, it's the mangled name.
94af9270 10301
af6b7be1
JB
10302 For Ada, return the DIE's linkage name rather than the fully qualified
10303 name. PHYSNAME is ignored..
10304
5989a64e 10305 The result is allocated on the objfile->per_bfd's obstack and
45940949 10306 canonicalized. */
94af9270
KS
10307
10308static const char *
15d034d0
TT
10309dwarf2_compute_name (const char *name,
10310 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10311 int physname)
10312{
5e22e966 10313 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10314
94af9270
KS
10315 if (name == NULL)
10316 name = dwarf2_name (die, cu);
10317
2ee7123e
DE
10318 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10319 but otherwise compute it by typename_concat inside GDB.
10320 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10321 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10322 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10323 will set the demangled name to the result of dwarf2_full_name, and it is
10324 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10325 if (cu->language == language_ada
10326 || (cu->language == language_fortran && physname))
10327 {
10328 /* For Ada unit, we prefer the linkage name over the name, as
10329 the former contains the exported name, which the user expects
10330 to be able to reference. Ideally, we want the user to be able
10331 to reference this entity using either natural or linkage name,
10332 but we haven't started looking at this enhancement yet. */
73b9be8b 10333 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10334
2ee7123e
DE
10335 if (linkage_name != NULL)
10336 return linkage_name;
f55ee35c
JK
10337 }
10338
94af9270
KS
10339 /* These are the only languages we know how to qualify names in. */
10340 if (name != NULL
9c37b5ae 10341 && (cu->language == language_cplus
c44af4eb
TT
10342 || cu->language == language_fortran || cu->language == language_d
10343 || cu->language == language_rust))
94af9270
KS
10344 {
10345 if (die_needs_namespace (die, cu))
10346 {
0d5cff50 10347 const char *prefix;
34a68019 10348 const char *canonical_name = NULL;
94af9270 10349
d7e74731
PA
10350 string_file buf;
10351
94af9270 10352 prefix = determine_prefix (die, cu);
94af9270
KS
10353 if (*prefix != '\0')
10354 {
43816ebc
TT
10355 gdb::unique_xmalloc_ptr<char> prefixed_name
10356 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10357
43816ebc 10358 buf.puts (prefixed_name.get ());
94af9270
KS
10359 }
10360 else
d7e74731 10361 buf.puts (name);
94af9270 10362
98bfdba5
PA
10363 /* Template parameters may be specified in the DIE's DW_AT_name, or
10364 as children with DW_TAG_template_type_param or
10365 DW_TAG_value_type_param. If the latter, add them to the name
10366 here. If the name already has template parameters, then
10367 skip this step; some versions of GCC emit both, and
10368 it is more efficient to use the pre-computed name.
10369
10370 Something to keep in mind about this process: it is very
10371 unlikely, or in some cases downright impossible, to produce
10372 something that will match the mangled name of a function.
10373 If the definition of the function has the same debug info,
10374 we should be able to match up with it anyway. But fallbacks
10375 using the minimal symbol, for instance to find a method
10376 implemented in a stripped copy of libstdc++, will not work.
10377 If we do not have debug info for the definition, we will have to
10378 match them up some other way.
10379
10380 When we do name matching there is a related problem with function
10381 templates; two instantiated function templates are allowed to
10382 differ only by their return types, which we do not add here. */
10383
10384 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10385 {
10386 struct attribute *attr;
10387 struct die_info *child;
10388 int first = 1;
10389
10390 die->building_fullname = 1;
10391
10392 for (child = die->child; child != NULL; child = child->sibling)
10393 {
10394 struct type *type;
12df843f 10395 LONGEST value;
d521ce57 10396 const gdb_byte *bytes;
98bfdba5
PA
10397 struct dwarf2_locexpr_baton *baton;
10398 struct value *v;
10399
10400 if (child->tag != DW_TAG_template_type_param
10401 && child->tag != DW_TAG_template_value_param)
10402 continue;
10403
10404 if (first)
10405 {
d7e74731 10406 buf.puts ("<");
98bfdba5
PA
10407 first = 0;
10408 }
10409 else
d7e74731 10410 buf.puts (", ");
98bfdba5
PA
10411
10412 attr = dwarf2_attr (child, DW_AT_type, cu);
10413 if (attr == NULL)
10414 {
b98664d3 10415 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10416 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10417 continue;
10418 }
10419 type = die_type (child, cu);
10420
10421 if (child->tag == DW_TAG_template_type_param)
10422 {
c1ec8cea
TT
10423 c_print_type (type, "", &buf, -1, 0, cu->language,
10424 &type_print_raw_options);
98bfdba5
PA
10425 continue;
10426 }
10427
10428 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10429 if (attr == NULL)
10430 {
b98664d3 10431 complaint (_("template parameter missing "
3e43a32a 10432 "DW_AT_const_value"));
d7e74731 10433 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10434 continue;
10435 }
10436
10437 dwarf2_const_value_attr (attr, type, name,
10438 &cu->comp_unit_obstack, cu,
10439 &value, &bytes, &baton);
10440
10441 if (TYPE_NOSIGN (type))
10442 /* GDB prints characters as NUMBER 'CHAR'. If that's
10443 changed, this can use value_print instead. */
d7e74731 10444 c_printchar (value, type, &buf);
98bfdba5
PA
10445 else
10446 {
10447 struct value_print_options opts;
10448
10449 if (baton != NULL)
10450 v = dwarf2_evaluate_loc_desc (type, NULL,
10451 baton->data,
10452 baton->size,
9f47c707
SM
10453 baton->per_cu,
10454 baton->per_objfile);
98bfdba5
PA
10455 else if (bytes != NULL)
10456 {
10457 v = allocate_value (type);
10458 memcpy (value_contents_writeable (v), bytes,
10459 TYPE_LENGTH (type));
10460 }
10461 else
10462 v = value_from_longest (type, value);
10463
3e43a32a
MS
10464 /* Specify decimal so that we do not depend on
10465 the radix. */
98bfdba5
PA
10466 get_formatted_print_options (&opts, 'd');
10467 opts.raw = 1;
d7e74731 10468 value_print (v, &buf, &opts);
98bfdba5 10469 release_value (v);
98bfdba5
PA
10470 }
10471 }
10472
10473 die->building_fullname = 0;
10474
10475 if (!first)
10476 {
10477 /* Close the argument list, with a space if necessary
10478 (nested templates). */
d7e74731
PA
10479 if (!buf.empty () && buf.string ().back () == '>')
10480 buf.puts (" >");
98bfdba5 10481 else
d7e74731 10482 buf.puts (">");
98bfdba5
PA
10483 }
10484 }
10485
9c37b5ae 10486 /* For C++ methods, append formal parameter type
94af9270 10487 information, if PHYSNAME. */
6e70227d 10488
94af9270 10489 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10490 && cu->language == language_cplus)
94af9270
KS
10491 {
10492 struct type *type = read_type_die (die, cu);
10493
d7e74731 10494 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10495 &type_print_raw_options);
94af9270 10496
9c37b5ae 10497 if (cu->language == language_cplus)
94af9270 10498 {
60430eff
DJ
10499 /* Assume that an artificial first parameter is
10500 "this", but do not crash if it is not. RealView
10501 marks unnamed (and thus unused) parameters as
10502 artificial; there is no way to differentiate
10503 the two cases. */
1f704f76 10504 if (type->num_fields () > 0
94af9270 10505 && TYPE_FIELD_ARTIFICIAL (type, 0)
78134374 10506 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
3e43a32a
MS
10507 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10508 0))))
d7e74731 10509 buf.puts (" const");
94af9270
KS
10510 }
10511 }
10512
d7e74731 10513 const std::string &intermediate_name = buf.string ();
94af9270
KS
10514
10515 if (cu->language == language_cplus)
34a68019 10516 canonical_name
322a8516 10517 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10518 objfile);
34a68019
TT
10519
10520 /* If we only computed INTERMEDIATE_NAME, or if
10521 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10522 intern it. */
322a8516 10523 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10524 name = objfile->intern (intermediate_name);
34a68019
TT
10525 else
10526 name = canonical_name;
94af9270
KS
10527 }
10528 }
10529
10530 return name;
10531}
10532
0114d602
DJ
10533/* Return the fully qualified name of DIE, based on its DW_AT_name.
10534 If scope qualifiers are appropriate they will be added. The result
34a68019 10535 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10536 not have a name. NAME may either be from a previous call to
10537 dwarf2_name or NULL.
10538
9c37b5ae 10539 The output string will be canonicalized (if C++). */
0114d602
DJ
10540
10541static const char *
15d034d0 10542dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10543{
94af9270
KS
10544 return dwarf2_compute_name (name, die, cu, 0);
10545}
0114d602 10546
94af9270
KS
10547/* Construct a physname for the given DIE in CU. NAME may either be
10548 from a previous call to dwarf2_name or NULL. The result will be
10549 allocated on the objfile_objstack or NULL if the DIE does not have a
10550 name.
0114d602 10551
9c37b5ae 10552 The output string will be canonicalized (if C++). */
0114d602 10553
94af9270 10554static const char *
15d034d0 10555dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10556{
5e22e966 10557 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10558 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10559 int need_copy = 1;
10560
10561 /* In this case dwarf2_compute_name is just a shortcut not building anything
10562 on its own. */
10563 if (!die_needs_namespace (die, cu))
10564 return dwarf2_compute_name (name, die, cu, 1);
10565
906bb4c5
TT
10566 if (cu->language != language_rust)
10567 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10568
10569 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10570 has computed. */
791afaa2 10571 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10572 if (mangled != NULL)
900e11f9 10573 {
900e11f9 10574
59cc4834
JB
10575 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10576 {
10577 /* Do nothing (do not demangle the symbol name). */
10578 }
10579 else if (cu->language == language_go)
a766d390 10580 {
5e2db402
TT
10581 /* This is a lie, but we already lie to the caller new_symbol.
10582 new_symbol assumes we return the mangled name.
a766d390 10583 This just undoes that lie until things are cleaned up. */
a766d390
DE
10584 }
10585 else
10586 {
0eb876f5
JB
10587 /* Use DMGL_RET_DROP for C++ template functions to suppress
10588 their return type. It is easier for GDB users to search
10589 for such functions as `name(params)' than `long name(params)'.
10590 In such case the minimal symbol names do not match the full
10591 symbol names but for template functions there is never a need
10592 to look up their definition from their declaration so
10593 the only disadvantage remains the minimal symbol variant
10594 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10595 demangled.reset (gdb_demangle (mangled,
10596 (DMGL_PARAMS | DMGL_ANSI
10597 | DMGL_RET_DROP)));
a766d390 10598 }
900e11f9 10599 if (demangled)
791afaa2 10600 canon = demangled.get ();
900e11f9
JK
10601 else
10602 {
10603 canon = mangled;
10604 need_copy = 0;
10605 }
10606 }
10607
10608 if (canon == NULL || check_physname)
10609 {
10610 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10611
10612 if (canon != NULL && strcmp (physname, canon) != 0)
10613 {
10614 /* It may not mean a bug in GDB. The compiler could also
10615 compute DW_AT_linkage_name incorrectly. But in such case
10616 GDB would need to be bug-to-bug compatible. */
10617
b98664d3 10618 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10619 "(from linkage <%s>) - DIE at %s [in module %s]"),
10620 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10621 objfile_name (objfile));
900e11f9
JK
10622
10623 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10624 is available here - over computed PHYSNAME. It is safer
10625 against both buggy GDB and buggy compilers. */
10626
10627 retval = canon;
10628 }
10629 else
10630 {
10631 retval = physname;
10632 need_copy = 0;
10633 }
10634 }
10635 else
10636 retval = canon;
10637
10638 if (need_copy)
be1e3d3e 10639 retval = objfile->intern (retval);
900e11f9 10640
900e11f9 10641 return retval;
0114d602
DJ
10642}
10643
74921315
KS
10644/* Inspect DIE in CU for a namespace alias. If one exists, record
10645 a new symbol for it.
10646
10647 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10648
10649static int
10650read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10651{
10652 struct attribute *attr;
10653
10654 /* If the die does not have a name, this is not a namespace
10655 alias. */
10656 attr = dwarf2_attr (die, DW_AT_name, cu);
10657 if (attr != NULL)
10658 {
10659 int num;
10660 struct die_info *d = die;
10661 struct dwarf2_cu *imported_cu = cu;
10662
10663 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10664 keep inspecting DIEs until we hit the underlying import. */
10665#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10666 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10667 {
10668 attr = dwarf2_attr (d, DW_AT_import, cu);
10669 if (attr == NULL)
10670 break;
10671
10672 d = follow_die_ref (d, attr, &imported_cu);
10673 if (d->tag != DW_TAG_imported_declaration)
10674 break;
10675 }
10676
10677 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10678 {
b98664d3 10679 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10680 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10681 return 0;
10682 }
10683
10684 if (attr != NULL)
10685 {
10686 struct type *type;
0826b30a 10687 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10688
aa66c379 10689 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10690 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10691 {
10692 /* This declaration is a global namespace alias. Add
10693 a symbol for it whose type is the aliased namespace. */
10694 new_symbol (die, type, cu);
10695 return 1;
10696 }
10697 }
10698 }
10699
10700 return 0;
10701}
10702
22cee43f 10703/* Return the using directives repository (global or local?) to use in the
804d2729 10704 current context for CU.
22cee43f
PMR
10705
10706 For Ada, imported declarations can materialize renamings, which *may* be
10707 global. However it is impossible (for now?) in DWARF to distinguish
10708 "external" imported declarations and "static" ones. As all imported
10709 declarations seem to be static in all other languages, make them all CU-wide
10710 global only in Ada. */
10711
10712static struct using_direct **
804d2729 10713using_directives (struct dwarf2_cu *cu)
22cee43f 10714{
c24bdb02
KS
10715 if (cu->language == language_ada
10716 && cu->get_builder ()->outermost_context_p ())
10717 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10718 else
c24bdb02 10719 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10720}
10721
27aa8d6a
SW
10722/* Read the import statement specified by the given die and record it. */
10723
10724static void
10725read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10726{
5e22e966 10727 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10728 struct attribute *import_attr;
32019081 10729 struct die_info *imported_die, *child_die;
de4affc9 10730 struct dwarf2_cu *imported_cu;
27aa8d6a 10731 const char *imported_name;
794684b6 10732 const char *imported_name_prefix;
13387711
SW
10733 const char *canonical_name;
10734 const char *import_alias;
10735 const char *imported_declaration = NULL;
794684b6 10736 const char *import_prefix;
eb1e02fd 10737 std::vector<const char *> excludes;
13387711 10738
27aa8d6a
SW
10739 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10740 if (import_attr == NULL)
10741 {
b98664d3 10742 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10743 dwarf_tag_name (die->tag));
10744 return;
10745 }
10746
de4affc9
CC
10747 imported_cu = cu;
10748 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10749 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10750 if (imported_name == NULL)
10751 {
10752 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10753
10754 The import in the following code:
10755 namespace A
10756 {
10757 typedef int B;
10758 }
10759
10760 int main ()
10761 {
10762 using A::B;
10763 B b;
10764 return b;
10765 }
10766
10767 ...
10768 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10769 <52> DW_AT_decl_file : 1
10770 <53> DW_AT_decl_line : 6
10771 <54> DW_AT_import : <0x75>
10772 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10773 <59> DW_AT_name : B
10774 <5b> DW_AT_decl_file : 1
10775 <5c> DW_AT_decl_line : 2
10776 <5d> DW_AT_type : <0x6e>
10777 ...
10778 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10779 <76> DW_AT_byte_size : 4
10780 <77> DW_AT_encoding : 5 (signed)
10781
10782 imports the wrong die ( 0x75 instead of 0x58 ).
10783 This case will be ignored until the gcc bug is fixed. */
10784 return;
10785 }
10786
82856980
SW
10787 /* Figure out the local name after import. */
10788 import_alias = dwarf2_name (die, cu);
27aa8d6a 10789
794684b6
SW
10790 /* Figure out where the statement is being imported to. */
10791 import_prefix = determine_prefix (die, cu);
10792
10793 /* Figure out what the scope of the imported die is and prepend it
10794 to the name of the imported die. */
de4affc9 10795 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10796
f55ee35c
JK
10797 if (imported_die->tag != DW_TAG_namespace
10798 && imported_die->tag != DW_TAG_module)
794684b6 10799 {
13387711
SW
10800 imported_declaration = imported_name;
10801 canonical_name = imported_name_prefix;
794684b6 10802 }
13387711 10803 else if (strlen (imported_name_prefix) > 0)
12aaed36 10804 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10805 imported_name_prefix,
10806 (cu->language == language_d ? "." : "::"),
10807 imported_name, (char *) NULL);
13387711
SW
10808 else
10809 canonical_name = imported_name;
794684b6 10810
32019081
JK
10811 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10812 for (child_die = die->child; child_die && child_die->tag;
436c571c 10813 child_die = child_die->sibling)
32019081
JK
10814 {
10815 /* DWARF-4: A Fortran use statement with a “rename list” may be
10816 represented by an imported module entry with an import attribute
10817 referring to the module and owned entries corresponding to those
10818 entities that are renamed as part of being imported. */
10819
10820 if (child_die->tag != DW_TAG_imported_declaration)
10821 {
b98664d3 10822 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10823 "- DIE at %s [in module %s]"),
10824 sect_offset_str (child_die->sect_off),
10825 objfile_name (objfile));
32019081
JK
10826 continue;
10827 }
10828
10829 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10830 if (import_attr == NULL)
10831 {
b98664d3 10832 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10833 dwarf_tag_name (child_die->tag));
10834 continue;
10835 }
10836
10837 imported_cu = cu;
10838 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10839 &imported_cu);
10840 imported_name = dwarf2_name (imported_die, imported_cu);
10841 if (imported_name == NULL)
10842 {
b98664d3 10843 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10844 "imported name - DIE at %s [in module %s]"),
10845 sect_offset_str (child_die->sect_off),
10846 objfile_name (objfile));
32019081
JK
10847 continue;
10848 }
10849
eb1e02fd 10850 excludes.push_back (imported_name);
32019081
JK
10851
10852 process_die (child_die, cu);
10853 }
10854
804d2729 10855 add_using_directive (using_directives (cu),
22cee43f
PMR
10856 import_prefix,
10857 canonical_name,
10858 import_alias,
10859 imported_declaration,
10860 excludes,
10861 0,
10862 &objfile->objfile_obstack);
27aa8d6a
SW
10863}
10864
5230b05a
WT
10865/* ICC<14 does not output the required DW_AT_declaration on incomplete
10866 types, but gives them a size of zero. Starting with version 14,
10867 ICC is compatible with GCC. */
10868
9068261f 10869static bool
5230b05a
WT
10870producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10871{
10872 if (!cu->checked_producer)
10873 check_producer (cu);
10874
10875 return cu->producer_is_icc_lt_14;
10876}
10877
eb77c9df
AB
10878/* ICC generates a DW_AT_type for C void functions. This was observed on
10879 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10880 which says that void functions should not have a DW_AT_type. */
10881
10882static bool
10883producer_is_icc (struct dwarf2_cu *cu)
10884{
10885 if (!cu->checked_producer)
10886 check_producer (cu);
10887
10888 return cu->producer_is_icc;
10889}
10890
1b80a9fa
JK
10891/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10892 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10893 this, it was first present in GCC release 4.3.0. */
10894
9068261f 10895static bool
1b80a9fa
JK
10896producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10897{
10898 if (!cu->checked_producer)
10899 check_producer (cu);
10900
10901 return cu->producer_is_gcc_lt_4_3;
10902}
10903
d721ba37
PA
10904static file_and_directory
10905find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10906{
d721ba37
PA
10907 file_and_directory res;
10908
9291a0cd
TT
10909 /* Find the filename. Do not use dwarf2_name here, since the filename
10910 is not a source language identifier. */
d721ba37
PA
10911 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10912 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10913
d721ba37
PA
10914 if (res.comp_dir == NULL
10915 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10916 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10917 {
d721ba37
PA
10918 res.comp_dir_storage = ldirname (res.name);
10919 if (!res.comp_dir_storage.empty ())
10920 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10921 }
d721ba37 10922 if (res.comp_dir != NULL)
9291a0cd
TT
10923 {
10924 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10925 directory, get rid of it. */
d721ba37 10926 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10927
d721ba37
PA
10928 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10929 res.comp_dir = cp + 1;
9291a0cd
TT
10930 }
10931
d721ba37
PA
10932 if (res.name == NULL)
10933 res.name = "<unknown>";
10934
10935 return res;
9291a0cd
TT
10936}
10937
f4dc4d17
DE
10938/* Handle DW_AT_stmt_list for a compilation unit.
10939 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10940 COMP_DIR is the compilation directory. LOWPC is passed to
10941 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10942
10943static void
10944handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10945 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10946{
5e22e966 10947 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
2ab95328 10948 struct attribute *attr;
527f3840
JK
10949 struct line_header line_header_local;
10950 hashval_t line_header_local_hash;
527f3840
JK
10951 void **slot;
10952 int decode_mapping;
2ab95328 10953
f4dc4d17
DE
10954 gdb_assert (! cu->per_cu->is_debug_types);
10955
2ab95328 10956 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10957 if (attr == NULL)
10958 return;
10959
9c541725 10960 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10961
10962 /* The line header hash table is only created if needed (it exists to
10963 prevent redundant reading of the line table for partial_units).
10964 If we're given a partial_unit, we'll need it. If we're given a
10965 compile_unit, then use the line header hash table if it's already
10966 created, but don't create one just yet. */
10967
39b16f87 10968 if (dwarf2_per_objfile->line_header_hash == NULL
527f3840 10969 && die->tag == DW_TAG_partial_unit)
2ab95328 10970 {
39b16f87 10971 dwarf2_per_objfile->line_header_hash
d15acc42
TT
10972 .reset (htab_create_alloc (127, line_header_hash_voidp,
10973 line_header_eq_voidp,
10974 free_line_header_voidp,
10975 xcalloc, xfree));
527f3840 10976 }
2ab95328 10977
9c541725 10978 line_header_local.sect_off = line_offset;
527f3840
JK
10979 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10980 line_header_local_hash = line_header_hash (&line_header_local);
39b16f87 10981 if (dwarf2_per_objfile->line_header_hash != NULL)
527f3840 10982 {
39b16f87 10983 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10984 &line_header_local,
10985 line_header_local_hash, NO_INSERT);
10986
10987 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10988 is not present in *SLOT (since if there is something in *SLOT then
10989 it will be for a partial_unit). */
10990 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10991 {
527f3840 10992 gdb_assert (*slot != NULL);
9a3c8263 10993 cu->line_header = (struct line_header *) *slot;
527f3840 10994 return;
dee91e82 10995 }
2ab95328 10996 }
527f3840
JK
10997
10998 /* dwarf_decode_line_header does not yet provide sufficient information.
10999 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11000 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11001 if (lh == NULL)
527f3840 11002 return;
4c8aa72d
PA
11003
11004 cu->line_header = lh.release ();
11005 cu->line_header_die_owner = die;
527f3840 11006
39b16f87 11007 if (dwarf2_per_objfile->line_header_hash == NULL)
527f3840
JK
11008 slot = NULL;
11009 else
11010 {
39b16f87 11011 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
11012 &line_header_local,
11013 line_header_local_hash, INSERT);
11014 gdb_assert (slot != NULL);
11015 }
11016 if (slot != NULL && *slot == NULL)
11017 {
11018 /* This newly decoded line number information unit will be owned
11019 by line_header_hash hash table. */
11020 *slot = cu->line_header;
4c8aa72d 11021 cu->line_header_die_owner = NULL;
527f3840
JK
11022 }
11023 else
11024 {
11025 /* We cannot free any current entry in (*slot) as that struct line_header
11026 may be already used by multiple CUs. Create only temporary decoded
11027 line_header for this CU - it may happen at most once for each line
11028 number information unit. And if we're not using line_header_hash
11029 then this is what we want as well. */
11030 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11031 }
11032 decode_mapping = (die->tag != DW_TAG_partial_unit);
11033 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11034 decode_mapping);
fff8551c 11035
2ab95328
TT
11036}
11037
95554aad 11038/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11039
c906108c 11040static void
e7c27a73 11041read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11042{
5e22e966 11043 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
dee91e82 11044 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 11045 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 11046 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11047 CORE_ADDR highpc = ((CORE_ADDR) 0);
11048 struct attribute *attr;
c906108c 11049 struct die_info *child_die;
e142c38c 11050 CORE_ADDR baseaddr;
6e70227d 11051
380618d6 11052 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 11053 baseaddr = objfile->text_section_offset ();
c906108c 11054
fae299cd 11055 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11056
11057 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11058 from finish_block. */
2acceee2 11059 if (lowpc == ((CORE_ADDR) -1))
c906108c 11060 lowpc = highpc;
3e29f34a 11061 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11062
d721ba37 11063 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11064
f4b8a18d
KW
11065 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11066 standardised yet. As a workaround for the language detection we fall
11067 back to the DW_AT_producer string. */
11068 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11069 cu->language = language_opencl;
11070
3019eac3
DE
11071 /* Similar hack for Go. */
11072 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11073 set_cu_language (DW_LANG_Go, cu);
11074
c24bdb02 11075 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11076
11077 /* Decode line number information if present. We do this before
11078 processing child DIEs, so that the line header table is available
11079 for DW_AT_decl_file. */
d721ba37 11080 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11081
11082 /* Process all dies in compilation unit. */
11083 if (die->child != NULL)
11084 {
11085 child_die = die->child;
11086 while (child_die && child_die->tag)
11087 {
11088 process_die (child_die, cu);
436c571c 11089 child_die = child_die->sibling;
3019eac3
DE
11090 }
11091 }
11092
11093 /* Decode macro information, if present. Dwarf 2 macro information
11094 refers to information in the line number info statement program
11095 header, so we can only read it if we've read the header
11096 successfully. */
0af92d60
JK
11097 attr = dwarf2_attr (die, DW_AT_macros, cu);
11098 if (attr == NULL)
11099 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11100 if (attr && cu->line_header)
11101 {
11102 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11103 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11104
43f3e411 11105 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11106 }
11107 else
11108 {
11109 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11110 if (attr && cu->line_header)
11111 {
11112 unsigned int macro_offset = DW_UNSND (attr);
11113
43f3e411 11114 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11115 }
11116 }
3019eac3
DE
11117}
11118
c24bdb02
KS
11119void
11120dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11121{
f4dc4d17
DE
11122 struct type_unit_group *tu_group;
11123 int first_time;
3019eac3 11124 struct attribute *attr;
9c541725 11125 unsigned int i;
0186c6a7 11126 struct signatured_type *sig_type;
3019eac3 11127
f4dc4d17 11128 gdb_assert (per_cu->is_debug_types);
0186c6a7 11129 sig_type = (struct signatured_type *) per_cu;
3019eac3 11130
c24bdb02 11131 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11132
f4dc4d17 11133 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11134 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11135 if (sig_type->type_unit_group == NULL)
c24bdb02 11136 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11137 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11138
11139 /* If we've already processed this stmt_list there's no real need to
11140 do it again, we could fake it and just recreate the part we need
11141 (file name,index -> symtab mapping). If data shows this optimization
11142 is useful we can do it then. */
8adb8487
TT
11143 type_unit_group_unshareable *tug_unshare
11144 = per_objfile->get_type_unit_group_unshareable (tu_group);
11145 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
11146
11147 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11148 debug info. */
fff8551c 11149 line_header_up lh;
f4dc4d17 11150 if (attr != NULL)
3019eac3 11151 {
9c541725 11152 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11153 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11154 }
11155 if (lh == NULL)
11156 {
11157 if (first_time)
c24bdb02 11158 start_symtab ("", NULL, 0);
f4dc4d17
DE
11159 else
11160 {
8adb8487 11161 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 11162 gdb_assert (m_builder == nullptr);
8adb8487 11163 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11164 m_builder.reset (new struct buildsym_compunit
11165 (COMPUNIT_OBJFILE (cust), "",
11166 COMPUNIT_DIRNAME (cust),
11167 compunit_language (cust),
11168 0, cust));
770479f2 11169 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11170 }
f4dc4d17 11171 return;
3019eac3
DE
11172 }
11173
c24bdb02
KS
11174 line_header = lh.release ();
11175 line_header_die_owner = die;
3019eac3 11176
f4dc4d17
DE
11177 if (first_time)
11178 {
c24bdb02 11179 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11180
1fd60fc0
DE
11181 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11182 still initializing it, and our caller (a few levels up)
11183 process_full_type_unit still needs to know if this is the first
11184 time. */
11185
8adb8487 11186 tug_unshare->symtabs
4ac93832
TT
11187 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11188 struct symtab *, line_header->file_names_size ());
3019eac3 11189
7ba99d21
AT
11190 auto &file_names = line_header->file_names ();
11191 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11192 {
7ba99d21 11193 file_entry &fe = file_names[i];
c24bdb02
KS
11194 dwarf2_start_subfile (this, fe.name,
11195 fe.include_dir (line_header));
11196 buildsym_compunit *b = get_builder ();
11197 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11198 {
4c8aa72d
PA
11199 /* NOTE: start_subfile will recognize when it's been
11200 passed a file it has already seen. So we can't
11201 assume there's a simple mapping from
11202 cu->line_header->file_names to subfiles, plus
11203 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11204 b->get_current_subfile ()->symtab
11205 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11206 }
11207
c24bdb02 11208 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 11209 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
11210 }
11211 }
11212 else
3019eac3 11213 {
c24bdb02 11214 gdb_assert (m_builder == nullptr);
8adb8487 11215 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11216 m_builder.reset (new struct buildsym_compunit
11217 (COMPUNIT_OBJFILE (cust), "",
11218 COMPUNIT_DIRNAME (cust),
11219 compunit_language (cust),
11220 0, cust));
770479f2 11221 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11222
7ba99d21
AT
11223 auto &file_names = line_header->file_names ();
11224 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11225 {
7ba99d21 11226 file_entry &fe = file_names[i];
8adb8487 11227 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 11228 }
3019eac3
DE
11229 }
11230
f4dc4d17
DE
11231 /* The main symtab is allocated last. Type units don't have DW_AT_name
11232 so they don't have a "real" (so to speak) symtab anyway.
11233 There is later code that will assign the main symtab to all symbols
11234 that don't have one. We need to handle the case of a symbol with a
11235 missing symtab (DW_AT_decl_file) anyway. */
11236}
3019eac3 11237
f4dc4d17
DE
11238/* Process DW_TAG_type_unit.
11239 For TUs we want to skip the first top level sibling if it's not the
11240 actual type being defined by this TU. In this case the first top
11241 level sibling is there to provide context only. */
3019eac3 11242
f4dc4d17
DE
11243static void
11244read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11245{
11246 struct die_info *child_die;
3019eac3 11247
f4dc4d17
DE
11248 prepare_one_comp_unit (cu, die, language_minimal);
11249
11250 /* Initialize (or reinitialize) the machinery for building symtabs.
11251 We do this before processing child DIEs, so that the line header table
11252 is available for DW_AT_decl_file. */
c24bdb02 11253 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11254
11255 if (die->child != NULL)
11256 {
11257 child_die = die->child;
11258 while (child_die && child_die->tag)
11259 {
11260 process_die (child_die, cu);
436c571c 11261 child_die = child_die->sibling;
f4dc4d17
DE
11262 }
11263 }
3019eac3
DE
11264}
11265\f
80626a55
DE
11266/* DWO/DWP files.
11267
11268 http://gcc.gnu.org/wiki/DebugFission
11269 http://gcc.gnu.org/wiki/DebugFissionDWP
11270
11271 To simplify handling of both DWO files ("object" files with the DWARF info)
11272 and DWP files (a file with the DWOs packaged up into one file), we treat
11273 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11274
11275static hashval_t
11276hash_dwo_file (const void *item)
11277{
9a3c8263 11278 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11279 hashval_t hash;
3019eac3 11280
a2ce51a0
DE
11281 hash = htab_hash_string (dwo_file->dwo_name);
11282 if (dwo_file->comp_dir != NULL)
11283 hash += htab_hash_string (dwo_file->comp_dir);
11284 return hash;
3019eac3
DE
11285}
11286
11287static int
11288eq_dwo_file (const void *item_lhs, const void *item_rhs)
11289{
9a3c8263
SM
11290 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11291 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11292
a2ce51a0
DE
11293 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11294 return 0;
11295 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11296 return lhs->comp_dir == rhs->comp_dir;
11297 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11298}
11299
11300/* Allocate a hash table for DWO files. */
11301
51ac9db5 11302static htab_up
298e9637 11303allocate_dwo_file_hash_table ()
3019eac3 11304{
51ac9db5
SM
11305 auto delete_dwo_file = [] (void *item)
11306 {
11307 struct dwo_file *dwo_file = (struct dwo_file *) item;
11308
11309 delete dwo_file;
11310 };
11311
bc68fb19
TT
11312 return htab_up (htab_create_alloc (41,
11313 hash_dwo_file,
11314 eq_dwo_file,
11315 delete_dwo_file,
11316 xcalloc, xfree));
3019eac3
DE
11317}
11318
80626a55
DE
11319/* Lookup DWO file DWO_NAME. */
11320
11321static void **
ed2dc618
SM
11322lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11323 const char *dwo_name,
11324 const char *comp_dir)
80626a55
DE
11325{
11326 struct dwo_file find_entry;
11327 void **slot;
11328
5989a64e
SM
11329 if (dwarf2_per_objfile->per_bfd->dwo_files == NULL)
11330 dwarf2_per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11331
0ac5b59e
DE
11332 find_entry.dwo_name = dwo_name;
11333 find_entry.comp_dir = comp_dir;
5989a64e 11334 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11335 INSERT);
80626a55
DE
11336
11337 return slot;
11338}
11339
3019eac3
DE
11340static hashval_t
11341hash_dwo_unit (const void *item)
11342{
9a3c8263 11343 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11344
11345 /* This drops the top 32 bits of the id, but is ok for a hash. */
11346 return dwo_unit->signature;
11347}
11348
11349static int
11350eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11351{
9a3c8263
SM
11352 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11353 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11354
11355 /* The signature is assumed to be unique within the DWO file.
11356 So while object file CU dwo_id's always have the value zero,
11357 that's OK, assuming each object file DWO file has only one CU,
11358 and that's the rule for now. */
11359 return lhs->signature == rhs->signature;
11360}
11361
11362/* Allocate a hash table for DWO CUs,TUs.
11363 There is one of these tables for each of CUs,TUs for each DWO file. */
11364
b0b6a987 11365static htab_up
298e9637 11366allocate_dwo_unit_table ()
3019eac3
DE
11367{
11368 /* Start out with a pretty small number.
11369 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11370 return htab_up (htab_create_alloc (3,
11371 hash_dwo_unit,
11372 eq_dwo_unit,
11373 NULL, xcalloc, xfree));
3019eac3
DE
11374}
11375
19c3d4c9 11376/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11377
11378static void
19c3d4c9
DE
11379create_dwo_cu_reader (const struct die_reader_specs *reader,
11380 const gdb_byte *info_ptr,
11381 struct die_info *comp_unit_die,
c0ab21c2
TT
11382 struct dwo_file *dwo_file,
11383 struct dwo_unit *dwo_unit)
3019eac3
DE
11384{
11385 struct dwarf2_cu *cu = reader->cu;
9c541725 11386 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11387 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11388
a084a2a6
AT
11389 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11390 if (!signature.has_value ())
3019eac3 11391 {
b98664d3 11392 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11393 " its dwo_id [in module %s]"),
9d8780f0 11394 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11395 return;
11396 }
11397
3019eac3 11398 dwo_unit->dwo_file = dwo_file;
a084a2a6 11399 dwo_unit->signature = *signature;
8a0459fd 11400 dwo_unit->section = section;
9c541725 11401 dwo_unit->sect_off = sect_off;
3019eac3
DE
11402 dwo_unit->length = cu->per_cu->length;
11403
b4f54984 11404 if (dwarf_read_debug)
9d8780f0
SM
11405 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11406 sect_offset_str (sect_off),
9c541725 11407 hex_string (dwo_unit->signature));
3019eac3
DE
11408}
11409
33c5cd75 11410/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11411 Note: This function processes DWO files only, not DWP files. */
3019eac3 11412
33c5cd75 11413static void
ed2dc618 11414create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e 11415 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11416 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3
DE
11417{
11418 struct objfile *objfile = dwarf2_per_objfile->objfile;
1859c670 11419 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
d521ce57 11420 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11421
96b79293 11422 section.read (objfile);
33c5cd75 11423 info_ptr = section.buffer;
3019eac3
DE
11424
11425 if (info_ptr == NULL)
33c5cd75 11426 return;
3019eac3 11427
b4f54984 11428 if (dwarf_read_debug)
19c3d4c9
DE
11429 {
11430 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11431 section.get_name (),
11432 section.get_file_name ());
19c3d4c9 11433 }
3019eac3 11434
33c5cd75 11435 end_ptr = info_ptr + section.size;
3019eac3
DE
11436 while (info_ptr < end_ptr)
11437 {
11438 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11439 struct dwo_unit read_unit {};
33c5cd75
DB
11440 struct dwo_unit *dwo_unit;
11441 void **slot;
11442 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11443
11444 memset (&per_cu, 0, sizeof (per_cu));
1859c670 11445 per_cu.per_bfd = per_bfd;
3019eac3 11446 per_cu.is_debug_types = 0;
33c5cd75
DB
11447 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11448 per_cu.section = &section;
11449
ab432490 11450 cutu_reader reader (&per_cu, dwarf2_per_objfile, cu, &dwo_file);
c0ab21c2
TT
11451 if (!reader.dummy_p)
11452 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11453 &dwo_file, &read_unit);
33c5cd75
DB
11454 info_ptr += per_cu.length;
11455
11456 // If the unit could not be parsed, skip it.
c0ab21c2 11457 if (read_unit.dwo_file == NULL)
33c5cd75 11458 continue;
3019eac3 11459
33c5cd75 11460 if (cus_htab == NULL)
298e9637 11461 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11462
1859c670 11463 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11464 struct dwo_unit);
c0ab21c2 11465 *dwo_unit = read_unit;
b0b6a987 11466 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11467 gdb_assert (slot != NULL);
11468 if (*slot != NULL)
19c3d4c9 11469 {
33c5cd75
DB
11470 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11471 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11472
b98664d3 11473 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11474 " the entry at offset %s, signature %s"),
11475 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11476 hex_string (dwo_unit->signature));
19c3d4c9 11477 }
33c5cd75 11478 *slot = (void *)dwo_unit;
3019eac3 11479 }
3019eac3
DE
11480}
11481
80626a55
DE
11482/* DWP file .debug_{cu,tu}_index section format:
11483 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11484
d2415c6c
DE
11485 DWP Version 1:
11486
80626a55
DE
11487 Both index sections have the same format, and serve to map a 64-bit
11488 signature to a set of section numbers. Each section begins with a header,
11489 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11490 indexes, and a pool of 32-bit section numbers. The index sections will be
11491 aligned at 8-byte boundaries in the file.
11492
d2415c6c
DE
11493 The index section header consists of:
11494
11495 V, 32 bit version number
11496 -, 32 bits unused
11497 N, 32 bit number of compilation units or type units in the index
11498 M, 32 bit number of slots in the hash table
80626a55 11499
d2415c6c 11500 Numbers are recorded using the byte order of the application binary.
80626a55 11501
d2415c6c
DE
11502 The hash table begins at offset 16 in the section, and consists of an array
11503 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11504 order of the application binary). Unused slots in the hash table are 0.
11505 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11506
d2415c6c
DE
11507 The parallel table begins immediately after the hash table
11508 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11509 array of 32-bit indexes (using the byte order of the application binary),
11510 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11511 table contains a 32-bit index into the pool of section numbers. For unused
11512 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11513
73869dc2
DE
11514 The pool of section numbers begins immediately following the hash table
11515 (at offset 16 + 12 * M from the beginning of the section). The pool of
11516 section numbers consists of an array of 32-bit words (using the byte order
11517 of the application binary). Each item in the array is indexed starting
11518 from 0. The hash table entry provides the index of the first section
11519 number in the set. Additional section numbers in the set follow, and the
11520 set is terminated by a 0 entry (section number 0 is not used in ELF).
11521
11522 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11523 section must be the first entry in the set, and the .debug_abbrev.dwo must
11524 be the second entry. Other members of the set may follow in any order.
11525
11526 ---
11527
11528 DWP Version 2:
11529
11530 DWP Version 2 combines all the .debug_info, etc. sections into one,
11531 and the entries in the index tables are now offsets into these sections.
11532 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11533 section.
11534
11535 Index Section Contents:
11536 Header
11537 Hash Table of Signatures dwp_hash_table.hash_table
11538 Parallel Table of Indices dwp_hash_table.unit_table
11539 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11540 Table of Section Sizes dwp_hash_table.v2.sizes
11541
11542 The index section header consists of:
11543
11544 V, 32 bit version number
11545 L, 32 bit number of columns in the table of section offsets
11546 N, 32 bit number of compilation units or type units in the index
11547 M, 32 bit number of slots in the hash table
11548
11549 Numbers are recorded using the byte order of the application binary.
11550
11551 The hash table has the same format as version 1.
11552 The parallel table of indices has the same format as version 1,
11553 except that the entries are origin-1 indices into the table of sections
11554 offsets and the table of section sizes.
11555
11556 The table of offsets begins immediately following the parallel table
11557 (at offset 16 + 12 * M from the beginning of the section). The table is
11558 a two-dimensional array of 32-bit words (using the byte order of the
11559 application binary), with L columns and N+1 rows, in row-major order.
11560 Each row in the array is indexed starting from 0. The first row provides
11561 a key to the remaining rows: each column in this row provides an identifier
11562 for a debug section, and the offsets in the same column of subsequent rows
11563 refer to that section. The section identifiers are:
11564
11565 DW_SECT_INFO 1 .debug_info.dwo
11566 DW_SECT_TYPES 2 .debug_types.dwo
11567 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11568 DW_SECT_LINE 4 .debug_line.dwo
11569 DW_SECT_LOC 5 .debug_loc.dwo
11570 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11571 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11572 DW_SECT_MACRO 8 .debug_macro.dwo
11573
11574 The offsets provided by the CU and TU index sections are the base offsets
11575 for the contributions made by each CU or TU to the corresponding section
11576 in the package file. Each CU and TU header contains an abbrev_offset
11577 field, used to find the abbreviations table for that CU or TU within the
11578 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11579 be interpreted as relative to the base offset given in the index section.
11580 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11581 should be interpreted as relative to the base offset for .debug_line.dwo,
11582 and offsets into other debug sections obtained from DWARF attributes should
11583 also be interpreted as relative to the corresponding base offset.
11584
11585 The table of sizes begins immediately following the table of offsets.
11586 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11587 with L columns and N rows, in row-major order. Each row in the array is
11588 indexed starting from 1 (row 0 is shared by the two tables).
11589
11590 ---
11591
11592 Hash table lookup is handled the same in version 1 and 2:
11593
11594 We assume that N and M will not exceed 2^32 - 1.
11595 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11596
d2415c6c
DE
11597 Given a 64-bit compilation unit signature or a type signature S, an entry
11598 in the hash table is located as follows:
80626a55 11599
d2415c6c
DE
11600 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11601 the low-order k bits all set to 1.
80626a55 11602
d2415c6c 11603 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11604
d2415c6c
DE
11605 3) If the hash table entry at index H matches the signature, use that
11606 entry. If the hash table entry at index H is unused (all zeroes),
11607 terminate the search: the signature is not present in the table.
80626a55 11608
d2415c6c 11609 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11610
d2415c6c 11611 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11612 to stop at an unused slot or find the match. */
80626a55
DE
11613
11614/* Create a hash table to map DWO IDs to their CU/TU entry in
11615 .debug_{info,types}.dwo in DWP_FILE.
11616 Returns NULL if there isn't one.
11617 Note: This function processes DWP files only, not DWO files. */
11618
11619static struct dwp_hash_table *
ed2dc618
SM
11620create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11621 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11622{
11623 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11624 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11625 const gdb_byte *index_ptr, *index_end;
80626a55 11626 struct dwarf2_section_info *index;
73869dc2 11627 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11628 struct dwp_hash_table *htab;
11629
11630 if (is_debug_types)
11631 index = &dwp_file->sections.tu_index;
11632 else
11633 index = &dwp_file->sections.cu_index;
11634
96b79293 11635 if (index->empty ())
80626a55 11636 return NULL;
96b79293 11637 index->read (objfile);
80626a55
DE
11638
11639 index_ptr = index->buffer;
11640 index_end = index_ptr + index->size;
11641
11642 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11643 index_ptr += 4;
11644 if (version == 2)
11645 nr_columns = read_4_bytes (dbfd, index_ptr);
11646 else
11647 nr_columns = 0;
11648 index_ptr += 4;
80626a55
DE
11649 nr_units = read_4_bytes (dbfd, index_ptr);
11650 index_ptr += 4;
11651 nr_slots = read_4_bytes (dbfd, index_ptr);
11652 index_ptr += 4;
11653
73869dc2 11654 if (version != 1 && version != 2)
80626a55 11655 {
21aa081e 11656 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11657 " [in module %s]"),
21aa081e 11658 pulongest (version), dwp_file->name);
80626a55
DE
11659 }
11660 if (nr_slots != (nr_slots & -nr_slots))
11661 {
21aa081e 11662 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11663 " is not power of 2 [in module %s]"),
21aa081e 11664 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11665 }
11666
5989a64e 11667 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11668 htab->version = version;
11669 htab->nr_columns = nr_columns;
80626a55
DE
11670 htab->nr_units = nr_units;
11671 htab->nr_slots = nr_slots;
11672 htab->hash_table = index_ptr;
11673 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11674
11675 /* Exit early if the table is empty. */
11676 if (nr_slots == 0 || nr_units == 0
11677 || (version == 2 && nr_columns == 0))
11678 {
11679 /* All must be zero. */
11680 if (nr_slots != 0 || nr_units != 0
11681 || (version == 2 && nr_columns != 0))
11682 {
b98664d3 11683 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11684 " all zero [in modules %s]"),
11685 dwp_file->name);
11686 }
11687 return htab;
11688 }
11689
11690 if (version == 1)
11691 {
11692 htab->section_pool.v1.indices =
11693 htab->unit_table + sizeof (uint32_t) * nr_slots;
11694 /* It's harder to decide whether the section is too small in v1.
11695 V1 is deprecated anyway so we punt. */
11696 }
11697 else
11698 {
11699 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11700 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11701 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11702 /* Reverse map for error checking. */
11703 int ids_seen[DW_SECT_MAX + 1];
11704 int i;
11705
11706 if (nr_columns < 2)
11707 {
11708 error (_("Dwarf Error: bad DWP hash table, too few columns"
11709 " in section table [in module %s]"),
11710 dwp_file->name);
11711 }
11712 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11713 {
11714 error (_("Dwarf Error: bad DWP hash table, too many columns"
11715 " in section table [in module %s]"),
11716 dwp_file->name);
11717 }
04fd5eed
GB
11718 memset (ids, 255, sizeof_ids);
11719 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11720 for (i = 0; i < nr_columns; ++i)
11721 {
11722 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11723
11724 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11725 {
11726 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11727 " in section table [in module %s]"),
11728 id, dwp_file->name);
11729 }
11730 if (ids_seen[id] != -1)
11731 {
11732 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11733 " id %d in section table [in module %s]"),
11734 id, dwp_file->name);
11735 }
11736 ids_seen[id] = i;
11737 ids[i] = id;
11738 }
11739 /* Must have exactly one info or types section. */
11740 if (((ids_seen[DW_SECT_INFO] != -1)
11741 + (ids_seen[DW_SECT_TYPES] != -1))
11742 != 1)
11743 {
11744 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11745 " DWO info/types section [in module %s]"),
11746 dwp_file->name);
11747 }
11748 /* Must have an abbrev section. */
11749 if (ids_seen[DW_SECT_ABBREV] == -1)
11750 {
11751 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11752 " section [in module %s]"),
11753 dwp_file->name);
11754 }
11755 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11756 htab->section_pool.v2.sizes =
11757 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11758 * nr_units * nr_columns);
11759 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11760 * nr_units * nr_columns))
11761 > index_end)
11762 {
11763 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11764 " [in module %s]"),
11765 dwp_file->name);
11766 }
11767 }
80626a55
DE
11768
11769 return htab;
11770}
11771
11772/* Update SECTIONS with the data from SECTP.
11773
11774 This function is like the other "locate" section routines that are
11775 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11776 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11777
11778 The result is non-zero for success, or zero if an error was found. */
11779
11780static int
73869dc2
DE
11781locate_v1_virtual_dwo_sections (asection *sectp,
11782 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11783{
11784 const struct dwop_section_names *names = &dwop_section_names;
11785
11786 if (section_is_p (sectp->name, &names->abbrev_dwo))
11787 {
11788 /* There can be only one. */
049412e3 11789 if (sections->abbrev.s.section != NULL)
80626a55 11790 return 0;
049412e3 11791 sections->abbrev.s.section = sectp;
fd361982 11792 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11793 }
11794 else if (section_is_p (sectp->name, &names->info_dwo)
11795 || section_is_p (sectp->name, &names->types_dwo))
11796 {
11797 /* There can be only one. */
049412e3 11798 if (sections->info_or_types.s.section != NULL)
80626a55 11799 return 0;
049412e3 11800 sections->info_or_types.s.section = sectp;
fd361982 11801 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11802 }
11803 else if (section_is_p (sectp->name, &names->line_dwo))
11804 {
11805 /* There can be only one. */
049412e3 11806 if (sections->line.s.section != NULL)
80626a55 11807 return 0;
049412e3 11808 sections->line.s.section = sectp;
fd361982 11809 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11810 }
11811 else if (section_is_p (sectp->name, &names->loc_dwo))
11812 {
11813 /* There can be only one. */
049412e3 11814 if (sections->loc.s.section != NULL)
80626a55 11815 return 0;
049412e3 11816 sections->loc.s.section = sectp;
fd361982 11817 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11818 }
11819 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11820 {
11821 /* There can be only one. */
049412e3 11822 if (sections->macinfo.s.section != NULL)
80626a55 11823 return 0;
049412e3 11824 sections->macinfo.s.section = sectp;
fd361982 11825 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11826 }
11827 else if (section_is_p (sectp->name, &names->macro_dwo))
11828 {
11829 /* There can be only one. */
049412e3 11830 if (sections->macro.s.section != NULL)
80626a55 11831 return 0;
049412e3 11832 sections->macro.s.section = sectp;
fd361982 11833 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11834 }
11835 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11836 {
11837 /* There can be only one. */
049412e3 11838 if (sections->str_offsets.s.section != NULL)
80626a55 11839 return 0;
049412e3 11840 sections->str_offsets.s.section = sectp;
fd361982 11841 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11842 }
11843 else
11844 {
11845 /* No other kind of section is valid. */
11846 return 0;
11847 }
11848
11849 return 1;
11850}
11851
73869dc2
DE
11852/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11853 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11854 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11855 This is for DWP version 1 files. */
80626a55
DE
11856
11857static struct dwo_unit *
ed2dc618
SM
11858create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11859 struct dwp_file *dwp_file,
73869dc2
DE
11860 uint32_t unit_index,
11861 const char *comp_dir,
11862 ULONGEST signature, int is_debug_types)
80626a55 11863{
73869dc2
DE
11864 const struct dwp_hash_table *dwp_htab =
11865 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11866 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11867 const char *kind = is_debug_types ? "TU" : "CU";
11868 struct dwo_file *dwo_file;
11869 struct dwo_unit *dwo_unit;
73869dc2 11870 struct virtual_v1_dwo_sections sections;
80626a55 11871 void **dwo_file_slot;
80626a55
DE
11872 int i;
11873
73869dc2
DE
11874 gdb_assert (dwp_file->version == 1);
11875
b4f54984 11876 if (dwarf_read_debug)
80626a55 11877 {
73869dc2 11878 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11879 kind,
73869dc2 11880 pulongest (unit_index), hex_string (signature),
80626a55
DE
11881 dwp_file->name);
11882 }
11883
19ac8c2e 11884 /* Fetch the sections of this DWO unit.
80626a55
DE
11885 Put a limit on the number of sections we look for so that bad data
11886 doesn't cause us to loop forever. */
11887
73869dc2 11888#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11889 (1 /* .debug_info or .debug_types */ \
11890 + 1 /* .debug_abbrev */ \
11891 + 1 /* .debug_line */ \
11892 + 1 /* .debug_loc */ \
11893 + 1 /* .debug_str_offsets */ \
19ac8c2e 11894 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11895 + 1 /* trailing zero */)
11896
11897 memset (&sections, 0, sizeof (sections));
80626a55 11898
73869dc2 11899 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11900 {
11901 asection *sectp;
11902 uint32_t section_nr =
11903 read_4_bytes (dbfd,
73869dc2
DE
11904 dwp_htab->section_pool.v1.indices
11905 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11906
11907 if (section_nr == 0)
11908 break;
11909 if (section_nr >= dwp_file->num_sections)
11910 {
11911 error (_("Dwarf Error: bad DWP hash table, section number too large"
11912 " [in module %s]"),
11913 dwp_file->name);
11914 }
11915
11916 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11917 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11918 {
11919 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11920 " [in module %s]"),
11921 dwp_file->name);
11922 }
11923 }
11924
11925 if (i < 2
96b79293
TT
11926 || sections.info_or_types.empty ()
11927 || sections.abbrev.empty ())
80626a55
DE
11928 {
11929 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11930 " [in module %s]"),
11931 dwp_file->name);
11932 }
73869dc2 11933 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11934 {
11935 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11936 " [in module %s]"),
11937 dwp_file->name);
11938 }
11939
11940 /* It's easier for the rest of the code if we fake a struct dwo_file and
11941 have dwo_unit "live" in that. At least for now.
11942
11943 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11944 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11945 file, we can combine them back into a virtual DWO file to save space
11946 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11947 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11948
791afaa2
TT
11949 std::string virtual_dwo_name =
11950 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11951 sections.abbrev.get_id (),
11952 sections.line.get_id (),
11953 sections.loc.get_id (),
11954 sections.str_offsets.get_id ());
80626a55 11955 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11956 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11957 virtual_dwo_name.c_str (),
11958 comp_dir);
80626a55
DE
11959 /* Create one if necessary. */
11960 if (*dwo_file_slot == NULL)
11961 {
b4f54984 11962 if (dwarf_read_debug)
80626a55
DE
11963 {
11964 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11965 virtual_dwo_name.c_str ());
80626a55 11966 }
51ac9db5 11967 dwo_file = new struct dwo_file;
45940949 11968 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 11969 dwo_file->comp_dir = comp_dir;
80626a55
DE
11970 dwo_file->sections.abbrev = sections.abbrev;
11971 dwo_file->sections.line = sections.line;
11972 dwo_file->sections.loc = sections.loc;
11973 dwo_file->sections.macinfo = sections.macinfo;
11974 dwo_file->sections.macro = sections.macro;
11975 dwo_file->sections.str_offsets = sections.str_offsets;
11976 /* The "str" section is global to the entire DWP file. */
11977 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11978 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11979 there's no need to record it in dwo_file.
11980 Also, we can't simply record type sections in dwo_file because
11981 we record a pointer into the vector in dwo_unit. As we collect more
11982 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11983 for it, invalidating all copies of pointers into the previous
11984 contents. */
80626a55
DE
11985 *dwo_file_slot = dwo_file;
11986 }
11987 else
11988 {
b4f54984 11989 if (dwarf_read_debug)
80626a55
DE
11990 {
11991 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11992 virtual_dwo_name.c_str ());
80626a55 11993 }
9a3c8263 11994 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11995 }
80626a55 11996
5989a64e 11997 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
11998 dwo_unit->dwo_file = dwo_file;
11999 dwo_unit->signature = signature;
8d749320 12000 dwo_unit->section =
5989a64e 12001 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 12002 *dwo_unit->section = sections.info_or_types;
57d63ce2 12003 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12004
12005 return dwo_unit;
12006}
12007
73869dc2
DE
12008/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12009 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12010 piece within that section used by a TU/CU, return a virtual section
12011 of just that piece. */
12012
12013static struct dwarf2_section_info
ed2dc618
SM
12014create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12015 struct dwarf2_section_info *section,
73869dc2
DE
12016 bfd_size_type offset, bfd_size_type size)
12017{
12018 struct dwarf2_section_info result;
12019 asection *sectp;
12020
12021 gdb_assert (section != NULL);
12022 gdb_assert (!section->is_virtual);
12023
12024 memset (&result, 0, sizeof (result));
12025 result.s.containing_section = section;
dc4ccb6f 12026 result.is_virtual = true;
73869dc2
DE
12027
12028 if (size == 0)
12029 return result;
12030
96b79293 12031 sectp = section->get_bfd_section ();
73869dc2
DE
12032
12033 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12034 bounds of the real section. This is a pretty-rare event, so just
12035 flag an error (easier) instead of a warning and trying to cope. */
12036 if (sectp == NULL
fd361982 12037 || offset + size > bfd_section_size (sectp))
73869dc2 12038 {
73869dc2
DE
12039 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12040 " in section %s [in module %s]"),
fd361982 12041 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
12042 objfile_name (dwarf2_per_objfile->objfile));
12043 }
12044
12045 result.virtual_offset = offset;
12046 result.size = size;
12047 return result;
12048}
12049
12050/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12051 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12052 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12053 This is for DWP version 2 files. */
12054
12055static struct dwo_unit *
ed2dc618
SM
12056create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12057 struct dwp_file *dwp_file,
73869dc2
DE
12058 uint32_t unit_index,
12059 const char *comp_dir,
12060 ULONGEST signature, int is_debug_types)
12061{
73869dc2
DE
12062 const struct dwp_hash_table *dwp_htab =
12063 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12064 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12065 const char *kind = is_debug_types ? "TU" : "CU";
12066 struct dwo_file *dwo_file;
12067 struct dwo_unit *dwo_unit;
12068 struct virtual_v2_dwo_sections sections;
12069 void **dwo_file_slot;
73869dc2
DE
12070 int i;
12071
12072 gdb_assert (dwp_file->version == 2);
12073
b4f54984 12074 if (dwarf_read_debug)
73869dc2
DE
12075 {
12076 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12077 kind,
12078 pulongest (unit_index), hex_string (signature),
12079 dwp_file->name);
12080 }
12081
12082 /* Fetch the section offsets of this DWO unit. */
12083
12084 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12085
12086 for (i = 0; i < dwp_htab->nr_columns; ++i)
12087 {
12088 uint32_t offset = read_4_bytes (dbfd,
12089 dwp_htab->section_pool.v2.offsets
12090 + (((unit_index - 1) * dwp_htab->nr_columns
12091 + i)
12092 * sizeof (uint32_t)));
12093 uint32_t size = read_4_bytes (dbfd,
12094 dwp_htab->section_pool.v2.sizes
12095 + (((unit_index - 1) * dwp_htab->nr_columns
12096 + i)
12097 * sizeof (uint32_t)));
12098
12099 switch (dwp_htab->section_pool.v2.section_ids[i])
12100 {
12101 case DW_SECT_INFO:
12102 case DW_SECT_TYPES:
12103 sections.info_or_types_offset = offset;
12104 sections.info_or_types_size = size;
12105 break;
12106 case DW_SECT_ABBREV:
12107 sections.abbrev_offset = offset;
12108 sections.abbrev_size = size;
12109 break;
12110 case DW_SECT_LINE:
12111 sections.line_offset = offset;
12112 sections.line_size = size;
12113 break;
12114 case DW_SECT_LOC:
12115 sections.loc_offset = offset;
12116 sections.loc_size = size;
12117 break;
12118 case DW_SECT_STR_OFFSETS:
12119 sections.str_offsets_offset = offset;
12120 sections.str_offsets_size = size;
12121 break;
12122 case DW_SECT_MACINFO:
12123 sections.macinfo_offset = offset;
12124 sections.macinfo_size = size;
12125 break;
12126 case DW_SECT_MACRO:
12127 sections.macro_offset = offset;
12128 sections.macro_size = size;
12129 break;
12130 }
12131 }
12132
12133 /* It's easier for the rest of the code if we fake a struct dwo_file and
12134 have dwo_unit "live" in that. At least for now.
12135
12136 The DWP file can be made up of a random collection of CUs and TUs.
12137 However, for each CU + set of TUs that came from the same original DWO
12138 file, we can combine them back into a virtual DWO file to save space
12139 (fewer struct dwo_file objects to allocate). Remember that for really
12140 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12141
791afaa2
TT
12142 std::string virtual_dwo_name =
12143 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12144 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12145 (long) (sections.line_size ? sections.line_offset : 0),
12146 (long) (sections.loc_size ? sections.loc_offset : 0),
12147 (long) (sections.str_offsets_size
12148 ? sections.str_offsets_offset : 0));
73869dc2 12149 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12150 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12151 virtual_dwo_name.c_str (),
12152 comp_dir);
73869dc2
DE
12153 /* Create one if necessary. */
12154 if (*dwo_file_slot == NULL)
12155 {
b4f54984 12156 if (dwarf_read_debug)
73869dc2
DE
12157 {
12158 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12159 virtual_dwo_name.c_str ());
73869dc2 12160 }
51ac9db5 12161 dwo_file = new struct dwo_file;
45940949 12162 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12163 dwo_file->comp_dir = comp_dir;
12164 dwo_file->sections.abbrev =
ed2dc618 12165 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12166 sections.abbrev_offset, sections.abbrev_size);
12167 dwo_file->sections.line =
ed2dc618 12168 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12169 sections.line_offset, sections.line_size);
12170 dwo_file->sections.loc =
ed2dc618 12171 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12172 sections.loc_offset, sections.loc_size);
12173 dwo_file->sections.macinfo =
ed2dc618 12174 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12175 sections.macinfo_offset, sections.macinfo_size);
12176 dwo_file->sections.macro =
ed2dc618 12177 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12178 sections.macro_offset, sections.macro_size);
12179 dwo_file->sections.str_offsets =
ed2dc618
SM
12180 create_dwp_v2_section (dwarf2_per_objfile,
12181 &dwp_file->sections.str_offsets,
73869dc2
DE
12182 sections.str_offsets_offset,
12183 sections.str_offsets_size);
12184 /* The "str" section is global to the entire DWP file. */
12185 dwo_file->sections.str = dwp_file->sections.str;
12186 /* The info or types section is assigned below to dwo_unit,
12187 there's no need to record it in dwo_file.
12188 Also, we can't simply record type sections in dwo_file because
12189 we record a pointer into the vector in dwo_unit. As we collect more
12190 types we'll grow the vector and eventually have to reallocate space
12191 for it, invalidating all copies of pointers into the previous
12192 contents. */
12193 *dwo_file_slot = dwo_file;
12194 }
12195 else
12196 {
b4f54984 12197 if (dwarf_read_debug)
73869dc2
DE
12198 {
12199 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12200 virtual_dwo_name.c_str ());
73869dc2 12201 }
9a3c8263 12202 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12203 }
73869dc2 12204
5989a64e 12205 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12206 dwo_unit->dwo_file = dwo_file;
12207 dwo_unit->signature = signature;
8d749320 12208 dwo_unit->section =
5989a64e 12209 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
ed2dc618
SM
12210 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12211 is_debug_types
73869dc2
DE
12212 ? &dwp_file->sections.types
12213 : &dwp_file->sections.info,
12214 sections.info_or_types_offset,
12215 sections.info_or_types_size);
12216 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12217
12218 return dwo_unit;
12219}
12220
57d63ce2
DE
12221/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12222 Returns NULL if the signature isn't found. */
80626a55
DE
12223
12224static struct dwo_unit *
ed2dc618
SM
12225lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12226 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12227 ULONGEST signature, int is_debug_types)
80626a55 12228{
57d63ce2
DE
12229 const struct dwp_hash_table *dwp_htab =
12230 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12231 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12232 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12233 uint32_t hash = signature & mask;
12234 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12235 unsigned int i;
12236 void **slot;
870f88f7 12237 struct dwo_unit find_dwo_cu;
80626a55
DE
12238
12239 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12240 find_dwo_cu.signature = signature;
19ac8c2e 12241 slot = htab_find_slot (is_debug_types
48b490f2
TT
12242 ? dwp_file->loaded_tus.get ()
12243 : dwp_file->loaded_cus.get (),
19ac8c2e 12244 &find_dwo_cu, INSERT);
80626a55
DE
12245
12246 if (*slot != NULL)
9a3c8263 12247 return (struct dwo_unit *) *slot;
80626a55
DE
12248
12249 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12250 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12251 {
12252 ULONGEST signature_in_table;
12253
12254 signature_in_table =
57d63ce2 12255 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12256 if (signature_in_table == signature)
12257 {
57d63ce2
DE
12258 uint32_t unit_index =
12259 read_4_bytes (dbfd,
12260 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12261
73869dc2
DE
12262 if (dwp_file->version == 1)
12263 {
ed2dc618
SM
12264 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12265 dwp_file, unit_index,
73869dc2
DE
12266 comp_dir, signature,
12267 is_debug_types);
12268 }
12269 else
12270 {
ed2dc618
SM
12271 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12272 dwp_file, unit_index,
73869dc2
DE
12273 comp_dir, signature,
12274 is_debug_types);
12275 }
9a3c8263 12276 return (struct dwo_unit *) *slot;
80626a55
DE
12277 }
12278 if (signature_in_table == 0)
12279 return NULL;
12280 hash = (hash + hash2) & mask;
12281 }
12282
12283 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12284 " [in module %s]"),
12285 dwp_file->name);
12286}
12287
ab5088bf 12288/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12289 Open the file specified by FILE_NAME and hand it off to BFD for
12290 preliminary analysis. Return a newly initialized bfd *, which
12291 includes a canonicalized copy of FILE_NAME.
80626a55 12292 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12293 SEARCH_CWD is true if the current directory is to be searched.
12294 It will be searched before debug-file-directory.
13aaf454
DE
12295 If successful, the file is added to the bfd include table of the
12296 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12297 If unable to find/open the file, return NULL.
3019eac3
DE
12298 NOTE: This function is derived from symfile_bfd_open. */
12299
192b62ce 12300static gdb_bfd_ref_ptr
ed2dc618
SM
12301try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12302 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12303{
24b9144d 12304 int desc;
9c02c129
DE
12305 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12306 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12307 to debug_file_directory. */
e0cc99a6 12308 const char *search_path;
9c02c129
DE
12309 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12310
e0cc99a6 12311 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12312 if (search_cwd)
12313 {
12314 if (*debug_file_directory != '\0')
e0cc99a6
TT
12315 {
12316 search_path_holder.reset (concat (".", dirname_separator_string,
12317 debug_file_directory,
12318 (char *) NULL));
12319 search_path = search_path_holder.get ();
12320 }
6ac97d4c 12321 else
e0cc99a6 12322 search_path = ".";
6ac97d4c 12323 }
9c02c129 12324 else
e0cc99a6 12325 search_path = debug_file_directory;
3019eac3 12326
24b9144d 12327 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12328 if (is_dwp)
12329 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12330
12331 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12332 desc = openp (search_path, flags, file_name,
3019eac3
DE
12333 O_RDONLY | O_BINARY, &absolute_name);
12334 if (desc < 0)
12335 return NULL;
12336
e0cc99a6
TT
12337 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12338 gnutarget, desc));
9c02c129
DE
12339 if (sym_bfd == NULL)
12340 return NULL;
192b62ce 12341 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12342
192b62ce
TT
12343 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12344 return NULL;
3019eac3 12345
13aaf454
DE
12346 /* Success. Record the bfd as having been included by the objfile's bfd.
12347 This is important because things like demangled_names_hash lives in the
12348 objfile's per_bfd space and may have references to things like symbol
12349 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12350 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12351
3019eac3
DE
12352 return sym_bfd;
12353}
12354
ab5088bf 12355/* Try to open DWO file FILE_NAME.
3019eac3
DE
12356 COMP_DIR is the DW_AT_comp_dir attribute.
12357 The result is the bfd handle of the file.
12358 If there is a problem finding or opening the file, return NULL.
12359 Upon success, the canonicalized path of the file is stored in the bfd,
12360 same as symfile_bfd_open. */
12361
192b62ce 12362static gdb_bfd_ref_ptr
ed2dc618
SM
12363open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12364 const char *file_name, const char *comp_dir)
3019eac3 12365{
80626a55 12366 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12367 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12368 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12369
12370 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12371
12372 if (comp_dir != NULL)
12373 {
43816ebc
TT
12374 gdb::unique_xmalloc_ptr<char> path_to_try
12375 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12376
12377 /* NOTE: If comp_dir is a relative path, this will also try the
12378 search path, which seems useful. */
ed2dc618 12379 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12380 path_to_try.get (),
ed2dc618 12381 0 /*is_dwp*/,
192b62ce 12382 1 /*search_cwd*/));
3019eac3
DE
12383 if (abfd != NULL)
12384 return abfd;
12385 }
12386
12387 /* That didn't work, try debug-file-directory, which, despite its name,
12388 is a list of paths. */
12389
12390 if (*debug_file_directory == '\0')
12391 return NULL;
12392
ed2dc618
SM
12393 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12394 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12395}
12396
80626a55
DE
12397/* This function is mapped across the sections and remembers the offset and
12398 size of each of the DWO debugging sections we are interested in. */
12399
12400static void
12401dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12402{
9a3c8263 12403 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12404 const struct dwop_section_names *names = &dwop_section_names;
12405
12406 if (section_is_p (sectp->name, &names->abbrev_dwo))
12407 {
049412e3 12408 dwo_sections->abbrev.s.section = sectp;
fd361982 12409 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12410 }
12411 else if (section_is_p (sectp->name, &names->info_dwo))
12412 {
049412e3 12413 dwo_sections->info.s.section = sectp;
fd361982 12414 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12415 }
12416 else if (section_is_p (sectp->name, &names->line_dwo))
12417 {
049412e3 12418 dwo_sections->line.s.section = sectp;
fd361982 12419 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12420 }
12421 else if (section_is_p (sectp->name, &names->loc_dwo))
12422 {
049412e3 12423 dwo_sections->loc.s.section = sectp;
fd361982 12424 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12425 }
41144253 12426 else if (section_is_p (sectp->name, &names->loclists_dwo))
12427 {
12428 dwo_sections->loclists.s.section = sectp;
12429 dwo_sections->loclists.size = bfd_section_size (sectp);
12430 }
80626a55
DE
12431 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12432 {
049412e3 12433 dwo_sections->macinfo.s.section = sectp;
fd361982 12434 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12435 }
12436 else if (section_is_p (sectp->name, &names->macro_dwo))
12437 {
049412e3 12438 dwo_sections->macro.s.section = sectp;
fd361982 12439 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12440 }
12441 else if (section_is_p (sectp->name, &names->str_dwo))
12442 {
049412e3 12443 dwo_sections->str.s.section = sectp;
fd361982 12444 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12445 }
12446 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12447 {
049412e3 12448 dwo_sections->str_offsets.s.section = sectp;
fd361982 12449 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12450 }
12451 else if (section_is_p (sectp->name, &names->types_dwo))
12452 {
12453 struct dwarf2_section_info type_section;
12454
12455 memset (&type_section, 0, sizeof (type_section));
049412e3 12456 type_section.s.section = sectp;
fd361982 12457 type_section.size = bfd_section_size (sectp);
fd5866f6 12458 dwo_sections->types.push_back (type_section);
80626a55
DE
12459 }
12460}
12461
ab5088bf 12462/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12463 by PER_CU. This is for the non-DWP case.
80626a55 12464 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12465
12466static struct dwo_file *
4ab09049
SM
12467open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12468 const char *comp_dir)
3019eac3 12469{
4ab09049 12470 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 12471
fb1eb2f9 12472 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12473 if (dbfd == NULL)
12474 {
b4f54984 12475 if (dwarf_read_debug)
80626a55
DE
12476 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12477 return NULL;
12478 }
263db9a1 12479
51ac9db5 12480 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12481 dwo_file->dwo_name = dwo_name;
12482 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12483 dwo_file->dbfd = std::move (dbfd);
3019eac3 12484
fb1eb2f9 12485 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12486 &dwo_file->sections);
3019eac3 12487
4ab09049 12488 create_cus_hash_table (dwarf2_per_objfile, cu, *dwo_file,
18a8505e 12489 dwo_file->sections.info, dwo_file->cus);
3019eac3 12490
263db9a1 12491 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12492 dwo_file->sections.types, dwo_file->tus);
3019eac3 12493
b4f54984 12494 if (dwarf_read_debug)
80626a55
DE
12495 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12496
263db9a1 12497 return dwo_file.release ();
3019eac3
DE
12498}
12499
80626a55 12500/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12501 size of each of the DWP debugging sections common to version 1 and 2 that
12502 we are interested in. */
3019eac3 12503
80626a55 12504static void
73869dc2
DE
12505dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12506 void *dwp_file_ptr)
3019eac3 12507{
9a3c8263 12508 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12509 const struct dwop_section_names *names = &dwop_section_names;
12510 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12511
80626a55 12512 /* Record the ELF section number for later lookup: this is what the
73869dc2 12513 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12514 gdb_assert (elf_section_nr < dwp_file->num_sections);
12515 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12516
80626a55
DE
12517 /* Look for specific sections that we need. */
12518 if (section_is_p (sectp->name, &names->str_dwo))
12519 {
049412e3 12520 dwp_file->sections.str.s.section = sectp;
fd361982 12521 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12522 }
12523 else if (section_is_p (sectp->name, &names->cu_index))
12524 {
049412e3 12525 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12526 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12527 }
12528 else if (section_is_p (sectp->name, &names->tu_index))
12529 {
049412e3 12530 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12531 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12532 }
12533}
3019eac3 12534
73869dc2
DE
12535/* This function is mapped across the sections and remembers the offset and
12536 size of each of the DWP version 2 debugging sections that we are interested
12537 in. This is split into a separate function because we don't know if we
12538 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12539
12540static void
12541dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12542{
9a3c8263 12543 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12544 const struct dwop_section_names *names = &dwop_section_names;
12545 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12546
12547 /* Record the ELF section number for later lookup: this is what the
12548 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12549 gdb_assert (elf_section_nr < dwp_file->num_sections);
12550 dwp_file->elf_sections[elf_section_nr] = sectp;
12551
12552 /* Look for specific sections that we need. */
12553 if (section_is_p (sectp->name, &names->abbrev_dwo))
12554 {
049412e3 12555 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12556 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12557 }
12558 else if (section_is_p (sectp->name, &names->info_dwo))
12559 {
049412e3 12560 dwp_file->sections.info.s.section = sectp;
fd361982 12561 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12562 }
12563 else if (section_is_p (sectp->name, &names->line_dwo))
12564 {
049412e3 12565 dwp_file->sections.line.s.section = sectp;
fd361982 12566 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12567 }
12568 else if (section_is_p (sectp->name, &names->loc_dwo))
12569 {
049412e3 12570 dwp_file->sections.loc.s.section = sectp;
fd361982 12571 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12572 }
12573 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12574 {
049412e3 12575 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12576 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12577 }
12578 else if (section_is_p (sectp->name, &names->macro_dwo))
12579 {
049412e3 12580 dwp_file->sections.macro.s.section = sectp;
fd361982 12581 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12582 }
12583 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12584 {
049412e3 12585 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12586 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12587 }
12588 else if (section_is_p (sectp->name, &names->types_dwo))
12589 {
049412e3 12590 dwp_file->sections.types.s.section = sectp;
fd361982 12591 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12592 }
12593}
12594
80626a55 12595/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12596
80626a55
DE
12597static hashval_t
12598hash_dwp_loaded_cutus (const void *item)
12599{
9a3c8263 12600 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12601
80626a55
DE
12602 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12603 return dwo_unit->signature;
3019eac3
DE
12604}
12605
80626a55 12606/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12607
80626a55
DE
12608static int
12609eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12610{
9a3c8263
SM
12611 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12612 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12613
80626a55
DE
12614 return dua->signature == dub->signature;
12615}
3019eac3 12616
80626a55 12617/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12618
48b490f2 12619static htab_up
298e9637 12620allocate_dwp_loaded_cutus_table ()
80626a55 12621{
48b490f2
TT
12622 return htab_up (htab_create_alloc (3,
12623 hash_dwp_loaded_cutus,
12624 eq_dwp_loaded_cutus,
12625 NULL, xcalloc, xfree));
80626a55 12626}
3019eac3 12627
ab5088bf
DE
12628/* Try to open DWP file FILE_NAME.
12629 The result is the bfd handle of the file.
12630 If there is a problem finding or opening the file, return NULL.
12631 Upon success, the canonicalized path of the file is stored in the bfd,
12632 same as symfile_bfd_open. */
12633
192b62ce 12634static gdb_bfd_ref_ptr
ed2dc618
SM
12635open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12636 const char *file_name)
ab5088bf 12637{
ed2dc618
SM
12638 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12639 1 /*is_dwp*/,
192b62ce 12640 1 /*search_cwd*/));
6ac97d4c
DE
12641 if (abfd != NULL)
12642 return abfd;
12643
12644 /* Work around upstream bug 15652.
12645 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12646 [Whether that's a "bug" is debatable, but it is getting in our way.]
12647 We have no real idea where the dwp file is, because gdb's realpath-ing
12648 of the executable's path may have discarded the needed info.
12649 [IWBN if the dwp file name was recorded in the executable, akin to
12650 .gnu_debuglink, but that doesn't exist yet.]
12651 Strip the directory from FILE_NAME and search again. */
12652 if (*debug_file_directory != '\0')
12653 {
12654 /* Don't implicitly search the current directory here.
12655 If the user wants to search "." to handle this case,
12656 it must be added to debug-file-directory. */
ed2dc618
SM
12657 return try_open_dwop_file (dwarf2_per_objfile,
12658 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12659 0 /*search_cwd*/);
12660 }
12661
12662 return NULL;
ab5088bf
DE
12663}
12664
80626a55
DE
12665/* Initialize the use of the DWP file for the current objfile.
12666 By convention the name of the DWP file is ${objfile}.dwp.
12667 The result is NULL if it can't be found. */
a766d390 12668
400174b1 12669static std::unique_ptr<struct dwp_file>
ed2dc618 12670open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12671{
12672 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12673
82bf32bc
JK
12674 /* Try to find first .dwp for the binary file before any symbolic links
12675 resolving. */
6c447423
DE
12676
12677 /* If the objfile is a debug file, find the name of the real binary
12678 file and get the name of dwp file from there. */
d721ba37 12679 std::string dwp_name;
6c447423
DE
12680 if (objfile->separate_debug_objfile_backlink != NULL)
12681 {
12682 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12683 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12684
d721ba37 12685 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12686 }
12687 else
d721ba37
PA
12688 dwp_name = objfile->original_name;
12689
12690 dwp_name += ".dwp";
80626a55 12691
ed2dc618 12692 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12693 if (dbfd == NULL
12694 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12695 {
12696 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12697 dwp_name = objfile_name (objfile);
12698 dwp_name += ".dwp";
ed2dc618 12699 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12700 }
12701
80626a55
DE
12702 if (dbfd == NULL)
12703 {
b4f54984 12704 if (dwarf_read_debug)
d721ba37 12705 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12706 return std::unique_ptr<dwp_file> ();
3019eac3 12707 }
400174b1
TT
12708
12709 const char *name = bfd_get_filename (dbfd.get ());
12710 std::unique_ptr<struct dwp_file> dwp_file
12711 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12712
0a0f4c01 12713 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 12714 dwp_file->elf_sections =
5989a64e 12715 OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
80626a55
DE
12716 dwp_file->num_sections, asection *);
12717
400174b1
TT
12718 bfd_map_over_sections (dwp_file->dbfd.get (),
12719 dwarf2_locate_common_dwp_sections,
12720 dwp_file.get ());
80626a55 12721
400174b1
TT
12722 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12723 0);
80626a55 12724
400174b1
TT
12725 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12726 1);
80626a55 12727
73869dc2 12728 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12729 if (dwp_file->cus && dwp_file->tus
12730 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12731 {
12732 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12733 pretty bizarre. We use pulongest here because that's the established
4d65956b 12734 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12735 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12736 " TU version %s [in DWP file %s]"),
12737 pulongest (dwp_file->cus->version),
d721ba37 12738 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12739 }
08302ed2
DE
12740
12741 if (dwp_file->cus)
12742 dwp_file->version = dwp_file->cus->version;
12743 else if (dwp_file->tus)
12744 dwp_file->version = dwp_file->tus->version;
12745 else
12746 dwp_file->version = 2;
73869dc2
DE
12747
12748 if (dwp_file->version == 2)
400174b1
TT
12749 bfd_map_over_sections (dwp_file->dbfd.get (),
12750 dwarf2_locate_v2_dwp_sections,
12751 dwp_file.get ());
73869dc2 12752
298e9637
SM
12753 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12754 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12755
b4f54984 12756 if (dwarf_read_debug)
80626a55
DE
12757 {
12758 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12759 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12760 " %s CUs, %s TUs\n",
12761 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12762 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12763 }
12764
12765 return dwp_file;
3019eac3 12766}
c906108c 12767
ab5088bf
DE
12768/* Wrapper around open_and_init_dwp_file, only open it once. */
12769
12770static struct dwp_file *
ed2dc618 12771get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf 12772{
5989a64e 12773 if (! dwarf2_per_objfile->per_bfd->dwp_checked)
ab5088bf 12774 {
5989a64e 12775 dwarf2_per_objfile->per_bfd->dwp_file
ed2dc618 12776 = open_and_init_dwp_file (dwarf2_per_objfile);
5989a64e 12777 dwarf2_per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 12778 }
5989a64e 12779 return dwarf2_per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
12780}
12781
80626a55
DE
12782/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12783 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12784 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12785 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12786 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12787
12788 This is called, for example, when wanting to read a variable with a
12789 complex location. Therefore we don't want to do file i/o for every call.
12790 Therefore we don't want to look for a DWO file on every call.
12791 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12792 then we check if we've already seen DWO_NAME, and only THEN do we check
12793 for a DWO file.
12794
1c658ad5 12795 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12796 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12797
3019eac3 12798static struct dwo_unit *
4ab09049 12799lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 12800 ULONGEST signature, int is_debug_types)
3019eac3 12801{
4ab09049 12802 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 12803 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12804 const char *kind = is_debug_types ? "TU" : "CU";
12805 void **dwo_file_slot;
3019eac3 12806 struct dwo_file *dwo_file;
80626a55 12807 struct dwp_file *dwp_file;
cb1df416 12808
6a506a2d
DE
12809 /* First see if there's a DWP file.
12810 If we have a DWP file but didn't find the DWO inside it, don't
12811 look for the original DWO file. It makes gdb behave differently
12812 depending on whether one is debugging in the build tree. */
cf2c3c16 12813
ed2dc618 12814 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12815 if (dwp_file != NULL)
cf2c3c16 12816 {
80626a55
DE
12817 const struct dwp_hash_table *dwp_htab =
12818 is_debug_types ? dwp_file->tus : dwp_file->cus;
12819
12820 if (dwp_htab != NULL)
12821 {
12822 struct dwo_unit *dwo_cutu =
ed2dc618 12823 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12824 signature, is_debug_types);
80626a55
DE
12825
12826 if (dwo_cutu != NULL)
12827 {
b4f54984 12828 if (dwarf_read_debug)
80626a55
DE
12829 {
12830 fprintf_unfiltered (gdb_stdlog,
12831 "Virtual DWO %s %s found: @%s\n",
12832 kind, hex_string (signature),
12833 host_address_to_string (dwo_cutu));
12834 }
12835 return dwo_cutu;
12836 }
12837 }
12838 }
6a506a2d 12839 else
80626a55 12840 {
6a506a2d 12841 /* No DWP file, look for the DWO file. */
80626a55 12842
ed2dc618
SM
12843 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12844 dwo_name, comp_dir);
6a506a2d 12845 if (*dwo_file_slot == NULL)
80626a55 12846 {
6a506a2d 12847 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 12848 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 12849 }
6a506a2d 12850 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12851 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12852
6a506a2d 12853 if (dwo_file != NULL)
19c3d4c9 12854 {
6a506a2d
DE
12855 struct dwo_unit *dwo_cutu = NULL;
12856
12857 if (is_debug_types && dwo_file->tus)
12858 {
12859 struct dwo_unit find_dwo_cutu;
12860
12861 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12862 find_dwo_cutu.signature = signature;
9a3c8263 12863 dwo_cutu
b0b6a987
TT
12864 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12865 &find_dwo_cutu);
6a506a2d 12866 }
33c5cd75 12867 else if (!is_debug_types && dwo_file->cus)
80626a55 12868 {
33c5cd75
DB
12869 struct dwo_unit find_dwo_cutu;
12870
12871 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12872 find_dwo_cutu.signature = signature;
b0b6a987 12873 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12874 &find_dwo_cutu);
6a506a2d
DE
12875 }
12876
12877 if (dwo_cutu != NULL)
12878 {
b4f54984 12879 if (dwarf_read_debug)
6a506a2d
DE
12880 {
12881 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12882 kind, dwo_name, hex_string (signature),
12883 host_address_to_string (dwo_cutu));
12884 }
12885 return dwo_cutu;
80626a55
DE
12886 }
12887 }
2e276125 12888 }
9cdd5dbd 12889
80626a55
DE
12890 /* We didn't find it. This could mean a dwo_id mismatch, or
12891 someone deleted the DWO/DWP file, or the search path isn't set up
12892 correctly to find the file. */
12893
b4f54984 12894 if (dwarf_read_debug)
80626a55
DE
12895 {
12896 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12897 kind, dwo_name, hex_string (signature));
12898 }
3019eac3 12899
6656a72d
DE
12900 /* This is a warning and not a complaint because it can be caused by
12901 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12902 {
12903 /* Print the name of the DWP file if we looked there, helps the user
12904 better diagnose the problem. */
791afaa2 12905 std::string dwp_text;
43942612
DE
12906
12907 if (dwp_file != NULL)
791afaa2
TT
12908 dwp_text = string_printf (" [in DWP file %s]",
12909 lbasename (dwp_file->name));
43942612 12910
9d8780f0 12911 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 12912 " [in module %s]"),
4ab09049
SM
12913 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12914 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 12915 }
3019eac3 12916 return NULL;
5fb290d7
DJ
12917}
12918
80626a55
DE
12919/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12920 See lookup_dwo_cutu_unit for details. */
12921
12922static struct dwo_unit *
4ab09049 12923lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
12924 ULONGEST signature)
12925{
4ab09049
SM
12926 gdb_assert (!cu->per_cu->is_debug_types);
12927
12928 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
12929}
12930
12931/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12932 See lookup_dwo_cutu_unit for details. */
12933
12934static struct dwo_unit *
4ab09049 12935lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 12936{
4ab09049
SM
12937 gdb_assert (cu->per_cu->is_debug_types);
12938
12939 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12940
12941 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
12942}
12943
89e63ee4
DE
12944/* Traversal function for queue_and_load_all_dwo_tus. */
12945
12946static int
12947queue_and_load_dwo_tu (void **slot, void *info)
12948{
12949 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 12950 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 12951 ULONGEST signature = dwo_unit->signature;
d460f660 12952 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
12953
12954 if (sig_type != NULL)
12955 {
12956 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12957
12958 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12959 a real dependency of PER_CU on SIG_TYPE. That is detected later
12960 while processing PER_CU. */
120ce1b5 12961 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
d460f660
SM
12962 load_full_type_unit (sig_cu, cu->per_objfile);
12963 cu->per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12964 }
12965
12966 return 1;
12967}
12968
1b555f17 12969/* Queue all TUs contained in the DWO of CU to be read in.
89e63ee4
DE
12970 The DWO may have the only definition of the type, though it may not be
12971 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12972 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12973
12974static void
1b555f17 12975queue_and_load_all_dwo_tus (dwarf2_cu *cu)
89e63ee4
DE
12976{
12977 struct dwo_unit *dwo_unit;
12978 struct dwo_file *dwo_file;
12979
1b555f17
SM
12980 gdb_assert (cu != nullptr);
12981 gdb_assert (!cu->per_cu->is_debug_types);
12982 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
89e63ee4 12983
1b555f17 12984 dwo_unit = cu->dwo_unit;
89e63ee4
DE
12985 gdb_assert (dwo_unit != NULL);
12986
12987 dwo_file = dwo_unit->dwo_file;
12988 if (dwo_file->tus != NULL)
1b555f17 12989 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
89e63ee4
DE
12990}
12991
3019eac3 12992/* Read in various DIEs. */
348e048f 12993
d389af10 12994/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12995 Inherit only the children of the DW_AT_abstract_origin DIE not being
12996 already referenced by DW_AT_abstract_origin from the children of the
12997 current DIE. */
d389af10
JK
12998
12999static void
13000inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13001{
13002 struct die_info *child_die;
791afaa2 13003 sect_offset *offsetp;
d389af10
JK
13004 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13005 struct die_info *origin_die;
13006 /* Iterator of the ORIGIN_DIE children. */
13007 struct die_info *origin_child_die;
d389af10 13008 struct attribute *attr;
cd02d79d
PA
13009 struct dwarf2_cu *origin_cu;
13010 struct pending **origin_previous_list_in_scope;
d389af10
JK
13011
13012 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13013 if (!attr)
13014 return;
13015
cd02d79d
PA
13016 /* Note that following die references may follow to a die in a
13017 different cu. */
13018
13019 origin_cu = cu;
13020 origin_die = follow_die_ref (die, attr, &origin_cu);
13021
13022 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13023 symbols in. */
13024 origin_previous_list_in_scope = origin_cu->list_in_scope;
13025 origin_cu->list_in_scope = cu->list_in_scope;
13026
edb3359d
DJ
13027 if (die->tag != origin_die->tag
13028 && !(die->tag == DW_TAG_inlined_subroutine
13029 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13030 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13031 sect_offset_str (die->sect_off),
13032 sect_offset_str (origin_die->sect_off));
d389af10 13033
791afaa2 13034 std::vector<sect_offset> offsets;
d389af10 13035
3ea89b92
PMR
13036 for (child_die = die->child;
13037 child_die && child_die->tag;
436c571c 13038 child_die = child_die->sibling)
3ea89b92
PMR
13039 {
13040 struct die_info *child_origin_die;
13041 struct dwarf2_cu *child_origin_cu;
13042
13043 /* We are trying to process concrete instance entries:
216f72a1 13044 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13045 it's not relevant to our analysis here. i.e. detecting DIEs that are
13046 present in the abstract instance but not referenced in the concrete
13047 one. */
216f72a1
JK
13048 if (child_die->tag == DW_TAG_call_site
13049 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13050 continue;
13051
c38f313d
DJ
13052 /* For each CHILD_DIE, find the corresponding child of
13053 ORIGIN_DIE. If there is more than one layer of
13054 DW_AT_abstract_origin, follow them all; there shouldn't be,
13055 but GCC versions at least through 4.4 generate this (GCC PR
13056 40573). */
3ea89b92
PMR
13057 child_origin_die = child_die;
13058 child_origin_cu = cu;
c38f313d
DJ
13059 while (1)
13060 {
cd02d79d
PA
13061 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13062 child_origin_cu);
c38f313d
DJ
13063 if (attr == NULL)
13064 break;
cd02d79d
PA
13065 child_origin_die = follow_die_ref (child_origin_die, attr,
13066 &child_origin_cu);
c38f313d
DJ
13067 }
13068
d389af10
JK
13069 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13070 counterpart may exist. */
c38f313d 13071 if (child_origin_die != child_die)
d389af10 13072 {
edb3359d
DJ
13073 if (child_die->tag != child_origin_die->tag
13074 && !(child_die->tag == DW_TAG_inlined_subroutine
13075 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13076 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13077 "different tags"),
9d8780f0
SM
13078 sect_offset_str (child_die->sect_off),
13079 sect_offset_str (child_origin_die->sect_off));
c38f313d 13080 if (child_origin_die->parent != origin_die)
b98664d3 13081 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13082 "different parents"),
9d8780f0
SM
13083 sect_offset_str (child_die->sect_off),
13084 sect_offset_str (child_origin_die->sect_off));
c38f313d 13085 else
791afaa2 13086 offsets.push_back (child_origin_die->sect_off);
d389af10 13087 }
d389af10 13088 }
791afaa2
TT
13089 std::sort (offsets.begin (), offsets.end ());
13090 sect_offset *offsets_end = offsets.data () + offsets.size ();
13091 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13092 if (offsetp[-1] == *offsetp)
b98664d3 13093 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13094 "to DIE %s as their abstract origin"),
13095 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13096
791afaa2 13097 offsetp = offsets.data ();
d389af10
JK
13098 origin_child_die = origin_die->child;
13099 while (origin_child_die && origin_child_die->tag)
13100 {
13101 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13102 while (offsetp < offsets_end
9c541725 13103 && *offsetp < origin_child_die->sect_off)
d389af10 13104 offsetp++;
b64f50a1 13105 if (offsetp >= offsets_end
9c541725 13106 || *offsetp > origin_child_die->sect_off)
d389af10 13107 {
adde2bff
DE
13108 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13109 Check whether we're already processing ORIGIN_CHILD_DIE.
13110 This can happen with mutually referenced abstract_origins.
13111 PR 16581. */
13112 if (!origin_child_die->in_process)
13113 process_die (origin_child_die, origin_cu);
d389af10 13114 }
436c571c 13115 origin_child_die = origin_child_die->sibling;
d389af10 13116 }
cd02d79d 13117 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13118
13119 if (cu != origin_cu)
13120 compute_delayed_physnames (origin_cu);
d389af10
JK
13121}
13122
c906108c 13123static void
e7c27a73 13124read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13125{
5e22e966 13126 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13127 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13128 struct context_stack *newobj;
c906108c
SS
13129 CORE_ADDR lowpc;
13130 CORE_ADDR highpc;
13131 struct die_info *child_die;
edb3359d 13132 struct attribute *attr, *call_line, *call_file;
15d034d0 13133 const char *name;
e142c38c 13134 CORE_ADDR baseaddr;
801e3a5b 13135 struct block *block;
edb3359d 13136 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13137 std::vector<struct symbol *> template_args;
34eaf542 13138 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13139
13140 if (inlined_func)
13141 {
13142 /* If we do not have call site information, we can't show the
13143 caller of this inlined function. That's too confusing, so
13144 only use the scope for local variables. */
13145 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13146 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13147 if (call_line == NULL || call_file == NULL)
13148 {
13149 read_lexical_block_scope (die, cu);
13150 return;
13151 }
13152 }
c906108c 13153
b3b3bada 13154 baseaddr = objfile->text_section_offset ();
e142c38c 13155
94af9270 13156 name = dwarf2_name (die, cu);
c906108c 13157
e8d05480
JB
13158 /* Ignore functions with missing or empty names. These are actually
13159 illegal according to the DWARF standard. */
13160 if (name == NULL)
13161 {
b98664d3 13162 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13163 sect_offset_str (die->sect_off));
e8d05480
JB
13164 return;
13165 }
13166
13167 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13168 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13169 <= PC_BOUNDS_INVALID)
e8d05480 13170 {
ae4d0c03
PM
13171 attr = dwarf2_attr (die, DW_AT_external, cu);
13172 if (!attr || !DW_UNSND (attr))
b98664d3 13173 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13174 "for subprogram DIE at %s"),
13175 sect_offset_str (die->sect_off));
e8d05480
JB
13176 return;
13177 }
c906108c 13178
3e29f34a
MR
13179 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13180 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13181
34eaf542
TT
13182 /* If we have any template arguments, then we must allocate a
13183 different sort of symbol. */
436c571c 13184 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13185 {
13186 if (child_die->tag == DW_TAG_template_type_param
13187 || child_die->tag == DW_TAG_template_value_param)
13188 {
8c14c3a3 13189 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13190 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13191 break;
13192 }
13193 }
13194
c24bdb02 13195 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13196 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13197 (struct symbol *) templ_func);
4c2df51b 13198
81873cc8 13199 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13200 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13201 cu->language);
13202
4cecd739
DJ
13203 /* If there is a location expression for DW_AT_frame_base, record
13204 it. */
e142c38c 13205 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13206 if (attr != nullptr)
fe978cb0 13207 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13208
63e43d3a
PMR
13209 /* If there is a location for the static link, record it. */
13210 newobj->static_link = NULL;
13211 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13212 if (attr != nullptr)
63e43d3a 13213 {
224c3ddb
SM
13214 newobj->static_link
13215 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13216 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13217 cu->addr_type ());
63e43d3a
PMR
13218 }
13219
c24bdb02 13220 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13221
639d11d3 13222 if (die->child != NULL)
c906108c 13223 {
639d11d3 13224 child_die = die->child;
c906108c
SS
13225 while (child_die && child_die->tag)
13226 {
34eaf542
TT
13227 if (child_die->tag == DW_TAG_template_type_param
13228 || child_die->tag == DW_TAG_template_value_param)
13229 {
13230 struct symbol *arg = new_symbol (child_die, NULL, cu);
13231
f1078f66 13232 if (arg != NULL)
2f4732b0 13233 template_args.push_back (arg);
34eaf542
TT
13234 }
13235 else
13236 process_die (child_die, cu);
436c571c 13237 child_die = child_die->sibling;
c906108c
SS
13238 }
13239 }
13240
d389af10
JK
13241 inherit_abstract_dies (die, cu);
13242
4a811a97
UW
13243 /* If we have a DW_AT_specification, we might need to import using
13244 directives from the context of the specification DIE. See the
13245 comment in determine_prefix. */
13246 if (cu->language == language_cplus
13247 && dwarf2_attr (die, DW_AT_specification, cu))
13248 {
13249 struct dwarf2_cu *spec_cu = cu;
13250 struct die_info *spec_die = die_specification (die, &spec_cu);
13251
13252 while (spec_die)
13253 {
13254 child_die = spec_die->child;
13255 while (child_die && child_die->tag)
13256 {
13257 if (child_die->tag == DW_TAG_imported_module)
13258 process_die (child_die, spec_cu);
436c571c 13259 child_die = child_die->sibling;
4a811a97
UW
13260 }
13261
13262 /* In some cases, GCC generates specification DIEs that
13263 themselves contain DW_AT_specification attributes. */
13264 spec_die = die_specification (spec_die, &spec_cu);
13265 }
13266 }
13267
c24bdb02 13268 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13269 /* Make a block for the local symbols within. */
c24bdb02 13270 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13271 cstk.static_link, lowpc, highpc);
801e3a5b 13272
df8a16a1 13273 /* For C++, set the block's scope. */
45280282
IB
13274 if ((cu->language == language_cplus
13275 || cu->language == language_fortran
c44af4eb
TT
13276 || cu->language == language_d
13277 || cu->language == language_rust)
4d4ec4e5 13278 && cu->processing_has_namespace_info)
195a3f6c
TT
13279 block_set_scope (block, determine_prefix (die, cu),
13280 &objfile->objfile_obstack);
df8a16a1 13281
801e3a5b
JB
13282 /* If we have address ranges, record them. */
13283 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13284
a60f3166 13285 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13286
34eaf542 13287 /* Attach template arguments to function. */
2f4732b0 13288 if (!template_args.empty ())
34eaf542
TT
13289 {
13290 gdb_assert (templ_func != NULL);
13291
2f4732b0 13292 templ_func->n_template_arguments = template_args.size ();
34eaf542 13293 templ_func->template_arguments
8d749320
SM
13294 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13295 templ_func->n_template_arguments);
34eaf542 13296 memcpy (templ_func->template_arguments,
2f4732b0 13297 template_args.data (),
34eaf542 13298 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13299
13300 /* Make sure that the symtab is set on the new symbols. Even
13301 though they don't appear in this symtab directly, other parts
13302 of gdb assume that symbols do, and this is reasonably
13303 true. */
8634679f 13304 for (symbol *sym : template_args)
3e1d3d8c 13305 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13306 }
13307
208d8187
JB
13308 /* In C++, we can have functions nested inside functions (e.g., when
13309 a function declares a class that has methods). This means that
13310 when we finish processing a function scope, we may need to go
13311 back to building a containing block's symbol lists. */
c24bdb02
KS
13312 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13313 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13314
921e78cf
JB
13315 /* If we've finished processing a top-level function, subsequent
13316 symbols go in the file symbol list. */
c24bdb02
KS
13317 if (cu->get_builder ()->outermost_context_p ())
13318 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13319}
13320
13321/* Process all the DIES contained within a lexical block scope. Start
13322 a new scope, process the dies, and then close the scope. */
13323
13324static void
e7c27a73 13325read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13326{
5e22e966 13327 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13328 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13329 CORE_ADDR lowpc, highpc;
13330 struct die_info *child_die;
e142c38c
DJ
13331 CORE_ADDR baseaddr;
13332
b3b3bada 13333 baseaddr = objfile->text_section_offset ();
c906108c
SS
13334
13335 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13336 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13337 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13338 be nasty. Might be easier to properly extend generic blocks to
af34e669 13339 describe ranges. */
e385593e
JK
13340 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13341 {
13342 case PC_BOUNDS_NOT_PRESENT:
13343 /* DW_TAG_lexical_block has no attributes, process its children as if
13344 there was no wrapping by that DW_TAG_lexical_block.
13345 GCC does no longer produces such DWARF since GCC r224161. */
13346 for (child_die = die->child;
13347 child_die != NULL && child_die->tag;
436c571c 13348 child_die = child_die->sibling)
4f7bc5ed
TT
13349 {
13350 /* We might already be processing this DIE. This can happen
13351 in an unusual circumstance -- where a subroutine A
13352 appears lexically in another subroutine B, but A actually
13353 inlines B. The recursion is broken here, rather than in
13354 inherit_abstract_dies, because it seems better to simply
13355 drop concrete children here. */
13356 if (!child_die->in_process)
13357 process_die (child_die, cu);
13358 }
e385593e
JK
13359 return;
13360 case PC_BOUNDS_INVALID:
13361 return;
13362 }
3e29f34a
MR
13363 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13364 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13365
c24bdb02 13366 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13367 if (die->child != NULL)
c906108c 13368 {
639d11d3 13369 child_die = die->child;
c906108c
SS
13370 while (child_die && child_die->tag)
13371 {
e7c27a73 13372 process_die (child_die, cu);
436c571c 13373 child_die = child_die->sibling;
c906108c
SS
13374 }
13375 }
3ea89b92 13376 inherit_abstract_dies (die, cu);
c24bdb02 13377 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13378
c24bdb02
KS
13379 if (*cu->get_builder ()->get_local_symbols () != NULL
13380 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13381 {
801e3a5b 13382 struct block *block
c24bdb02 13383 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13384 cstk.start_addr, highpc);
801e3a5b
JB
13385
13386 /* Note that recording ranges after traversing children, as we
13387 do here, means that recording a parent's ranges entails
13388 walking across all its children's ranges as they appear in
13389 the address map, which is quadratic behavior.
13390
13391 It would be nicer to record the parent's ranges before
13392 traversing its children, simply overriding whatever you find
13393 there. But since we don't even decide whether to create a
13394 block until after we've traversed its children, that's hard
13395 to do. */
13396 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13397 }
c24bdb02
KS
13398 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13399 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13400}
13401
216f72a1 13402/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13403
13404static void
13405read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13406{
5e22e966 13407 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13408 struct objfile *objfile = per_objfile->objfile;
08feed99 13409 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13410 CORE_ADDR pc, baseaddr;
13411 struct attribute *attr;
13412 struct call_site *call_site, call_site_local;
13413 void **slot;
13414 int nparams;
13415 struct die_info *child_die;
13416
b3b3bada 13417 baseaddr = objfile->text_section_offset ();
96408a79 13418
216f72a1
JK
13419 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13420 if (attr == NULL)
13421 {
13422 /* This was a pre-DWARF-5 GNU extension alias
13423 for DW_AT_call_return_pc. */
13424 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13425 }
96408a79
SA
13426 if (!attr)
13427 {
b98664d3 13428 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13429 "DIE %s [in module %s]"),
13430 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13431 return;
13432 }
cd6c91b4 13433 pc = attr->value_as_address () + baseaddr;
3e29f34a 13434 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13435
13436 if (cu->call_site_htab == NULL)
13437 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13438 NULL, &objfile->objfile_obstack,
13439 hashtab_obstack_allocate, NULL);
13440 call_site_local.pc = pc;
13441 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13442 if (*slot != NULL)
13443 {
b98664d3 13444 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13445 "DIE %s [in module %s]"),
13446 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13447 objfile_name (objfile));
96408a79
SA
13448 return;
13449 }
13450
13451 /* Count parameters at the caller. */
13452
13453 nparams = 0;
13454 for (child_die = die->child; child_die && child_die->tag;
436c571c 13455 child_die = child_die->sibling)
96408a79 13456 {
216f72a1
JK
13457 if (child_die->tag != DW_TAG_call_site_parameter
13458 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13459 {
b98664d3 13460 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13461 "DW_TAG_call_site child DIE %s [in module %s]"),
13462 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13463 objfile_name (objfile));
96408a79
SA
13464 continue;
13465 }
13466
13467 nparams++;
13468 }
13469
224c3ddb
SM
13470 call_site
13471 = ((struct call_site *)
13472 obstack_alloc (&objfile->objfile_obstack,
13473 sizeof (*call_site)
13474 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13475 *slot = call_site;
13476 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13477 call_site->pc = pc;
13478
216f72a1
JK
13479 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13480 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13481 {
13482 struct die_info *func_die;
13483
13484 /* Skip also over DW_TAG_inlined_subroutine. */
13485 for (func_die = die->parent;
13486 func_die && func_die->tag != DW_TAG_subprogram
13487 && func_die->tag != DW_TAG_subroutine_type;
13488 func_die = func_die->parent);
13489
216f72a1
JK
13490 /* DW_AT_call_all_calls is a superset
13491 of DW_AT_call_all_tail_calls. */
96408a79 13492 if (func_die
216f72a1 13493 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13494 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13495 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13496 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13497 {
13498 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13499 not complete. But keep CALL_SITE for look ups via call_site_htab,
13500 both the initial caller containing the real return address PC and
13501 the final callee containing the current PC of a chain of tail
13502 calls do not need to have the tail call list complete. But any
13503 function candidate for a virtual tail call frame searched via
13504 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13505 determined unambiguously. */
13506 }
13507 else
13508 {
13509 struct type *func_type = NULL;
13510
13511 if (func_die)
13512 func_type = get_die_type (func_die, cu);
13513 if (func_type != NULL)
13514 {
78134374 13515 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13516
13517 /* Enlist this call site to the function. */
13518 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13519 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13520 }
13521 else
b98664d3 13522 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13523 "DIE %s [in module %s]"),
13524 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13525 }
13526 }
13527
216f72a1
JK
13528 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13529 if (attr == NULL)
13530 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13531 if (attr == NULL)
13532 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13533 if (attr == NULL)
216f72a1
JK
13534 {
13535 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13536 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13537 }
96408a79 13538 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13539 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13540 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13541 else if (attr->form_is_block ())
96408a79
SA
13542 {
13543 struct dwarf2_locexpr_baton *dlbaton;
13544
8d749320 13545 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13546 dlbaton->data = DW_BLOCK (attr)->data;
13547 dlbaton->size = DW_BLOCK (attr)->size;
a50264ba 13548 dlbaton->per_objfile = per_objfile;
96408a79
SA
13549 dlbaton->per_cu = cu->per_cu;
13550
13551 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13552 }
cd6c91b4 13553 else if (attr->form_is_ref ())
96408a79 13554 {
96408a79
SA
13555 struct dwarf2_cu *target_cu = cu;
13556 struct die_info *target_die;
13557
ac9ec31b 13558 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13559 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13560 if (die_is_declaration (target_die, target_cu))
13561 {
7d45c7c3 13562 const char *target_physname;
9112db09
JK
13563
13564 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13565 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13566 if (target_physname == NULL)
9112db09 13567 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13568 if (target_physname == NULL)
b98664d3 13569 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13570 "physname, for referencing DIE %s [in module %s]"),
13571 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13572 else
7d455152 13573 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13574 }
13575 else
13576 {
13577 CORE_ADDR lowpc;
13578
13579 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13580 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13581 <= PC_BOUNDS_INVALID)
b98664d3 13582 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13583 "low pc, for referencing DIE %s [in module %s]"),
13584 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13585 else
3e29f34a
MR
13586 {
13587 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13588 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13589 }
96408a79
SA
13590 }
13591 }
13592 else
b98664d3 13593 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13594 "block nor reference, for DIE %s [in module %s]"),
13595 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13596
13597 call_site->per_cu = cu->per_cu;
9f47c707 13598 call_site->per_objfile = per_objfile;
96408a79
SA
13599
13600 for (child_die = die->child;
13601 child_die && child_die->tag;
436c571c 13602 child_die = child_die->sibling)
96408a79 13603 {
96408a79 13604 struct call_site_parameter *parameter;
1788b2d3 13605 struct attribute *loc, *origin;
96408a79 13606
216f72a1
JK
13607 if (child_die->tag != DW_TAG_call_site_parameter
13608 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13609 {
13610 /* Already printed the complaint above. */
13611 continue;
13612 }
13613
13614 gdb_assert (call_site->parameter_count < nparams);
13615 parameter = &call_site->parameter[call_site->parameter_count];
13616
1788b2d3
JK
13617 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13618 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13619 register is contained in DW_AT_call_value. */
96408a79 13620
24c5c679 13621 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13622 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13623 if (origin == NULL)
13624 {
13625 /* This was a pre-DWARF-5 GNU extension alias
13626 for DW_AT_call_parameter. */
13627 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13628 }
cd6c91b4 13629 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13630 {
1788b2d3 13631 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13632
0826b30a 13633 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13634 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13635 {
13636 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13637 binding can be done only inside one CU. Such referenced DIE
13638 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13639 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13640 "DW_TAG_call_site child DIE %s [in module %s]"),
13641 sect_offset_str (child_die->sect_off),
9c541725 13642 objfile_name (objfile));
d76b7dbc
JK
13643 continue;
13644 }
9c541725
PA
13645 parameter->u.param_cu_off
13646 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13647 }
4fc6c0d5 13648 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13649 {
b98664d3 13650 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13651 "DW_TAG_call_site child DIE %s [in module %s]"),
13652 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13653 continue;
13654 }
24c5c679 13655 else
96408a79 13656 {
24c5c679
JK
13657 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13658 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13659 if (parameter->u.dwarf_reg != -1)
13660 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13661 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13662 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13663 &parameter->u.fb_offset))
13664 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13665 else
13666 {
b98664d3 13667 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13668 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13669 "DW_TAG_call_site child DIE %s "
24c5c679 13670 "[in module %s]"),
9d8780f0 13671 sect_offset_str (child_die->sect_off),
9c541725 13672 objfile_name (objfile));
24c5c679
JK
13673 continue;
13674 }
96408a79
SA
13675 }
13676
216f72a1
JK
13677 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13678 if (attr == NULL)
13679 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13680 if (attr == NULL || !attr->form_is_block ())
96408a79 13681 {
b98664d3 13682 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13683 "DW_TAG_call_site child DIE %s [in module %s]"),
13684 sect_offset_str (child_die->sect_off),
9c541725 13685 objfile_name (objfile));
96408a79
SA
13686 continue;
13687 }
13688 parameter->value = DW_BLOCK (attr)->data;
13689 parameter->value_size = DW_BLOCK (attr)->size;
13690
13691 /* Parameters are not pre-cleared by memset above. */
13692 parameter->data_value = NULL;
13693 parameter->data_value_size = 0;
13694 call_site->parameter_count++;
13695
216f72a1
JK
13696 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13697 if (attr == NULL)
13698 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13699 if (attr != nullptr)
96408a79 13700 {
4fc6c0d5 13701 if (!attr->form_is_block ())
b98664d3 13702 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13703 "DW_TAG_call_site child DIE %s [in module %s]"),
13704 sect_offset_str (child_die->sect_off),
9c541725 13705 objfile_name (objfile));
96408a79
SA
13706 else
13707 {
13708 parameter->data_value = DW_BLOCK (attr)->data;
13709 parameter->data_value_size = DW_BLOCK (attr)->size;
13710 }
13711 }
13712 }
13713}
13714
71a3c369
TT
13715/* Helper function for read_variable. If DIE represents a virtual
13716 table, then return the type of the concrete object that is
13717 associated with the virtual table. Otherwise, return NULL. */
13718
13719static struct type *
13720rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13721{
13722 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13723 if (attr == NULL)
13724 return NULL;
13725
13726 /* Find the type DIE. */
13727 struct die_info *type_die = NULL;
13728 struct dwarf2_cu *type_cu = cu;
13729
cd6c91b4 13730 if (attr->form_is_ref ())
71a3c369
TT
13731 type_die = follow_die_ref (die, attr, &type_cu);
13732 if (type_die == NULL)
13733 return NULL;
13734
13735 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13736 return NULL;
13737 return die_containing_type (type_die, type_cu);
13738}
13739
13740/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13741
13742static void
13743read_variable (struct die_info *die, struct dwarf2_cu *cu)
13744{
13745 struct rust_vtable_symbol *storage = NULL;
13746
13747 if (cu->language == language_rust)
13748 {
13749 struct type *containing_type = rust_containing_type (die, cu);
13750
13751 if (containing_type != NULL)
13752 {
5e22e966 13753 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 13754
8c14c3a3 13755 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 13756 storage->concrete_type = containing_type;
cf724bc9 13757 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13758 }
13759 }
13760
e4a62c65
TV
13761 struct symbol *res = new_symbol (die, NULL, cu, storage);
13762 struct attribute *abstract_origin
13763 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13764 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13765 if (res == NULL && loc && abstract_origin)
13766 {
13767 /* We have a variable without a name, but with a location and an abstract
13768 origin. This may be a concrete instance of an abstract variable
13769 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13770 later. */
13771 struct dwarf2_cu *origin_cu = cu;
13772 struct die_info *origin_die
13773 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
13774 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13775 per_objfile->per_bfd->abstract_to_concrete
13776 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13777 }
71a3c369
TT
13778}
13779
43988095
JK
13780/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13781 reading .debug_rnglists.
13782 Callback's type should be:
13783 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13784 Return true if the attributes are present and valid, otherwise,
13785 return false. */
13786
13787template <typename Callback>
13788static bool
13789dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13790 Callback &&callback)
13791{
5e22e966 13792 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 13793 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13794 bfd *obfd = objfile->obfd;
43988095 13795 /* Base address selection entry. */
2b24b6e4 13796 gdb::optional<CORE_ADDR> base;
43988095 13797 const gdb_byte *buffer;
43988095
JK
13798 CORE_ADDR baseaddr;
13799 bool overflow = false;
13800
43988095
JK
13801 base = cu->base_address;
13802
5989a64e
SM
13803 dwarf2_per_objfile->per_bfd->rnglists.read (objfile);
13804 if (offset >= dwarf2_per_objfile->per_bfd->rnglists.size)
43988095 13805 {
b98664d3 13806 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13807 offset);
13808 return false;
13809 }
5989a64e 13810 buffer = dwarf2_per_objfile->per_bfd->rnglists.buffer + offset;
43988095 13811
b3b3bada 13812 baseaddr = objfile->text_section_offset ();
43988095
JK
13813
13814 while (1)
13815 {
7814882a
JK
13816 /* Initialize it due to a false compiler warning. */
13817 CORE_ADDR range_beginning = 0, range_end = 0;
5989a64e
SM
13818 const gdb_byte *buf_end = (dwarf2_per_objfile->per_bfd->rnglists.buffer
13819 + dwarf2_per_objfile->per_bfd->rnglists.size);
43988095
JK
13820 unsigned int bytes_read;
13821
13822 if (buffer == buf_end)
13823 {
13824 overflow = true;
13825 break;
13826 }
13827 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13828 switch (rlet)
13829 {
13830 case DW_RLE_end_of_list:
13831 break;
13832 case DW_RLE_base_address:
13833 if (buffer + cu->header.addr_size > buf_end)
13834 {
13835 overflow = true;
13836 break;
13837 }
c8a7a66f 13838 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13839 buffer += bytes_read;
13840 break;
13841 case DW_RLE_start_length:
13842 if (buffer + cu->header.addr_size > buf_end)
13843 {
13844 overflow = true;
13845 break;
13846 }
c8a7a66f
TT
13847 range_beginning = cu->header.read_address (obfd, buffer,
13848 &bytes_read);
43988095
JK
13849 buffer += bytes_read;
13850 range_end = (range_beginning
13851 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13852 buffer += bytes_read;
13853 if (buffer > buf_end)
13854 {
13855 overflow = true;
13856 break;
13857 }
13858 break;
13859 case DW_RLE_offset_pair:
13860 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13861 buffer += bytes_read;
13862 if (buffer > buf_end)
13863 {
13864 overflow = true;
13865 break;
13866 }
13867 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13868 buffer += bytes_read;
13869 if (buffer > buf_end)
13870 {
13871 overflow = true;
13872 break;
13873 }
13874 break;
13875 case DW_RLE_start_end:
13876 if (buffer + 2 * cu->header.addr_size > buf_end)
13877 {
13878 overflow = true;
13879 break;
13880 }
c8a7a66f
TT
13881 range_beginning = cu->header.read_address (obfd, buffer,
13882 &bytes_read);
43988095 13883 buffer += bytes_read;
c8a7a66f 13884 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13885 buffer += bytes_read;
13886 break;
13887 default:
b98664d3 13888 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13889 return false;
13890 }
13891 if (rlet == DW_RLE_end_of_list || overflow)
13892 break;
13893 if (rlet == DW_RLE_base_address)
13894 continue;
13895
2b24b6e4 13896 if (!base.has_value ())
43988095
JK
13897 {
13898 /* We have no valid base address for the ranges
13899 data. */
b98664d3 13900 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13901 return false;
13902 }
13903
13904 if (range_beginning > range_end)
13905 {
13906 /* Inverted range entries are invalid. */
b98664d3 13907 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13908 return false;
13909 }
13910
13911 /* Empty range entries have no effect. */
13912 if (range_beginning == range_end)
13913 continue;
13914
2b24b6e4
TT
13915 range_beginning += *base;
13916 range_end += *base;
43988095
JK
13917
13918 /* A not-uncommon case of bad debug info.
13919 Don't pollute the addrmap with bad data. */
13920 if (range_beginning + baseaddr == 0
5989a64e 13921 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
43988095 13922 {
b98664d3 13923 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13924 " [in module %s]"), objfile_name (objfile));
13925 continue;
13926 }
13927
13928 callback (range_beginning, range_end);
13929 }
13930
13931 if (overflow)
13932 {
b98664d3 13933 complaint (_("Offset %d is not terminated "
43988095
JK
13934 "for DW_AT_ranges attribute"),
13935 offset);
13936 return false;
13937 }
13938
13939 return true;
13940}
13941
13942/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13943 Callback's type should be:
13944 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13945 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13946
43988095 13947template <typename Callback>
43039443 13948static int
5f46c5a5 13949dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13950 Callback &&callback)
43039443 13951{
5e22e966
SM
13952 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13953 struct objfile *objfile = per_objfile->objfile;
43039443
JK
13954 struct comp_unit_head *cu_header = &cu->header;
13955 bfd *obfd = objfile->obfd;
13956 unsigned int addr_size = cu_header->addr_size;
13957 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13958 /* Base address selection entry. */
2b24b6e4 13959 gdb::optional<CORE_ADDR> base;
43039443 13960 unsigned int dummy;
d521ce57 13961 const gdb_byte *buffer;
ff013f42 13962 CORE_ADDR baseaddr;
43039443 13963
43988095
JK
13964 if (cu_header->version >= 5)
13965 return dwarf2_rnglists_process (offset, cu, callback);
13966
d00adf39 13967 base = cu->base_address;
43039443 13968
5e22e966
SM
13969 per_objfile->per_bfd->ranges.read (objfile);
13970 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 13971 {
b98664d3 13972 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13973 offset);
13974 return 0;
13975 }
5e22e966 13976 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 13977
b3b3bada 13978 baseaddr = objfile->text_section_offset ();
ff013f42 13979
43039443
JK
13980 while (1)
13981 {
13982 CORE_ADDR range_beginning, range_end;
13983
c8a7a66f 13984 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13985 buffer += addr_size;
c8a7a66f 13986 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13987 buffer += addr_size;
13988 offset += 2 * addr_size;
13989
13990 /* An end of list marker is a pair of zero addresses. */
13991 if (range_beginning == 0 && range_end == 0)
13992 /* Found the end of list entry. */
13993 break;
13994
13995 /* Each base address selection entry is a pair of 2 values.
13996 The first is the largest possible address, the second is
13997 the base address. Check for a base address here. */
13998 if ((range_beginning & mask) == mask)
13999 {
28d2bfb9
AB
14000 /* If we found the largest possible address, then we already
14001 have the base address in range_end. */
14002 base = range_end;
43039443
JK
14003 continue;
14004 }
14005
2b24b6e4 14006 if (!base.has_value ())
43039443
JK
14007 {
14008 /* We have no valid base address for the ranges
14009 data. */
b98664d3 14010 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14011 return 0;
14012 }
14013
9277c30c
UW
14014 if (range_beginning > range_end)
14015 {
14016 /* Inverted range entries are invalid. */
b98664d3 14017 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14018 return 0;
14019 }
14020
14021 /* Empty range entries have no effect. */
14022 if (range_beginning == range_end)
14023 continue;
14024
2b24b6e4
TT
14025 range_beginning += *base;
14026 range_end += *base;
43039443 14027
01093045
DE
14028 /* A not-uncommon case of bad debug info.
14029 Don't pollute the addrmap with bad data. */
14030 if (range_beginning + baseaddr == 0
5e22e966 14031 && !per_objfile->per_bfd->has_section_at_zero)
01093045 14032 {
b98664d3 14033 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14034 " [in module %s]"), objfile_name (objfile));
01093045
DE
14035 continue;
14036 }
14037
5f46c5a5
JK
14038 callback (range_beginning, range_end);
14039 }
14040
14041 return 1;
14042}
14043
14044/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14045 Return 1 if the attributes are present and valid, otherwise, return 0.
14046 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14047
14048static int
14049dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14050 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 14051 dwarf2_psymtab *ranges_pst)
5f46c5a5 14052{
5e22e966 14053 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14054 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 14055 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
14056 int low_set = 0;
14057 CORE_ADDR low = 0;
14058 CORE_ADDR high = 0;
14059 int retval;
14060
14061 retval = dwarf2_ranges_process (offset, cu,
14062 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14063 {
9277c30c 14064 if (ranges_pst != NULL)
3e29f34a
MR
14065 {
14066 CORE_ADDR lowpc;
14067 CORE_ADDR highpc;
14068
79748972
TT
14069 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14070 range_beginning + baseaddr)
14071 - baseaddr);
14072 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14073 range_end + baseaddr)
14074 - baseaddr);
d320c2b5
TT
14075 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14076 lowpc, highpc - 1, ranges_pst);
3e29f34a 14077 }
ff013f42 14078
43039443
JK
14079 /* FIXME: This is recording everything as a low-high
14080 segment of consecutive addresses. We should have a
14081 data structure for discontiguous block ranges
14082 instead. */
14083 if (! low_set)
14084 {
14085 low = range_beginning;
14086 high = range_end;
14087 low_set = 1;
14088 }
14089 else
14090 {
14091 if (range_beginning < low)
14092 low = range_beginning;
14093 if (range_end > high)
14094 high = range_end;
14095 }
5f46c5a5
JK
14096 });
14097 if (!retval)
14098 return 0;
43039443
JK
14099
14100 if (! low_set)
14101 /* If the first entry is an end-of-list marker, the range
14102 describes an empty scope, i.e. no instructions. */
14103 return 0;
14104
14105 if (low_return)
14106 *low_return = low;
14107 if (high_return)
14108 *high_return = high;
14109 return 1;
14110}
14111
3a2b436a
JK
14112/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14113 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14114 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14115
3a2b436a 14116static enum pc_bounds_kind
af34e669 14117dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14118 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14119 dwarf2_psymtab *pst)
c906108c 14120{
5e22e966 14121 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
c906108c 14122 struct attribute *attr;
91da1414 14123 struct attribute *attr_high;
af34e669
DJ
14124 CORE_ADDR low = 0;
14125 CORE_ADDR high = 0;
e385593e 14126 enum pc_bounds_kind ret;
c906108c 14127
91da1414
MW
14128 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14129 if (attr_high)
af34e669 14130 {
e142c38c 14131 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14132 if (attr != nullptr)
91da1414 14133 {
cd6c91b4
TT
14134 low = attr->value_as_address ();
14135 high = attr_high->value_as_address ();
14136 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14137 high += low;
91da1414 14138 }
af34e669
DJ
14139 else
14140 /* Found high w/o low attribute. */
e385593e 14141 return PC_BOUNDS_INVALID;
af34e669
DJ
14142
14143 /* Found consecutive range of addresses. */
3a2b436a 14144 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14145 }
c906108c 14146 else
af34e669 14147 {
e142c38c 14148 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14149 if (attr != NULL)
14150 {
18a8505e 14151 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14152 We take advantage of the fact that DW_AT_ranges does not appear
14153 in DW_TAG_compile_unit of DWO files. */
14154 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14155 unsigned int ranges_offset = (DW_UNSND (attr)
14156 + (need_ranges_base
14157 ? cu->ranges_base
14158 : 0));
2e3cf129 14159
af34e669 14160 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14161 .debug_ranges section. */
2e3cf129 14162 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14163 return PC_BOUNDS_INVALID;
43039443 14164 /* Found discontinuous range of addresses. */
3a2b436a 14165 ret = PC_BOUNDS_RANGES;
af34e669 14166 }
e385593e
JK
14167 else
14168 return PC_BOUNDS_NOT_PRESENT;
af34e669 14169 }
c906108c 14170
48fbe735 14171 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14172 if (high <= low)
e385593e 14173 return PC_BOUNDS_INVALID;
c906108c
SS
14174
14175 /* When using the GNU linker, .gnu.linkonce. sections are used to
14176 eliminate duplicate copies of functions and vtables and such.
14177 The linker will arbitrarily choose one and discard the others.
14178 The AT_*_pc values for such functions refer to local labels in
14179 these sections. If the section from that file was discarded, the
14180 labels are not in the output, so the relocs get a value of 0.
14181 If this is a discarded function, mark the pc bounds as invalid,
14182 so that GDB will ignore it. */
5989a64e 14183 if (low == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
e385593e 14184 return PC_BOUNDS_INVALID;
c906108c
SS
14185
14186 *lowpc = low;
96408a79
SA
14187 if (highpc)
14188 *highpc = high;
af34e669 14189 return ret;
c906108c
SS
14190}
14191
b084d499
JB
14192/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14193 its low and high PC addresses. Do nothing if these addresses could not
14194 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14195 and HIGHPC to the high address if greater than HIGHPC. */
14196
14197static void
14198dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14199 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14200 struct dwarf2_cu *cu)
14201{
14202 CORE_ADDR low, high;
14203 struct die_info *child = die->child;
14204
e385593e 14205 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14206 {
325fac50
PA
14207 *lowpc = std::min (*lowpc, low);
14208 *highpc = std::max (*highpc, high);
b084d499
JB
14209 }
14210
14211 /* If the language does not allow nested subprograms (either inside
14212 subprograms or lexical blocks), we're done. */
14213 if (cu->language != language_ada)
14214 return;
6e70227d 14215
b084d499
JB
14216 /* Check all the children of the given DIE. If it contains nested
14217 subprograms, then check their pc bounds. Likewise, we need to
14218 check lexical blocks as well, as they may also contain subprogram
14219 definitions. */
14220 while (child && child->tag)
14221 {
14222 if (child->tag == DW_TAG_subprogram
14223 || child->tag == DW_TAG_lexical_block)
14224 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14225 child = child->sibling;
b084d499
JB
14226 }
14227}
14228
fae299cd
DC
14229/* Get the low and high pc's represented by the scope DIE, and store
14230 them in *LOWPC and *HIGHPC. If the correct values can't be
14231 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14232
14233static void
14234get_scope_pc_bounds (struct die_info *die,
14235 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14236 struct dwarf2_cu *cu)
14237{
14238 CORE_ADDR best_low = (CORE_ADDR) -1;
14239 CORE_ADDR best_high = (CORE_ADDR) 0;
14240 CORE_ADDR current_low, current_high;
14241
3a2b436a 14242 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14243 >= PC_BOUNDS_RANGES)
fae299cd
DC
14244 {
14245 best_low = current_low;
14246 best_high = current_high;
14247 }
14248 else
14249 {
14250 struct die_info *child = die->child;
14251
14252 while (child && child->tag)
14253 {
14254 switch (child->tag) {
14255 case DW_TAG_subprogram:
b084d499 14256 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14257 break;
14258 case DW_TAG_namespace:
f55ee35c 14259 case DW_TAG_module:
fae299cd
DC
14260 /* FIXME: carlton/2004-01-16: Should we do this for
14261 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14262 that current GCC's always emit the DIEs corresponding
14263 to definitions of methods of classes as children of a
14264 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14265 the DIEs giving the declarations, which could be
14266 anywhere). But I don't see any reason why the
14267 standards says that they have to be there. */
14268 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14269
14270 if (current_low != ((CORE_ADDR) -1))
14271 {
325fac50
PA
14272 best_low = std::min (best_low, current_low);
14273 best_high = std::max (best_high, current_high);
fae299cd
DC
14274 }
14275 break;
14276 default:
0963b4bd 14277 /* Ignore. */
fae299cd
DC
14278 break;
14279 }
14280
436c571c 14281 child = child->sibling;
fae299cd
DC
14282 }
14283 }
14284
14285 *lowpc = best_low;
14286 *highpc = best_high;
14287}
14288
801e3a5b
JB
14289/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14290 in DIE. */
380bca97 14291
801e3a5b
JB
14292static void
14293dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14294 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14295{
5e22e966 14296 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14297 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14298 struct attribute *attr;
91da1414 14299 struct attribute *attr_high;
801e3a5b 14300
91da1414
MW
14301 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14302 if (attr_high)
801e3a5b 14303 {
801e3a5b 14304 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14305 if (attr != nullptr)
801e3a5b 14306 {
cd6c91b4
TT
14307 CORE_ADDR low = attr->value_as_address ();
14308 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 14309
cd6c91b4 14310 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14311 high += low;
9a619af0 14312
3e29f34a
MR
14313 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14314 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14315 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14316 }
14317 }
14318
14319 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14320 if (attr != nullptr)
801e3a5b 14321 {
18a8505e 14322 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14323 We take advantage of the fact that DW_AT_ranges does not appear
14324 in DW_TAG_compile_unit of DWO files. */
14325 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14326
14327 /* The value of the DW_AT_ranges attribute is the offset of the
14328 address range list in the .debug_ranges section. */
ab435259
DE
14329 unsigned long offset = (DW_UNSND (attr)
14330 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14331
2d5f09ec 14332 std::vector<blockrange> blockvec;
5f46c5a5
JK
14333 dwarf2_ranges_process (offset, cu,
14334 [&] (CORE_ADDR start, CORE_ADDR end)
14335 {
58fdfd2c
JK
14336 start += baseaddr;
14337 end += baseaddr;
5f46c5a5
JK
14338 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14339 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14340 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14341 blockvec.emplace_back (start, end);
5f46c5a5 14342 });
2d5f09ec
KB
14343
14344 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14345 }
14346}
14347
685b1105
JK
14348/* Check whether the producer field indicates either of GCC < 4.6, or the
14349 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14350
685b1105
JK
14351static void
14352check_producer (struct dwarf2_cu *cu)
60d5a603 14353{
38360086 14354 int major, minor;
60d5a603
JK
14355
14356 if (cu->producer == NULL)
14357 {
14358 /* For unknown compilers expect their behavior is DWARF version
14359 compliant.
14360
14361 GCC started to support .debug_types sections by -gdwarf-4 since
14362 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14363 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14364 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14365 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14366 }
b1ffba5a 14367 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14368 {
38360086
MW
14369 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14370 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14371 }
5230b05a 14372 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14373 {
14374 cu->producer_is_icc = true;
14375 cu->producer_is_icc_lt_14 = major < 14;
14376 }
c258c396
JD
14377 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14378 cu->producer_is_codewarrior = true;
685b1105
JK
14379 else
14380 {
14381 /* For other non-GCC compilers, expect their behavior is DWARF version
14382 compliant. */
60d5a603
JK
14383 }
14384
9068261f 14385 cu->checked_producer = true;
685b1105 14386}
ba919b58 14387
685b1105
JK
14388/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14389 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14390 during 4.6.0 experimental. */
14391
9068261f 14392static bool
685b1105
JK
14393producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14394{
14395 if (!cu->checked_producer)
14396 check_producer (cu);
14397
14398 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14399}
14400
c258c396
JD
14401
14402/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14403 with incorrect is_stmt attributes. */
14404
14405static bool
14406producer_is_codewarrior (struct dwarf2_cu *cu)
14407{
14408 if (!cu->checked_producer)
14409 check_producer (cu);
14410
14411 return cu->producer_is_codewarrior;
14412}
14413
405feb71 14414/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14415 DW_AT_accessibility. */
14416
14417static enum dwarf_access_attribute
14418dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14419{
14420 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14421 {
14422 /* The default DWARF 2 accessibility for members is public, the default
14423 accessibility for inheritance is private. */
14424
14425 if (die->tag != DW_TAG_inheritance)
14426 return DW_ACCESS_public;
14427 else
14428 return DW_ACCESS_private;
14429 }
14430 else
14431 {
14432 /* DWARF 3+ defines the default accessibility a different way. The same
14433 rules apply now for DW_TAG_inheritance as for the members and it only
14434 depends on the container kind. */
14435
14436 if (die->parent->tag == DW_TAG_class_type)
14437 return DW_ACCESS_private;
14438 else
14439 return DW_ACCESS_public;
14440 }
14441}
14442
74ac6d43
TT
14443/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14444 offset. If the attribute was not found return 0, otherwise return
14445 1. If it was found but could not properly be handled, set *OFFSET
14446 to 0. */
14447
14448static int
14449handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14450 LONGEST *offset)
14451{
14452 struct attribute *attr;
14453
14454 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14455 if (attr != NULL)
14456 {
14457 *offset = 0;
14458
14459 /* Note that we do not check for a section offset first here.
14460 This is because DW_AT_data_member_location is new in DWARF 4,
14461 so if we see it, we can assume that a constant form is really
14462 a constant and not a section offset. */
cd6c91b4 14463 if (attr->form_is_constant ())
0826b30a 14464 *offset = attr->constant_value (0);
cd6c91b4 14465 else if (attr->form_is_section_offset ())
74ac6d43 14466 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14467 else if (attr->form_is_block ())
74ac6d43
TT
14468 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14469 else
14470 dwarf2_complex_location_expr_complaint ();
14471
14472 return 1;
14473 }
14474
14475 return 0;
14476}
14477
7d79de9a
TT
14478/* Look for DW_AT_data_member_location and store the results in FIELD. */
14479
14480static void
14481handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14482 struct field *field)
14483{
14484 struct attribute *attr;
14485
14486 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14487 if (attr != NULL)
14488 {
14489 if (attr->form_is_constant ())
14490 {
14491 LONGEST offset = attr->constant_value (0);
14492 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14493 }
14494 else if (attr->form_is_section_offset ())
14495 dwarf2_complex_location_expr_complaint ();
14496 else if (attr->form_is_block ())
14497 {
14498 bool handled;
14499 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14500 if (handled)
14501 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14502 else
14503 {
5e22e966
SM
14504 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14505 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14506 struct dwarf2_locexpr_baton *dlbaton
14507 = XOBNEW (&objfile->objfile_obstack,
14508 struct dwarf2_locexpr_baton);
14509 dlbaton->data = DW_BLOCK (attr)->data;
14510 dlbaton->size = DW_BLOCK (attr)->size;
14511 /* When using this baton, we want to compute the address
14512 of the field, not the value. This is why
14513 is_reference is set to false here. */
14514 dlbaton->is_reference = false;
5e22e966 14515 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14516 dlbaton->per_cu = cu->per_cu;
14517
14518 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14519 }
14520 }
14521 else
14522 dwarf2_complex_location_expr_complaint ();
14523 }
14524}
14525
c906108c
SS
14526/* Add an aggregate field to the field list. */
14527
14528static void
107d2387 14529dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14530 struct dwarf2_cu *cu)
6e70227d 14531{
5e22e966 14532 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14533 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14534 struct nextfield *new_field;
14535 struct attribute *attr;
14536 struct field *fp;
15d034d0 14537 const char *fieldname = "";
c906108c 14538
7d0ccb61
DJ
14539 if (die->tag == DW_TAG_inheritance)
14540 {
be2daae6
TT
14541 fip->baseclasses.emplace_back ();
14542 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14543 }
14544 else
14545 {
be2daae6
TT
14546 fip->fields.emplace_back ();
14547 new_field = &fip->fields.back ();
7d0ccb61 14548 }
be2daae6 14549
9c6a1327
TT
14550 new_field->offset = die->sect_off;
14551
e142c38c 14552 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14553 if (attr != nullptr)
c906108c 14554 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14555 else
14556 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14557 if (new_field->accessibility != DW_ACCESS_public)
14558 fip->non_public_fields = 1;
60d5a603 14559
e142c38c 14560 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14561 if (attr != nullptr)
c906108c 14562 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14563 else
14564 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14565
14566 fp = &new_field->field;
a9a9bd0f 14567
e142c38c 14568 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14569 {
a9a9bd0f 14570 /* Data member other than a C++ static data member. */
6e70227d 14571
c906108c 14572 /* Get type of field. */
e7c27a73 14573 fp->type = die_type (die, cu);
c906108c 14574
d6a843b5 14575 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14576
c906108c 14577 /* Get bit size of field (zero if none). */
e142c38c 14578 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14579 if (attr != nullptr)
c906108c
SS
14580 {
14581 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14582 }
14583 else
14584 {
14585 FIELD_BITSIZE (*fp) = 0;
14586 }
14587
14588 /* Get bit offset of field. */
7d79de9a 14589 handle_data_member_location (die, cu, fp);
e142c38c 14590 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14591 if (attr != nullptr)
c906108c 14592 {
d5a22e77 14593 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14594 {
14595 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14596 additional bit offset from the MSB of the containing
14597 anonymous object to the MSB of the field. We don't
14598 have to do anything special since we don't need to
14599 know the size of the anonymous object. */
f41f5e61 14600 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14601 }
14602 else
14603 {
14604 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14605 MSB of the anonymous object, subtract off the number of
14606 bits from the MSB of the field to the MSB of the
14607 object, and then subtract off the number of bits of
14608 the field itself. The result is the bit offset of
14609 the LSB of the field. */
c906108c
SS
14610 int anonymous_size;
14611 int bit_offset = DW_UNSND (attr);
14612
e142c38c 14613 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14614 if (attr != nullptr)
c906108c
SS
14615 {
14616 /* The size of the anonymous object containing
14617 the bit field is explicit, so use the
14618 indicated size (in bytes). */
14619 anonymous_size = DW_UNSND (attr);
14620 }
14621 else
14622 {
14623 /* The size of the anonymous object containing
14624 the bit field must be inferred from the type
14625 attribute of the data member containing the
14626 bit field. */
14627 anonymous_size = TYPE_LENGTH (fp->type);
14628 }
f41f5e61
PA
14629 SET_FIELD_BITPOS (*fp,
14630 (FIELD_BITPOS (*fp)
14631 + anonymous_size * bits_per_byte
14632 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14633 }
14634 }
da5b30da
AA
14635 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14636 if (attr != NULL)
14637 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14638 + attr->constant_value (0)));
c906108c
SS
14639
14640 /* Get name of field. */
39cbfefa
DJ
14641 fieldname = dwarf2_name (die, cu);
14642 if (fieldname == NULL)
14643 fieldname = "";
d8151005
DJ
14644
14645 /* The name is already allocated along with this objfile, so we don't
14646 need to duplicate it for the type. */
14647 fp->name = fieldname;
c906108c
SS
14648
14649 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14650 pointer or virtual base class pointer) to private. */
e142c38c 14651 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14652 {
d48cc9dd 14653 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14654 new_field->accessibility = DW_ACCESS_private;
14655 fip->non_public_fields = 1;
14656 }
14657 }
a9a9bd0f 14658 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14659 {
a9a9bd0f
DC
14660 /* C++ static member. */
14661
14662 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14663 is a declaration, but all versions of G++ as of this writing
14664 (so through at least 3.2.1) incorrectly generate
14665 DW_TAG_variable tags. */
6e70227d 14666
ff355380 14667 const char *physname;
c906108c 14668
a9a9bd0f 14669 /* Get name of field. */
39cbfefa
DJ
14670 fieldname = dwarf2_name (die, cu);
14671 if (fieldname == NULL)
c906108c
SS
14672 return;
14673
254e6b9e 14674 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14675 if (attr
14676 /* Only create a symbol if this is an external value.
14677 new_symbol checks this and puts the value in the global symbol
14678 table, which we want. If it is not external, new_symbol
14679 will try to put the value in cu->list_in_scope which is wrong. */
14680 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14681 {
14682 /* A static const member, not much different than an enum as far as
14683 we're concerned, except that we can support more types. */
14684 new_symbol (die, NULL, cu);
14685 }
14686
2df3850c 14687 /* Get physical name. */
ff355380 14688 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14689
d8151005
DJ
14690 /* The name is already allocated along with this objfile, so we don't
14691 need to duplicate it for the type. */
14692 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14693 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14694 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14695 }
14696 else if (die->tag == DW_TAG_inheritance)
14697 {
74ac6d43 14698 /* C++ base class field. */
7d79de9a 14699 handle_data_member_location (die, cu, fp);
c906108c 14700 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14701 FIELD_TYPE (*fp) = die_type (die, cu);
7d93a1e0 14702 FIELD_NAME (*fp) = fp->type->name ();
c906108c 14703 }
2ddeaf8a
TT
14704 else
14705 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14706}
14707
883fd55a
KS
14708/* Can the type given by DIE define another type? */
14709
14710static bool
14711type_can_define_types (const struct die_info *die)
14712{
14713 switch (die->tag)
14714 {
14715 case DW_TAG_typedef:
14716 case DW_TAG_class_type:
14717 case DW_TAG_structure_type:
14718 case DW_TAG_union_type:
14719 case DW_TAG_enumeration_type:
14720 return true;
14721
14722 default:
14723 return false;
14724 }
14725}
14726
14727/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14728
14729static void
883fd55a
KS
14730dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14731 struct dwarf2_cu *cu)
6e70227d 14732{
be2daae6
TT
14733 struct decl_field fp;
14734 memset (&fp, 0, sizeof (fp));
98751a41 14735
883fd55a 14736 gdb_assert (type_can_define_types (die));
98751a41 14737
883fd55a 14738 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14739 fp.name = dwarf2_name (die, cu);
14740 fp.type = read_type_die (die, cu);
98751a41 14741
c191a687
KS
14742 /* Save accessibility. */
14743 enum dwarf_access_attribute accessibility;
14744 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14745 if (attr != NULL)
14746 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14747 else
14748 accessibility = dwarf2_default_access_attribute (die, cu);
14749 switch (accessibility)
14750 {
14751 case DW_ACCESS_public:
14752 /* The assumed value if neither private nor protected. */
14753 break;
14754 case DW_ACCESS_private:
be2daae6 14755 fp.is_private = 1;
c191a687
KS
14756 break;
14757 case DW_ACCESS_protected:
be2daae6 14758 fp.is_protected = 1;
c191a687
KS
14759 break;
14760 default:
b98664d3 14761 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14762 }
14763
883fd55a 14764 if (die->tag == DW_TAG_typedef)
be2daae6 14765 fip->typedef_field_list.push_back (fp);
883fd55a 14766 else
be2daae6 14767 fip->nested_types_list.push_back (fp);
98751a41
JK
14768}
14769
9c6a1327
TT
14770/* A convenience typedef that's used when finding the discriminant
14771 field for a variant part. */
1b95cdb7
SM
14772typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14773 offset_map_type;
9c6a1327
TT
14774
14775/* Compute the discriminant range for a given variant. OBSTACK is
14776 where the results will be stored. VARIANT is the variant to
14777 process. IS_UNSIGNED indicates whether the discriminant is signed
14778 or unsigned. */
14779
14780static const gdb::array_view<discriminant_range>
14781convert_variant_range (struct obstack *obstack, const variant_field &variant,
14782 bool is_unsigned)
14783{
14784 std::vector<discriminant_range> ranges;
14785
14786 if (variant.default_branch)
14787 return {};
14788
14789 if (variant.discr_list_data == nullptr)
14790 {
14791 discriminant_range r
14792 = {variant.discriminant_value, variant.discriminant_value};
14793 ranges.push_back (r);
14794 }
14795 else
14796 {
14797 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14798 variant.discr_list_data->size);
14799 while (!data.empty ())
14800 {
14801 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14802 {
14803 complaint (_("invalid discriminant marker: %d"), data[0]);
14804 break;
14805 }
14806 bool is_range = data[0] == DW_DSC_range;
14807 data = data.slice (1);
14808
14809 ULONGEST low, high;
14810 unsigned int bytes_read;
14811
14812 if (data.empty ())
14813 {
14814 complaint (_("DW_AT_discr_list missing low value"));
14815 break;
14816 }
14817 if (is_unsigned)
14818 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14819 else
14820 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14821 &bytes_read);
14822 data = data.slice (bytes_read);
14823
14824 if (is_range)
14825 {
14826 if (data.empty ())
14827 {
14828 complaint (_("DW_AT_discr_list missing high value"));
14829 break;
14830 }
14831 if (is_unsigned)
14832 high = read_unsigned_leb128 (nullptr, data.data (),
14833 &bytes_read);
14834 else
14835 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14836 &bytes_read);
14837 data = data.slice (bytes_read);
14838 }
14839 else
14840 high = low;
14841
14842 ranges.push_back ({ low, high });
14843 }
14844 }
14845
14846 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14847 ranges.size ());
14848 std::copy (ranges.begin (), ranges.end (), result);
14849 return gdb::array_view<discriminant_range> (result, ranges.size ());
14850}
14851
14852static const gdb::array_view<variant_part> create_variant_parts
14853 (struct obstack *obstack,
14854 const offset_map_type &offset_map,
14855 struct field_info *fi,
14856 const std::vector<variant_part_builder> &variant_parts);
14857
14858/* Fill in a "struct variant" for a given variant field. RESULT is
14859 the variant to fill in. OBSTACK is where any needed allocations
14860 will be done. OFFSET_MAP holds the mapping from section offsets to
14861 fields for the type. FI describes the fields of the type we're
14862 processing. FIELD is the variant field we're converting. */
14863
14864static void
14865create_one_variant (variant &result, struct obstack *obstack,
14866 const offset_map_type &offset_map,
14867 struct field_info *fi, const variant_field &field)
14868{
14869 result.discriminants = convert_variant_range (obstack, field, false);
14870 result.first_field = field.first_field + fi->baseclasses.size ();
14871 result.last_field = field.last_field + fi->baseclasses.size ();
14872 result.parts = create_variant_parts (obstack, offset_map, fi,
14873 field.variant_parts);
14874}
14875
14876/* Fill in a "struct variant_part" for a given variant part. RESULT
14877 is the variant part to fill in. OBSTACK is where any needed
14878 allocations will be done. OFFSET_MAP holds the mapping from
14879 section offsets to fields for the type. FI describes the fields of
14880 the type we're processing. BUILDER is the variant part to be
14881 converted. */
14882
14883static void
14884create_one_variant_part (variant_part &result,
14885 struct obstack *obstack,
14886 const offset_map_type &offset_map,
14887 struct field_info *fi,
14888 const variant_part_builder &builder)
14889{
14890 auto iter = offset_map.find (builder.discriminant_offset);
14891 if (iter == offset_map.end ())
14892 {
14893 result.discriminant_index = -1;
14894 /* Doesn't matter. */
14895 result.is_unsigned = false;
14896 }
14897 else
14898 {
14899 result.discriminant_index = iter->second;
14900 result.is_unsigned
14901 = TYPE_UNSIGNED (FIELD_TYPE
14902 (fi->fields[result.discriminant_index].field));
14903 }
14904
14905 size_t n = builder.variants.size ();
14906 variant *output = new (obstack) variant[n];
14907 for (size_t i = 0; i < n; ++i)
14908 create_one_variant (output[i], obstack, offset_map, fi,
14909 builder.variants[i]);
14910
14911 result.variants = gdb::array_view<variant> (output, n);
14912}
14913
14914/* Create a vector of variant parts that can be attached to a type.
14915 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14916 holds the mapping from section offsets to fields for the type. FI
14917 describes the fields of the type we're processing. VARIANT_PARTS
14918 is the vector to convert. */
14919
14920static const gdb::array_view<variant_part>
14921create_variant_parts (struct obstack *obstack,
14922 const offset_map_type &offset_map,
14923 struct field_info *fi,
14924 const std::vector<variant_part_builder> &variant_parts)
14925{
14926 if (variant_parts.empty ())
14927 return {};
14928
14929 size_t n = variant_parts.size ();
14930 variant_part *result = new (obstack) variant_part[n];
14931 for (size_t i = 0; i < n; ++i)
14932 create_one_variant_part (result[i], obstack, offset_map, fi,
14933 variant_parts[i]);
14934
14935 return gdb::array_view<variant_part> (result, n);
14936}
14937
14938/* Compute the variant part vector for FIP, attaching it to TYPE when
14939 done. */
14940
14941static void
14942add_variant_property (struct field_info *fip, struct type *type,
14943 struct dwarf2_cu *cu)
14944{
14945 /* Map section offsets of fields to their field index. Note the
14946 field index here does not take the number of baseclasses into
14947 account. */
14948 offset_map_type offset_map;
14949 for (int i = 0; i < fip->fields.size (); ++i)
14950 offset_map[fip->fields[i].offset] = i;
14951
5e22e966 14952 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
14953 gdb::array_view<variant_part> parts
14954 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14955 fip->variant_parts);
14956
14957 struct dynamic_prop prop;
14958 prop.kind = PROP_VARIANT_PARTS;
14959 prop.data.variant_parts
14960 = ((gdb::array_view<variant_part> *)
14961 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14962
5c54719c 14963 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
14964}
14965
c906108c
SS
14966/* Create the vector of fields, and attach it to the type. */
14967
14968static void
fba45db2 14969dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14970 struct dwarf2_cu *cu)
c906108c 14971{
317f7127 14972 int nfields = fip->nfields ();
c906108c
SS
14973
14974 /* Record the field count, allocate space for the array of fields,
14975 and create blank accessibility bitfields if necessary. */
5e33d5f4 14976 type->set_num_fields (nfields);
3cabb6b0
SM
14977 type->set_fields
14978 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 14979
b4ba55a1 14980 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14981 {
14982 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14983
14984 TYPE_FIELD_PRIVATE_BITS (type) =
14985 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14986 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14987
14988 TYPE_FIELD_PROTECTED_BITS (type) =
14989 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14990 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14991
774b6a14
TT
14992 TYPE_FIELD_IGNORE_BITS (type) =
14993 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14994 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14995 }
14996
14997 /* If the type has baseclasses, allocate and clear a bit vector for
14998 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14999 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15000 {
be2daae6 15001 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15002 unsigned char *pointer;
c906108c
SS
15003
15004 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15005 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15006 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15007 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15008 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15009 }
15010
9c6a1327
TT
15011 if (!fip->variant_parts.empty ())
15012 add_variant_property (fip, type, cu);
2ddeaf8a 15013
be2daae6
TT
15014 /* Copy the saved-up fields into the field vector. */
15015 for (int i = 0; i < nfields; ++i)
c906108c 15016 {
be2daae6
TT
15017 struct nextfield &field
15018 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15019 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15020
ceacbf6e 15021 type->field (i) = field.field;
be2daae6 15022 switch (field.accessibility)
c906108c 15023 {
c5aa993b 15024 case DW_ACCESS_private:
b4ba55a1 15025 if (cu->language != language_ada)
be2daae6 15026 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15027 break;
c906108c 15028
c5aa993b 15029 case DW_ACCESS_protected:
b4ba55a1 15030 if (cu->language != language_ada)
be2daae6 15031 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15032 break;
c906108c 15033
c5aa993b
JM
15034 case DW_ACCESS_public:
15035 break;
c906108c 15036
c5aa993b
JM
15037 default:
15038 /* Unknown accessibility. Complain and treat it as public. */
15039 {
b98664d3 15040 complaint (_("unsupported accessibility %d"),
be2daae6 15041 field.accessibility);
c5aa993b
JM
15042 }
15043 break;
c906108c 15044 }
be2daae6 15045 if (i < fip->baseclasses.size ())
c906108c 15046 {
be2daae6 15047 switch (field.virtuality)
c906108c 15048 {
c5aa993b
JM
15049 case DW_VIRTUALITY_virtual:
15050 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15051 if (cu->language == language_ada)
a73c6dcd 15052 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15053 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15054 break;
c906108c
SS
15055 }
15056 }
c906108c
SS
15057 }
15058}
15059
7d27a96d
TT
15060/* Return true if this member function is a constructor, false
15061 otherwise. */
15062
15063static int
15064dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15065{
15066 const char *fieldname;
fe978cb0 15067 const char *type_name;
7d27a96d
TT
15068 int len;
15069
15070 if (die->parent == NULL)
15071 return 0;
15072
15073 if (die->parent->tag != DW_TAG_structure_type
15074 && die->parent->tag != DW_TAG_union_type
15075 && die->parent->tag != DW_TAG_class_type)
15076 return 0;
15077
15078 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15079 type_name = dwarf2_name (die->parent, cu);
15080 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15081 return 0;
15082
15083 len = strlen (fieldname);
fe978cb0
PA
15084 return (strncmp (fieldname, type_name, len) == 0
15085 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15086}
15087
e35000a7
TBA
15088/* Check if the given VALUE is a recognized enum
15089 dwarf_defaulted_attribute constant according to DWARF5 spec,
15090 Table 7.24. */
15091
15092static bool
15093is_valid_DW_AT_defaulted (ULONGEST value)
15094{
15095 switch (value)
15096 {
15097 case DW_DEFAULTED_no:
15098 case DW_DEFAULTED_in_class:
15099 case DW_DEFAULTED_out_of_class:
15100 return true;
15101 }
15102
3142e908 15103 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
15104 return false;
15105}
15106
c906108c
SS
15107/* Add a member function to the proper fieldlist. */
15108
15109static void
107d2387 15110dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15111 struct type *type, struct dwarf2_cu *cu)
c906108c 15112{
5e22e966 15113 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15114 struct attribute *attr;
c906108c 15115 int i;
be2daae6 15116 struct fnfieldlist *flp = nullptr;
c906108c 15117 struct fn_field *fnp;
15d034d0 15118 const char *fieldname;
f792889a 15119 struct type *this_type;
60d5a603 15120 enum dwarf_access_attribute accessibility;
c906108c 15121
b4ba55a1 15122 if (cu->language == language_ada)
a73c6dcd 15123 error (_("unexpected member function in Ada type"));
b4ba55a1 15124
2df3850c 15125 /* Get name of member function. */
39cbfefa
DJ
15126 fieldname = dwarf2_name (die, cu);
15127 if (fieldname == NULL)
2df3850c 15128 return;
c906108c 15129
c906108c 15130 /* Look up member function name in fieldlist. */
be2daae6 15131 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15132 {
27bfe10e 15133 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15134 {
15135 flp = &fip->fnfieldlists[i];
15136 break;
15137 }
c906108c
SS
15138 }
15139
be2daae6
TT
15140 /* Create a new fnfieldlist if necessary. */
15141 if (flp == nullptr)
c906108c 15142 {
be2daae6
TT
15143 fip->fnfieldlists.emplace_back ();
15144 flp = &fip->fnfieldlists.back ();
c906108c 15145 flp->name = fieldname;
be2daae6 15146 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15147 }
15148
be2daae6
TT
15149 /* Create a new member function field and add it to the vector of
15150 fnfieldlists. */
15151 flp->fnfields.emplace_back ();
15152 fnp = &flp->fnfields.back ();
3da10d80
KS
15153
15154 /* Delay processing of the physname until later. */
9c37b5ae 15155 if (cu->language == language_cplus)
be2daae6
TT
15156 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15157 die, cu);
3da10d80
KS
15158 else
15159 {
1d06ead6 15160 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15161 fnp->physname = physname ? physname : "";
15162 }
15163
c906108c 15164 fnp->type = alloc_type (objfile);
f792889a 15165 this_type = read_type_die (die, cu);
78134374 15166 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15167 {
1f704f76 15168 int nparams = this_type->num_fields ();
c906108c 15169
f792889a 15170 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15171 of the method itself (TYPE_CODE_METHOD). */
15172 smash_to_method_type (fnp->type, type,
f792889a 15173 TYPE_TARGET_TYPE (this_type),
80fc5e77 15174 this_type->fields (),
1f704f76 15175 this_type->num_fields (),
f792889a 15176 TYPE_VARARGS (this_type));
c906108c
SS
15177
15178 /* Handle static member functions.
c5aa993b 15179 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15180 member functions. G++ helps GDB by marking the first
15181 parameter for non-static member functions (which is the this
15182 pointer) as artificial. We obtain this information from
15183 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15184 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15185 fnp->voffset = VOFFSET_STATIC;
15186 }
15187 else
b98664d3 15188 complaint (_("member function type missing for '%s'"),
3da10d80 15189 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15190
15191 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15192 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15193 fnp->fcontext = die_containing_type (die, cu);
c906108c 15194
3e43a32a
MS
15195 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15196 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15197
15198 /* Get accessibility. */
e142c38c 15199 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15200 if (attr != nullptr)
aead7601 15201 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15202 else
15203 accessibility = dwarf2_default_access_attribute (die, cu);
15204 switch (accessibility)
c906108c 15205 {
60d5a603
JK
15206 case DW_ACCESS_private:
15207 fnp->is_private = 1;
15208 break;
15209 case DW_ACCESS_protected:
15210 fnp->is_protected = 1;
15211 break;
c906108c
SS
15212 }
15213
b02dede2 15214 /* Check for artificial methods. */
e142c38c 15215 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15216 if (attr && DW_UNSND (attr) != 0)
15217 fnp->is_artificial = 1;
15218
e35000a7
TBA
15219 /* Check for defaulted methods. */
15220 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15221 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15222 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15223
15224 /* Check for deleted methods. */
15225 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15226 if (attr != nullptr && DW_UNSND (attr) != 0)
15227 fnp->is_deleted = 1;
15228
7d27a96d
TT
15229 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15230
0d564a31 15231 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15232 function. For older versions of GCC, this is an offset in the
15233 appropriate virtual table, as specified by DW_AT_containing_type.
15234 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15235 to the object address. */
15236
e142c38c 15237 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15238 if (attr != nullptr)
8e19ed76 15239 {
4fc6c0d5 15240 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 15241 {
aec5aa8b
TT
15242 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15243 {
15244 /* Old-style GCC. */
15245 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15246 }
15247 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15248 || (DW_BLOCK (attr)->size > 1
15249 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15250 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15251 {
aec5aa8b
TT
15252 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15253 if ((fnp->voffset % cu->header.addr_size) != 0)
15254 dwarf2_complex_location_expr_complaint ();
15255 else
15256 fnp->voffset /= cu->header.addr_size;
15257 fnp->voffset += 2;
15258 }
15259 else
15260 dwarf2_complex_location_expr_complaint ();
15261
15262 if (!fnp->fcontext)
7e993ebf
KS
15263 {
15264 /* If there is no `this' field and no DW_AT_containing_type,
15265 we cannot actually find a base class context for the
15266 vtable! */
1f704f76 15267 if (this_type->num_fields () == 0
7e993ebf
KS
15268 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15269 {
b98664d3 15270 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15271 "function \"%s\" (offset %s)"),
15272 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15273 }
15274 else
15275 {
15276 fnp->fcontext
15277 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15278 }
15279 }
aec5aa8b 15280 }
cd6c91b4 15281 else if (attr->form_is_section_offset ())
8e19ed76 15282 {
4d3c2250 15283 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15284 }
15285 else
15286 {
4d3c2250
KB
15287 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15288 fieldname);
8e19ed76 15289 }
0d564a31 15290 }
d48cc9dd
DJ
15291 else
15292 {
15293 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15294 if (attr && DW_UNSND (attr))
15295 {
15296 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15297 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15298 "but the vtable offset is not specified"),
9d8780f0 15299 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15300 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15301 TYPE_CPLUS_DYNAMIC (type) = 1;
15302 }
15303 }
c906108c
SS
15304}
15305
15306/* Create the vector of member function fields, and attach it to the type. */
15307
15308static void
fba45db2 15309dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15310 struct dwarf2_cu *cu)
c906108c 15311{
b4ba55a1 15312 if (cu->language == language_ada)
a73c6dcd 15313 error (_("unexpected member functions in Ada type"));
b4ba55a1 15314
c906108c
SS
15315 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15316 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15317 TYPE_ALLOC (type,
15318 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15319
be2daae6 15320 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15321 {
be2daae6 15322 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15323 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15324
be2daae6
TT
15325 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15326 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15327 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15328 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15329
15330 for (int k = 0; k < nf.fnfields.size (); ++k)
15331 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15332 }
15333
be2daae6 15334 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15335}
15336
1168df01
JB
15337/* Returns non-zero if NAME is the name of a vtable member in CU's
15338 language, zero otherwise. */
15339static int
15340is_vtable_name (const char *name, struct dwarf2_cu *cu)
15341{
15342 static const char vptr[] = "_vptr";
15343
9c37b5ae
TT
15344 /* Look for the C++ form of the vtable. */
15345 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15346 return 1;
15347
15348 return 0;
15349}
15350
c0dd20ea 15351/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15352 functions, with the ABI-specified layout. If TYPE describes
15353 such a structure, smash it into a member function type.
61049d3b
DJ
15354
15355 GCC shouldn't do this; it should just output pointer to member DIEs.
15356 This is GCC PR debug/28767. */
c0dd20ea 15357
0b92b5bb
TT
15358static void
15359quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15360{
09e2d7c7 15361 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15362
15363 /* Check for a structure with no name and two children. */
1f704f76 15364 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15365 return;
c0dd20ea
DJ
15366
15367 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15368 if (TYPE_FIELD_NAME (type, 0) == NULL
15369 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15370 || TYPE_FIELD_NAME (type, 1) == NULL
15371 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15372 return;
c0dd20ea
DJ
15373
15374 /* Find the type of the method. */
0b92b5bb 15375 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea 15376 if (pfn_type == NULL
78134374
SM
15377 || pfn_type->code () != TYPE_CODE_PTR
15378 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15379 return;
c0dd20ea
DJ
15380
15381 /* Look for the "this" argument. */
15382 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15383 if (pfn_type->num_fields () == 0
0b92b5bb 15384 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
78134374 15385 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
0b92b5bb 15386 return;
c0dd20ea 15387
09e2d7c7 15388 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15389 new_type = alloc_type (objfile);
09e2d7c7 15390 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15391 pfn_type->fields (), pfn_type->num_fields (),
c0dd20ea 15392 TYPE_VARARGS (pfn_type));
0b92b5bb 15393 smash_to_methodptr_type (type, new_type);
c0dd20ea 15394}
1168df01 15395
2b4424c3
TT
15396/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15397 appropriate error checking and issuing complaints if there is a
15398 problem. */
15399
15400static ULONGEST
15401get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15402{
15403 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15404
15405 if (attr == nullptr)
15406 return 0;
15407
cd6c91b4 15408 if (!attr->form_is_constant ())
2b4424c3 15409 {
b98664d3 15410 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15411 " - DIE at %s [in module %s]"),
15412 sect_offset_str (die->sect_off),
5e22e966 15413 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15414 return 0;
15415 }
15416
15417 ULONGEST align;
15418 if (attr->form == DW_FORM_sdata)
15419 {
15420 LONGEST val = DW_SND (attr);
15421 if (val < 0)
15422 {
b98664d3 15423 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15424 " - DIE at %s [in module %s]"),
15425 sect_offset_str (die->sect_off),
5e22e966 15426 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15427 return 0;
15428 }
15429 align = val;
15430 }
15431 else
15432 align = DW_UNSND (attr);
15433
15434 if (align == 0)
15435 {
b98664d3 15436 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15437 " - DIE at %s [in module %s]"),
15438 sect_offset_str (die->sect_off),
5e22e966 15439 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15440 return 0;
15441 }
15442 if ((align & (align - 1)) != 0)
15443 {
b98664d3 15444 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15445 " - DIE at %s [in module %s]"),
15446 sect_offset_str (die->sect_off),
5e22e966 15447 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15448 return 0;
15449 }
15450
15451 return align;
15452}
15453
15454/* If the DIE has a DW_AT_alignment attribute, use its value to set
15455 the alignment for TYPE. */
15456
15457static void
15458maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15459 struct type *type)
15460{
15461 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15462 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15463 " - DIE at %s [in module %s]"),
15464 sect_offset_str (die->sect_off),
5e22e966 15465 objfile_name (cu->per_objfile->objfile));
2b4424c3 15466}
685b1105 15467
e35000a7
TBA
15468/* Check if the given VALUE is a valid enum dwarf_calling_convention
15469 constant for a type, according to DWARF5 spec, Table 5.5. */
15470
15471static bool
15472is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15473{
15474 switch (value)
15475 {
15476 case DW_CC_normal:
15477 case DW_CC_pass_by_reference:
15478 case DW_CC_pass_by_value:
15479 return true;
15480
15481 default:
15482 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15483 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15484 return false;
15485 }
15486}
15487
d0922fcf
TBA
15488/* Check if the given VALUE is a valid enum dwarf_calling_convention
15489 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15490 also according to GNU-specific values (see include/dwarf2.h). */
15491
15492static bool
15493is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15494{
15495 switch (value)
15496 {
15497 case DW_CC_normal:
15498 case DW_CC_program:
15499 case DW_CC_nocall:
15500 return true;
15501
15502 case DW_CC_GNU_renesas_sh:
15503 case DW_CC_GNU_borland_fastcall_i386:
15504 case DW_CC_GDB_IBM_OpenCL:
15505 return true;
15506
15507 default:
15508 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15509 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15510 return false;
15511 }
15512}
15513
c906108c 15514/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15515 (definition) to create a type for the structure or union. Fill in
15516 the type's name and general properties; the members will not be
83655187
DE
15517 processed until process_structure_scope. A symbol table entry for
15518 the type will also not be done until process_structure_scope (assuming
15519 the type has a name).
c906108c 15520
c767944b
DJ
15521 NOTE: we need to call these functions regardless of whether or not the
15522 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15523 structure or union. This gets the type entered into our set of
83655187 15524 user defined types. */
c906108c 15525
f792889a 15526static struct type *
134d01f1 15527read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15528{
5e22e966 15529 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
15530 struct type *type;
15531 struct attribute *attr;
15d034d0 15532 const char *name;
c906108c 15533
348e048f
DE
15534 /* If the definition of this type lives in .debug_types, read that type.
15535 Don't follow DW_AT_specification though, that will take us back up
15536 the chain and we want to go down. */
052c8bb8 15537 attr = die->attr (DW_AT_signature);
435d3d88 15538 if (attr != nullptr)
348e048f 15539 {
ac9ec31b 15540 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15541
ac9ec31b 15542 /* The type's CU may not be the same as CU.
02142a6c 15543 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15544 return set_die_type (die, type, cu);
15545 }
15546
c0dd20ea 15547 type = alloc_type (objfile);
c906108c 15548 INIT_CPLUS_SPECIFIC (type);
93311388 15549
39cbfefa
DJ
15550 name = dwarf2_name (die, cu);
15551 if (name != NULL)
c906108c 15552 {
987504bb 15553 if (cu->language == language_cplus
c44af4eb
TT
15554 || cu->language == language_d
15555 || cu->language == language_rust)
63d06c5c 15556 {
15d034d0 15557 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15558
15559 /* dwarf2_full_name might have already finished building the DIE's
15560 type. If so, there is no need to continue. */
15561 if (get_die_type (die, cu) != NULL)
15562 return get_die_type (die, cu);
15563
d0e39ea2 15564 type->set_name (full_name);
63d06c5c
DC
15565 }
15566 else
15567 {
d8151005
DJ
15568 /* The name is already allocated along with this objfile, so
15569 we don't need to duplicate it for the type. */
d0e39ea2 15570 type->set_name (name);
63d06c5c 15571 }
c906108c
SS
15572 }
15573
15574 if (die->tag == DW_TAG_structure_type)
15575 {
67607e24 15576 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15577 }
15578 else if (die->tag == DW_TAG_union_type)
15579 {
67607e24 15580 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15581 }
15582 else
15583 {
67607e24 15584 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15585 }
15586
0cc2414c
TT
15587 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15588 TYPE_DECLARED_CLASS (type) = 1;
15589
e35000a7
TBA
15590 /* Store the calling convention in the type if it's available in
15591 the die. Otherwise the calling convention remains set to
15592 the default value DW_CC_normal. */
15593 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15594 if (attr != nullptr
15595 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15596 {
15597 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15598 TYPE_CPLUS_CALLING_CONVENTION (type)
15599 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15600 }
15601
e142c38c 15602 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15603 if (attr != nullptr)
c906108c 15604 {
cd6c91b4 15605 if (attr->form_is_constant ())
155bfbd3
JB
15606 TYPE_LENGTH (type) = DW_UNSND (attr);
15607 else
15608 {
f8e89861 15609 struct dynamic_prop prop;
293e7e51 15610 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 15611 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
155bfbd3
JB
15612 TYPE_LENGTH (type) = 0;
15613 }
c906108c
SS
15614 }
15615 else
15616 {
15617 TYPE_LENGTH (type) = 0;
15618 }
15619
2b4424c3
TT
15620 maybe_set_alignment (cu, die, type);
15621
5230b05a 15622 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15623 {
5230b05a
WT
15624 /* ICC<14 does not output the required DW_AT_declaration on
15625 incomplete types, but gives them a size of zero. */
422b1cb0 15626 TYPE_STUB (type) = 1;
685b1105
JK
15627 }
15628 else
15629 TYPE_STUB_SUPPORTED (type) = 1;
15630
dc718098 15631 if (die_is_declaration (die, cu))
876cecd0 15632 TYPE_STUB (type) = 1;
a6c727b2
DJ
15633 else if (attr == NULL && die->child == NULL
15634 && producer_is_realview (cu->producer))
15635 /* RealView does not output the required DW_AT_declaration
15636 on incomplete types. */
15637 TYPE_STUB (type) = 1;
dc718098 15638
c906108c
SS
15639 /* We need to add the type field to the die immediately so we don't
15640 infinitely recurse when dealing with pointers to the structure
0963b4bd 15641 type within the structure itself. */
1c379e20 15642 set_die_type (die, type, cu);
c906108c 15643
7e314c57
JK
15644 /* set_die_type should be already done. */
15645 set_descriptive_type (type, die, cu);
15646
c767944b
DJ
15647 return type;
15648}
15649
9c6a1327
TT
15650static void handle_struct_member_die
15651 (struct die_info *child_die,
15652 struct type *type,
15653 struct field_info *fi,
15654 std::vector<struct symbol *> *template_args,
15655 struct dwarf2_cu *cu);
15656
15657/* A helper for handle_struct_member_die that handles
15658 DW_TAG_variant_part. */
15659
15660static void
15661handle_variant_part (struct die_info *die, struct type *type,
15662 struct field_info *fi,
15663 std::vector<struct symbol *> *template_args,
15664 struct dwarf2_cu *cu)
15665{
15666 variant_part_builder *new_part;
15667 if (fi->current_variant_part == nullptr)
15668 {
15669 fi->variant_parts.emplace_back ();
15670 new_part = &fi->variant_parts.back ();
15671 }
15672 else if (!fi->current_variant_part->processing_variant)
15673 {
15674 complaint (_("nested DW_TAG_variant_part seen "
15675 "- DIE at %s [in module %s]"),
15676 sect_offset_str (die->sect_off),
5e22e966 15677 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15678 return;
15679 }
15680 else
15681 {
15682 variant_field &current = fi->current_variant_part->variants.back ();
15683 current.variant_parts.emplace_back ();
15684 new_part = &current.variant_parts.back ();
15685 }
15686
15687 /* When we recurse, we want callees to add to this new variant
15688 part. */
15689 scoped_restore save_current_variant_part
15690 = make_scoped_restore (&fi->current_variant_part, new_part);
15691
15692 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15693 if (discr == NULL)
15694 {
15695 /* It's a univariant form, an extension we support. */
15696 }
15697 else if (discr->form_is_ref ())
15698 {
15699 struct dwarf2_cu *target_cu = cu;
15700 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15701
15702 new_part->discriminant_offset = target_die->sect_off;
15703 }
15704 else
15705 {
15706 complaint (_("DW_AT_discr does not have DIE reference form"
15707 " - DIE at %s [in module %s]"),
15708 sect_offset_str (die->sect_off),
5e22e966 15709 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15710 }
15711
15712 for (die_info *child_die = die->child;
15713 child_die != NULL;
15714 child_die = child_die->sibling)
15715 handle_struct_member_die (child_die, type, fi, template_args, cu);
15716}
15717
15718/* A helper for handle_struct_member_die that handles
15719 DW_TAG_variant. */
15720
15721static void
15722handle_variant (struct die_info *die, struct type *type,
15723 struct field_info *fi,
15724 std::vector<struct symbol *> *template_args,
15725 struct dwarf2_cu *cu)
15726{
15727 if (fi->current_variant_part == nullptr)
15728 {
15729 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15730 "- DIE at %s [in module %s]"),
15731 sect_offset_str (die->sect_off),
5e22e966 15732 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15733 return;
15734 }
15735 if (fi->current_variant_part->processing_variant)
15736 {
15737 complaint (_("nested DW_TAG_variant seen "
15738 "- DIE at %s [in module %s]"),
15739 sect_offset_str (die->sect_off),
5e22e966 15740 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15741 return;
15742 }
15743
15744 scoped_restore save_processing_variant
15745 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15746 true);
15747
15748 fi->current_variant_part->variants.emplace_back ();
15749 variant_field &variant = fi->current_variant_part->variants.back ();
15750 variant.first_field = fi->fields.size ();
15751
15752 /* In a variant we want to get the discriminant and also add a
15753 field for our sole member child. */
15754 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15755 if (discr == nullptr)
15756 {
15757 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15758 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15759 variant.default_branch = true;
15760 else
15761 variant.discr_list_data = DW_BLOCK (discr);
15762 }
15763 else
15764 variant.discriminant_value = DW_UNSND (discr);
15765
15766 for (die_info *variant_child = die->child;
15767 variant_child != NULL;
15768 variant_child = variant_child->sibling)
15769 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15770
15771 variant.last_field = fi->fields.size ();
15772}
15773
2ddeaf8a
TT
15774/* A helper for process_structure_scope that handles a single member
15775 DIE. */
15776
15777static void
15778handle_struct_member_die (struct die_info *child_die, struct type *type,
15779 struct field_info *fi,
15780 std::vector<struct symbol *> *template_args,
15781 struct dwarf2_cu *cu)
15782{
15783 if (child_die->tag == DW_TAG_member
9c6a1327 15784 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
15785 {
15786 /* NOTE: carlton/2002-11-05: A C++ static data member
15787 should be a DW_TAG_member that is a declaration, but
15788 all versions of G++ as of this writing (so through at
15789 least 3.2.1) incorrectly generate DW_TAG_variable
15790 tags for them instead. */
15791 dwarf2_add_field (fi, child_die, cu);
15792 }
15793 else if (child_die->tag == DW_TAG_subprogram)
15794 {
15795 /* Rust doesn't have member functions in the C++ sense.
15796 However, it does emit ordinary functions as children
15797 of a struct DIE. */
15798 if (cu->language == language_rust)
15799 read_func_scope (child_die, cu);
15800 else
15801 {
15802 /* C++ member function. */
15803 dwarf2_add_member_fn (fi, child_die, type, cu);
15804 }
15805 }
15806 else if (child_die->tag == DW_TAG_inheritance)
15807 {
15808 /* C++ base class field. */
15809 dwarf2_add_field (fi, child_die, cu);
15810 }
15811 else if (type_can_define_types (child_die))
15812 dwarf2_add_type_defn (fi, child_die, cu);
15813 else if (child_die->tag == DW_TAG_template_type_param
15814 || child_die->tag == DW_TAG_template_value_param)
15815 {
15816 struct symbol *arg = new_symbol (child_die, NULL, cu);
15817
15818 if (arg != NULL)
15819 template_args->push_back (arg);
15820 }
9c6a1327
TT
15821 else if (child_die->tag == DW_TAG_variant_part)
15822 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 15823 else if (child_die->tag == DW_TAG_variant)
9c6a1327 15824 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
15825}
15826
c767944b
DJ
15827/* Finish creating a structure or union type, including filling in
15828 its members and creating a symbol for it. */
15829
15830static void
15831process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15832{
5e22e966 15833 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 15834 struct die_info *child_die;
c767944b
DJ
15835 struct type *type;
15836
15837 type = get_die_type (die, cu);
15838 if (type == NULL)
15839 type = read_structure_type (die, cu);
15840
3e1d3d8c 15841 bool has_template_parameters = false;
e142c38c 15842 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15843 {
15844 struct field_info fi;
2f4732b0 15845 std::vector<struct symbol *> template_args;
c906108c 15846
639d11d3 15847 child_die = die->child;
c906108c
SS
15848
15849 while (child_die && child_die->tag)
15850 {
2ddeaf8a 15851 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 15852 child_die = child_die->sibling;
c906108c
SS
15853 }
15854
34eaf542 15855 /* Attach template arguments to type. */
2f4732b0 15856 if (!template_args.empty ())
34eaf542 15857 {
3e1d3d8c 15858 has_template_parameters = true;
34eaf542 15859 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15860 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15861 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15862 = XOBNEWVEC (&objfile->objfile_obstack,
15863 struct symbol *,
15864 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15865 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15866 template_args.data (),
34eaf542
TT
15867 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15868 * sizeof (struct symbol *)));
34eaf542
TT
15869 }
15870
c906108c 15871 /* Attach fields and member functions to the type. */
317f7127 15872 if (fi.nfields () > 0)
e7c27a73 15873 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15874 if (!fi.fnfieldlists.empty ())
c906108c 15875 {
e7c27a73 15876 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15877
c5aa993b 15878 /* Get the type which refers to the base class (possibly this
c906108c 15879 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15880 class from the DW_AT_containing_type attribute. This use of
15881 DW_AT_containing_type is a GNU extension. */
c906108c 15882
e142c38c 15883 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15884 {
e7c27a73 15885 struct type *t = die_containing_type (die, cu);
c906108c 15886
ae6ae975 15887 set_type_vptr_basetype (type, t);
c906108c
SS
15888 if (type == t)
15889 {
c906108c
SS
15890 int i;
15891
15892 /* Our own class provides vtbl ptr. */
1f704f76 15893 for (i = t->num_fields () - 1;
c906108c
SS
15894 i >= TYPE_N_BASECLASSES (t);
15895 --i)
15896 {
0d5cff50 15897 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15898
1168df01 15899 if (is_vtable_name (fieldname, cu))
c906108c 15900 {
ae6ae975 15901 set_type_vptr_fieldno (type, i);
c906108c
SS
15902 break;
15903 }
15904 }
15905
15906 /* Complain if virtual function table field not found. */
15907 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15908 complaint (_("virtual function table pointer "
3e43a32a 15909 "not found when defining class '%s'"),
7d93a1e0 15910 type->name () ? type->name () : "");
c906108c
SS
15911 }
15912 else
15913 {
ae6ae975 15914 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15915 }
15916 }
f6235d4c 15917 else if (cu->producer
61012eef 15918 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15919 {
15920 /* The IBM XLC compiler does not provide direct indication
15921 of the containing type, but the vtable pointer is
15922 always named __vfp. */
15923
15924 int i;
15925
1f704f76 15926 for (i = type->num_fields () - 1;
f6235d4c
EZ
15927 i >= TYPE_N_BASECLASSES (type);
15928 --i)
15929 {
15930 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15931 {
ae6ae975
DE
15932 set_type_vptr_fieldno (type, i);
15933 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15934 break;
15935 }
15936 }
15937 }
c906108c 15938 }
98751a41
JK
15939
15940 /* Copy fi.typedef_field_list linked list elements content into the
15941 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15942 if (!fi.typedef_field_list.empty ())
98751a41 15943 {
be2daae6 15944 int count = fi.typedef_field_list.size ();
98751a41 15945
a0d7a4ff 15946 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15947 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15948 = ((struct decl_field *)
be2daae6
TT
15949 TYPE_ALLOC (type,
15950 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15951 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15952
be2daae6
TT
15953 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15954 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15955 }
c767944b 15956
883fd55a
KS
15957 /* Copy fi.nested_types_list linked list elements content into the
15958 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15959 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15960 {
be2daae6 15961 int count = fi.nested_types_list.size ();
883fd55a
KS
15962
15963 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15964 TYPE_NESTED_TYPES_ARRAY (type)
15965 = ((struct decl_field *)
be2daae6
TT
15966 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15967 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15968
be2daae6
TT
15969 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15970 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15971 }
c906108c 15972 }
63d06c5c 15973
bb5ed363 15974 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15975 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15976 cu->rust_unions.push_back (type);
0b92b5bb 15977
90aeadfc
DC
15978 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15979 snapshots) has been known to create a die giving a declaration
15980 for a class that has, as a child, a die giving a definition for a
15981 nested class. So we have to process our children even if the
15982 current die is a declaration. Normally, of course, a declaration
15983 won't have any children at all. */
134d01f1 15984
ca040673
DE
15985 child_die = die->child;
15986
90aeadfc
DC
15987 while (child_die != NULL && child_die->tag)
15988 {
15989 if (child_die->tag == DW_TAG_member
15990 || child_die->tag == DW_TAG_variable
34eaf542
TT
15991 || child_die->tag == DW_TAG_inheritance
15992 || child_die->tag == DW_TAG_template_value_param
15993 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15994 {
90aeadfc 15995 /* Do nothing. */
134d01f1 15996 }
90aeadfc
DC
15997 else
15998 process_die (child_die, cu);
134d01f1 15999
436c571c 16000 child_die = child_die->sibling;
134d01f1
DJ
16001 }
16002
fa4028e9
JB
16003 /* Do not consider external references. According to the DWARF standard,
16004 these DIEs are identified by the fact that they have no byte_size
16005 attribute, and a declaration attribute. */
16006 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
16007 || !die_is_declaration (die, cu)
16008 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
16009 {
16010 struct symbol *sym = new_symbol (die, type, cu);
16011
16012 if (has_template_parameters)
16013 {
a776957c
TT
16014 struct symtab *symtab;
16015 if (sym != nullptr)
16016 symtab = symbol_symtab (sym);
16017 else if (cu->line_header != nullptr)
16018 {
16019 /* Any related symtab will do. */
16020 symtab
7ba99d21 16021 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16022 }
16023 else
16024 {
16025 symtab = nullptr;
16026 complaint (_("could not find suitable "
16027 "symtab for template parameter"
16028 " - DIE at %s [in module %s]"),
16029 sect_offset_str (die->sect_off),
16030 objfile_name (objfile));
16031 }
16032
16033 if (symtab != nullptr)
16034 {
16035 /* Make sure that the symtab is set on the new symbols.
16036 Even though they don't appear in this symtab directly,
16037 other parts of gdb assume that symbols do, and this is
16038 reasonably true. */
16039 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16040 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16041 }
3e1d3d8c
TT
16042 }
16043 }
134d01f1
DJ
16044}
16045
ed6acedd
TT
16046/* Assuming DIE is an enumeration type, and TYPE is its associated
16047 type, update TYPE using some information only available in DIE's
16048 children. In particular, the fields are computed. */
55426c9d
JB
16049
16050static void
16051update_enumeration_type_from_children (struct die_info *die,
16052 struct type *type,
16053 struct dwarf2_cu *cu)
16054{
60f7655a 16055 struct die_info *child_die;
55426c9d
JB
16056 int unsigned_enum = 1;
16057 int flag_enum = 1;
55426c9d 16058
8268c778 16059 auto_obstack obstack;
ed6acedd 16060 std::vector<struct field> fields;
55426c9d 16061
60f7655a
DE
16062 for (child_die = die->child;
16063 child_die != NULL && child_die->tag;
436c571c 16064 child_die = child_die->sibling)
55426c9d
JB
16065 {
16066 struct attribute *attr;
16067 LONGEST value;
16068 const gdb_byte *bytes;
16069 struct dwarf2_locexpr_baton *baton;
16070 const char *name;
60f7655a 16071
55426c9d
JB
16072 if (child_die->tag != DW_TAG_enumerator)
16073 continue;
16074
16075 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16076 if (attr == NULL)
16077 continue;
16078
16079 name = dwarf2_name (child_die, cu);
16080 if (name == NULL)
16081 name = "<anonymous enumerator>";
16082
16083 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16084 &value, &bytes, &baton);
16085 if (value < 0)
16086 {
16087 unsigned_enum = 0;
16088 flag_enum = 0;
16089 }
55426c9d 16090 else
edd45eb0
SM
16091 {
16092 if (count_one_bits_ll (value) >= 2)
16093 flag_enum = 0;
edd45eb0 16094 }
55426c9d 16095
ed6acedd
TT
16096 fields.emplace_back ();
16097 struct field &field = fields.back ();
16098 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16099 SET_FIELD_ENUMVAL (field, value);
16100 }
16101
16102 if (!fields.empty ())
16103 {
5e33d5f4 16104 type->set_num_fields (fields.size ());
3cabb6b0
SM
16105 type->set_fields
16106 ((struct field *)
16107 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16108 memcpy (type->fields (), fields.data (),
ed6acedd 16109 sizeof (struct field) * fields.size ());
55426c9d
JB
16110 }
16111
16112 if (unsigned_enum)
16113 TYPE_UNSIGNED (type) = 1;
16114 if (flag_enum)
16115 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16116}
16117
134d01f1
DJ
16118/* Given a DW_AT_enumeration_type die, set its type. We do not
16119 complete the type's fields yet, or create any symbols. */
c906108c 16120
f792889a 16121static struct type *
134d01f1 16122read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16123{
5e22e966 16124 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16125 struct type *type;
c906108c 16126 struct attribute *attr;
0114d602 16127 const char *name;
134d01f1 16128
348e048f
DE
16129 /* If the definition of this type lives in .debug_types, read that type.
16130 Don't follow DW_AT_specification though, that will take us back up
16131 the chain and we want to go down. */
052c8bb8 16132 attr = die->attr (DW_AT_signature);
435d3d88 16133 if (attr != nullptr)
348e048f 16134 {
ac9ec31b 16135 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16136
ac9ec31b 16137 /* The type's CU may not be the same as CU.
02142a6c 16138 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16139 return set_die_type (die, type, cu);
16140 }
16141
c906108c
SS
16142 type = alloc_type (objfile);
16143
67607e24 16144 type->set_code (TYPE_CODE_ENUM);
94af9270 16145 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16146 if (name != NULL)
d0e39ea2 16147 type->set_name (name);
c906108c 16148
0626fc76
TT
16149 attr = dwarf2_attr (die, DW_AT_type, cu);
16150 if (attr != NULL)
16151 {
16152 struct type *underlying_type = die_type (die, cu);
16153
16154 TYPE_TARGET_TYPE (type) = underlying_type;
16155 }
16156
e142c38c 16157 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16158 if (attr != nullptr)
c906108c
SS
16159 {
16160 TYPE_LENGTH (type) = DW_UNSND (attr);
16161 }
16162 else
16163 {
16164 TYPE_LENGTH (type) = 0;
16165 }
16166
2b4424c3
TT
16167 maybe_set_alignment (cu, die, type);
16168
137033e9
JB
16169 /* The enumeration DIE can be incomplete. In Ada, any type can be
16170 declared as private in the package spec, and then defined only
16171 inside the package body. Such types are known as Taft Amendment
16172 Types. When another package uses such a type, an incomplete DIE
16173 may be generated by the compiler. */
02eb380e 16174 if (die_is_declaration (die, cu))
876cecd0 16175 TYPE_STUB (type) = 1;
02eb380e 16176
0626fc76
TT
16177 /* If this type has an underlying type that is not a stub, then we
16178 may use its attributes. We always use the "unsigned" attribute
16179 in this situation, because ordinarily we guess whether the type
16180 is unsigned -- but the guess can be wrong and the underlying type
16181 can tell us the reality. However, we defer to a local size
16182 attribute if one exists, because this lets the compiler override
16183 the underlying type if needed. */
16184 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16185 {
9e7c9a03
HD
16186 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16187 underlying_type = check_typedef (underlying_type);
16188 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
0626fc76 16189 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16190 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
2b4424c3 16191 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16192 && TYPE_RAW_ALIGN (underlying_type) != 0)
16193 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16194 }
16195
3d567982
TT
16196 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16197
ed6acedd
TT
16198 set_die_type (die, type, cu);
16199
16200 /* Finish the creation of this type by using the enum's children.
16201 Note that, as usual, this must come after set_die_type to avoid
16202 infinite recursion when trying to compute the names of the
16203 enumerators. */
16204 update_enumeration_type_from_children (die, type, cu);
16205
16206 return type;
134d01f1
DJ
16207}
16208
16209/* Given a pointer to a die which begins an enumeration, process all
16210 the dies that define the members of the enumeration, and create the
16211 symbol for the enumeration type.
16212
16213 NOTE: We reverse the order of the element list. */
16214
16215static void
16216process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16217{
f792889a 16218 struct type *this_type;
134d01f1 16219
f792889a
DJ
16220 this_type = get_die_type (die, cu);
16221 if (this_type == NULL)
16222 this_type = read_enumeration_type (die, cu);
9dc481d3 16223
639d11d3 16224 if (die->child != NULL)
c906108c 16225 {
9dc481d3 16226 struct die_info *child_die;
15d034d0 16227 const char *name;
9dc481d3 16228
639d11d3 16229 child_die = die->child;
c906108c
SS
16230 while (child_die && child_die->tag)
16231 {
16232 if (child_die->tag != DW_TAG_enumerator)
16233 {
e7c27a73 16234 process_die (child_die, cu);
c906108c
SS
16235 }
16236 else
16237 {
39cbfefa
DJ
16238 name = dwarf2_name (child_die, cu);
16239 if (name)
ed6acedd 16240 new_symbol (child_die, this_type, cu);
c906108c
SS
16241 }
16242
436c571c 16243 child_die = child_die->sibling;
c906108c 16244 }
c906108c 16245 }
134d01f1 16246
6c83ed52
TT
16247 /* If we are reading an enum from a .debug_types unit, and the enum
16248 is a declaration, and the enum is not the signatured type in the
16249 unit, then we do not want to add a symbol for it. Adding a
16250 symbol would in some cases obscure the true definition of the
16251 enum, giving users an incomplete type when the definition is
16252 actually available. Note that we do not want to do this for all
16253 enums which are just declarations, because C++0x allows forward
16254 enum declarations. */
3019eac3 16255 if (cu->per_cu->is_debug_types
6c83ed52
TT
16256 && die_is_declaration (die, cu))
16257 {
52dc124a 16258 struct signatured_type *sig_type;
6c83ed52 16259
c0f78cd4 16260 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16261 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16262 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16263 return;
16264 }
16265
f792889a 16266 new_symbol (die, this_type, cu);
c906108c
SS
16267}
16268
16269/* Extract all information from a DW_TAG_array_type DIE and put it in
16270 the DIE's type field. For now, this only handles one dimensional
16271 arrays. */
16272
f792889a 16273static struct type *
e7c27a73 16274read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16275{
5e22e966 16276 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16277 struct die_info *child_die;
7e314c57 16278 struct type *type;
c906108c 16279 struct type *element_type, *range_type, *index_type;
c906108c 16280 struct attribute *attr;
15d034d0 16281 const char *name;
a405673c 16282 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16283 unsigned int bit_stride = 0;
c906108c 16284
e7c27a73 16285 element_type = die_type (die, cu);
c906108c 16286
7e314c57
JK
16287 /* The die_type call above may have already set the type for this DIE. */
16288 type = get_die_type (die, cu);
16289 if (type)
16290 return type;
16291
dc53a7ad
JB
16292 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16293 if (attr != NULL)
a405673c
JB
16294 {
16295 int stride_ok;
293e7e51 16296 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
16297
16298 byte_stride_prop
16299 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16300 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16301 prop_type);
a405673c
JB
16302 if (!stride_ok)
16303 {
b98664d3 16304 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16305 " - DIE at %s [in module %s]"),
16306 sect_offset_str (die->sect_off),
5e22e966 16307 objfile_name (cu->per_objfile->objfile));
a405673c
JB
16308 /* Ignore this attribute. We will likely not be able to print
16309 arrays of this type correctly, but there is little we can do
16310 to help if we cannot read the attribute's value. */
16311 byte_stride_prop = NULL;
16312 }
16313 }
dc53a7ad
JB
16314
16315 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16316 if (attr != NULL)
16317 bit_stride = DW_UNSND (attr);
16318
c906108c
SS
16319 /* Irix 6.2 native cc creates array types without children for
16320 arrays with unspecified length. */
639d11d3 16321 if (die->child == NULL)
c906108c 16322 {
46bf5051 16323 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16324 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16325 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16326 byte_stride_prop, bit_stride);
f792889a 16327 return set_die_type (die, type, cu);
c906108c
SS
16328 }
16329
791afaa2 16330 std::vector<struct type *> range_types;
639d11d3 16331 child_die = die->child;
c906108c
SS
16332 while (child_die && child_die->tag)
16333 {
16334 if (child_die->tag == DW_TAG_subrange_type)
16335 {
f792889a 16336 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16337
f792889a 16338 if (child_type != NULL)
a02abb62 16339 {
0963b4bd
MS
16340 /* The range type was succesfully read. Save it for the
16341 array type creation. */
791afaa2 16342 range_types.push_back (child_type);
a02abb62 16343 }
c906108c 16344 }
436c571c 16345 child_die = child_die->sibling;
c906108c
SS
16346 }
16347
16348 /* Dwarf2 dimensions are output from left to right, create the
16349 necessary array types in backwards order. */
7ca2d3a3 16350
c906108c 16351 type = element_type;
7ca2d3a3
DL
16352
16353 if (read_array_order (die, cu) == DW_ORD_col_major)
16354 {
16355 int i = 0;
9a619af0 16356
791afaa2 16357 while (i < range_types.size ())
dc53a7ad 16358 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16359 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16360 }
16361 else
16362 {
791afaa2 16363 size_t ndim = range_types.size ();
7ca2d3a3 16364 while (ndim-- > 0)
dc53a7ad 16365 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16366 byte_stride_prop, bit_stride);
7ca2d3a3 16367 }
c906108c 16368
f5f8a009
EZ
16369 /* Understand Dwarf2 support for vector types (like they occur on
16370 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16371 array type. This is not part of the Dwarf2/3 standard yet, but a
16372 custom vendor extension. The main difference between a regular
16373 array and the vector variant is that vectors are passed by value
16374 to functions. */
e142c38c 16375 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16376 if (attr != nullptr)
ea37ba09 16377 make_vector_type (type);
f5f8a009 16378
dbc98a8b
KW
16379 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16380 implementation may choose to implement triple vectors using this
16381 attribute. */
16382 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16383 if (attr != nullptr)
dbc98a8b
KW
16384 {
16385 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16386 TYPE_LENGTH (type) = DW_UNSND (attr);
16387 else
b98664d3 16388 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16389 "than the total size of elements"));
dbc98a8b
KW
16390 }
16391
39cbfefa
DJ
16392 name = dwarf2_name (die, cu);
16393 if (name)
d0e39ea2 16394 type->set_name (name);
6e70227d 16395
2b4424c3
TT
16396 maybe_set_alignment (cu, die, type);
16397
0963b4bd 16398 /* Install the type in the die. */
7e314c57
JK
16399 set_die_type (die, type, cu);
16400
16401 /* set_die_type should be already done. */
b4ba55a1
JB
16402 set_descriptive_type (type, die, cu);
16403
7e314c57 16404 return type;
c906108c
SS
16405}
16406
7ca2d3a3 16407static enum dwarf_array_dim_ordering
6e70227d 16408read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16409{
16410 struct attribute *attr;
16411
16412 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16413
435d3d88 16414 if (attr != nullptr)
aead7601 16415 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16416
0963b4bd
MS
16417 /* GNU F77 is a special case, as at 08/2004 array type info is the
16418 opposite order to the dwarf2 specification, but data is still
16419 laid out as per normal fortran.
7ca2d3a3 16420
0963b4bd
MS
16421 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16422 version checking. */
7ca2d3a3 16423
905e0470
PM
16424 if (cu->language == language_fortran
16425 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16426 {
16427 return DW_ORD_row_major;
16428 }
16429
6e70227d 16430 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16431 {
16432 case array_column_major:
16433 return DW_ORD_col_major;
16434 case array_row_major:
16435 default:
16436 return DW_ORD_row_major;
16437 };
16438}
16439
72019c9c 16440/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16441 the DIE's type field. */
72019c9c 16442
f792889a 16443static struct type *
72019c9c
GM
16444read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16445{
7e314c57
JK
16446 struct type *domain_type, *set_type;
16447 struct attribute *attr;
f792889a 16448
7e314c57
JK
16449 domain_type = die_type (die, cu);
16450
16451 /* The die_type call above may have already set the type for this DIE. */
16452 set_type = get_die_type (die, cu);
16453 if (set_type)
16454 return set_type;
16455
16456 set_type = create_set_type (NULL, domain_type);
16457
16458 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16459 if (attr != nullptr)
d09039dd 16460 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16461
2b4424c3
TT
16462 maybe_set_alignment (cu, die, set_type);
16463
f792889a 16464 return set_die_type (die, set_type, cu);
72019c9c 16465}
7ca2d3a3 16466
0971de02
TT
16467/* A helper for read_common_block that creates a locexpr baton.
16468 SYM is the symbol which we are marking as computed.
16469 COMMON_DIE is the DIE for the common block.
16470 COMMON_LOC is the location expression attribute for the common
16471 block itself.
16472 MEMBER_LOC is the location expression attribute for the particular
16473 member of the common block that we are processing.
16474 CU is the CU from which the above come. */
16475
16476static void
16477mark_common_block_symbol_computed (struct symbol *sym,
16478 struct die_info *common_die,
16479 struct attribute *common_loc,
16480 struct attribute *member_loc,
16481 struct dwarf2_cu *cu)
16482{
5e22e966 16483 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 16484 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
16485 struct dwarf2_locexpr_baton *baton;
16486 gdb_byte *ptr;
16487 unsigned int cu_off;
08feed99 16488 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
16489 LONGEST offset = 0;
16490
16491 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16492 gdb_assert (common_loc->form_is_block ());
16493 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16494 || member_loc->form_is_constant ());
0971de02 16495
8d749320 16496 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 16497 baton->per_objfile = per_objfile;
0971de02
TT
16498 baton->per_cu = cu->per_cu;
16499 gdb_assert (baton->per_cu);
16500
16501 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16502
cd6c91b4 16503 if (member_loc->form_is_constant ())
0971de02 16504 {
0826b30a 16505 offset = member_loc->constant_value (0);
0971de02
TT
16506 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16507 }
16508 else
16509 baton->size += DW_BLOCK (member_loc)->size;
16510
224c3ddb 16511 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16512 baton->data = ptr;
16513
16514 *ptr++ = DW_OP_call4;
9c541725 16515 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16516 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16517 ptr += 4;
16518
cd6c91b4 16519 if (member_loc->form_is_constant ())
0971de02
TT
16520 {
16521 *ptr++ = DW_OP_addr;
16522 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16523 ptr += cu->header.addr_size;
16524 }
16525 else
16526 {
16527 /* We have to copy the data here, because DW_OP_call4 will only
16528 use a DW_AT_location attribute. */
16529 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16530 ptr += DW_BLOCK (member_loc)->size;
16531 }
16532
16533 *ptr++ = DW_OP_plus;
16534 gdb_assert (ptr - baton->data == baton->size);
16535
0971de02 16536 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16537 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16538}
16539
4357ac6c
TT
16540/* Create appropriate locally-scoped variables for all the
16541 DW_TAG_common_block entries. Also create a struct common_block
16542 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16543 is used to separate the common blocks name namespace from regular
4357ac6c 16544 variable names. */
c906108c
SS
16545
16546static void
e7c27a73 16547read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16548{
0971de02
TT
16549 struct attribute *attr;
16550
16551 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16552 if (attr != nullptr)
0971de02
TT
16553 {
16554 /* Support the .debug_loc offsets. */
4fc6c0d5 16555 if (attr->form_is_block ())
0971de02
TT
16556 {
16557 /* Ok. */
16558 }
cd6c91b4 16559 else if (attr->form_is_section_offset ())
0971de02
TT
16560 {
16561 dwarf2_complex_location_expr_complaint ();
16562 attr = NULL;
16563 }
16564 else
16565 {
16566 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16567 "common block member");
16568 attr = NULL;
16569 }
16570 }
16571
639d11d3 16572 if (die->child != NULL)
c906108c 16573 {
5e22e966 16574 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
16575 struct die_info *child_die;
16576 size_t n_entries = 0, size;
16577 struct common_block *common_block;
16578 struct symbol *sym;
74ac6d43 16579
4357ac6c
TT
16580 for (child_die = die->child;
16581 child_die && child_die->tag;
436c571c 16582 child_die = child_die->sibling)
4357ac6c
TT
16583 ++n_entries;
16584
16585 size = (sizeof (struct common_block)
16586 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16587 common_block
16588 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16589 size);
4357ac6c
TT
16590 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16591 common_block->n_entries = 0;
16592
16593 for (child_die = die->child;
16594 child_die && child_die->tag;
436c571c 16595 child_die = child_die->sibling)
4357ac6c
TT
16596 {
16597 /* Create the symbol in the DW_TAG_common_block block in the current
16598 symbol scope. */
e7c27a73 16599 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16600 if (sym != NULL)
16601 {
16602 struct attribute *member_loc;
16603
16604 common_block->contents[common_block->n_entries++] = sym;
16605
16606 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16607 cu);
16608 if (member_loc)
16609 {
16610 /* GDB has handled this for a long time, but it is
16611 not specified by DWARF. It seems to have been
16612 emitted by gfortran at least as recently as:
16613 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16614 complaint (_("Variable in common block has "
0971de02 16615 "DW_AT_data_member_location "
9d8780f0
SM
16616 "- DIE at %s [in module %s]"),
16617 sect_offset_str (child_die->sect_off),
518817b3 16618 objfile_name (objfile));
0971de02 16619
cd6c91b4 16620 if (member_loc->form_is_section_offset ())
0971de02 16621 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16622 else if (member_loc->form_is_constant ()
4fc6c0d5 16623 || member_loc->form_is_block ())
0971de02 16624 {
435d3d88 16625 if (attr != nullptr)
0971de02
TT
16626 mark_common_block_symbol_computed (sym, die, attr,
16627 member_loc, cu);
16628 }
16629 else
16630 dwarf2_complex_location_expr_complaint ();
16631 }
16632 }
c906108c 16633 }
4357ac6c
TT
16634
16635 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16636 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16637 }
16638}
16639
0114d602 16640/* Create a type for a C++ namespace. */
d9fa45fe 16641
0114d602
DJ
16642static struct type *
16643read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16644{
5e22e966 16645 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16646 const char *previous_prefix, *name;
9219021c 16647 int is_anonymous;
0114d602
DJ
16648 struct type *type;
16649
16650 /* For extensions, reuse the type of the original namespace. */
16651 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16652 {
16653 struct die_info *ext_die;
16654 struct dwarf2_cu *ext_cu = cu;
9a619af0 16655
0114d602
DJ
16656 ext_die = dwarf2_extension (die, &ext_cu);
16657 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16658
16659 /* EXT_CU may not be the same as CU.
02142a6c 16660 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16661 return set_die_type (die, type, cu);
16662 }
9219021c 16663
e142c38c 16664 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16665
16666 /* Now build the name of the current namespace. */
16667
0114d602
DJ
16668 previous_prefix = determine_prefix (die, cu);
16669 if (previous_prefix[0] != '\0')
16670 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16671 previous_prefix, name, 0, cu);
0114d602
DJ
16672
16673 /* Create the type. */
19f392bc 16674 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16675
60531b24 16676 return set_die_type (die, type, cu);
0114d602
DJ
16677}
16678
22cee43f 16679/* Read a namespace scope. */
0114d602
DJ
16680
16681static void
16682read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16683{
5e22e966 16684 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16685 int is_anonymous;
9219021c 16686
5c4e30ca
DC
16687 /* Add a symbol associated to this if we haven't seen the namespace
16688 before. Also, add a using directive if it's an anonymous
16689 namespace. */
9219021c 16690
f2f0e013 16691 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16692 {
16693 struct type *type;
16694
0114d602 16695 type = read_type_die (die, cu);
e7c27a73 16696 new_symbol (die, type, cu);
5c4e30ca 16697
e8e80198 16698 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16699 if (is_anonymous)
0114d602
DJ
16700 {
16701 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16702
eb1e02fd 16703 std::vector<const char *> excludes;
804d2729 16704 add_using_directive (using_directives (cu),
7d93a1e0 16705 previous_prefix, type->name (), NULL,
eb1e02fd 16706 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16707 }
5c4e30ca 16708 }
9219021c 16709
639d11d3 16710 if (die->child != NULL)
d9fa45fe 16711 {
639d11d3 16712 struct die_info *child_die = die->child;
6e70227d 16713
d9fa45fe
DC
16714 while (child_die && child_die->tag)
16715 {
e7c27a73 16716 process_die (child_die, cu);
436c571c 16717 child_die = child_die->sibling;
d9fa45fe
DC
16718 }
16719 }
38d518c9
EZ
16720}
16721
f55ee35c
JK
16722/* Read a Fortran module as type. This DIE can be only a declaration used for
16723 imported module. Still we need that type as local Fortran "use ... only"
16724 declaration imports depend on the created type in determine_prefix. */
16725
16726static struct type *
16727read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16728{
5e22e966 16729 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 16730 const char *module_name;
f55ee35c
JK
16731 struct type *type;
16732
16733 module_name = dwarf2_name (die, cu);
19f392bc 16734 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16735
f55ee35c
JK
16736 return set_die_type (die, type, cu);
16737}
16738
5d7cb8df
JK
16739/* Read a Fortran module. */
16740
16741static void
16742read_module (struct die_info *die, struct dwarf2_cu *cu)
16743{
16744 struct die_info *child_die = die->child;
530e8392
KB
16745 struct type *type;
16746
16747 type = read_type_die (die, cu);
16748 new_symbol (die, type, cu);
5d7cb8df 16749
5d7cb8df
JK
16750 while (child_die && child_die->tag)
16751 {
16752 process_die (child_die, cu);
436c571c 16753 child_die = child_die->sibling;
5d7cb8df
JK
16754 }
16755}
16756
38d518c9
EZ
16757/* Return the name of the namespace represented by DIE. Set
16758 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16759 namespace. */
16760
16761static const char *
e142c38c 16762namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16763{
16764 struct die_info *current_die;
16765 const char *name = NULL;
16766
16767 /* Loop through the extensions until we find a name. */
16768
16769 for (current_die = die;
16770 current_die != NULL;
f2f0e013 16771 current_die = dwarf2_extension (die, &cu))
38d518c9 16772 {
96553a0c
DE
16773 /* We don't use dwarf2_name here so that we can detect the absence
16774 of a name -> anonymous namespace. */
7d45c7c3 16775 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16776
38d518c9
EZ
16777 if (name != NULL)
16778 break;
16779 }
16780
16781 /* Is it an anonymous namespace? */
16782
16783 *is_anonymous = (name == NULL);
16784 if (*is_anonymous)
2b1dbab0 16785 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16786
16787 return name;
d9fa45fe
DC
16788}
16789
c906108c
SS
16790/* Extract all information from a DW_TAG_pointer_type DIE and add to
16791 the user defined type vector. */
16792
f792889a 16793static struct type *
e7c27a73 16794read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16795{
5e22e966 16796 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 16797 struct comp_unit_head *cu_header = &cu->header;
c906108c 16798 struct type *type;
8b2dbe47
KB
16799 struct attribute *attr_byte_size;
16800 struct attribute *attr_address_class;
16801 int byte_size, addr_class;
7e314c57
JK
16802 struct type *target_type;
16803
16804 target_type = die_type (die, cu);
c906108c 16805
7e314c57
JK
16806 /* The die_type call above may have already set the type for this DIE. */
16807 type = get_die_type (die, cu);
16808 if (type)
16809 return type;
16810
16811 type = lookup_pointer_type (target_type);
8b2dbe47 16812
e142c38c 16813 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16814 if (attr_byte_size)
16815 byte_size = DW_UNSND (attr_byte_size);
c906108c 16816 else
8b2dbe47
KB
16817 byte_size = cu_header->addr_size;
16818
e142c38c 16819 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16820 if (attr_address_class)
16821 addr_class = DW_UNSND (attr_address_class);
16822 else
16823 addr_class = DW_ADDR_none;
16824
2b4424c3
TT
16825 ULONGEST alignment = get_alignment (cu, die);
16826
16827 /* If the pointer size, alignment, or address class is different
16828 than the default, create a type variant marked as such and set
16829 the length accordingly. */
16830 if (TYPE_LENGTH (type) != byte_size
16831 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16832 && alignment != TYPE_RAW_ALIGN (type))
16833 || addr_class != DW_ADDR_none)
c906108c 16834 {
5e2b427d 16835 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16836 {
16837 int type_flags;
16838
849957d9 16839 type_flags = gdbarch_address_class_type_flags
5e2b427d 16840 (gdbarch, byte_size, addr_class);
876cecd0
TT
16841 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16842 == 0);
8b2dbe47
KB
16843 type = make_type_with_address_space (type, type_flags);
16844 }
16845 else if (TYPE_LENGTH (type) != byte_size)
16846 {
b98664d3 16847 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16848 }
2b4424c3
TT
16849 else if (TYPE_RAW_ALIGN (type) != alignment)
16850 {
b98664d3 16851 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16852 " - DIE at %s [in module %s]"),
16853 sect_offset_str (die->sect_off),
5e22e966 16854 objfile_name (cu->per_objfile->objfile));
2b4424c3 16855 }
6e70227d 16856 else
9a619af0
MS
16857 {
16858 /* Should we also complain about unhandled address classes? */
16859 }
c906108c 16860 }
8b2dbe47
KB
16861
16862 TYPE_LENGTH (type) = byte_size;
2b4424c3 16863 set_type_align (type, alignment);
f792889a 16864 return set_die_type (die, type, cu);
c906108c
SS
16865}
16866
16867/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16868 the user defined type vector. */
16869
f792889a 16870static struct type *
e7c27a73 16871read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16872{
16873 struct type *type;
16874 struct type *to_type;
16875 struct type *domain;
16876
e7c27a73
DJ
16877 to_type = die_type (die, cu);
16878 domain = die_containing_type (die, cu);
0d5de010 16879
7e314c57
JK
16880 /* The calls above may have already set the type for this DIE. */
16881 type = get_die_type (die, cu);
16882 if (type)
16883 return type;
16884
78134374 16885 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 16886 type = lookup_methodptr_type (to_type);
78134374 16887 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 16888 {
5e22e966 16889 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
16890
16891 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 16892 to_type->fields (), to_type->num_fields (),
7078baeb
TT
16893 TYPE_VARARGS (to_type));
16894 type = lookup_methodptr_type (new_type);
16895 }
0d5de010
DJ
16896 else
16897 type = lookup_memberptr_type (to_type, domain);
c906108c 16898
f792889a 16899 return set_die_type (die, type, cu);
c906108c
SS
16900}
16901
4297a3f0 16902/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16903 the user defined type vector. */
16904
f792889a 16905static struct type *
4297a3f0
AV
16906read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16907 enum type_code refcode)
c906108c 16908{
e7c27a73 16909 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16910 struct type *type, *target_type;
c906108c
SS
16911 struct attribute *attr;
16912
4297a3f0
AV
16913 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16914
7e314c57
JK
16915 target_type = die_type (die, cu);
16916
16917 /* The die_type call above may have already set the type for this DIE. */
16918 type = get_die_type (die, cu);
16919 if (type)
16920 return type;
16921
4297a3f0 16922 type = lookup_reference_type (target_type, refcode);
e142c38c 16923 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16924 if (attr != nullptr)
c906108c
SS
16925 {
16926 TYPE_LENGTH (type) = DW_UNSND (attr);
16927 }
16928 else
16929 {
107d2387 16930 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16931 }
2b4424c3 16932 maybe_set_alignment (cu, die, type);
f792889a 16933 return set_die_type (die, type, cu);
c906108c
SS
16934}
16935
cf363f18
MW
16936/* Add the given cv-qualifiers to the element type of the array. GCC
16937 outputs DWARF type qualifiers that apply to an array, not the
16938 element type. But GDB relies on the array element type to carry
16939 the cv-qualifiers. This mimics section 6.7.3 of the C99
16940 specification. */
16941
16942static struct type *
16943add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16944 struct type *base_type, int cnst, int voltl)
16945{
16946 struct type *el_type, *inner_array;
16947
16948 base_type = copy_type (base_type);
16949 inner_array = base_type;
16950
78134374 16951 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16952 {
16953 TYPE_TARGET_TYPE (inner_array) =
16954 copy_type (TYPE_TARGET_TYPE (inner_array));
16955 inner_array = TYPE_TARGET_TYPE (inner_array);
16956 }
16957
16958 el_type = TYPE_TARGET_TYPE (inner_array);
16959 cnst |= TYPE_CONST (el_type);
16960 voltl |= TYPE_VOLATILE (el_type);
16961 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16962
16963 return set_die_type (die, base_type, cu);
16964}
16965
f792889a 16966static struct type *
e7c27a73 16967read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16968{
f792889a 16969 struct type *base_type, *cv_type;
c906108c 16970
e7c27a73 16971 base_type = die_type (die, cu);
7e314c57
JK
16972
16973 /* The die_type call above may have already set the type for this DIE. */
16974 cv_type = get_die_type (die, cu);
16975 if (cv_type)
16976 return cv_type;
16977
2f608a3a
KW
16978 /* In case the const qualifier is applied to an array type, the element type
16979 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 16980 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 16981 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16982
f792889a
DJ
16983 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16984 return set_die_type (die, cv_type, cu);
c906108c
SS
16985}
16986
f792889a 16987static struct type *
e7c27a73 16988read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16989{
f792889a 16990 struct type *base_type, *cv_type;
c906108c 16991
e7c27a73 16992 base_type = die_type (die, cu);
7e314c57
JK
16993
16994 /* The die_type call above may have already set the type for this DIE. */
16995 cv_type = get_die_type (die, cu);
16996 if (cv_type)
16997 return cv_type;
16998
cf363f18
MW
16999 /* In case the volatile qualifier is applied to an array type, the
17000 element type is so qualified, not the array type (section 6.7.3
17001 of C99). */
78134374 17002 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17003 return add_array_cv_type (die, cu, base_type, 0, 1);
17004
f792889a
DJ
17005 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17006 return set_die_type (die, cv_type, cu);
c906108c
SS
17007}
17008
06d66ee9
TT
17009/* Handle DW_TAG_restrict_type. */
17010
17011static struct type *
17012read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17013{
17014 struct type *base_type, *cv_type;
17015
17016 base_type = die_type (die, cu);
17017
17018 /* The die_type call above may have already set the type for this DIE. */
17019 cv_type = get_die_type (die, cu);
17020 if (cv_type)
17021 return cv_type;
17022
17023 cv_type = make_restrict_type (base_type);
17024 return set_die_type (die, cv_type, cu);
17025}
17026
a2c2acaf
MW
17027/* Handle DW_TAG_atomic_type. */
17028
17029static struct type *
17030read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17031{
17032 struct type *base_type, *cv_type;
17033
17034 base_type = die_type (die, cu);
17035
17036 /* The die_type call above may have already set the type for this DIE. */
17037 cv_type = get_die_type (die, cu);
17038 if (cv_type)
17039 return cv_type;
17040
17041 cv_type = make_atomic_type (base_type);
17042 return set_die_type (die, cv_type, cu);
17043}
17044
c906108c
SS
17045/* Extract all information from a DW_TAG_string_type DIE and add to
17046 the user defined type vector. It isn't really a user defined type,
17047 but it behaves like one, with other DIE's using an AT_user_def_type
17048 attribute to reference it. */
17049
f792889a 17050static struct type *
e7c27a73 17051read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17052{
5e22e966 17053 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 17054 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
17055 struct type *type, *range_type, *index_type, *char_type;
17056 struct attribute *attr;
216a7e6b
AB
17057 struct dynamic_prop prop;
17058 bool length_is_constant = true;
17059 LONGEST length;
17060
17061 /* There are a couple of places where bit sizes might be made use of
17062 when parsing a DW_TAG_string_type, however, no producer that we know
17063 of make use of these. Handling bit sizes that are a multiple of the
17064 byte size is easy enough, but what about other bit sizes? Lets deal
17065 with that problem when we have to. Warn about these attributes being
17066 unsupported, then parse the type and ignore them like we always
17067 have. */
17068 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17069 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17070 {
17071 static bool warning_printed = false;
17072 if (!warning_printed)
17073 {
17074 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17075 "currently supported on DW_TAG_string_type."));
17076 warning_printed = true;
17077 }
17078 }
c906108c 17079
e142c38c 17080 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 17081 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
17082 {
17083 /* The string length describes the location at which the length of
17084 the string can be found. The size of the length field can be
17085 specified with one of the attributes below. */
17086 struct type *prop_type;
17087 struct attribute *len
17088 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17089 if (len == nullptr)
17090 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17091 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17092 {
17093 /* Pass 0 as the default as we know this attribute is constant
17094 and the default value will not be returned. */
0826b30a 17095 LONGEST sz = len->constant_value (0);
293e7e51 17096 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17097 }
17098 else
17099 {
17100 /* If the size is not specified then we assume it is the size of
17101 an address on this target. */
293e7e51 17102 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17103 }
17104
17105 /* Convert the attribute into a dynamic property. */
17106 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17107 length = 1;
17108 else
17109 length_is_constant = false;
17110 }
17111 else if (attr != nullptr)
17112 {
17113 /* This DW_AT_string_length just contains the length with no
17114 indirection. There's no need to create a dynamic property in this
17115 case. Pass 0 for the default value as we know it will not be
17116 returned in this case. */
0826b30a 17117 length = attr->constant_value (0);
216a7e6b
AB
17118 }
17119 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17120 {
216a7e6b 17121 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17122 length = attr->constant_value (1);
c906108c
SS
17123 }
17124 else
17125 {
216a7e6b
AB
17126 /* Use 1 as a fallback length if we have nothing else. */
17127 length = 1;
c906108c 17128 }
6ccb9162 17129
46bf5051 17130 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17131 if (length_is_constant)
17132 range_type = create_static_range_type (NULL, index_type, 1, length);
17133 else
17134 {
17135 struct dynamic_prop low_bound;
17136
17137 low_bound.kind = PROP_CONST;
17138 low_bound.data.const_val = 1;
17139 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17140 }
3b7538c0
UW
17141 char_type = language_string_char_type (cu->language_defn, gdbarch);
17142 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17143
f792889a 17144 return set_die_type (die, type, cu);
c906108c
SS
17145}
17146
4d804846
JB
17147/* Assuming that DIE corresponds to a function, returns nonzero
17148 if the function is prototyped. */
17149
17150static int
17151prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17152{
17153 struct attribute *attr;
17154
17155 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17156 if (attr && (DW_UNSND (attr) != 0))
17157 return 1;
17158
17159 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17160 is only meaningful for C, but the concept also extends to other
4d804846
JB
17161 languages that allow unprototyped functions (Eg: Objective C).
17162 For all other languages, assume that functions are always
17163 prototyped. */
17164 if (cu->language != language_c
17165 && cu->language != language_objc
17166 && cu->language != language_opencl)
17167 return 1;
17168
17169 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17170 prototyped and unprototyped functions; default to prototyped,
17171 since that is more common in modern code (and RealView warns
17172 about unprototyped functions). */
17173 if (producer_is_realview (cu->producer))
17174 return 1;
17175
17176 return 0;
17177}
17178
c906108c
SS
17179/* Handle DIES due to C code like:
17180
17181 struct foo
c5aa993b
JM
17182 {
17183 int (*funcp)(int a, long l);
17184 int b;
17185 };
c906108c 17186
0963b4bd 17187 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17188
f792889a 17189static struct type *
e7c27a73 17190read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17191{
5e22e966 17192 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
17193 struct type *type; /* Type that this function returns. */
17194 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17195 struct attribute *attr;
17196
e7c27a73 17197 type = die_type (die, cu);
7e314c57
JK
17198
17199 /* The die_type call above may have already set the type for this DIE. */
17200 ftype = get_die_type (die, cu);
17201 if (ftype)
17202 return ftype;
17203
0c8b41f1 17204 ftype = lookup_function_type (type);
c906108c 17205
4d804846 17206 if (prototyped_function_p (die, cu))
a6c727b2 17207 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17208
c055b101
CV
17209 /* Store the calling convention in the type if it's available in
17210 the subroutine die. Otherwise set the calling convention to
17211 the default value DW_CC_normal. */
17212 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17213 if (attr != nullptr
17214 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17215 TYPE_CALLING_CONVENTION (ftype)
17216 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17217 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17218 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17219 else
17220 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17221
743649fd
MW
17222 /* Record whether the function returns normally to its caller or not
17223 if the DWARF producer set that information. */
17224 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17225 if (attr && (DW_UNSND (attr) != 0))
17226 TYPE_NO_RETURN (ftype) = 1;
17227
76c10ea2
GM
17228 /* We need to add the subroutine type to the die immediately so
17229 we don't infinitely recurse when dealing with parameters
0963b4bd 17230 declared as the same subroutine type. */
76c10ea2 17231 set_die_type (die, ftype, cu);
6e70227d 17232
639d11d3 17233 if (die->child != NULL)
c906108c 17234 {
bb5ed363 17235 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17236 struct die_info *child_die;
8072405b 17237 int nparams, iparams;
c906108c
SS
17238
17239 /* Count the number of parameters.
17240 FIXME: GDB currently ignores vararg functions, but knows about
17241 vararg member functions. */
8072405b 17242 nparams = 0;
639d11d3 17243 child_die = die->child;
c906108c
SS
17244 while (child_die && child_die->tag)
17245 {
17246 if (child_die->tag == DW_TAG_formal_parameter)
17247 nparams++;
17248 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17249 TYPE_VARARGS (ftype) = 1;
436c571c 17250 child_die = child_die->sibling;
c906108c
SS
17251 }
17252
17253 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17254 ftype->set_num_fields (nparams);
3cabb6b0
SM
17255 ftype->set_fields
17256 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17257
8072405b
JK
17258 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17259 even if we error out during the parameters reading below. */
17260 for (iparams = 0; iparams < nparams; iparams++)
17261 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17262
17263 iparams = 0;
639d11d3 17264 child_die = die->child;
c906108c
SS
17265 while (child_die && child_die->tag)
17266 {
17267 if (child_die->tag == DW_TAG_formal_parameter)
17268 {
3ce3b1ba
PA
17269 struct type *arg_type;
17270
17271 /* DWARF version 2 has no clean way to discern C++
17272 static and non-static member functions. G++ helps
17273 GDB by marking the first parameter for non-static
17274 member functions (which is the this pointer) as
17275 artificial. We pass this information to
17276 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17277
17278 DWARF version 3 added DW_AT_object_pointer, which GCC
17279 4.5 does not yet generate. */
e142c38c 17280 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17281 if (attr != nullptr)
c906108c
SS
17282 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17283 else
9c37b5ae 17284 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17285 arg_type = die_type (child_die, cu);
17286
17287 /* RealView does not mark THIS as const, which the testsuite
17288 expects. GCC marks THIS as const in method definitions,
17289 but not in the class specifications (GCC PR 43053). */
17290 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17291 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17292 {
17293 int is_this = 0;
17294 struct dwarf2_cu *arg_cu = cu;
17295 const char *name = dwarf2_name (child_die, cu);
17296
17297 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17298 if (attr != nullptr)
3ce3b1ba
PA
17299 {
17300 /* If the compiler emits this, use it. */
17301 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17302 is_this = 1;
17303 }
17304 else if (name && strcmp (name, "this") == 0)
17305 /* Function definitions will have the argument names. */
17306 is_this = 1;
17307 else if (name == NULL && iparams == 0)
17308 /* Declarations may not have the names, so like
17309 elsewhere in GDB, assume an artificial first
17310 argument is "this". */
17311 is_this = 1;
17312
17313 if (is_this)
17314 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17315 arg_type, 0);
17316 }
17317
17318 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17319 iparams++;
17320 }
436c571c 17321 child_die = child_die->sibling;
c906108c
SS
17322 }
17323 }
17324
76c10ea2 17325 return ftype;
c906108c
SS
17326}
17327
f792889a 17328static struct type *
e7c27a73 17329read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17330{
5e22e966 17331 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17332 const char *name = NULL;
3c8e0968 17333 struct type *this_type, *target_type;
c906108c 17334
94af9270 17335 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17336 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17337 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17338 set_die_type (die, this_type, cu);
3c8e0968
DE
17339 target_type = die_type (die, cu);
17340 if (target_type != this_type)
17341 TYPE_TARGET_TYPE (this_type) = target_type;
17342 else
17343 {
17344 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17345 spec and cause infinite loops in GDB. */
b98664d3 17346 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17347 "- DIE at %s [in module %s]"),
17348 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17349 TYPE_TARGET_TYPE (this_type) = NULL;
17350 }
e4003a34
TV
17351 if (name == NULL)
17352 {
17353 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17354 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17355 Handle these by just returning the target type, rather than
17356 constructing an anonymous typedef type and trying to handle this
17357 elsewhere. */
17358 set_die_type (die, target_type, cu);
17359 return target_type;
17360 }
f792889a 17361 return this_type;
c906108c
SS
17362}
17363
9b790ce7
UW
17364/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17365 (which may be different from NAME) to the architecture back-end to allow
17366 it to guess the correct format if necessary. */
17367
17368static struct type *
17369dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17370 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 17371{
08feed99 17372 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
17373 const struct floatformat **format;
17374 struct type *type;
17375
17376 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17377 if (format)
103a685e 17378 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17379 else
77b7c781 17380 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17381
17382 return type;
17383}
17384
eb77c9df
AB
17385/* Allocate an integer type of size BITS and name NAME. */
17386
17387static struct type *
17388dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17389 int bits, int unsigned_p, const char *name)
17390{
17391 struct type *type;
17392
17393 /* Versions of Intel's C Compiler generate an integer type called "void"
17394 instead of using DW_TAG_unspecified_type. This has been seen on
17395 at least versions 14, 17, and 18. */
35ee2dc2
AB
17396 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17397 && strcmp (name, "void") == 0)
eb77c9df
AB
17398 type = objfile_type (objfile)->builtin_void;
17399 else
17400 type = init_integer_type (objfile, bits, unsigned_p, name);
17401
17402 return type;
17403}
17404
8bdc1658
AB
17405/* Initialise and return a floating point type of size BITS suitable for
17406 use as a component of a complex number. The NAME_HINT is passed through
17407 when initialising the floating point type and is the name of the complex
17408 type.
17409
17410 As DWARF doesn't currently provide an explicit name for the components
17411 of a complex number, but it can be helpful to have these components
17412 named, we try to select a suitable name based on the size of the
17413 component. */
17414static struct type *
17415dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17416 struct objfile *objfile,
103a685e
TT
17417 int bits, const char *name_hint,
17418 enum bfd_endian byte_order)
8bdc1658 17419{
08feed99 17420 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
17421 struct type *tt = nullptr;
17422
35add35e
AB
17423 /* Try to find a suitable floating point builtin type of size BITS.
17424 We're going to use the name of this type as the name for the complex
17425 target type that we are about to create. */
1db455a7 17426 switch (cu->language)
8bdc1658 17427 {
1db455a7
AB
17428 case language_fortran:
17429 switch (bits)
17430 {
17431 case 32:
17432 tt = builtin_f_type (gdbarch)->builtin_real;
17433 break;
17434 case 64:
17435 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17436 break;
17437 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17438 case 128:
17439 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17440 break;
17441 }
8bdc1658 17442 break;
1db455a7
AB
17443 default:
17444 switch (bits)
17445 {
17446 case 32:
17447 tt = builtin_type (gdbarch)->builtin_float;
17448 break;
17449 case 64:
17450 tt = builtin_type (gdbarch)->builtin_double;
17451 break;
17452 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17453 case 128:
17454 tt = builtin_type (gdbarch)->builtin_long_double;
17455 break;
17456 }
8bdc1658
AB
17457 break;
17458 }
17459
35add35e
AB
17460 /* If the type we found doesn't match the size we were looking for, then
17461 pretend we didn't find a type at all, the complex target type we
17462 create will then be nameless. */
a12e5744 17463 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17464 tt = nullptr;
17465
7d93a1e0 17466 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 17467 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17468}
17469
c906108c
SS
17470/* Find a representation of a given base type and install
17471 it in the TYPE field of the die. */
17472
f792889a 17473static struct type *
e7c27a73 17474read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17475{
5e22e966 17476 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
17477 struct type *type;
17478 struct attribute *attr;
19f392bc 17479 int encoding = 0, bits = 0;
15d034d0 17480 const char *name;
34877895 17481 gdbarch *arch;
c906108c 17482
e142c38c 17483 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17484 if (attr != nullptr)
34877895 17485 encoding = DW_UNSND (attr);
e142c38c 17486 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17487 if (attr != nullptr)
34877895 17488 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17489 name = dwarf2_name (die, cu);
6ccb9162 17490 if (!name)
34877895 17491 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 17492
08feed99 17493 arch = objfile->arch ();
103a685e
TT
17494 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17495
34877895
PJ
17496 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17497 if (attr)
103a685e
TT
17498 {
17499 int endianity = DW_UNSND (attr);
17500
17501 switch (endianity)
17502 {
17503 case DW_END_big:
17504 byte_order = BFD_ENDIAN_BIG;
17505 break;
17506 case DW_END_little:
17507 byte_order = BFD_ENDIAN_LITTLE;
17508 break;
17509 default:
17510 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17511 break;
17512 }
17513 }
6ccb9162
UW
17514
17515 switch (encoding)
c906108c 17516 {
6ccb9162
UW
17517 case DW_ATE_address:
17518 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17519 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17520 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17521 break;
17522 case DW_ATE_boolean:
19f392bc 17523 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17524 break;
17525 case DW_ATE_complex_float:
103a685e
TT
17526 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17527 byte_order);
78134374 17528 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
17529 {
17530 if (name == nullptr)
17531 {
17532 struct obstack *obstack
5e22e966 17533 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 17534 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
17535 nullptr);
17536 }
17537 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17538 }
17539 else
17540 type = init_complex_type (name, type);
6ccb9162
UW
17541 break;
17542 case DW_ATE_decimal_float:
19f392bc 17543 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17544 break;
17545 case DW_ATE_float:
103a685e 17546 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17547 break;
17548 case DW_ATE_signed:
eb77c9df 17549 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17550 break;
17551 case DW_ATE_unsigned:
3b2b8fea
TT
17552 if (cu->language == language_fortran
17553 && name
61012eef 17554 && startswith (name, "character("))
19f392bc
UW
17555 type = init_character_type (objfile, bits, 1, name);
17556 else
eb77c9df 17557 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17558 break;
17559 case DW_ATE_signed_char:
6e70227d 17560 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17561 || cu->language == language_pascal
17562 || cu->language == language_fortran)
19f392bc
UW
17563 type = init_character_type (objfile, bits, 0, name);
17564 else
eb77c9df 17565 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17566 break;
17567 case DW_ATE_unsigned_char:
868a0084 17568 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17569 || cu->language == language_pascal
c44af4eb
TT
17570 || cu->language == language_fortran
17571 || cu->language == language_rust)
19f392bc
UW
17572 type = init_character_type (objfile, bits, 1, name);
17573 else
eb77c9df 17574 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17575 break;
75079b2b 17576 case DW_ATE_UTF:
53e710ac 17577 {
53e710ac
PA
17578 if (bits == 16)
17579 type = builtin_type (arch)->builtin_char16;
17580 else if (bits == 32)
17581 type = builtin_type (arch)->builtin_char32;
17582 else
17583 {
b98664d3 17584 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17585 bits);
eb77c9df 17586 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17587 }
17588 return set_die_type (die, type, cu);
17589 }
75079b2b
TT
17590 break;
17591
6ccb9162 17592 default:
b98664d3 17593 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17594 dwarf_type_encoding_name (encoding));
77b7c781 17595 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17596 break;
c906108c 17597 }
6ccb9162 17598
0114d602 17599 if (name && strcmp (name, "char") == 0)
876cecd0 17600 TYPE_NOSIGN (type) = 1;
0114d602 17601
2b4424c3
TT
17602 maybe_set_alignment (cu, die, type);
17603
103a685e 17604 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17605
f792889a 17606 return set_die_type (die, type, cu);
c906108c
SS
17607}
17608
80180f79
SA
17609/* Parse dwarf attribute if it's a block, reference or constant and put the
17610 resulting value of the attribute into struct bound_prop.
17611 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17612
17613static int
17614attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17615 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17616 struct type *default_type)
80180f79
SA
17617{
17618 struct dwarf2_property_baton *baton;
5e22e966 17619 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
17620 struct objfile *objfile = per_objfile->objfile;
17621 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 17622
9a49df9d
AB
17623 gdb_assert (default_type != NULL);
17624
80180f79
SA
17625 if (attr == NULL || prop == NULL)
17626 return 0;
17627
4fc6c0d5 17628 if (attr->form_is_block ())
80180f79 17629 {
8d749320 17630 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17631 baton->property_type = default_type;
80180f79 17632 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17633 baton->locexpr.per_objfile = per_objfile;
80180f79
SA
17634 baton->locexpr.size = DW_BLOCK (attr)->size;
17635 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17636 switch (attr->name)
17637 {
17638 case DW_AT_string_length:
17639 baton->locexpr.is_reference = true;
17640 break;
17641 default:
17642 baton->locexpr.is_reference = false;
17643 break;
17644 }
80180f79
SA
17645 prop->data.baton = baton;
17646 prop->kind = PROP_LOCEXPR;
17647 gdb_assert (prop->data.baton != NULL);
17648 }
cd6c91b4 17649 else if (attr->form_is_ref ())
80180f79
SA
17650 {
17651 struct dwarf2_cu *target_cu = cu;
17652 struct die_info *target_die;
17653 struct attribute *target_attr;
17654
17655 target_die = follow_die_ref (die, attr, &target_cu);
17656 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17657 if (target_attr == NULL)
17658 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17659 target_cu);
80180f79
SA
17660 if (target_attr == NULL)
17661 return 0;
17662
df25ebbd 17663 switch (target_attr->name)
80180f79 17664 {
df25ebbd 17665 case DW_AT_location:
cd6c91b4 17666 if (target_attr->form_is_section_offset ())
df25ebbd 17667 {
8d749320 17668 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17669 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17670 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17671 prop->data.baton = baton;
17672 prop->kind = PROP_LOCLIST;
17673 gdb_assert (prop->data.baton != NULL);
17674 }
4fc6c0d5 17675 else if (target_attr->form_is_block ())
df25ebbd 17676 {
8d749320 17677 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17678 baton->property_type = die_type (target_die, target_cu);
df25ebbd 17679 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17680 baton->locexpr.per_objfile = per_objfile;
df25ebbd
JB
17681 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17682 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17683 baton->locexpr.is_reference = true;
df25ebbd
JB
17684 prop->data.baton = baton;
17685 prop->kind = PROP_LOCEXPR;
17686 gdb_assert (prop->data.baton != NULL);
17687 }
17688 else
17689 {
17690 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17691 "dynamic property");
17692 return 0;
17693 }
17694 break;
17695 case DW_AT_data_member_location:
17696 {
17697 LONGEST offset;
17698
17699 if (!handle_data_member_location (target_die, target_cu,
17700 &offset))
17701 return 0;
17702
8d749320 17703 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17704 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17705 target_cu);
df25ebbd
JB
17706 baton->offset_info.offset = offset;
17707 baton->offset_info.type = die_type (target_die, target_cu);
17708 prop->data.baton = baton;
17709 prop->kind = PROP_ADDR_OFFSET;
17710 break;
17711 }
80180f79
SA
17712 }
17713 }
cd6c91b4 17714 else if (attr->form_is_constant ())
80180f79 17715 {
0826b30a 17716 prop->data.const_val = attr->constant_value (0);
80180f79
SA
17717 prop->kind = PROP_CONST;
17718 }
17719 else
17720 {
17721 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17722 dwarf2_name (die, cu));
17723 return 0;
17724 }
17725
17726 return 1;
17727}
17728
09ba997f 17729/* See read.h. */
9a49df9d 17730
09ba997f 17731struct type *
293e7e51 17732dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17733{
9a49df9d
AB
17734 struct type *int_type;
17735
17736 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17737#define TRY_TYPE(F) \
17738 int_type = (unsigned_p \
17739 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17740 : objfile_type (objfile)->builtin_ ## F); \
17741 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17742 return int_type
17743
17744 TRY_TYPE (char);
17745 TRY_TYPE (short);
17746 TRY_TYPE (int);
17747 TRY_TYPE (long);
17748 TRY_TYPE (long_long);
17749
17750#undef TRY_TYPE
17751
17752 gdb_assert_not_reached ("unable to find suitable integer type");
17753}
17754
09ba997f 17755/* See read.h. */
11a8b164 17756
09ba997f 17757struct type *
293e7e51 17758dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
11a8b164 17759{
293e7e51
SM
17760 int addr_size = this->per_cu->addr_size ();
17761 return this->per_objfile->int_type (addr_size, unsigned_p);
11a8b164
AB
17762}
17763
b86352cf
AB
17764/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17765 present (which is valid) then compute the default type based on the
17766 compilation units address size. */
17767
17768static struct type *
17769read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17770{
17771 struct type *index_type = die_type (die, cu);
17772
17773 /* Dwarf-2 specifications explicitly allows to create subrange types
17774 without specifying a base type.
17775 In that case, the base type must be set to the type of
17776 the lower bound, upper bound or count, in that order, if any of these
17777 three attributes references an object that has a type.
17778 If no base type is found, the Dwarf-2 specifications say that
17779 a signed integer type of size equal to the size of an address should
17780 be used.
17781 For the following C code: `extern char gdb_int [];'
17782 GCC produces an empty range DIE.
17783 FIXME: muller/2010-05-28: Possible references to object for low bound,
17784 high bound or count are not yet handled by this code. */
78134374 17785 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 17786 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
17787
17788 return index_type;
17789}
17790
a02abb62
JB
17791/* Read the given DW_AT_subrange DIE. */
17792
f792889a 17793static struct type *
a02abb62
JB
17794read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17795{
4c9ad8c2 17796 struct type *base_type, *orig_base_type;
a02abb62
JB
17797 struct type *range_type;
17798 struct attribute *attr;
729efb13 17799 struct dynamic_prop low, high;
4fae6e18 17800 int low_default_is_valid;
c451ebe5 17801 int high_bound_is_count = 0;
15d034d0 17802 const char *name;
d359392f 17803 ULONGEST negative_mask;
e77813c8 17804
b86352cf
AB
17805 orig_base_type = read_subrange_index_type (die, cu);
17806
4c9ad8c2
TT
17807 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17808 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17809 creating the range type, but we use the result of check_typedef
17810 when examining properties of the type. */
17811 base_type = check_typedef (orig_base_type);
a02abb62 17812
7e314c57
JK
17813 /* The die_type call above may have already set the type for this DIE. */
17814 range_type = get_die_type (die, cu);
17815 if (range_type)
17816 return range_type;
17817
729efb13
SA
17818 low.kind = PROP_CONST;
17819 high.kind = PROP_CONST;
17820 high.data.const_val = 0;
17821
4fae6e18
JK
17822 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17823 omitting DW_AT_lower_bound. */
17824 switch (cu->language)
6e70227d 17825 {
4fae6e18
JK
17826 case language_c:
17827 case language_cplus:
729efb13 17828 low.data.const_val = 0;
4fae6e18
JK
17829 low_default_is_valid = 1;
17830 break;
17831 case language_fortran:
729efb13 17832 low.data.const_val = 1;
4fae6e18
JK
17833 low_default_is_valid = 1;
17834 break;
17835 case language_d:
4fae6e18 17836 case language_objc:
c44af4eb 17837 case language_rust:
729efb13 17838 low.data.const_val = 0;
4fae6e18
JK
17839 low_default_is_valid = (cu->header.version >= 4);
17840 break;
17841 case language_ada:
17842 case language_m2:
17843 case language_pascal:
729efb13 17844 low.data.const_val = 1;
4fae6e18
JK
17845 low_default_is_valid = (cu->header.version >= 4);
17846 break;
17847 default:
729efb13 17848 low.data.const_val = 0;
4fae6e18
JK
17849 low_default_is_valid = 0;
17850 break;
a02abb62
JB
17851 }
17852
e142c38c 17853 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17854 if (attr != nullptr)
9a49df9d 17855 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17856 else if (!low_default_is_valid)
b98664d3 17857 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17858 "- DIE at %s [in module %s]"),
17859 sect_offset_str (die->sect_off),
5e22e966 17860 objfile_name (cu->per_objfile->objfile));
a02abb62 17861
506f5c41
TV
17862 struct attribute *attr_ub, *attr_count;
17863 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17864 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17865 {
506f5c41 17866 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17867 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17868 {
c451ebe5
SA
17869 /* If bounds are constant do the final calculation here. */
17870 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17871 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17872 else
17873 high_bound_is_count = 1;
c2ff108b 17874 }
506f5c41
TV
17875 else
17876 {
17877 if (attr_ub != NULL)
17878 complaint (_("Unresolved DW_AT_upper_bound "
17879 "- DIE at %s [in module %s]"),
17880 sect_offset_str (die->sect_off),
5e22e966 17881 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
17882 if (attr_count != NULL)
17883 complaint (_("Unresolved DW_AT_count "
17884 "- DIE at %s [in module %s]"),
17885 sect_offset_str (die->sect_off),
5e22e966 17886 objfile_name (cu->per_objfile->objfile));
506f5c41 17887 }
e77813c8 17888 }
a02abb62 17889
4e962e74
TT
17890 LONGEST bias = 0;
17891 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17892 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 17893 bias = bias_attr->constant_value (0);
4e962e74 17894
dbb9c2b1
JB
17895 /* Normally, the DWARF producers are expected to use a signed
17896 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17897 But this is unfortunately not always the case, as witnessed
17898 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17899 is used instead. To work around that ambiguity, we treat
17900 the bounds as signed, and thus sign-extend their values, when
17901 the base type is signed. */
6e70227d 17902 negative_mask =
d359392f 17903 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17904 if (low.kind == PROP_CONST
17905 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17906 low.data.const_val |= negative_mask;
17907 if (high.kind == PROP_CONST
17908 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17909 high.data.const_val |= negative_mask;
43bbcdc2 17910
5bbd8269
AB
17911 /* Check for bit and byte strides. */
17912 struct dynamic_prop byte_stride_prop;
17913 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17914 if (attr_byte_stride != nullptr)
17915 {
293e7e51 17916 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
17917 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17918 prop_type);
17919 }
17920
17921 struct dynamic_prop bit_stride_prop;
17922 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17923 if (attr_bit_stride != nullptr)
17924 {
17925 /* It only makes sense to have either a bit or byte stride. */
17926 if (attr_byte_stride != nullptr)
17927 {
17928 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17929 "- DIE at %s [in module %s]"),
17930 sect_offset_str (die->sect_off),
5e22e966 17931 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
17932 attr_bit_stride = nullptr;
17933 }
17934 else
17935 {
293e7e51 17936 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
17937 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17938 prop_type);
17939 }
17940 }
17941
17942 if (attr_byte_stride != nullptr
17943 || attr_bit_stride != nullptr)
17944 {
17945 bool byte_stride_p = (attr_byte_stride != nullptr);
17946 struct dynamic_prop *stride
17947 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17948
17949 range_type
17950 = create_range_type_with_stride (NULL, orig_base_type, &low,
17951 &high, bias, stride, byte_stride_p);
17952 }
17953 else
17954 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17955
c451ebe5
SA
17956 if (high_bound_is_count)
17957 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17958
c2ff108b
JK
17959 /* Ada expects an empty array on no boundary attributes. */
17960 if (attr == NULL && cu->language != language_ada)
729efb13 17961 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17962
39cbfefa
DJ
17963 name = dwarf2_name (die, cu);
17964 if (name)
d0e39ea2 17965 range_type->set_name (name);
6e70227d 17966
e142c38c 17967 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17968 if (attr != nullptr)
a02abb62
JB
17969 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17970
2b4424c3
TT
17971 maybe_set_alignment (cu, die, range_type);
17972
7e314c57
JK
17973 set_die_type (die, range_type, cu);
17974
17975 /* set_die_type should be already done. */
b4ba55a1
JB
17976 set_descriptive_type (range_type, die, cu);
17977
7e314c57 17978 return range_type;
a02abb62 17979}
6e70227d 17980
f792889a 17981static struct type *
81a17f79
JB
17982read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17983{
17984 struct type *type;
81a17f79 17985
5e22e966 17986 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 17987 type->set_name (dwarf2_name (die, cu));
81a17f79 17988
74a2f8ff 17989 /* In Ada, an unspecified type is typically used when the description
85102364 17990 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17991 such a type, we treat it as a stub, and try to resolve it later on,
17992 when needed. */
17993 if (cu->language == language_ada)
17994 TYPE_STUB (type) = 1;
17995
f792889a 17996 return set_die_type (die, type, cu);
81a17f79 17997}
a02abb62 17998
639d11d3
DC
17999/* Read a single die and all its descendents. Set the die's sibling
18000 field to NULL; set other fields in the die correctly, and set all
18001 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18002 location of the info_ptr after reading all of those dies. PARENT
18003 is the parent of the die in question. */
18004
18005static struct die_info *
dee91e82 18006read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18007 const gdb_byte *info_ptr,
18008 const gdb_byte **new_info_ptr,
dee91e82 18009 struct die_info *parent)
639d11d3
DC
18010{
18011 struct die_info *die;
d521ce57 18012 const gdb_byte *cur_ptr;
639d11d3 18013
3e225074 18014 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
18015 if (die == NULL)
18016 {
18017 *new_info_ptr = cur_ptr;
18018 return NULL;
18019 }
93311388 18020 store_in_ref_table (die, reader->cu);
639d11d3 18021
3e225074 18022 if (die->has_children)
bf6af496 18023 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18024 else
18025 {
18026 die->child = NULL;
18027 *new_info_ptr = cur_ptr;
18028 }
18029
18030 die->sibling = NULL;
18031 die->parent = parent;
18032 return die;
18033}
18034
18035/* Read a die, all of its descendents, and all of its siblings; set
18036 all of the fields of all of the dies correctly. Arguments are as
18037 in read_die_and_children. */
18038
18039static struct die_info *
bf6af496 18040read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18041 const gdb_byte *info_ptr,
18042 const gdb_byte **new_info_ptr,
bf6af496 18043 struct die_info *parent)
639d11d3
DC
18044{
18045 struct die_info *first_die, *last_sibling;
d521ce57 18046 const gdb_byte *cur_ptr;
639d11d3 18047
c906108c 18048 cur_ptr = info_ptr;
639d11d3
DC
18049 first_die = last_sibling = NULL;
18050
18051 while (1)
c906108c 18052 {
639d11d3 18053 struct die_info *die
dee91e82 18054 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18055
1d325ec1 18056 if (die == NULL)
c906108c 18057 {
639d11d3
DC
18058 *new_info_ptr = cur_ptr;
18059 return first_die;
c906108c 18060 }
1d325ec1
DJ
18061
18062 if (!first_die)
18063 first_die = die;
c906108c 18064 else
1d325ec1
DJ
18065 last_sibling->sibling = die;
18066
18067 last_sibling = die;
c906108c 18068 }
c906108c
SS
18069}
18070
bf6af496
DE
18071/* Read a die, all of its descendents, and all of its siblings; set
18072 all of the fields of all of the dies correctly. Arguments are as
18073 in read_die_and_children.
18074 This the main entry point for reading a DIE and all its children. */
18075
18076static struct die_info *
18077read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18078 const gdb_byte *info_ptr,
18079 const gdb_byte **new_info_ptr,
bf6af496
DE
18080 struct die_info *parent)
18081{
18082 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18083 new_info_ptr, parent);
18084
b4f54984 18085 if (dwarf_die_debug)
bf6af496
DE
18086 {
18087 fprintf_unfiltered (gdb_stdlog,
18088 "Read die from %s@0x%x of %s:\n",
96b79293 18089 reader->die_section->get_name (),
bf6af496
DE
18090 (unsigned) (info_ptr - reader->die_section->buffer),
18091 bfd_get_filename (reader->abfd));
b4f54984 18092 dump_die (die, dwarf_die_debug);
bf6af496
DE
18093 }
18094
18095 return die;
18096}
18097
3019eac3
DE
18098/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18099 attributes.
18100 The caller is responsible for filling in the extra attributes
18101 and updating (*DIEP)->num_attrs.
18102 Set DIEP to point to a newly allocated die with its information,
3e225074 18103 except for its child, sibling, and parent fields. */
93311388 18104
d521ce57 18105static const gdb_byte *
3019eac3 18106read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18107 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 18108 int num_extra_attrs)
93311388 18109{
b64f50a1 18110 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18111 struct abbrev_info *abbrev;
18112 struct die_info *die;
18113 struct dwarf2_cu *cu = reader->cu;
18114 bfd *abfd = reader->abfd;
18115
9c541725 18116 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18117 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18118 info_ptr += bytes_read;
18119 if (!abbrev_number)
18120 {
18121 *diep = NULL;
93311388
DE
18122 return info_ptr;
18123 }
18124
685af9cd 18125 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18126 if (!abbrev)
348e048f
DE
18127 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18128 abbrev_number,
18129 bfd_get_filename (abfd));
18130
3019eac3 18131 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18132 die->sect_off = sect_off;
93311388
DE
18133 die->tag = abbrev->tag;
18134 die->abbrev = abbrev_number;
3e225074 18135 die->has_children = abbrev->has_children;
93311388 18136
3019eac3
DE
18137 /* Make the result usable.
18138 The caller needs to update num_attrs after adding the extra
18139 attributes. */
93311388
DE
18140 die->num_attrs = abbrev->num_attrs;
18141
18a8505e 18142 std::vector<int> indexes_that_need_reprocess;
93311388 18143 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
18144 {
18145 bool need_reprocess;
18146 info_ptr =
18147 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18148 info_ptr, &need_reprocess);
18149 if (need_reprocess)
18150 indexes_that_need_reprocess.push_back (i);
18151 }
18152
052c8bb8 18153 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18a8505e
AT
18154 if (attr != nullptr)
18155 cu->str_offsets_base = DW_UNSND (attr);
93311388 18156
41144253 18157 attr = die->attr (DW_AT_loclists_base);
18158 if (attr != nullptr)
18159 cu->loclist_base = DW_UNSND (attr);
18160
a39fdb41 18161 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
18162 if (maybe_addr_base.has_value ())
18163 cu->addr_base = *maybe_addr_base;
18164 for (int index : indexes_that_need_reprocess)
18165 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 18166 *diep = die;
93311388
DE
18167 return info_ptr;
18168}
18169
3019eac3
DE
18170/* Read a die and all its attributes.
18171 Set DIEP to point to a newly allocated die with its information,
3e225074 18172 except for its child, sibling, and parent fields. */
3019eac3 18173
d521ce57 18174static const gdb_byte *
3019eac3 18175read_full_die (const struct die_reader_specs *reader,
3e225074 18176 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 18177{
d521ce57 18178 const gdb_byte *result;
bf6af496 18179
3e225074 18180 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 18181
b4f54984 18182 if (dwarf_die_debug)
bf6af496
DE
18183 {
18184 fprintf_unfiltered (gdb_stdlog,
18185 "Read die from %s@0x%x of %s:\n",
96b79293 18186 reader->die_section->get_name (),
bf6af496
DE
18187 (unsigned) (info_ptr - reader->die_section->buffer),
18188 bfd_get_filename (reader->abfd));
b4f54984 18189 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18190 }
18191
18192 return result;
3019eac3 18193}
433df2d4 18194\f
c906108c 18195
72bf9492
DJ
18196/* Returns nonzero if TAG represents a type that we might generate a partial
18197 symbol for. */
18198
18199static int
18200is_type_tag_for_partial (int tag)
18201{
18202 switch (tag)
18203 {
18204#if 0
18205 /* Some types that would be reasonable to generate partial symbols for,
18206 that we don't at present. */
18207 case DW_TAG_array_type:
18208 case DW_TAG_file_type:
18209 case DW_TAG_ptr_to_member_type:
18210 case DW_TAG_set_type:
18211 case DW_TAG_string_type:
18212 case DW_TAG_subroutine_type:
18213#endif
18214 case DW_TAG_base_type:
18215 case DW_TAG_class_type:
680b30c7 18216 case DW_TAG_interface_type:
72bf9492
DJ
18217 case DW_TAG_enumeration_type:
18218 case DW_TAG_structure_type:
18219 case DW_TAG_subrange_type:
18220 case DW_TAG_typedef:
18221 case DW_TAG_union_type:
18222 return 1;
18223 default:
18224 return 0;
18225 }
18226}
18227
18228/* Load all DIEs that are interesting for partial symbols into memory. */
18229
18230static struct partial_die_info *
dee91e82 18231load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18232 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18233{
dee91e82 18234 struct dwarf2_cu *cu = reader->cu;
5e22e966 18235 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 18236 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18237 unsigned int bytes_read;
5afb4e99 18238 unsigned int load_all = 0;
72bf9492
DJ
18239 int nesting_level = 1;
18240
18241 parent_die = NULL;
18242 last_die = NULL;
18243
7adf1e79
DE
18244 gdb_assert (cu->per_cu != NULL);
18245 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18246 load_all = 1;
18247
72bf9492
DJ
18248 cu->partial_dies
18249 = htab_create_alloc_ex (cu->header.length / 12,
18250 partial_die_hash,
18251 partial_die_eq,
18252 NULL,
18253 &cu->comp_unit_obstack,
18254 hashtab_obstack_allocate,
18255 dummy_obstack_deallocate);
18256
72bf9492
DJ
18257 while (1)
18258 {
685af9cd 18259 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18260
18261 /* A NULL abbrev means the end of a series of children. */
18262 if (abbrev == NULL)
18263 {
18264 if (--nesting_level == 0)
cd9983dd
YQ
18265 return first_die;
18266
72bf9492
DJ
18267 info_ptr += bytes_read;
18268 last_die = parent_die;
18269 parent_die = parent_die->die_parent;
18270 continue;
18271 }
18272
98bfdba5
PA
18273 /* Check for template arguments. We never save these; if
18274 they're seen, we just mark the parent, and go on our way. */
18275 if (parent_die != NULL
18276 && cu->language == language_cplus
18277 && (abbrev->tag == DW_TAG_template_type_param
18278 || abbrev->tag == DW_TAG_template_value_param))
18279 {
18280 parent_die->has_template_arguments = 1;
18281
18282 if (!load_all)
18283 {
18284 /* We don't need a partial DIE for the template argument. */
dee91e82 18285 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18286 continue;
18287 }
18288 }
18289
0d99eb77 18290 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18291 Skip their other children. */
18292 if (!load_all
18293 && cu->language == language_cplus
18294 && parent_die != NULL
18295 && parent_die->tag == DW_TAG_subprogram)
18296 {
dee91e82 18297 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18298 continue;
18299 }
18300
5afb4e99
DJ
18301 /* Check whether this DIE is interesting enough to save. Normally
18302 we would not be interested in members here, but there may be
18303 later variables referencing them via DW_AT_specification (for
18304 static members). */
18305 if (!load_all
18306 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18307 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18308 && abbrev->tag != DW_TAG_enumerator
18309 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18310 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18311 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18312 && abbrev->tag != DW_TAG_variable
5afb4e99 18313 && abbrev->tag != DW_TAG_namespace
f55ee35c 18314 && abbrev->tag != DW_TAG_module
95554aad 18315 && abbrev->tag != DW_TAG_member
74921315
KS
18316 && abbrev->tag != DW_TAG_imported_unit
18317 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18318 {
18319 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18320 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18321 continue;
18322 }
18323
6f06d47b
YQ
18324 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18325 abbrev);
cd9983dd 18326
48fbe735 18327 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18328
18329 /* This two-pass algorithm for processing partial symbols has a
18330 high cost in cache pressure. Thus, handle some simple cases
18331 here which cover the majority of C partial symbols. DIEs
18332 which neither have specification tags in them, nor could have
18333 specification tags elsewhere pointing at them, can simply be
18334 processed and discarded.
18335
18336 This segment is also optional; scan_partial_symbols and
18337 add_partial_symbol will handle these DIEs if we chain
18338 them in normally. When compilers which do not emit large
18339 quantities of duplicate debug information are more common,
18340 this code can probably be removed. */
18341
18342 /* Any complete simple types at the top level (pretty much all
18343 of them, for a language without namespaces), can be processed
18344 directly. */
18345 if (parent_die == NULL
cd9983dd
YQ
18346 && pdi.has_specification == 0
18347 && pdi.is_declaration == 0
18348 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18349 || pdi.tag == DW_TAG_base_type
18350 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18351 {
cd9983dd 18352 if (building_psymtab && pdi.name != NULL)
31edb802 18353 add_psymbol_to_list (pdi.name, false,
79748972 18354 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18355 psymbol_placement::STATIC,
1762568f 18356 0, cu->language, objfile);
cd9983dd 18357 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18358 continue;
18359 }
18360
d8228535
JK
18361 /* The exception for DW_TAG_typedef with has_children above is
18362 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18363 type_name_or_error will error on such types later.
d8228535
JK
18364
18365 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18366 it could not find the child DIEs referenced later, this is checked
18367 above. In correct DWARF DW_TAG_typedef should have no children. */
18368
cd9983dd 18369 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18370 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18371 "- DIE at %s [in module %s]"),
cd9983dd 18372 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18373
72bf9492
DJ
18374 /* If we're at the second level, and we're an enumerator, and
18375 our parent has no specification (meaning possibly lives in a
18376 namespace elsewhere), then we can add the partial symbol now
18377 instead of queueing it. */
cd9983dd 18378 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18379 && parent_die != NULL
18380 && parent_die->die_parent == NULL
18381 && parent_die->tag == DW_TAG_enumeration_type
18382 && parent_die->has_specification == 0)
18383 {
cd9983dd 18384 if (pdi.name == NULL)
b98664d3 18385 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18386 else if (building_psymtab)
31edb802 18387 add_psymbol_to_list (pdi.name, false,
79748972 18388 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18389 cu->language == language_cplus
75aedd27
TT
18390 ? psymbol_placement::GLOBAL
18391 : psymbol_placement::STATIC,
1762568f 18392 0, cu->language, objfile);
72bf9492 18393
cd9983dd 18394 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18395 continue;
18396 }
18397
cd9983dd 18398 struct partial_die_info *part_die
6f06d47b 18399 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18400
72bf9492
DJ
18401 /* We'll save this DIE so link it in. */
18402 part_die->die_parent = parent_die;
18403 part_die->die_sibling = NULL;
18404 part_die->die_child = NULL;
18405
18406 if (last_die && last_die == parent_die)
18407 last_die->die_child = part_die;
18408 else if (last_die)
18409 last_die->die_sibling = part_die;
18410
18411 last_die = part_die;
18412
18413 if (first_die == NULL)
18414 first_die = part_die;
18415
18416 /* Maybe add the DIE to the hash table. Not all DIEs that we
18417 find interesting need to be in the hash table, because we
18418 also have the parent/sibling/child chains; only those that we
18419 might refer to by offset later during partial symbol reading.
18420
18421 For now this means things that might have be the target of a
18422 DW_AT_specification, DW_AT_abstract_origin, or
18423 DW_AT_extension. DW_AT_extension will refer only to
18424 namespaces; DW_AT_abstract_origin refers to functions (and
18425 many things under the function DIE, but we do not recurse
18426 into function DIEs during partial symbol reading) and
18427 possibly variables as well; DW_AT_specification refers to
18428 declarations. Declarations ought to have the DW_AT_declaration
18429 flag. It happens that GCC forgets to put it in sometimes, but
18430 only for functions, not for types.
18431
18432 Adding more things than necessary to the hash table is harmless
18433 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18434 wasted time in find_partial_die, when we reread the compilation
18435 unit with load_all_dies set. */
72bf9492 18436
5afb4e99 18437 if (load_all
72929c62 18438 || abbrev->tag == DW_TAG_constant
5afb4e99 18439 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18440 || abbrev->tag == DW_TAG_variable
18441 || abbrev->tag == DW_TAG_namespace
18442 || part_die->is_declaration)
18443 {
18444 void **slot;
18445
18446 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18447 to_underlying (part_die->sect_off),
18448 INSERT);
72bf9492
DJ
18449 *slot = part_die;
18450 }
18451
72bf9492 18452 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18453 we have no reason to follow the children of structures; for other
98bfdba5
PA
18454 languages we have to, so that we can get at method physnames
18455 to infer fully qualified class names, for DW_AT_specification,
18456 and for C++ template arguments. For C++, we also look one level
18457 inside functions to find template arguments (if the name of the
18458 function does not already contain the template arguments).
bc30ff58 18459
0a4b0913
AB
18460 For Ada and Fortran, we need to scan the children of subprograms
18461 and lexical blocks as well because these languages allow the
18462 definition of nested entities that could be interesting for the
18463 debugger, such as nested subprograms for instance. */
72bf9492 18464 if (last_die->has_children
5afb4e99
DJ
18465 && (load_all
18466 || last_die->tag == DW_TAG_namespace
f55ee35c 18467 || last_die->tag == DW_TAG_module
72bf9492 18468 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18469 || (cu->language == language_cplus
18470 && last_die->tag == DW_TAG_subprogram
18471 && (last_die->name == NULL
18472 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18473 || (cu->language != language_c
18474 && (last_die->tag == DW_TAG_class_type
680b30c7 18475 || last_die->tag == DW_TAG_interface_type
72bf9492 18476 || last_die->tag == DW_TAG_structure_type
bc30ff58 18477 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18478 || ((cu->language == language_ada
18479 || cu->language == language_fortran)
bc30ff58
JB
18480 && (last_die->tag == DW_TAG_subprogram
18481 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18482 {
18483 nesting_level++;
18484 parent_die = last_die;
18485 continue;
18486 }
18487
18488 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18489 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18490
18491 /* Back to the top, do it again. */
18492 }
18493}
18494
6f06d47b
YQ
18495partial_die_info::partial_die_info (sect_offset sect_off_,
18496 struct abbrev_info *abbrev)
18497 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18498{
18499}
18500
35cc7ed7
YQ
18501/* Read a minimal amount of information into the minimal die structure.
18502 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18503
48fbe735
YQ
18504const gdb_byte *
18505partial_die_info::read (const struct die_reader_specs *reader,
18506 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18507{
dee91e82 18508 struct dwarf2_cu *cu = reader->cu;
5e22e966 18509 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
fa238c03 18510 unsigned int i;
c5aa993b 18511 int has_low_pc_attr = 0;
c906108c 18512 int has_high_pc_attr = 0;
91da1414 18513 int high_pc_relative = 0;
c906108c 18514
fd0a254f 18515 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18516 {
e7da7f8f 18517 attribute attr;
18a8505e 18518 bool need_reprocess;
e7da7f8f 18519 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18a8505e
AT
18520 info_ptr, &need_reprocess);
18521 /* String and address offsets that need to do the reprocessing have
18522 already been read at this point, so there is no need to wait until
18523 the loop terminates to do the reprocessing. */
18524 if (need_reprocess)
e7da7f8f 18525 read_attribute_reprocess (reader, &attr);
c906108c 18526 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18527 partial symbol table. */
c906108c
SS
18528 switch (attr.name)
18529 {
18530 case DW_AT_name:
48fbe735 18531 switch (tag)
71c25dea
TT
18532 {
18533 case DW_TAG_compile_unit:
95554aad 18534 case DW_TAG_partial_unit:
348e048f 18535 case DW_TAG_type_unit:
71c25dea
TT
18536 /* Compilation units have a DW_AT_name that is a filename, not
18537 a source language identifier. */
18538 case DW_TAG_enumeration_type:
18539 case DW_TAG_enumerator:
18540 /* These tags always have simple identifiers already; no need
18541 to canonicalize them. */
48fbe735 18542 name = DW_STRING (&attr);
71c25dea
TT
18543 break;
18544 default:
48fbe735
YQ
18545 {
18546 struct objfile *objfile = dwarf2_per_objfile->objfile;
18547
18548 name
be1e3d3e 18549 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
48fbe735 18550 }
71c25dea
TT
18551 break;
18552 }
c906108c 18553 break;
31ef98ae 18554 case DW_AT_linkage_name:
c906108c 18555 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18556 /* Note that both forms of linkage name might appear. We
18557 assume they will be the same, and we only store the last
18558 one we see. */
e61108c9 18559 linkage_name = attr.value_as_string ();
787de330
TT
18560 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18561 See https://github.com/rust-lang/rust/issues/32925. */
18562 if (cu->language == language_rust && linkage_name != NULL
18563 && strchr (linkage_name, '{') != NULL)
18564 linkage_name = NULL;
c906108c
SS
18565 break;
18566 case DW_AT_low_pc:
18567 has_low_pc_attr = 1;
cd6c91b4 18568 lowpc = attr.value_as_address ();
c906108c
SS
18569 break;
18570 case DW_AT_high_pc:
18571 has_high_pc_attr = 1;
cd6c91b4
TT
18572 highpc = attr.value_as_address ();
18573 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 18574 high_pc_relative = 1;
c906108c
SS
18575 break;
18576 case DW_AT_location:
0963b4bd 18577 /* Support the .debug_loc offsets. */
4fc6c0d5 18578 if (attr.form_is_block ())
8e19ed76 18579 {
48fbe735 18580 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18581 }
cd6c91b4 18582 else if (attr.form_is_section_offset ())
8e19ed76 18583 {
4d3c2250 18584 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18585 }
18586 else
18587 {
4d3c2250
KB
18588 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18589 "partial symbol information");
8e19ed76 18590 }
c906108c 18591 break;
c906108c 18592 case DW_AT_external:
48fbe735 18593 is_external = DW_UNSND (&attr);
c906108c
SS
18594 break;
18595 case DW_AT_declaration:
48fbe735 18596 is_declaration = DW_UNSND (&attr);
c906108c
SS
18597 break;
18598 case DW_AT_type:
48fbe735 18599 has_type = 1;
c906108c
SS
18600 break;
18601 case DW_AT_abstract_origin:
18602 case DW_AT_specification:
72bf9492 18603 case DW_AT_extension:
48fbe735 18604 has_specification = 1;
0826b30a 18605 spec_offset = attr.get_ref_die_offset ();
48fbe735 18606 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18607 || cu->per_cu->is_dwz);
c906108c
SS
18608 break;
18609 case DW_AT_sibling:
18610 /* Ignore absolute siblings, they might point outside of
18611 the current compile unit. */
18612 if (attr.form == DW_FORM_ref_addr)
b98664d3 18613 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18614 else
b9502d3f 18615 {
48fbe735 18616 const gdb_byte *buffer = reader->buffer;
0826b30a 18617 sect_offset off = attr.get_ref_die_offset ();
9c541725 18618 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18619
18620 if (sibling_ptr < info_ptr)
b98664d3 18621 complaint (_("DW_AT_sibling points backwards"));
22869d73 18622 else if (sibling_ptr > reader->buffer_end)
a0194fa8 18623 reader->die_section->overflow_complaint ();
b9502d3f 18624 else
48fbe735 18625 sibling = sibling_ptr;
b9502d3f 18626 }
c906108c 18627 break;
fa4028e9 18628 case DW_AT_byte_size:
48fbe735 18629 has_byte_size = 1;
fa4028e9 18630 break;
ff908ebf 18631 case DW_AT_const_value:
48fbe735 18632 has_const_value = 1;
ff908ebf 18633 break;
68511cec
CES
18634 case DW_AT_calling_convention:
18635 /* DWARF doesn't provide a way to identify a program's source-level
18636 entry point. DW_AT_calling_convention attributes are only meant
18637 to describe functions' calling conventions.
18638
18639 However, because it's a necessary piece of information in
0c1b455e
TT
18640 Fortran, and before DWARF 4 DW_CC_program was the only
18641 piece of debugging information whose definition refers to
18642 a 'main program' at all, several compilers marked Fortran
18643 main programs with DW_CC_program --- even when those
18644 functions use the standard calling conventions.
18645
18646 Although DWARF now specifies a way to provide this
18647 information, we support this practice for backward
18648 compatibility. */
68511cec 18649 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18650 && cu->language == language_fortran)
48fbe735 18651 main_subprogram = 1;
68511cec 18652 break;
481860b3
GB
18653 case DW_AT_inline:
18654 if (DW_UNSND (&attr) == DW_INL_inlined
18655 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18656 may_be_inlined = 1;
481860b3 18657 break;
95554aad
TT
18658
18659 case DW_AT_import:
48fbe735 18660 if (tag == DW_TAG_imported_unit)
36586728 18661 {
0826b30a 18662 d.sect_off = attr.get_ref_die_offset ();
48fbe735 18663 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18664 || cu->per_cu->is_dwz);
18665 }
95554aad
TT
18666 break;
18667
0c1b455e 18668 case DW_AT_main_subprogram:
48fbe735 18669 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18670 break;
18671
05caa1d2
TT
18672 case DW_AT_ranges:
18673 {
18674 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18675 but that requires a full DIE, so instead we just
18676 reimplement it. */
18677 int need_ranges_base = tag != DW_TAG_compile_unit;
18678 unsigned int ranges_offset = (DW_UNSND (&attr)
18679 + (need_ranges_base
18680 ? cu->ranges_base
18681 : 0));
18682
18683 /* Value of the DW_AT_ranges attribute is the offset in the
18684 .debug_ranges section. */
18685 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18686 nullptr))
18687 has_pc_info = 1;
18688 }
18689 break;
18690
c906108c
SS
18691 default:
18692 break;
18693 }
18694 }
18695
10d06d82
TT
18696 /* For Ada, if both the name and the linkage name appear, we prefer
18697 the latter. This lets "catch exception" work better, regardless
18698 of the order in which the name and linkage name were emitted.
18699 Really, though, this is just a workaround for the fact that gdb
18700 doesn't store both the name and the linkage name. */
18701 if (cu->language == language_ada && linkage_name != nullptr)
18702 name = linkage_name;
18703
91da1414 18704 if (high_pc_relative)
48fbe735 18705 highpc += lowpc;
91da1414 18706
9373cf26
JK
18707 if (has_low_pc_attr && has_high_pc_attr)
18708 {
18709 /* When using the GNU linker, .gnu.linkonce. sections are used to
18710 eliminate duplicate copies of functions and vtables and such.
18711 The linker will arbitrarily choose one and discard the others.
18712 The AT_*_pc values for such functions refer to local labels in
18713 these sections. If the section from that file was discarded, the
18714 labels are not in the output, so the relocs get a value of 0.
18715 If this is a discarded function, mark the pc bounds as invalid,
18716 so that GDB will ignore it. */
5989a64e 18717 if (lowpc == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
9373cf26 18718 {
48fbe735 18719 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18720 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18721
b98664d3 18722 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18723 "for DIE at %s [in module %s]"),
48fbe735
YQ
18724 paddress (gdbarch, lowpc),
18725 sect_offset_str (sect_off),
9d8780f0 18726 objfile_name (objfile));
9373cf26
JK
18727 }
18728 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18729 else if (lowpc >= highpc)
9373cf26 18730 {
48fbe735 18731 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18732 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18733
b98664d3 18734 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18735 "for DIE at %s [in module %s]"),
48fbe735
YQ
18736 paddress (gdbarch, lowpc),
18737 paddress (gdbarch, highpc),
18738 sect_offset_str (sect_off),
9c541725 18739 objfile_name (objfile));
9373cf26
JK
18740 }
18741 else
48fbe735 18742 has_pc_info = 1;
9373cf26 18743 }
85cbf3d3 18744
c906108c
SS
18745 return info_ptr;
18746}
18747
72bf9492
DJ
18748/* Find a cached partial DIE at OFFSET in CU. */
18749
d590ff25
YQ
18750struct partial_die_info *
18751dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18752{
18753 struct partial_die_info *lookup_die = NULL;
6f06d47b 18754 struct partial_die_info part_die (sect_off);
72bf9492 18755
9a3c8263 18756 lookup_die = ((struct partial_die_info *)
d590ff25 18757 htab_find_with_hash (partial_dies, &part_die,
9c541725 18758 to_underlying (sect_off)));
72bf9492 18759
72bf9492
DJ
18760 return lookup_die;
18761}
18762
348e048f
DE
18763/* Find a partial DIE at OFFSET, which may or may not be in CU,
18764 except in the case of .debug_types DIEs which do not reference
18765 outside their CU (they do however referencing other types via
55f1336d 18766 DW_FORM_ref_sig8). */
72bf9492 18767
122cf0f2 18768static const struct cu_partial_die_info
9c541725 18769find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18770{
5e22e966 18771 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 18772 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99 18773 struct partial_die_info *pd = NULL;
72bf9492 18774
36586728 18775 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18776 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18777 {
d590ff25 18778 pd = cu->find_partial_die (sect_off);
5afb4e99 18779 if (pd != NULL)
fb816e8b 18780 return { cu, pd };
0d99eb77
DE
18781 /* We missed recording what we needed.
18782 Load all dies and try again. */
5afb4e99 18783 }
0d99eb77
DE
18784 else
18785 {
18786 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18787 if (cu->per_cu->is_debug_types)
0d99eb77 18788 {
9d8780f0
SM
18789 error (_("Dwarf Error: Type Unit at offset %s contains"
18790 " external reference to offset %s [in module %s].\n"),
18791 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18792 bfd_get_filename (objfile->obfd));
18793 }
7188ed02
SM
18794 dwarf2_per_cu_data *per_cu
18795 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18796 dwarf2_per_objfile);
72bf9492 18797
7188ed02
SM
18798 cu = dwarf2_per_objfile->get_cu (per_cu);
18799 if (cu == NULL || cu->partial_dies == NULL)
18800 load_partial_comp_unit (per_cu, dwarf2_per_objfile, nullptr);
ae038cb0 18801
7188ed02
SM
18802 cu = dwarf2_per_objfile->get_cu (per_cu);
18803
18804 cu->last_used = 0;
18805 pd = cu->find_partial_die (sect_off);
0d99eb77 18806 }
5afb4e99 18807
dee91e82
DE
18808 /* If we didn't find it, and not all dies have been loaded,
18809 load them all and try again. */
18810
7188ed02 18811 if (pd == NULL && cu->per_cu->load_all_dies == 0)
5afb4e99 18812 {
7188ed02 18813 cu->per_cu->load_all_dies = 1;
fd820528
DE
18814
18815 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18816 THIS_CU->cu may already be in use. So we can't just free it and
18817 replace its DIEs with the ones we read in. Instead, we leave those
18818 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18819 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18820 set. */
7188ed02 18821 load_partial_comp_unit (cu->per_cu, dwarf2_per_objfile, cu);
5afb4e99 18822
7188ed02 18823 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
18824 }
18825
18826 if (pd == NULL)
18827 internal_error (__FILE__, __LINE__,
9d8780f0 18828 _("could not find partial DIE %s "
3e43a32a 18829 "in cache [from module %s]\n"),
9d8780f0 18830 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
7188ed02 18831 return { cu, pd };
72bf9492
DJ
18832}
18833
abc72ce4
DE
18834/* See if we can figure out if the class lives in a namespace. We do
18835 this by looking for a member function; its demangled name will
18836 contain namespace info, if there is any. */
18837
18838static void
18839guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18840 struct dwarf2_cu *cu)
18841{
18842 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18843 what template types look like, because the demangler
18844 frequently doesn't give the same name as the debug info. We
18845 could fix this by only using the demangled name to get the
18846 prefix (but see comment in read_structure_type). */
18847
18848 struct partial_die_info *real_pdi;
18849 struct partial_die_info *child_pdi;
18850
18851 /* If this DIE (this DIE's specification, if any) has a parent, then
18852 we should not do this. We'll prepend the parent's fully qualified
18853 name when we create the partial symbol. */
18854
18855 real_pdi = struct_pdi;
18856 while (real_pdi->has_specification)
fb816e8b 18857 {
122cf0f2
AB
18858 auto res = find_partial_die (real_pdi->spec_offset,
18859 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18860 real_pdi = res.pdi;
18861 cu = res.cu;
18862 }
abc72ce4
DE
18863
18864 if (real_pdi->die_parent != NULL)
18865 return;
18866
18867 for (child_pdi = struct_pdi->die_child;
18868 child_pdi != NULL;
18869 child_pdi = child_pdi->die_sibling)
18870 {
18871 if (child_pdi->tag == DW_TAG_subprogram
18872 && child_pdi->linkage_name != NULL)
18873 {
43816ebc
TT
18874 gdb::unique_xmalloc_ptr<char> actual_class_name
18875 (language_class_name_from_physname (cu->language_defn,
18876 child_pdi->linkage_name));
abc72ce4
DE
18877 if (actual_class_name != NULL)
18878 {
5e22e966 18879 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 18880 struct_pdi->name = objfile->intern (actual_class_name.get ());
abc72ce4
DE
18881 }
18882 break;
18883 }
18884 }
18885}
18886
25c11aca
TV
18887/* Return true if a DIE with TAG may have the DW_AT_const_value
18888 attribute. */
18889
18890static bool
18891can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18892{
18893 switch (tag)
18894 {
18895 case DW_TAG_constant:
18896 case DW_TAG_enumerator:
18897 case DW_TAG_formal_parameter:
18898 case DW_TAG_template_value_param:
18899 case DW_TAG_variable:
18900 return true;
18901 }
18902
18903 return false;
18904}
18905
52356b79
YQ
18906void
18907partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18908{
abc72ce4
DE
18909 /* Once we've fixed up a die, there's no point in doing so again.
18910 This also avoids a memory leak if we were to call
18911 guess_partial_die_structure_name multiple times. */
52356b79 18912 if (fixup_called)
abc72ce4
DE
18913 return;
18914
72bf9492
DJ
18915 /* If we found a reference attribute and the DIE has no name, try
18916 to find a name in the referred to DIE. */
18917
52356b79 18918 if (name == NULL && has_specification)
72bf9492
DJ
18919 {
18920 struct partial_die_info *spec_die;
72bf9492 18921
122cf0f2 18922 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18923 spec_die = res.pdi;
18924 cu = res.cu;
72bf9492 18925
52356b79 18926 spec_die->fixup (cu);
72bf9492
DJ
18927
18928 if (spec_die->name)
18929 {
52356b79 18930 name = spec_die->name;
72bf9492
DJ
18931
18932 /* Copy DW_AT_external attribute if it is set. */
18933 if (spec_die->is_external)
52356b79 18934 is_external = spec_die->is_external;
72bf9492
DJ
18935 }
18936 }
18937
25c11aca
TV
18938 if (!has_const_value && has_specification
18939 && can_have_DW_AT_const_value_p (tag))
18940 {
18941 struct partial_die_info *spec_die;
18942
18943 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18944 spec_die = res.pdi;
18945 cu = res.cu;
18946
18947 spec_die->fixup (cu);
18948
18949 if (spec_die->has_const_value)
18950 {
18951 /* Copy DW_AT_const_value attribute if it is set. */
18952 has_const_value = spec_die->has_const_value;
18953 }
18954 }
18955
72bf9492 18956 /* Set default names for some unnamed DIEs. */
72bf9492 18957
52356b79
YQ
18958 if (name == NULL && tag == DW_TAG_namespace)
18959 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18960
abc72ce4
DE
18961 /* If there is no parent die to provide a namespace, and there are
18962 children, see if we can determine the namespace from their linkage
122d1940 18963 name. */
abc72ce4 18964 if (cu->language == language_cplus
5e22e966 18965 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
18966 && die_parent == NULL
18967 && has_children
18968 && (tag == DW_TAG_class_type
18969 || tag == DW_TAG_structure_type
18970 || tag == DW_TAG_union_type))
18971 guess_partial_die_structure_name (this, cu);
abc72ce4 18972
53832f31
TT
18973 /* GCC might emit a nameless struct or union that has a linkage
18974 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18975 if (name == NULL
18976 && (tag == DW_TAG_class_type
18977 || tag == DW_TAG_interface_type
18978 || tag == DW_TAG_structure_type
18979 || tag == DW_TAG_union_type)
18980 && linkage_name != NULL)
53832f31 18981 {
43816ebc
TT
18982 gdb::unique_xmalloc_ptr<char> demangled
18983 (gdb_demangle (linkage_name, DMGL_TYPES));
18984 if (demangled != nullptr)
53832f31 18985 {
96408a79
SA
18986 const char *base;
18987
18988 /* Strip any leading namespaces/classes, keep only the base name.
18989 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18990 base = strrchr (demangled.get (), ':');
18991 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18992 base++;
18993 else
43816ebc 18994 base = demangled.get ();
96408a79 18995
5e22e966 18996 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 18997 name = objfile->intern (base);
53832f31
TT
18998 }
18999 }
19000
52356b79 19001 fixup_called = 1;
72bf9492
DJ
19002}
19003
41144253 19004/* Read the .debug_loclists header contents from the given SECTION in the
19005 HEADER. */
19006static void
19007read_loclist_header (struct loclist_header *header,
19008 struct dwarf2_section_info *section)
19009{
19010 unsigned int bytes_read;
19011 bfd *abfd = section->get_bfd_owner ();
19012 const gdb_byte *info_ptr = section->buffer;
19013 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19014 info_ptr += bytes_read;
19015 header->version = read_2_bytes (abfd, info_ptr);
19016 info_ptr += 2;
19017 header->addr_size = read_1_byte (abfd, info_ptr);
19018 info_ptr += 1;
19019 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19020 info_ptr += 1;
19021 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19022}
19023
19024/* Return the DW_AT_loclists_base value for the CU. */
19025static ULONGEST
19026lookup_loclist_base (struct dwarf2_cu *cu)
19027{
19028 /* For the .dwo unit, the loclist_base points to the first offset following
19029 the header. The header consists of the following entities-
19030 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19031 bit format)
19032 2. version (2 bytes)
19033 3. address size (1 byte)
19034 4. segment selector size (1 byte)
19035 5. offset entry count (4 bytes)
19036 These sizes are derived as per the DWARFv5 standard. */
19037 if (cu->dwo_unit != nullptr)
19038 {
19039 if (cu->header.initial_length_size == 4)
19040 return LOCLIST_HEADER_SIZE32;
19041 return LOCLIST_HEADER_SIZE64;
19042 }
19043 return cu->loclist_base;
19044}
19045
19046/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19047 array of offsets in the .debug_loclists section. */
19048static CORE_ADDR
19049read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19050{
5e22e966 19051 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
41144253 19052 struct objfile *objfile = dwarf2_per_objfile->objfile;
19053 bfd *abfd = objfile->obfd;
19054 ULONGEST loclist_base = lookup_loclist_base (cu);
19055 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19056
19057 section->read (objfile);
19058 if (section->buffer == NULL)
19059 complaint (_("DW_FORM_loclistx used without .debug_loclists "
19060 "section [in module %s]"), objfile_name (objfile));
19061 struct loclist_header header;
19062 read_loclist_header (&header, section);
19063 if (loclist_index >= header.offset_entry_count)
19064 complaint (_("DW_FORM_loclistx pointing outside of "
19065 ".debug_loclists offset array [in module %s]"),
19066 objfile_name (objfile));
19067 if (loclist_base + loclist_index * cu->header.offset_size
19068 >= section->size)
19069 complaint (_("DW_FORM_loclistx pointing outside of "
19070 ".debug_loclists section [in module %s]"),
19071 objfile_name (objfile));
19072 const gdb_byte *info_ptr
19073 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
19074
19075 if (cu->header.offset_size == 4)
19076 return bfd_get_32 (abfd, info_ptr) + loclist_base;
19077 else
19078 return bfd_get_64 (abfd, info_ptr) + loclist_base;
19079}
19080
18a8505e
AT
19081/* Process the attributes that had to be skipped in the first round. These
19082 attributes are the ones that need str_offsets_base or addr_base attributes.
19083 They could not have been processed in the first round, because at the time
19084 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
19085static void
19086read_attribute_reprocess (const struct die_reader_specs *reader,
19087 struct attribute *attr)
18a8505e
AT
19088{
19089 struct dwarf2_cu *cu = reader->cu;
19090 switch (attr->form)
19091 {
19092 case DW_FORM_addrx:
19093 case DW_FORM_GNU_addr_index:
19094 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19095 break;
41144253 19096 case DW_FORM_loclistx:
19097 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
19098 break;
18a8505e
AT
19099 case DW_FORM_strx:
19100 case DW_FORM_strx1:
19101 case DW_FORM_strx2:
19102 case DW_FORM_strx3:
19103 case DW_FORM_strx4:
19104 case DW_FORM_GNU_str_index:
19105 {
19106 unsigned int str_index = DW_UNSND (attr);
19107 if (reader->dwo_file != NULL)
19108 {
19109 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19110 DW_STRING_IS_CANONICAL (attr) = 0;
19111 }
19112 else
19113 {
19114 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19115 DW_STRING_IS_CANONICAL (attr) = 0;
19116 }
19117 break;
19118 }
19119 default:
19120 gdb_assert_not_reached (_("Unexpected DWARF form."));
19121 }
19122}
19123
a8329558 19124/* Read an attribute value described by an attribute form. */
c906108c 19125
d521ce57 19126static const gdb_byte *
dee91e82
DE
19127read_attribute_value (const struct die_reader_specs *reader,
19128 struct attribute *attr, unsigned form,
18a8505e
AT
19129 LONGEST implicit_const, const gdb_byte *info_ptr,
19130 bool *need_reprocess)
c906108c 19131{
dee91e82 19132 struct dwarf2_cu *cu = reader->cu;
5e22e966 19133 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 19134 struct objfile *objfile = dwarf2_per_objfile->objfile;
dee91e82 19135 bfd *abfd = reader->abfd;
e7c27a73 19136 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19137 unsigned int bytes_read;
19138 struct dwarf_block *blk;
18a8505e 19139 *need_reprocess = false;
c906108c 19140
aead7601 19141 attr->form = (enum dwarf_form) form;
a8329558 19142 switch (form)
c906108c 19143 {
c906108c 19144 case DW_FORM_ref_addr:
ae411497 19145 if (cu->header.version == 2)
c8a7a66f
TT
19146 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19147 &bytes_read);
ae411497 19148 else
8266302d
TT
19149 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19150 &bytes_read);
ae411497
TT
19151 info_ptr += bytes_read;
19152 break;
36586728 19153 case DW_FORM_GNU_ref_alt:
8266302d 19154 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
19155 info_ptr += bytes_read;
19156 break;
ae411497 19157 case DW_FORM_addr:
08feed99
TT
19158 {
19159 struct gdbarch *gdbarch = objfile->arch ();
19160 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19161 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19162 info_ptr += bytes_read;
19163 }
c906108c
SS
19164 break;
19165 case DW_FORM_block2:
7b5a2f43 19166 blk = dwarf_alloc_block (cu);
c906108c
SS
19167 blk->size = read_2_bytes (abfd, info_ptr);
19168 info_ptr += 2;
19169 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19170 info_ptr += blk->size;
19171 DW_BLOCK (attr) = blk;
19172 break;
19173 case DW_FORM_block4:
7b5a2f43 19174 blk = dwarf_alloc_block (cu);
c906108c
SS
19175 blk->size = read_4_bytes (abfd, info_ptr);
19176 info_ptr += 4;
19177 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19178 info_ptr += blk->size;
19179 DW_BLOCK (attr) = blk;
19180 break;
19181 case DW_FORM_data2:
19182 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19183 info_ptr += 2;
19184 break;
19185 case DW_FORM_data4:
19186 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19187 info_ptr += 4;
19188 break;
19189 case DW_FORM_data8:
19190 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19191 info_ptr += 8;
19192 break;
0224619f
JK
19193 case DW_FORM_data16:
19194 blk = dwarf_alloc_block (cu);
19195 blk->size = 16;
19196 blk->data = read_n_bytes (abfd, info_ptr, 16);
19197 info_ptr += 16;
19198 DW_BLOCK (attr) = blk;
19199 break;
2dc7f7b3 19200 case DW_FORM_sec_offset:
8266302d 19201 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
19202 info_ptr += bytes_read;
19203 break;
41144253 19204 case DW_FORM_loclistx:
19205 {
19206 *need_reprocess = true;
19207 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19208 info_ptr += bytes_read;
19209 }
19210 break;
c906108c 19211 case DW_FORM_string:
9b1c24c8 19212 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19213 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19214 info_ptr += bytes_read;
19215 break;
4bdf3d34 19216 case DW_FORM_strp:
36586728
TT
19217 if (!cu->per_cu->is_dwz)
19218 {
ed2dc618
SM
19219 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19220 abfd, info_ptr, cu_header,
36586728
TT
19221 &bytes_read);
19222 DW_STRING_IS_CANONICAL (attr) = 0;
19223 info_ptr += bytes_read;
19224 break;
19225 }
19226 /* FALLTHROUGH */
43988095
JK
19227 case DW_FORM_line_strp:
19228 if (!cu->per_cu->is_dwz)
19229 {
86c0bb4c
TT
19230 DW_STRING (attr)
19231 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19232 &bytes_read);
43988095
JK
19233 DW_STRING_IS_CANONICAL (attr) = 0;
19234 info_ptr += bytes_read;
19235 break;
19236 }
19237 /* FALLTHROUGH */
36586728
TT
19238 case DW_FORM_GNU_strp_alt:
19239 {
c3699833 19240 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
8266302d
TT
19241 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19242 &bytes_read);
36586728 19243
0314b390 19244 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
36586728
TT
19245 DW_STRING_IS_CANONICAL (attr) = 0;
19246 info_ptr += bytes_read;
19247 }
4bdf3d34 19248 break;
2dc7f7b3 19249 case DW_FORM_exprloc:
c906108c 19250 case DW_FORM_block:
7b5a2f43 19251 blk = dwarf_alloc_block (cu);
c906108c
SS
19252 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19253 info_ptr += bytes_read;
19254 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19255 info_ptr += blk->size;
19256 DW_BLOCK (attr) = blk;
19257 break;
19258 case DW_FORM_block1:
7b5a2f43 19259 blk = dwarf_alloc_block (cu);
c906108c
SS
19260 blk->size = read_1_byte (abfd, info_ptr);
19261 info_ptr += 1;
19262 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19263 info_ptr += blk->size;
19264 DW_BLOCK (attr) = blk;
19265 break;
19266 case DW_FORM_data1:
19267 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19268 info_ptr += 1;
19269 break;
19270 case DW_FORM_flag:
19271 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19272 info_ptr += 1;
19273 break;
2dc7f7b3
TT
19274 case DW_FORM_flag_present:
19275 DW_UNSND (attr) = 1;
19276 break;
c906108c
SS
19277 case DW_FORM_sdata:
19278 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19279 info_ptr += bytes_read;
19280 break;
19281 case DW_FORM_udata:
18a8505e 19282 case DW_FORM_rnglistx:
c906108c
SS
19283 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19284 info_ptr += bytes_read;
19285 break;
19286 case DW_FORM_ref1:
9c541725 19287 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19288 + read_1_byte (abfd, info_ptr));
c906108c
SS
19289 info_ptr += 1;
19290 break;
19291 case DW_FORM_ref2:
9c541725 19292 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19293 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19294 info_ptr += 2;
19295 break;
19296 case DW_FORM_ref4:
9c541725 19297 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19298 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19299 info_ptr += 4;
19300 break;
613e1657 19301 case DW_FORM_ref8:
9c541725 19302 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19303 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19304 info_ptr += 8;
19305 break;
55f1336d 19306 case DW_FORM_ref_sig8:
ac9ec31b 19307 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19308 info_ptr += 8;
19309 break;
c906108c 19310 case DW_FORM_ref_udata:
9c541725 19311 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19312 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19313 info_ptr += bytes_read;
19314 break;
c906108c 19315 case DW_FORM_indirect:
a8329558
KW
19316 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19317 info_ptr += bytes_read;
43988095
JK
19318 if (form == DW_FORM_implicit_const)
19319 {
19320 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19321 info_ptr += bytes_read;
19322 }
19323 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 19324 info_ptr, need_reprocess);
43988095
JK
19325 break;
19326 case DW_FORM_implicit_const:
19327 DW_SND (attr) = implicit_const;
a8329558 19328 break;
336d760d 19329 case DW_FORM_addrx:
3019eac3 19330 case DW_FORM_GNU_addr_index:
18a8505e
AT
19331 *need_reprocess = true;
19332 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
19333 info_ptr += bytes_read;
19334 break;
cf532bd1 19335 case DW_FORM_strx:
15f18d14
AT
19336 case DW_FORM_strx1:
19337 case DW_FORM_strx2:
19338 case DW_FORM_strx3:
19339 case DW_FORM_strx4:
3019eac3 19340 case DW_FORM_GNU_str_index:
3019eac3 19341 {
15f18d14
AT
19342 ULONGEST str_index;
19343 if (form == DW_FORM_strx1)
19344 {
19345 str_index = read_1_byte (abfd, info_ptr);
19346 info_ptr += 1;
19347 }
19348 else if (form == DW_FORM_strx2)
19349 {
19350 str_index = read_2_bytes (abfd, info_ptr);
19351 info_ptr += 2;
19352 }
19353 else if (form == DW_FORM_strx3)
19354 {
19355 str_index = read_3_bytes (abfd, info_ptr);
19356 info_ptr += 3;
19357 }
19358 else if (form == DW_FORM_strx4)
19359 {
19360 str_index = read_4_bytes (abfd, info_ptr);
19361 info_ptr += 4;
19362 }
19363 else
19364 {
19365 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19366 info_ptr += bytes_read;
19367 }
18a8505e
AT
19368 *need_reprocess = true;
19369 DW_UNSND (attr) = str_index;
19370 }
3019eac3 19371 break;
c906108c 19372 default:
8a3fe4f8 19373 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19374 dwarf_form_name (form),
19375 bfd_get_filename (abfd));
c906108c 19376 }
28e94949 19377
36586728 19378 /* Super hack. */
cd6c91b4 19379 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
19380 attr->form = DW_FORM_GNU_ref_alt;
19381
28e94949
JB
19382 /* We have seen instances where the compiler tried to emit a byte
19383 size attribute of -1 which ended up being encoded as an unsigned
19384 0xffffffff. Although 0xffffffff is technically a valid size value,
19385 an object of this size seems pretty unlikely so we can relatively
19386 safely treat these cases as if the size attribute was invalid and
19387 treat them as zero by default. */
19388 if (attr->name == DW_AT_byte_size
19389 && form == DW_FORM_data4
19390 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19391 {
19392 complaint
b98664d3 19393 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19394 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19395 DW_UNSND (attr) = 0;
19396 }
28e94949 19397
c906108c
SS
19398 return info_ptr;
19399}
19400
a8329558
KW
19401/* Read an attribute described by an abbreviated attribute. */
19402
d521ce57 19403static const gdb_byte *
dee91e82
DE
19404read_attribute (const struct die_reader_specs *reader,
19405 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 19406 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
19407{
19408 attr->name = abbrev->name;
43988095 19409 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
19410 abbrev->implicit_const, info_ptr,
19411 need_reprocess);
a8329558
KW
19412}
19413
43988095
JK
19414/* Return pointer to string at .debug_str offset STR_OFFSET. */
19415
19416static const char *
ed2dc618 19417read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
4f44ae6c 19418 LONGEST str_offset)
43988095 19419{
5989a64e
SM
19420 return dwarf2_per_objfile->per_bfd->str.read_string
19421 (dwarf2_per_objfile->objfile, str_offset, "DW_FORM_strp");
c906108c
SS
19422}
19423
43988095
JK
19424/* Return pointer to string at .debug_str offset as read from BUF.
19425 BUF is assumed to be in a compilation unit described by CU_HEADER.
19426 Return *BYTES_READ_PTR count of bytes read from BUF. */
19427
d521ce57 19428static const char *
ed2dc618
SM
19429read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19430 const gdb_byte *buf,
cf2c3c16
TT
19431 const struct comp_unit_head *cu_header,
19432 unsigned int *bytes_read_ptr)
19433{
8266302d 19434 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 19435
4f44ae6c 19436 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
cf2c3c16
TT
19437}
19438
86c0bb4c 19439/* See read.h. */
43988095 19440
86c0bb4c
TT
19441const char *
19442dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
19443 const struct comp_unit_head *cu_header,
19444 unsigned int *bytes_read_ptr)
43988095 19445{
86c0bb4c 19446 bfd *abfd = objfile->obfd;
8266302d 19447 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 19448
5989a64e 19449 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
19450}
19451
3019eac3 19452/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 19453 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
19454 ADDR_SIZE is the size of addresses from the CU header. */
19455
19456static CORE_ADDR
ed2dc618 19457read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
19458 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19459 int addr_size)
3019eac3
DE
19460{
19461 struct objfile *objfile = dwarf2_per_objfile->objfile;
19462 bfd *abfd = objfile->obfd;
19463 const gdb_byte *info_ptr;
18a8505e 19464 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 19465
5989a64e
SM
19466 dwarf2_per_objfile->per_bfd->addr.read (objfile);
19467 if (dwarf2_per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 19468 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19469 objfile_name (objfile));
18a8505e 19470 if (addr_base_or_zero + addr_index * addr_size
5989a64e 19471 >= dwarf2_per_objfile->per_bfd->addr.size)
3019eac3
DE
19472 error (_("DW_FORM_addr_index pointing outside of "
19473 ".debug_addr section [in module %s]"),
4262abfb 19474 objfile_name (objfile));
5989a64e 19475 info_ptr = (dwarf2_per_objfile->per_bfd->addr.buffer
18a8505e 19476 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
19477 if (addr_size == 4)
19478 return bfd_get_32 (abfd, info_ptr);
19479 else
19480 return bfd_get_64 (abfd, info_ptr);
19481}
19482
19483/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19484
19485static CORE_ADDR
19486read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19487{
5e22e966 19488 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 19489 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19490}
19491
19492/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19493
19494static CORE_ADDR
d521ce57 19495read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19496 unsigned int *bytes_read)
19497{
5e22e966 19498 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
19499 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19500
19501 return read_addr_index (cu, addr_index);
19502}
19503
450a1bfc 19504/* See read.h. */
3019eac3
DE
19505
19506CORE_ADDR
82ca3f51
SM
19507dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
19508 dwarf2_per_objfile *dwarf2_per_objfile,
19509 unsigned int addr_index)
3019eac3 19510{
7188ed02 19511 struct dwarf2_cu *cu = dwarf2_per_objfile->get_cu (per_cu);
18a8505e 19512 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
19513 int addr_size;
19514
3019eac3
DE
19515 /* We need addr_base and addr_size.
19516 If we don't have PER_CU->cu, we have to get it.
19517 Nasty, but the alternative is storing the needed info in PER_CU,
19518 which at this point doesn't seem justified: it's not clear how frequently
19519 it would get used and it would increase the size of every PER_CU.
19520 Entry points like dwarf2_per_cu_addr_size do a similar thing
19521 so we're not in uncharted territory here.
19522 Alas we need to be a bit more complicated as addr_base is contained
19523 in the DIE.
19524
19525 We don't need to read the entire CU(/TU).
19526 We just need the header and top level die.
a1b64ce1 19527
3019eac3 19528 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19529 For now we skip this optimization. */
3019eac3
DE
19530
19531 if (cu != NULL)
19532 {
19533 addr_base = cu->addr_base;
19534 addr_size = cu->header.addr_size;
19535 }
19536 else
19537 {
2e671100 19538 cutu_reader reader (per_cu, dwarf2_per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
19539 addr_base = reader.cu->addr_base;
19540 addr_size = reader.cu->header.addr_size;
3019eac3
DE
19541 }
19542
ed2dc618
SM
19543 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19544 addr_size);
3019eac3
DE
19545}
19546
18a8505e
AT
19547/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19548 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19549 DWO file. */
3019eac3 19550
d521ce57 19551static const char *
18a8505e
AT
19552read_str_index (struct dwarf2_cu *cu,
19553 struct dwarf2_section_info *str_section,
19554 struct dwarf2_section_info *str_offsets_section,
19555 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 19556{
5e22e966 19557 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 19558 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19559 const char *objf_name = objfile_name (objfile);
3019eac3 19560 bfd *abfd = objfile->obfd;
d521ce57 19561 const gdb_byte *info_ptr;
3019eac3 19562 ULONGEST str_offset;
cf532bd1 19563 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 19564
96b79293
TT
19565 str_section->read (objfile);
19566 str_offsets_section->read (objfile);
73869dc2 19567 if (str_section->buffer == NULL)
18a8505e 19568 error (_("%s used without %s section"
9d8780f0 19569 " in CU at offset %s [in module %s]"),
96b79293 19570 form_name, str_section->get_name (),
18a8505e 19571 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19572 if (str_offsets_section->buffer == NULL)
18a8505e 19573 error (_("%s used without %s section"
9d8780f0 19574 " in CU at offset %s [in module %s]"),
96b79293 19575 form_name, str_section->get_name (),
18a8505e 19576 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19577 info_ptr = (str_offsets_section->buffer
18a8505e 19578 + str_offsets_base
3019eac3
DE
19579 + str_index * cu->header.offset_size);
19580 if (cu->header.offset_size == 4)
19581 str_offset = bfd_get_32 (abfd, info_ptr);
19582 else
19583 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19584 if (str_offset >= str_section->size)
57d63ce2 19585 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19586 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19587 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19588 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19589}
19590
18a8505e
AT
19591/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19592
19593static const char *
19594read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19595{
19596 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19597 ? reader->cu->header.addr_size : 0;
19598 return read_str_index (reader->cu,
19599 &reader->dwo_file->sections.str,
19600 &reader->dwo_file->sections.str_offsets,
19601 str_offsets_base, str_index);
19602}
19603
19604/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19605
19606static const char *
19607read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19608{
5e22e966 19609 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
19610 const char *objf_name = objfile_name (objfile);
19611 static const char form_name[] = "DW_FORM_GNU_str_index";
19612 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19613
19614 if (!cu->str_offsets_base.has_value ())
19615 error (_("%s used in Fission stub without %s"
19616 " in CU at offset 0x%lx [in module %s]"),
19617 form_name, str_offsets_attr_name,
19618 (long) cu->header.offset_size, objf_name);
19619
19620 return read_str_index (cu,
5e22e966
SM
19621 &cu->per_objfile->per_bfd->str,
19622 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
19623 *cu->str_offsets_base, str_index);
19624}
19625
3019eac3
DE
19626/* Return the length of an LEB128 number in BUF. */
19627
19628static int
19629leb128_size (const gdb_byte *buf)
19630{
19631 const gdb_byte *begin = buf;
19632 gdb_byte byte;
19633
19634 while (1)
19635 {
19636 byte = *buf++;
19637 if ((byte & 128) == 0)
19638 return buf - begin;
19639 }
19640}
19641
c906108c 19642static void
e142c38c 19643set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19644{
19645 switch (lang)
19646 {
19647 case DW_LANG_C89:
76bee0cc 19648 case DW_LANG_C99:
0cfd832f 19649 case DW_LANG_C11:
c906108c 19650 case DW_LANG_C:
d1be3247 19651 case DW_LANG_UPC:
e142c38c 19652 cu->language = language_c;
c906108c 19653 break;
9c37b5ae 19654 case DW_LANG_Java:
c906108c 19655 case DW_LANG_C_plus_plus:
0cfd832f
MW
19656 case DW_LANG_C_plus_plus_11:
19657 case DW_LANG_C_plus_plus_14:
e142c38c 19658 cu->language = language_cplus;
c906108c 19659 break;
6aecb9c2
JB
19660 case DW_LANG_D:
19661 cu->language = language_d;
19662 break;
c906108c
SS
19663 case DW_LANG_Fortran77:
19664 case DW_LANG_Fortran90:
b21b22e0 19665 case DW_LANG_Fortran95:
f7de9aab
MW
19666 case DW_LANG_Fortran03:
19667 case DW_LANG_Fortran08:
e142c38c 19668 cu->language = language_fortran;
c906108c 19669 break;
a766d390
DE
19670 case DW_LANG_Go:
19671 cu->language = language_go;
19672 break;
c906108c 19673 case DW_LANG_Mips_Assembler:
e142c38c 19674 cu->language = language_asm;
c906108c
SS
19675 break;
19676 case DW_LANG_Ada83:
8aaf0b47 19677 case DW_LANG_Ada95:
bc5f45f8
JB
19678 cu->language = language_ada;
19679 break;
72019c9c
GM
19680 case DW_LANG_Modula2:
19681 cu->language = language_m2;
19682 break;
fe8e67fd
PM
19683 case DW_LANG_Pascal83:
19684 cu->language = language_pascal;
19685 break;
22566fbd
DJ
19686 case DW_LANG_ObjC:
19687 cu->language = language_objc;
19688 break;
c44af4eb
TT
19689 case DW_LANG_Rust:
19690 case DW_LANG_Rust_old:
19691 cu->language = language_rust;
19692 break;
c906108c
SS
19693 case DW_LANG_Cobol74:
19694 case DW_LANG_Cobol85:
c906108c 19695 default:
e142c38c 19696 cu->language = language_minimal;
c906108c
SS
19697 break;
19698 }
e142c38c 19699 cu->language_defn = language_def (cu->language);
c906108c
SS
19700}
19701
19702/* Return the named attribute or NULL if not there. */
19703
19704static struct attribute *
e142c38c 19705dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19706{
a48e046c 19707 for (;;)
c906108c 19708 {
a48e046c
TT
19709 unsigned int i;
19710 struct attribute *spec = NULL;
19711
19712 for (i = 0; i < die->num_attrs; ++i)
19713 {
19714 if (die->attrs[i].name == name)
19715 return &die->attrs[i];
19716 if (die->attrs[i].name == DW_AT_specification
19717 || die->attrs[i].name == DW_AT_abstract_origin)
19718 spec = &die->attrs[i];
19719 }
19720
19721 if (!spec)
19722 break;
c906108c 19723
f2f0e013 19724 die = follow_die_ref (die, spec, &cu);
f2f0e013 19725 }
c5aa993b 19726
c906108c
SS
19727 return NULL;
19728}
19729
7d45c7c3
KB
19730/* Return the string associated with a string-typed attribute, or NULL if it
19731 is either not found or is of an incorrect type. */
19732
19733static const char *
19734dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19735{
19736 struct attribute *attr;
19737 const char *str = NULL;
19738
19739 attr = dwarf2_attr (die, name, cu);
19740
19741 if (attr != NULL)
19742 {
e61108c9
TT
19743 str = attr->value_as_string ();
19744 if (str == nullptr)
b98664d3 19745 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19746 "DIE at %s in module %s"),
19747 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 19748 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
19749 }
19750
19751 return str;
19752}
19753
a084a2a6 19754/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19755 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19756static const char *
19757dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19758{
19759 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19760 if (dwo_name == nullptr)
19761 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19762 return dwo_name;
19763}
19764
05cf31d1
JB
19765/* Return non-zero iff the attribute NAME is defined for the given DIE,
19766 and holds a non-zero value. This function should only be used for
2dc7f7b3 19767 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19768
19769static int
19770dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19771{
19772 struct attribute *attr = dwarf2_attr (die, name, cu);
19773
19774 return (attr && DW_UNSND (attr));
19775}
19776
3ca72b44 19777static int
e142c38c 19778die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19779{
05cf31d1
JB
19780 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19781 which value is non-zero. However, we have to be careful with
19782 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19783 (via dwarf2_flag_true_p) follows this attribute. So we may
19784 end up accidently finding a declaration attribute that belongs
19785 to a different DIE referenced by the specification attribute,
19786 even though the given DIE does not have a declaration attribute. */
19787 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19788 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19789}
19790
63d06c5c 19791/* Return the die giving the specification for DIE, if there is
f2f0e013 19792 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19793 containing the return value on output. If there is no
19794 specification, but there is an abstract origin, that is
19795 returned. */
63d06c5c
DC
19796
19797static struct die_info *
f2f0e013 19798die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19799{
f2f0e013
DJ
19800 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19801 *spec_cu);
63d06c5c 19802
edb3359d
DJ
19803 if (spec_attr == NULL)
19804 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19805
63d06c5c
DC
19806 if (spec_attr == NULL)
19807 return NULL;
19808 else
f2f0e013 19809 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19810}
c906108c 19811
527f3840
JK
19812/* Stub for free_line_header to match void * callback types. */
19813
19814static void
19815free_line_header_voidp (void *arg)
19816{
9a3c8263 19817 struct line_header *lh = (struct line_header *) arg;
527f3840 19818
fff8551c 19819 delete lh;
527f3840
JK
19820}
19821
83769d0b 19822/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19823
19824static struct dwarf2_section_info *
19825get_debug_line_section (struct dwarf2_cu *cu)
19826{
19827 struct dwarf2_section_info *section;
5e22e966 19828 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
36586728
TT
19829
19830 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19831 DWO file. */
19832 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19833 section = &cu->dwo_unit->dwo_file->sections.line;
19834 else if (cu->per_cu->is_dwz)
19835 {
c3699833 19836 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
36586728
TT
19837
19838 section = &dwz->line;
19839 }
19840 else
5989a64e 19841 section = &dwarf2_per_objfile->per_bfd->line;
36586728
TT
19842
19843 return section;
19844}
19845
debd256d 19846/* Read the statement program header starting at OFFSET in
3019eac3 19847 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19848 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19849 Returns NULL if there is a problem reading the header, e.g., if it
19850 has a version we don't understand.
debd256d
JB
19851
19852 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19853 the returned object point into the dwarf line section buffer,
19854 and must not be freed. */
ae2de4f8 19855
fff8551c 19856static line_header_up
9c541725 19857dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19858{
3019eac3 19859 struct dwarf2_section_info *section;
5e22e966 19860 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 19861
36586728 19862 section = get_debug_line_section (cu);
96b79293 19863 section->read (dwarf2_per_objfile->objfile);
3019eac3 19864 if (section->buffer == NULL)
debd256d 19865 {
3019eac3 19866 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19867 complaint (_("missing .debug_line.dwo section"));
3019eac3 19868 else
b98664d3 19869 complaint (_("missing .debug_line section"));
debd256d
JB
19870 return 0;
19871 }
19872
0df7ad3a
TT
19873 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19874 dwarf2_per_objfile, section,
19875 &cu->header);
debd256d 19876}
c906108c 19877
c6da4cef 19878/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19879 Return the file name of the psymtab for the given file_entry.
c6da4cef 19880 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19881 If space for the result is malloc'd, *NAME_HOLDER will be set.
19882 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19883
d521ce57 19884static const char *
7ba99d21 19885psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19886 const dwarf2_psymtab *pst,
c89b44cd
TT
19887 const char *comp_dir,
19888 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19889{
d521ce57
TT
19890 const char *include_name = fe.name;
19891 const char *include_name_to_compare = include_name;
72b9f47f 19892 const char *pst_filename;
c6da4cef
DE
19893 int file_is_pst;
19894
8c43009f 19895 const char *dir_name = fe.include_dir (lh);
c6da4cef 19896
c89b44cd 19897 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19898 if (!IS_ABSOLUTE_PATH (include_name)
19899 && (dir_name != NULL || comp_dir != NULL))
19900 {
19901 /* Avoid creating a duplicate psymtab for PST.
19902 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19903 Before we do the comparison, however, we need to account
19904 for DIR_NAME and COMP_DIR.
19905 First prepend dir_name (if non-NULL). If we still don't
19906 have an absolute path prepend comp_dir (if non-NULL).
19907 However, the directory we record in the include-file's
19908 psymtab does not contain COMP_DIR (to match the
19909 corresponding symtab(s)).
19910
19911 Example:
19912
19913 bash$ cd /tmp
19914 bash$ gcc -g ./hello.c
19915 include_name = "hello.c"
19916 dir_name = "."
19917 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19918 DW_AT_name = "./hello.c"
19919
19920 */
c6da4cef
DE
19921
19922 if (dir_name != NULL)
19923 {
c89b44cd
TT
19924 name_holder->reset (concat (dir_name, SLASH_STRING,
19925 include_name, (char *) NULL));
19926 include_name = name_holder->get ();
c6da4cef 19927 include_name_to_compare = include_name;
c6da4cef
DE
19928 }
19929 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19930 {
c89b44cd
TT
19931 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19932 include_name, (char *) NULL));
19933 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19934 }
19935 }
19936
19937 pst_filename = pst->filename;
c89b44cd 19938 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19939 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19940 {
c89b44cd
TT
19941 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19942 pst_filename, (char *) NULL));
19943 pst_filename = copied_name.get ();
c6da4cef
DE
19944 }
19945
1e3fad37 19946 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19947
c6da4cef
DE
19948 if (file_is_pst)
19949 return NULL;
19950 return include_name;
19951}
19952
d9b3de22
DE
19953/* State machine to track the state of the line number program. */
19954
6f77053d 19955class lnp_state_machine
d9b3de22 19956{
6f77053d
PA
19957public:
19958 /* Initialize a machine state for the start of a line number
19959 program. */
804d2729
TT
19960 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19961 bool record_lines_p);
6f77053d 19962
8c43009f
PA
19963 file_entry *current_file ()
19964 {
19965 /* lh->file_names is 0-based, but the file name numbers in the
19966 statement program are 1-based. */
6f77053d
PA
19967 return m_line_header->file_name_at (m_file);
19968 }
19969
19970 /* Record the line in the state machine. END_SEQUENCE is true if
19971 we're processing the end of a sequence. */
19972 void record_line (bool end_sequence);
19973
7ab6656f
OJ
19974 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19975 nop-out rest of the lines in this sequence. */
6f77053d
PA
19976 void check_line_address (struct dwarf2_cu *cu,
19977 const gdb_byte *line_ptr,
7ab6656f 19978 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19979
19980 void handle_set_discriminator (unsigned int discriminator)
19981 {
19982 m_discriminator = discriminator;
19983 m_line_has_non_zero_discriminator |= discriminator != 0;
19984 }
19985
19986 /* Handle DW_LNE_set_address. */
19987 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19988 {
19989 m_op_index = 0;
19990 address += baseaddr;
19991 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19992 }
19993
19994 /* Handle DW_LNS_advance_pc. */
19995 void handle_advance_pc (CORE_ADDR adjust);
19996
19997 /* Handle a special opcode. */
19998 void handle_special_opcode (unsigned char op_code);
19999
20000 /* Handle DW_LNS_advance_line. */
20001 void handle_advance_line (int line_delta)
20002 {
20003 advance_line (line_delta);
20004 }
20005
20006 /* Handle DW_LNS_set_file. */
20007 void handle_set_file (file_name_index file);
20008
20009 /* Handle DW_LNS_negate_stmt. */
20010 void handle_negate_stmt ()
20011 {
20012 m_is_stmt = !m_is_stmt;
20013 }
20014
20015 /* Handle DW_LNS_const_add_pc. */
20016 void handle_const_add_pc ();
20017
20018 /* Handle DW_LNS_fixed_advance_pc. */
20019 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20020 {
20021 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20022 m_op_index = 0;
20023 }
20024
20025 /* Handle DW_LNS_copy. */
20026 void handle_copy ()
20027 {
20028 record_line (false);
20029 m_discriminator = 0;
20030 }
20031
20032 /* Handle DW_LNE_end_sequence. */
20033 void handle_end_sequence ()
20034 {
804d2729 20035 m_currently_recording_lines = true;
6f77053d
PA
20036 }
20037
20038private:
20039 /* Advance the line by LINE_DELTA. */
20040 void advance_line (int line_delta)
20041 {
20042 m_line += line_delta;
20043
20044 if (line_delta != 0)
20045 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20046 }
20047
804d2729
TT
20048 struct dwarf2_cu *m_cu;
20049
6f77053d
PA
20050 gdbarch *m_gdbarch;
20051
20052 /* True if we're recording lines.
20053 Otherwise we're building partial symtabs and are just interested in
20054 finding include files mentioned by the line number program. */
20055 bool m_record_lines_p;
20056
8c43009f 20057 /* The line number header. */
6f77053d 20058 line_header *m_line_header;
8c43009f 20059
6f77053d
PA
20060 /* These are part of the standard DWARF line number state machine,
20061 and initialized according to the DWARF spec. */
d9b3de22 20062
6f77053d 20063 unsigned char m_op_index = 0;
7ba99d21
AT
20064 /* The line table index of the current file. */
20065 file_name_index m_file = 1;
6f77053d
PA
20066 unsigned int m_line = 1;
20067
20068 /* These are initialized in the constructor. */
20069
20070 CORE_ADDR m_address;
20071 bool m_is_stmt;
20072 unsigned int m_discriminator;
d9b3de22
DE
20073
20074 /* Additional bits of state we need to track. */
20075
20076 /* The last file that we called dwarf2_start_subfile for.
20077 This is only used for TLLs. */
6f77053d 20078 unsigned int m_last_file = 0;
d9b3de22 20079 /* The last file a line number was recorded for. */
6f77053d 20080 struct subfile *m_last_subfile = NULL;
d9b3de22 20081
804d2729
TT
20082 /* When true, record the lines we decode. */
20083 bool m_currently_recording_lines = false;
d9b3de22
DE
20084
20085 /* The last line number that was recorded, used to coalesce
20086 consecutive entries for the same line. This can happen, for
20087 example, when discriminators are present. PR 17276. */
6f77053d
PA
20088 unsigned int m_last_line = 0;
20089 bool m_line_has_non_zero_discriminator = false;
8c43009f 20090};
d9b3de22 20091
6f77053d
PA
20092void
20093lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20094{
20095 CORE_ADDR addr_adj = (((m_op_index + adjust)
20096 / m_line_header->maximum_ops_per_instruction)
20097 * m_line_header->minimum_instruction_length);
20098 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20099 m_op_index = ((m_op_index + adjust)
20100 % m_line_header->maximum_ops_per_instruction);
20101}
d9b3de22 20102
6f77053d
PA
20103void
20104lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20105{
6f77053d 20106 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
20107 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20108 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20109 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
20110 / m_line_header->maximum_ops_per_instruction)
20111 * m_line_header->minimum_instruction_length);
20112 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 20113 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 20114 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20115
258bf0ee 20116 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
20117 advance_line (line_delta);
20118 record_line (false);
20119 m_discriminator = 0;
20120}
d9b3de22 20121
6f77053d
PA
20122void
20123lnp_state_machine::handle_set_file (file_name_index file)
20124{
20125 m_file = file;
20126
20127 const file_entry *fe = current_file ();
20128 if (fe == NULL)
20129 dwarf2_debug_line_missing_file_complaint ();
20130 else if (m_record_lines_p)
20131 {
20132 const char *dir = fe->include_dir (m_line_header);
20133
c24bdb02 20134 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20135 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20136 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20137 }
20138}
20139
20140void
20141lnp_state_machine::handle_const_add_pc ()
20142{
20143 CORE_ADDR adjust
20144 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20145
20146 CORE_ADDR addr_adj
20147 = (((m_op_index + adjust)
20148 / m_line_header->maximum_ops_per_instruction)
20149 * m_line_header->minimum_instruction_length);
20150
20151 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20152 m_op_index = ((m_op_index + adjust)
20153 % m_line_header->maximum_ops_per_instruction);
20154}
d9b3de22 20155
a05a36a5
DE
20156/* Return non-zero if we should add LINE to the line number table.
20157 LINE is the line to add, LAST_LINE is the last line that was added,
20158 LAST_SUBFILE is the subfile for LAST_LINE.
20159 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20160 had a non-zero discriminator.
20161
20162 We have to be careful in the presence of discriminators.
20163 E.g., for this line:
20164
20165 for (i = 0; i < 100000; i++);
20166
20167 clang can emit four line number entries for that one line,
20168 each with a different discriminator.
20169 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20170
20171 However, we want gdb to coalesce all four entries into one.
20172 Otherwise the user could stepi into the middle of the line and
20173 gdb would get confused about whether the pc really was in the
20174 middle of the line.
20175
20176 Things are further complicated by the fact that two consecutive
20177 line number entries for the same line is a heuristic used by gcc
20178 to denote the end of the prologue. So we can't just discard duplicate
20179 entries, we have to be selective about it. The heuristic we use is
20180 that we only collapse consecutive entries for the same line if at least
20181 one of those entries has a non-zero discriminator. PR 17276.
20182
20183 Note: Addresses in the line number state machine can never go backwards
20184 within one sequence, thus this coalescing is ok. */
20185
20186static int
804d2729
TT
20187dwarf_record_line_p (struct dwarf2_cu *cu,
20188 unsigned int line, unsigned int last_line,
a05a36a5
DE
20189 int line_has_non_zero_discriminator,
20190 struct subfile *last_subfile)
20191{
c24bdb02 20192 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20193 return 1;
20194 if (line != last_line)
20195 return 1;
20196 /* Same line for the same file that we've seen already.
20197 As a last check, for pr 17276, only record the line if the line
20198 has never had a non-zero discriminator. */
20199 if (!line_has_non_zero_discriminator)
20200 return 1;
20201 return 0;
20202}
20203
804d2729
TT
20204/* Use the CU's builder to record line number LINE beginning at
20205 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20206
20207static void
d9b3de22 20208dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 20209 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 20210 struct dwarf2_cu *cu)
252a6764
DE
20211{
20212 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20213
27e0867f
DE
20214 if (dwarf_line_debug)
20215 {
20216 fprintf_unfiltered (gdb_stdlog,
20217 "Recording line %u, file %s, address %s\n",
20218 line, lbasename (subfile->name),
20219 paddress (gdbarch, address));
20220 }
20221
804d2729 20222 if (cu != nullptr)
8c95582d 20223 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
20224}
20225
20226/* Subroutine of dwarf_decode_lines_1 to simplify it.
20227 Mark the end of a set of line number records.
d9b3de22 20228 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20229 If SUBFILE is NULL the request is ignored. */
20230
20231static void
20232dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20233 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20234{
27e0867f
DE
20235 if (subfile == NULL)
20236 return;
20237
20238 if (dwarf_line_debug)
20239 {
20240 fprintf_unfiltered (gdb_stdlog,
20241 "Finishing current line, file %s, address %s\n",
20242 lbasename (subfile->name),
20243 paddress (gdbarch, address));
20244 }
20245
8c95582d 20246 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
20247}
20248
6f77053d
PA
20249void
20250lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20251{
d9b3de22
DE
20252 if (dwarf_line_debug)
20253 {
20254 fprintf_unfiltered (gdb_stdlog,
20255 "Processing actual line %u: file %u,"
94a72be7 20256 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 20257 m_line, m_file,
6f77053d 20258 paddress (m_gdbarch, m_address),
94a72be7
AB
20259 m_is_stmt, m_discriminator,
20260 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
20261 }
20262
6f77053d 20263 file_entry *fe = current_file ();
8c43009f
PA
20264
20265 if (fe == NULL)
d9b3de22
DE
20266 dwarf2_debug_line_missing_file_complaint ();
20267 /* For now we ignore lines not starting on an instruction boundary.
20268 But not when processing end_sequence for compatibility with the
20269 previous version of the code. */
6f77053d 20270 else if (m_op_index == 0 || end_sequence)
d9b3de22 20271 {
8c43009f 20272 fe->included_p = 1;
8c95582d 20273 if (m_record_lines_p)
d9b3de22 20274 {
c24bdb02 20275 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 20276 || end_sequence)
d9b3de22 20277 {
804d2729
TT
20278 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20279 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20280 }
20281
20282 if (!end_sequence)
20283 {
8c95582d
AB
20284 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20285
804d2729 20286 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20287 m_line_has_non_zero_discriminator,
20288 m_last_subfile))
d9b3de22 20289 {
c24bdb02 20290 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20291 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20292 builder->get_current_subfile (),
8c95582d 20293 m_line, m_address, is_stmt,
804d2729 20294 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20295 }
c24bdb02 20296 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20297 m_last_line = m_line;
d9b3de22
DE
20298 }
20299 }
20300 }
20301}
20302
804d2729
TT
20303lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20304 line_header *lh, bool record_lines_p)
d9b3de22 20305{
804d2729 20306 m_cu = cu;
6f77053d
PA
20307 m_gdbarch = arch;
20308 m_record_lines_p = record_lines_p;
20309 m_line_header = lh;
d9b3de22 20310
804d2729 20311 m_currently_recording_lines = true;
d9b3de22 20312
d9b3de22
DE
20313 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20314 was a line entry for it so that the backend has a chance to adjust it
20315 and also record it in case it needs it. This is currently used by MIPS
20316 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20317 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20318 m_is_stmt = lh->default_is_stmt;
20319 m_discriminator = 0;
252a6764
DE
20320}
20321
6f77053d
PA
20322void
20323lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20324 const gdb_byte *line_ptr,
7ab6656f 20325 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20326{
7ab6656f
OJ
20327 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20328 the pc range of the CU. However, we restrict the test to only ADDRESS
20329 values of zero to preserve GDB's previous behaviour which is to handle
20330 the specific case of a function being GC'd by the linker. */
924c2928 20331
7ab6656f 20332 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20333 {
20334 /* This line table is for a function which has been
20335 GCd by the linker. Ignore it. PR gdb/12528 */
20336
5e22e966 20337 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
20338 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20339
b98664d3 20340 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20341 line_offset, objfile_name (objfile));
804d2729
TT
20342 m_currently_recording_lines = false;
20343 /* Note: m_currently_recording_lines is left as false until we see
20344 DW_LNE_end_sequence. */
924c2928
DE
20345 }
20346}
20347
f3f5162e 20348/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20349 Process the line number information in LH.
20350 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20351 program in order to set included_p for every referenced header. */
debd256d 20352
c906108c 20353static void
43f3e411
DE
20354dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20355 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20356{
d521ce57
TT
20357 const gdb_byte *line_ptr, *extended_end;
20358 const gdb_byte *line_end;
a8c50c1f 20359 unsigned int bytes_read, extended_len;
699ca60a 20360 unsigned char op_code, extended_op;
e142c38c 20361 CORE_ADDR baseaddr;
5e22e966 20362 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20363 bfd *abfd = objfile->obfd;
08feed99 20364 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
20365 /* True if we're recording line info (as opposed to building partial
20366 symtabs and just interested in finding include files mentioned by
20367 the line number program). */
20368 bool record_lines_p = !decode_for_pst_p;
e142c38c 20369
b3b3bada 20370 baseaddr = objfile->text_section_offset ();
c906108c 20371
debd256d
JB
20372 line_ptr = lh->statement_program_start;
20373 line_end = lh->statement_program_end;
c906108c
SS
20374
20375 /* Read the statement sequences until there's nothing left. */
20376 while (line_ptr < line_end)
20377 {
6f77053d
PA
20378 /* The DWARF line number program state machine. Reset the state
20379 machine at the start of each sequence. */
804d2729 20380 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20381 bool end_sequence = false;
d9b3de22 20382
8c43009f 20383 if (record_lines_p)
c906108c 20384 {
8c43009f
PA
20385 /* Start a subfile for the current file of the state
20386 machine. */
20387 const file_entry *fe = state_machine.current_file ();
20388
20389 if (fe != NULL)
804d2729 20390 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20391 }
20392
a738430d 20393 /* Decode the table. */
d9b3de22 20394 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20395 {
20396 op_code = read_1_byte (abfd, line_ptr);
20397 line_ptr += 1;
9aa1fe7e 20398
debd256d 20399 if (op_code >= lh->opcode_base)
6e70227d 20400 {
8e07a239 20401 /* Special opcode. */
6f77053d 20402 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20403 }
20404 else switch (op_code)
c906108c
SS
20405 {
20406 case DW_LNS_extended_op:
3e43a32a
MS
20407 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20408 &bytes_read);
473b7be6 20409 line_ptr += bytes_read;
a8c50c1f 20410 extended_end = line_ptr + extended_len;
c906108c
SS
20411 extended_op = read_1_byte (abfd, line_ptr);
20412 line_ptr += 1;
20413 switch (extended_op)
20414 {
20415 case DW_LNE_end_sequence:
6f77053d
PA
20416 state_machine.handle_end_sequence ();
20417 end_sequence = true;
c906108c
SS
20418 break;
20419 case DW_LNE_set_address:
d9b3de22
DE
20420 {
20421 CORE_ADDR address
c8a7a66f 20422 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 20423 line_ptr += bytes_read;
6f77053d
PA
20424
20425 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20426 lowpc - baseaddr, address);
6f77053d 20427 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20428 }
c906108c
SS
20429 break;
20430 case DW_LNE_define_file:
debd256d 20431 {
d521ce57 20432 const char *cur_file;
ecfb656c
PA
20433 unsigned int mod_time, length;
20434 dir_index dindex;
6e70227d 20435
3e43a32a
MS
20436 cur_file = read_direct_string (abfd, line_ptr,
20437 &bytes_read);
debd256d 20438 line_ptr += bytes_read;
ecfb656c 20439 dindex = (dir_index)
debd256d
JB
20440 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20441 line_ptr += bytes_read;
20442 mod_time =
20443 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20444 line_ptr += bytes_read;
20445 length =
20446 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20447 line_ptr += bytes_read;
ecfb656c 20448 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20449 }
c906108c 20450 break;
d0c6ba3d 20451 case DW_LNE_set_discriminator:
6f77053d
PA
20452 {
20453 /* The discriminator is not interesting to the
20454 debugger; just ignore it. We still need to
20455 check its value though:
20456 if there are consecutive entries for the same
20457 (non-prologue) line we want to coalesce them.
20458 PR 17276. */
20459 unsigned int discr
20460 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20461 line_ptr += bytes_read;
20462
20463 state_machine.handle_set_discriminator (discr);
20464 }
d0c6ba3d 20465 break;
c906108c 20466 default:
b98664d3 20467 complaint (_("mangled .debug_line section"));
debd256d 20468 return;
c906108c 20469 }
a8c50c1f
DJ
20470 /* Make sure that we parsed the extended op correctly. If e.g.
20471 we expected a different address size than the producer used,
20472 we may have read the wrong number of bytes. */
20473 if (line_ptr != extended_end)
20474 {
b98664d3 20475 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20476 return;
20477 }
c906108c
SS
20478 break;
20479 case DW_LNS_copy:
6f77053d 20480 state_machine.handle_copy ();
c906108c
SS
20481 break;
20482 case DW_LNS_advance_pc:
2dc7f7b3
TT
20483 {
20484 CORE_ADDR adjust
20485 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20486 line_ptr += bytes_read;
6f77053d
PA
20487
20488 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20489 }
c906108c
SS
20490 break;
20491 case DW_LNS_advance_line:
a05a36a5
DE
20492 {
20493 int line_delta
20494 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20495 line_ptr += bytes_read;
6f77053d
PA
20496
20497 state_machine.handle_advance_line (line_delta);
a05a36a5 20498 }
c906108c
SS
20499 break;
20500 case DW_LNS_set_file:
d9b3de22 20501 {
6f77053d 20502 file_name_index file
ecfb656c
PA
20503 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20504 &bytes_read);
d9b3de22 20505 line_ptr += bytes_read;
8c43009f 20506
6f77053d 20507 state_machine.handle_set_file (file);
d9b3de22 20508 }
c906108c
SS
20509 break;
20510 case DW_LNS_set_column:
0ad93d4f 20511 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20512 line_ptr += bytes_read;
20513 break;
20514 case DW_LNS_negate_stmt:
6f77053d 20515 state_machine.handle_negate_stmt ();
c906108c
SS
20516 break;
20517 case DW_LNS_set_basic_block:
c906108c 20518 break;
c2c6d25f
JM
20519 /* Add to the address register of the state machine the
20520 address increment value corresponding to special opcode
a738430d
MK
20521 255. I.e., this value is scaled by the minimum
20522 instruction length since special opcode 255 would have
b021a221 20523 scaled the increment. */
c906108c 20524 case DW_LNS_const_add_pc:
6f77053d 20525 state_machine.handle_const_add_pc ();
c906108c
SS
20526 break;
20527 case DW_LNS_fixed_advance_pc:
3e29f34a 20528 {
6f77053d 20529 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20530 line_ptr += 2;
6f77053d
PA
20531
20532 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20533 }
c906108c 20534 break;
9aa1fe7e 20535 default:
a738430d
MK
20536 {
20537 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20538 int i;
a738430d 20539
debd256d 20540 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20541 {
20542 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20543 line_ptr += bytes_read;
20544 }
20545 }
c906108c
SS
20546 }
20547 }
d9b3de22
DE
20548
20549 if (!end_sequence)
20550 dwarf2_debug_line_missing_end_sequence_complaint ();
20551
20552 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20553 in which case we still finish recording the last line). */
6f77053d 20554 state_machine.record_line (true);
c906108c 20555 }
f3f5162e
DE
20556}
20557
20558/* Decode the Line Number Program (LNP) for the given line_header
20559 structure and CU. The actual information extracted and the type
20560 of structures created from the LNP depends on the value of PST.
20561
20562 1. If PST is NULL, then this procedure uses the data from the program
20563 to create all necessary symbol tables, and their linetables.
20564
20565 2. If PST is not NULL, this procedure reads the program to determine
20566 the list of files included by the unit represented by PST, and
20567 builds all the associated partial symbol tables.
20568
20569 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20570 It is used for relative paths in the line table.
20571 NOTE: When processing partial symtabs (pst != NULL),
20572 comp_dir == pst->dirname.
20573
20574 NOTE: It is important that psymtabs have the same file name (via strcmp)
20575 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20576 symtab we don't use it in the name of the psymtabs we create.
20577 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20578 A good testcase for this is mb-inline.exp.
20579
527f3840
JK
20580 LOWPC is the lowest address in CU (or 0 if not known).
20581
20582 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20583 for its PC<->lines mapping information. Otherwise only the filename
20584 table is read in. */
f3f5162e
DE
20585
20586static void
20587dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20588 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20589 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20590{
5e22e966 20591 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20592 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20593
527f3840
JK
20594 if (decode_mapping)
20595 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20596
20597 if (decode_for_pst_p)
20598 {
aaa75496
JB
20599 /* Now that we're done scanning the Line Header Program, we can
20600 create the psymtab of each included file. */
7ba99d21
AT
20601 for (auto &file_entry : lh->file_names ())
20602 if (file_entry.included_p == 1)
aaa75496 20603 {
c89b44cd 20604 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20605 const char *include_name =
7ba99d21
AT
20606 psymtab_include_file_name (lh, file_entry, pst,
20607 comp_dir, &name_holder);
c6da4cef 20608 if (include_name != NULL)
aaa75496
JB
20609 dwarf2_create_include_psymtab (include_name, pst, objfile);
20610 }
20611 }
cb1df416
DJ
20612 else
20613 {
20614 /* Make sure a symtab is created for every file, even files
20615 which contain only variables (i.e. no code with associated
20616 line numbers). */
c24bdb02
KS
20617 buildsym_compunit *builder = cu->get_builder ();
20618 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20619
7ba99d21 20620 for (auto &fe : lh->file_names ())
cb1df416 20621 {
804d2729 20622 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20623 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20624 {
c24bdb02 20625 builder->get_current_subfile ()->symtab
804d2729 20626 = allocate_symtab (cust,
c24bdb02 20627 builder->get_current_subfile ()->name);
43f3e411 20628 }
c24bdb02 20629 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20630 }
20631 }
c906108c
SS
20632}
20633
20634/* Start a subfile for DWARF. FILENAME is the name of the file and
20635 DIRNAME the name of the source directory which contains FILENAME
4d663531 20636 or NULL if not known.
c906108c
SS
20637 This routine tries to keep line numbers from identical absolute and
20638 relative file names in a common subfile.
20639
20640 Using the `list' example from the GDB testsuite, which resides in
20641 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20642 of /srcdir/list0.c yields the following debugging information for list0.c:
20643
c5aa993b 20644 DW_AT_name: /srcdir/list0.c
4d663531 20645 DW_AT_comp_dir: /compdir
357e46e7 20646 files.files[0].name: list0.h
c5aa993b 20647 files.files[0].dir: /srcdir
357e46e7 20648 files.files[1].name: list0.c
c5aa993b 20649 files.files[1].dir: /srcdir
c906108c
SS
20650
20651 The line number information for list0.c has to end up in a single
4f1520fb
FR
20652 subfile, so that `break /srcdir/list0.c:1' works as expected.
20653 start_subfile will ensure that this happens provided that we pass the
20654 concatenation of files.files[1].dir and files.files[1].name as the
20655 subfile's name. */
c906108c
SS
20656
20657static void
804d2729
TT
20658dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20659 const char *dirname)
c906108c 20660{
43816ebc 20661 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20662
4d663531 20663 /* In order not to lose the line information directory,
4f1520fb
FR
20664 we concatenate it to the filename when it makes sense.
20665 Note that the Dwarf3 standard says (speaking of filenames in line
20666 information): ``The directory index is ignored for file names
20667 that represent full path names''. Thus ignoring dirname in the
20668 `else' branch below isn't an issue. */
c906108c 20669
d5166ae1 20670 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20671 {
43816ebc
TT
20672 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20673 filename = copy.get ();
d521ce57 20674 }
c906108c 20675
c24bdb02 20676 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20677}
20678
804d2729
TT
20679/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20680 buildsym_compunit constructor. */
f4dc4d17 20681
c24bdb02
KS
20682struct compunit_symtab *
20683dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20684 CORE_ADDR low_pc)
f4dc4d17 20685{
c24bdb02 20686 gdb_assert (m_builder == nullptr);
43f3e411 20687
c24bdb02 20688 m_builder.reset (new struct buildsym_compunit
f6e649dd 20689 (this->per_objfile->objfile,
c24bdb02 20690 name, comp_dir, language, low_pc));
93b8bea4 20691
c24bdb02 20692 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20693
c24bdb02
KS
20694 get_builder ()->record_debugformat ("DWARF 2");
20695 get_builder ()->record_producer (producer);
f4dc4d17 20696
c24bdb02 20697 processing_has_namespace_info = false;
43f3e411 20698
c24bdb02 20699 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20700}
20701
4c2df51b
DJ
20702static void
20703var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20704 struct dwarf2_cu *cu)
4c2df51b 20705{
5e22e966 20706 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
20707 struct comp_unit_head *cu_header = &cu->header;
20708
4c2df51b
DJ
20709 /* NOTE drow/2003-01-30: There used to be a comment and some special
20710 code here to turn a symbol with DW_AT_external and a
20711 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20712 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20713 with some versions of binutils) where shared libraries could have
20714 relocations against symbols in their debug information - the
20715 minimal symbol would have the right address, but the debug info
20716 would not. It's no longer necessary, because we will explicitly
20717 apply relocations when we read in the debug information now. */
20718
20719 /* A DW_AT_location attribute with no contents indicates that a
20720 variable has been optimized away. */
4fc6c0d5 20721 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20722 {
f1e6e072 20723 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20724 return;
20725 }
20726
20727 /* Handle one degenerate form of location expression specially, to
20728 preserve GDB's previous behavior when section offsets are
336d760d
AT
20729 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20730 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20731
4fc6c0d5 20732 if (attr->form_is_block ()
3019eac3
DE
20733 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20734 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20735 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20736 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20737 && (DW_BLOCK (attr)->size
20738 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20739 {
891d2f0b 20740 unsigned int dummy;
4c2df51b 20741
3019eac3 20742 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20743 SET_SYMBOL_VALUE_ADDRESS
20744 (sym, cu->header.read_address (objfile->obfd,
20745 DW_BLOCK (attr)->data + 1,
20746 &dummy));
3019eac3 20747 else
38583298
TT
20748 SET_SYMBOL_VALUE_ADDRESS
20749 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20750 &dummy));
f1e6e072 20751 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20752 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20753 SET_SYMBOL_VALUE_ADDRESS
20754 (sym,
20755 SYMBOL_VALUE_ADDRESS (sym)
20756 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20757 return;
20758 }
20759
20760 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20761 expression evaluator, and use LOC_COMPUTED only when necessary
20762 (i.e. when the value of a register or memory location is
20763 referenced, or a thread-local block, etc.). Then again, it might
20764 not be worthwhile. I'm assuming that it isn't unless performance
20765 or memory numbers show me otherwise. */
20766
f1e6e072 20767 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20768
f1e6e072 20769 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20770 cu->has_loclist = true;
4c2df51b
DJ
20771}
20772
c906108c
SS
20773/* Given a pointer to a DWARF information entry, figure out if we need
20774 to make a symbol table entry for it, and if so, create a new entry
20775 and return a pointer to it.
20776 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20777 used the passed type.
20778 If SPACE is not NULL, use it to hold the new symbol. If it is
20779 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20780
20781static struct symbol *
5e2db402
TT
20782new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20783 struct symbol *space)
c906108c 20784{
5e22e966 20785 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 20786 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 20787 struct gdbarch *gdbarch = objfile->arch ();
c906108c 20788 struct symbol *sym = NULL;
15d034d0 20789 const char *name;
c906108c
SS
20790 struct attribute *attr = NULL;
20791 struct attribute *attr2 = NULL;
e142c38c 20792 CORE_ADDR baseaddr;
e37fd15a
SW
20793 struct pending **list_to_add = NULL;
20794
edb3359d 20795 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20796
b3b3bada 20797 baseaddr = objfile->text_section_offset ();
c906108c 20798
94af9270 20799 name = dwarf2_name (die, cu);
c906108c
SS
20800 if (name)
20801 {
34eaf542 20802 int suppress_add = 0;
94af9270 20803
34eaf542
TT
20804 if (space)
20805 sym = space;
20806 else
8c14c3a3 20807 sym = new (&objfile->objfile_obstack) symbol;
c906108c 20808 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20809
20810 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20811 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
20812 /* Fortran does not have mangling standard and the mangling does differ
20813 between gfortran, iFort etc. */
bcfe6157
TT
20814 const char *physname
20815 = (cu->language == language_fortran
20816 ? dwarf2_full_name (name, die, cu)
20817 : dwarf2_physname (name, die, cu));
20818 const char *linkagename = dw2_linkage_name (die, cu);
20819
20820 if (linkagename == nullptr || cu->language == language_ada)
20821 sym->set_linkage_name (physname);
20822 else
20823 {
20824 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20825 sym->set_linkage_name (linkagename);
20826 }
f55ee35c 20827
c906108c 20828 /* Default assumptions.
c5aa993b 20829 Use the passed type or decode it from the die. */
176620f1 20830 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20831 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20832 if (type != NULL)
20833 SYMBOL_TYPE (sym) = type;
20834 else
e7c27a73 20835 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20836 attr = dwarf2_attr (die,
20837 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20838 cu);
435d3d88 20839 if (attr != nullptr)
c906108c
SS
20840 {
20841 SYMBOL_LINE (sym) = DW_UNSND (attr);
20842 }
cb1df416 20843
edb3359d
DJ
20844 attr = dwarf2_attr (die,
20845 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20846 cu);
435d3d88 20847 if (attr != nullptr)
cb1df416 20848 {
ecfb656c 20849 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20850 struct file_entry *fe;
9a619af0 20851
ecfb656c
PA
20852 if (cu->line_header != NULL)
20853 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20854 else
20855 fe = NULL;
20856
20857 if (fe == NULL)
b98664d3 20858 complaint (_("file index out of range"));
8c43009f
PA
20859 else
20860 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20861 }
20862
c906108c
SS
20863 switch (die->tag)
20864 {
20865 case DW_TAG_label:
e142c38c 20866 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20867 if (attr != nullptr)
3e29f34a
MR
20868 {
20869 CORE_ADDR addr;
20870
cd6c91b4 20871 addr = attr->value_as_address ();
3e29f34a 20872 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20873 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20874 }
0f5238ed
TT
20875 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20876 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20877 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20878 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20879 break;
20880 case DW_TAG_subprogram:
20881 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20882 finish_block. */
f1e6e072 20883 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20884 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20885 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20886 || cu->language == language_ada
20887 || cu->language == language_fortran)
c906108c 20888 {
2cfa0c8d 20889 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20890 Ada and Fortran subprograms, whether marked external or
20891 not, are always stored as a global symbol, because we want
20892 to be able to access them globally. For instance, we want
20893 to be able to break on a nested subprogram without having
20894 to specify the context. */
c24bdb02 20895 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20896 }
20897 else
20898 {
e37fd15a 20899 list_to_add = cu->list_in_scope;
c906108c
SS
20900 }
20901 break;
edb3359d
DJ
20902 case DW_TAG_inlined_subroutine:
20903 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20904 finish_block. */
f1e6e072 20905 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20906 SYMBOL_INLINED (sym) = 1;
481860b3 20907 list_to_add = cu->list_in_scope;
edb3359d 20908 break;
34eaf542
TT
20909 case DW_TAG_template_value_param:
20910 suppress_add = 1;
20911 /* Fall through. */
72929c62 20912 case DW_TAG_constant:
c906108c 20913 case DW_TAG_variable:
254e6b9e 20914 case DW_TAG_member:
0963b4bd
MS
20915 /* Compilation with minimal debug info may result in
20916 variables with missing type entries. Change the
20917 misleading `void' type to something sensible. */
78134374 20918 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 20919 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20920
e142c38c 20921 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20922 /* In the case of DW_TAG_member, we should only be called for
20923 static const members. */
20924 if (die->tag == DW_TAG_member)
20925 {
3863f96c
DE
20926 /* dwarf2_add_field uses die_is_declaration,
20927 so we do the same. */
254e6b9e
DE
20928 gdb_assert (die_is_declaration (die, cu));
20929 gdb_assert (attr);
20930 }
435d3d88 20931 if (attr != nullptr)
c906108c 20932 {
e7c27a73 20933 dwarf2_const_value (attr, sym, cu);
e142c38c 20934 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20935 if (!suppress_add)
34eaf542
TT
20936 {
20937 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20938 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20939 else
e37fd15a 20940 list_to_add = cu->list_in_scope;
34eaf542 20941 }
c906108c
SS
20942 break;
20943 }
e142c38c 20944 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20945 if (attr != nullptr)
c906108c 20946 {
e7c27a73 20947 var_decode_location (attr, sym, cu);
e142c38c 20948 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20949
20950 /* Fortran explicitly imports any global symbols to the local
20951 scope by DW_TAG_common_block. */
20952 if (cu->language == language_fortran && die->parent
20953 && die->parent->tag == DW_TAG_common_block)
20954 attr2 = NULL;
20955
caac4577
JG
20956 if (SYMBOL_CLASS (sym) == LOC_STATIC
20957 && SYMBOL_VALUE_ADDRESS (sym) == 0
5989a64e 20958 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
20959 {
20960 /* When a static variable is eliminated by the linker,
20961 the corresponding debug information is not stripped
20962 out, but the variable address is set to null;
20963 do not add such variables into symbol table. */
20964 }
20965 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20966 {
4b610737
TT
20967 if (SYMBOL_CLASS (sym) == LOC_STATIC
20968 && (objfile->flags & OBJF_MAINLINE) == 0
5989a64e 20969 && dwarf2_per_objfile->per_bfd->can_copy)
4b610737
TT
20970 {
20971 /* A global static variable might be subject to
20972 copy relocation. We first check for a local
20973 minsym, though, because maybe the symbol was
20974 marked hidden, in which case this would not
20975 apply. */
20976 bound_minimal_symbol found
20977 = (lookup_minimal_symbol_linkage
987012b8 20978 (sym->linkage_name (), objfile));
4b610737
TT
20979 if (found.minsym != nullptr)
20980 sym->maybe_copied = 1;
20981 }
f55ee35c 20982
1c809c68
TT
20983 /* A variable with DW_AT_external is never static,
20984 but it may be block-scoped. */
804d2729 20985 list_to_add
c24bdb02
KS
20986 = ((cu->list_in_scope
20987 == cu->get_builder ()->get_file_symbols ())
20988 ? cu->get_builder ()->get_global_symbols ()
804d2729 20989 : cu->list_in_scope);
1c809c68 20990 }
c906108c 20991 else
e37fd15a 20992 list_to_add = cu->list_in_scope;
c906108c
SS
20993 }
20994 else
20995 {
20996 /* We do not know the address of this symbol.
c5aa993b
JM
20997 If it is an external symbol and we have type information
20998 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20999 The address of the variable will then be determined from
21000 the minimal symbol table whenever the variable is
21001 referenced. */
e142c38c 21002 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21003
21004 /* Fortran explicitly imports any global symbols to the local
21005 scope by DW_TAG_common_block. */
21006 if (cu->language == language_fortran && die->parent
21007 && die->parent->tag == DW_TAG_common_block)
21008 {
21009 /* SYMBOL_CLASS doesn't matter here because
21010 read_common_block is going to reset it. */
21011 if (!suppress_add)
21012 list_to_add = cu->list_in_scope;
21013 }
21014 else if (attr2 && (DW_UNSND (attr2) != 0)
21015 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21016 {
0fe7935b
DJ
21017 /* A variable with DW_AT_external is never static, but it
21018 may be block-scoped. */
804d2729 21019 list_to_add
c24bdb02
KS
21020 = ((cu->list_in_scope
21021 == cu->get_builder ()->get_file_symbols ())
21022 ? cu->get_builder ()->get_global_symbols ()
804d2729 21023 : cu->list_in_scope);
0fe7935b 21024
f1e6e072 21025 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21026 }
442ddf59
JK
21027 else if (!die_is_declaration (die, cu))
21028 {
21029 /* Use the default LOC_OPTIMIZED_OUT class. */
21030 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21031 if (!suppress_add)
21032 list_to_add = cu->list_in_scope;
442ddf59 21033 }
c906108c
SS
21034 }
21035 break;
21036 case DW_TAG_formal_parameter:
a60f3166
TT
21037 {
21038 /* If we are inside a function, mark this as an argument. If
21039 not, we might be looking at an argument to an inlined function
21040 when we do not have enough information to show inlined frames;
21041 pretend it's a local variable in that case so that the user can
21042 still see it. */
804d2729 21043 struct context_stack *curr
c24bdb02 21044 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
21045 if (curr != nullptr && curr->name != nullptr)
21046 SYMBOL_IS_ARGUMENT (sym) = 1;
21047 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21048 if (attr != nullptr)
a60f3166
TT
21049 {
21050 var_decode_location (attr, sym, cu);
21051 }
21052 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21053 if (attr != nullptr)
a60f3166
TT
21054 {
21055 dwarf2_const_value (attr, sym, cu);
21056 }
f346a30d 21057
a60f3166
TT
21058 list_to_add = cu->list_in_scope;
21059 }
c906108c
SS
21060 break;
21061 case DW_TAG_unspecified_parameters:
21062 /* From varargs functions; gdb doesn't seem to have any
21063 interest in this information, so just ignore it for now.
21064 (FIXME?) */
21065 break;
34eaf542
TT
21066 case DW_TAG_template_type_param:
21067 suppress_add = 1;
21068 /* Fall through. */
c906108c 21069 case DW_TAG_class_type:
680b30c7 21070 case DW_TAG_interface_type:
c906108c
SS
21071 case DW_TAG_structure_type:
21072 case DW_TAG_union_type:
72019c9c 21073 case DW_TAG_set_type:
c906108c 21074 case DW_TAG_enumeration_type:
f1e6e072 21075 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21076 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21077
63d06c5c 21078 {
9c37b5ae 21079 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21080 really ever be static objects: otherwise, if you try
21081 to, say, break of a class's method and you're in a file
21082 which doesn't mention that class, it won't work unless
21083 the check for all static symbols in lookup_symbol_aux
21084 saves you. See the OtherFileClass tests in
21085 gdb.c++/namespace.exp. */
21086
e37fd15a 21087 if (!suppress_add)
34eaf542 21088 {
c24bdb02 21089 buildsym_compunit *builder = cu->get_builder ();
804d2729 21090 list_to_add
c24bdb02 21091 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 21092 && cu->language == language_cplus
c24bdb02 21093 ? builder->get_global_symbols ()
804d2729 21094 : cu->list_in_scope);
63d06c5c 21095
64382290 21096 /* The semantics of C++ state that "struct foo {
9c37b5ae 21097 ... }" also defines a typedef for "foo". */
64382290 21098 if (cu->language == language_cplus
45280282 21099 || cu->language == language_ada
c44af4eb
TT
21100 || cu->language == language_d
21101 || cu->language == language_rust)
64382290
TT
21102 {
21103 /* The symbol's name is already allocated along
21104 with this objfile, so we don't need to
21105 duplicate it for the type. */
7d93a1e0 21106 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 21107 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 21108 }
63d06c5c
DC
21109 }
21110 }
c906108c
SS
21111 break;
21112 case DW_TAG_typedef:
f1e6e072 21113 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21114 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21115 list_to_add = cu->list_in_scope;
63d06c5c 21116 break;
c906108c 21117 case DW_TAG_base_type:
a02abb62 21118 case DW_TAG_subrange_type:
f1e6e072 21119 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21120 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21121 list_to_add = cu->list_in_scope;
c906108c
SS
21122 break;
21123 case DW_TAG_enumerator:
e142c38c 21124 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21125 if (attr != nullptr)
c906108c 21126 {
e7c27a73 21127 dwarf2_const_value (attr, sym, cu);
c906108c 21128 }
63d06c5c
DC
21129 {
21130 /* NOTE: carlton/2003-11-10: See comment above in the
21131 DW_TAG_class_type, etc. block. */
21132
804d2729 21133 list_to_add
c24bdb02 21134 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21135 && cu->language == language_cplus
c24bdb02 21136 ? cu->get_builder ()->get_global_symbols ()
804d2729 21137 : cu->list_in_scope);
63d06c5c 21138 }
c906108c 21139 break;
74921315 21140 case DW_TAG_imported_declaration:
5c4e30ca 21141 case DW_TAG_namespace:
f1e6e072 21142 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21143 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21144 break;
530e8392
KB
21145 case DW_TAG_module:
21146 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21147 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21148 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21149 break;
4357ac6c 21150 case DW_TAG_common_block:
f1e6e072 21151 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21152 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21153 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21154 break;
c906108c
SS
21155 default:
21156 /* Not a tag we recognize. Hopefully we aren't processing
21157 trash data, but since we must specifically ignore things
21158 we don't recognize, there is nothing else we should do at
0963b4bd 21159 this point. */
b98664d3 21160 complaint (_("unsupported tag: '%s'"),
4d3c2250 21161 dwarf_tag_name (die->tag));
c906108c
SS
21162 break;
21163 }
df8a16a1 21164
e37fd15a
SW
21165 if (suppress_add)
21166 {
21167 sym->hash_next = objfile->template_symbols;
21168 objfile->template_symbols = sym;
21169 list_to_add = NULL;
21170 }
21171
21172 if (list_to_add != NULL)
d3cb6808 21173 add_symbol_to_list (sym, list_to_add);
e37fd15a 21174
df8a16a1
DJ
21175 /* For the benefit of old versions of GCC, check for anonymous
21176 namespaces based on the demangled name. */
4d4ec4e5 21177 if (!cu->processing_has_namespace_info
94af9270 21178 && cu->language == language_cplus)
c24bdb02 21179 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21180 }
21181 return (sym);
21182}
21183
98bfdba5
PA
21184/* Given an attr with a DW_FORM_dataN value in host byte order,
21185 zero-extend it as appropriate for the symbol's type. The DWARF
21186 standard (v4) is not entirely clear about the meaning of using
21187 DW_FORM_dataN for a constant with a signed type, where the type is
21188 wider than the data. The conclusion of a discussion on the DWARF
21189 list was that this is unspecified. We choose to always zero-extend
21190 because that is the interpretation long in use by GCC. */
c906108c 21191
98bfdba5 21192static gdb_byte *
ff39bb5e 21193dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21194 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21195{
5e22e966 21196 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
21197 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21198 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21199 LONGEST l = DW_UNSND (attr);
21200
21201 if (bits < sizeof (*value) * 8)
21202 {
21203 l &= ((LONGEST) 1 << bits) - 1;
21204 *value = l;
21205 }
21206 else if (bits == sizeof (*value) * 8)
21207 *value = l;
21208 else
21209 {
224c3ddb 21210 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21211 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21212 return bytes;
21213 }
21214
21215 return NULL;
21216}
21217
21218/* Read a constant value from an attribute. Either set *VALUE, or if
21219 the value does not fit in *VALUE, set *BYTES - either already
21220 allocated on the objfile obstack, or newly allocated on OBSTACK,
21221 or, set *BATON, if we translated the constant to a location
21222 expression. */
21223
21224static void
ff39bb5e 21225dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21226 const char *name, struct obstack *obstack,
21227 struct dwarf2_cu *cu,
d521ce57 21228 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21229 struct dwarf2_locexpr_baton **baton)
21230{
5e22e966 21231 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 21232 struct objfile *objfile = per_objfile->objfile;
98bfdba5 21233 struct comp_unit_head *cu_header = &cu->header;
c906108c 21234 struct dwarf_block *blk;
98bfdba5
PA
21235 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21236 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21237
21238 *value = 0;
21239 *bytes = NULL;
21240 *baton = NULL;
c906108c
SS
21241
21242 switch (attr->form)
21243 {
21244 case DW_FORM_addr:
336d760d 21245 case DW_FORM_addrx:
3019eac3 21246 case DW_FORM_GNU_addr_index:
ac56253d 21247 {
ac56253d
TT
21248 gdb_byte *data;
21249
98bfdba5
PA
21250 if (TYPE_LENGTH (type) != cu_header->addr_size)
21251 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21252 cu_header->addr_size,
98bfdba5 21253 TYPE_LENGTH (type));
ac56253d
TT
21254 /* Symbols of this form are reasonably rare, so we just
21255 piggyback on the existing location code rather than writing
21256 a new implementation of symbol_computed_ops. */
8d749320 21257 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 21258 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
21259 (*baton)->per_cu = cu->per_cu;
21260 gdb_assert ((*baton)->per_cu);
ac56253d 21261
98bfdba5 21262 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21263 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21264 (*baton)->data = data;
ac56253d
TT
21265
21266 data[0] = DW_OP_addr;
21267 store_unsigned_integer (&data[1], cu_header->addr_size,
21268 byte_order, DW_ADDR (attr));
21269 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21270 }
c906108c 21271 break;
4ac36638 21272 case DW_FORM_string:
93b5768b 21273 case DW_FORM_strp:
cf532bd1 21274 case DW_FORM_strx:
3019eac3 21275 case DW_FORM_GNU_str_index:
36586728 21276 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21277 /* DW_STRING is already allocated on the objfile obstack, point
21278 directly to it. */
d521ce57 21279 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21280 break;
c906108c
SS
21281 case DW_FORM_block1:
21282 case DW_FORM_block2:
21283 case DW_FORM_block4:
21284 case DW_FORM_block:
2dc7f7b3 21285 case DW_FORM_exprloc:
0224619f 21286 case DW_FORM_data16:
c906108c 21287 blk = DW_BLOCK (attr);
98bfdba5
PA
21288 if (TYPE_LENGTH (type) != blk->size)
21289 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21290 TYPE_LENGTH (type));
21291 *bytes = blk->data;
c906108c 21292 break;
2df3850c
JM
21293
21294 /* The DW_AT_const_value attributes are supposed to carry the
21295 symbol's value "represented as it would be on the target
21296 architecture." By the time we get here, it's already been
21297 converted to host endianness, so we just need to sign- or
21298 zero-extend it as appropriate. */
21299 case DW_FORM_data1:
3aef2284 21300 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21301 break;
c906108c 21302 case DW_FORM_data2:
3aef2284 21303 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21304 break;
c906108c 21305 case DW_FORM_data4:
3aef2284 21306 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21307 break;
c906108c 21308 case DW_FORM_data8:
3aef2284 21309 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21310 break;
21311
c906108c 21312 case DW_FORM_sdata:
663c44ac 21313 case DW_FORM_implicit_const:
98bfdba5 21314 *value = DW_SND (attr);
2df3850c
JM
21315 break;
21316
c906108c 21317 case DW_FORM_udata:
98bfdba5 21318 *value = DW_UNSND (attr);
c906108c 21319 break;
2df3850c 21320
c906108c 21321 default:
b98664d3 21322 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21323 dwarf_form_name (attr->form));
98bfdba5 21324 *value = 0;
c906108c
SS
21325 break;
21326 }
21327}
21328
2df3850c 21329
98bfdba5
PA
21330/* Copy constant value from an attribute to a symbol. */
21331
2df3850c 21332static void
ff39bb5e 21333dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21334 struct dwarf2_cu *cu)
2df3850c 21335{
5e22e966 21336 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 21337 LONGEST value;
d521ce57 21338 const gdb_byte *bytes;
98bfdba5 21339 struct dwarf2_locexpr_baton *baton;
2df3850c 21340
98bfdba5 21341 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21342 sym->print_name (),
98bfdba5
PA
21343 &objfile->objfile_obstack, cu,
21344 &value, &bytes, &baton);
2df3850c 21345
98bfdba5
PA
21346 if (baton != NULL)
21347 {
98bfdba5 21348 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21349 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21350 }
21351 else if (bytes != NULL)
21352 {
21353 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21354 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21355 }
21356 else
21357 {
21358 SYMBOL_VALUE (sym) = value;
f1e6e072 21359 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21360 }
2df3850c
JM
21361}
21362
c906108c
SS
21363/* Return the type of the die in question using its DW_AT_type attribute. */
21364
21365static struct type *
e7c27a73 21366die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21367{
c906108c 21368 struct attribute *type_attr;
c906108c 21369
e142c38c 21370 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21371 if (!type_attr)
21372 {
5e22e966 21373 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21374 /* A missing DW_AT_type represents a void type. */
518817b3 21375 return objfile_type (objfile)->builtin_void;
c906108c 21376 }
348e048f 21377
673bfd45 21378 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21379}
21380
b4ba55a1
JB
21381/* True iff CU's producer generates GNAT Ada auxiliary information
21382 that allows to find parallel types through that information instead
21383 of having to do expensive parallel lookups by type name. */
21384
21385static int
21386need_gnat_info (struct dwarf2_cu *cu)
21387{
de4cb04a
JB
21388 /* Assume that the Ada compiler was GNAT, which always produces
21389 the auxiliary information. */
21390 return (cu->language == language_ada);
b4ba55a1
JB
21391}
21392
b4ba55a1
JB
21393/* Return the auxiliary type of the die in question using its
21394 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21395 attribute is not present. */
21396
21397static struct type *
21398die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21399{
b4ba55a1 21400 struct attribute *type_attr;
b4ba55a1
JB
21401
21402 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21403 if (!type_attr)
21404 return NULL;
21405
673bfd45 21406 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21407}
21408
21409/* If DIE has a descriptive_type attribute, then set the TYPE's
21410 descriptive type accordingly. */
21411
21412static void
21413set_descriptive_type (struct type *type, struct die_info *die,
21414 struct dwarf2_cu *cu)
21415{
21416 struct type *descriptive_type = die_descriptive_type (die, cu);
21417
21418 if (descriptive_type)
21419 {
21420 ALLOCATE_GNAT_AUX_TYPE (type);
21421 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21422 }
21423}
21424
c906108c
SS
21425/* Return the containing type of the die in question using its
21426 DW_AT_containing_type attribute. */
21427
21428static struct type *
e7c27a73 21429die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21430{
c906108c 21431 struct attribute *type_attr;
5e22e966 21432 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21433
e142c38c 21434 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21435 if (!type_attr)
21436 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21437 "[in module %s]"), objfile_name (objfile));
33ac96f0 21438
673bfd45 21439 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21440}
21441
ac9ec31b
DE
21442/* Return an error marker type to use for the ill formed type in DIE/CU. */
21443
21444static struct type *
21445build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21446{
5e22e966 21447 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ac9ec31b 21448 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 21449 char *saved;
ac9ec31b 21450
528e1572
SM
21451 std::string message
21452 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21453 objfile_name (objfile),
21454 sect_offset_str (cu->header.sect_off),
21455 sect_offset_str (die->sect_off));
efba19b0 21456 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21457
19f392bc 21458 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21459}
21460
673bfd45 21461/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21462 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21463 DW_AT_containing_type.
673bfd45
DE
21464 If there is no type substitute an error marker. */
21465
c906108c 21466static struct type *
ff39bb5e 21467lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21468 struct dwarf2_cu *cu)
c906108c 21469{
5e22e966 21470 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 21471 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21472 struct type *this_type;
21473
ac9ec31b
DE
21474 gdb_assert (attr->name == DW_AT_type
21475 || attr->name == DW_AT_GNAT_descriptive_type
21476 || attr->name == DW_AT_containing_type);
21477
673bfd45
DE
21478 /* First see if we have it cached. */
21479
36586728
TT
21480 if (attr->form == DW_FORM_GNU_ref_alt)
21481 {
21482 struct dwarf2_per_cu_data *per_cu;
0826b30a 21483 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 21484
ed2dc618
SM
21485 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21486 dwarf2_per_objfile);
aa66c379 21487 this_type = get_die_type_at_offset (sect_off, per_cu, dwarf2_per_objfile);
36586728 21488 }
cd6c91b4 21489 else if (attr->form_is_ref ())
673bfd45 21490 {
0826b30a 21491 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 21492
aa66c379
SM
21493 this_type = get_die_type_at_offset (sect_off, cu->per_cu,
21494 dwarf2_per_objfile);
673bfd45 21495 }
55f1336d 21496 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21497 {
ac9ec31b 21498 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21499
ac9ec31b 21500 return get_signatured_type (die, signature, cu);
673bfd45
DE
21501 }
21502 else
21503 {
b98664d3 21504 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21505 " at %s [in module %s]"),
21506 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21507 objfile_name (objfile));
ac9ec31b 21508 return build_error_marker_type (cu, die);
673bfd45
DE
21509 }
21510
21511 /* If not cached we need to read it in. */
21512
21513 if (this_type == NULL)
21514 {
ac9ec31b 21515 struct die_info *type_die = NULL;
673bfd45
DE
21516 struct dwarf2_cu *type_cu = cu;
21517
cd6c91b4 21518 if (attr->form_is_ref ())
ac9ec31b
DE
21519 type_die = follow_die_ref (die, attr, &type_cu);
21520 if (type_die == NULL)
21521 return build_error_marker_type (cu, die);
21522 /* If we find the type now, it's probably because the type came
3019eac3
DE
21523 from an inter-CU reference and the type's CU got expanded before
21524 ours. */
ac9ec31b 21525 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21526 }
21527
21528 /* If we still don't have a type use an error marker. */
21529
21530 if (this_type == NULL)
ac9ec31b 21531 return build_error_marker_type (cu, die);
673bfd45 21532
f792889a 21533 return this_type;
c906108c
SS
21534}
21535
673bfd45
DE
21536/* Return the type in DIE, CU.
21537 Returns NULL for invalid types.
21538
02142a6c 21539 This first does a lookup in die_type_hash,
673bfd45
DE
21540 and only reads the die in if necessary.
21541
21542 NOTE: This can be called when reading in partial or full symbols. */
21543
f792889a 21544static struct type *
e7c27a73 21545read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21546{
f792889a
DJ
21547 struct type *this_type;
21548
21549 this_type = get_die_type (die, cu);
21550 if (this_type)
21551 return this_type;
21552
673bfd45
DE
21553 return read_type_die_1 (die, cu);
21554}
21555
21556/* Read the type in DIE, CU.
21557 Returns NULL for invalid types. */
21558
21559static struct type *
21560read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21561{
21562 struct type *this_type = NULL;
21563
c906108c
SS
21564 switch (die->tag)
21565 {
21566 case DW_TAG_class_type:
680b30c7 21567 case DW_TAG_interface_type:
c906108c
SS
21568 case DW_TAG_structure_type:
21569 case DW_TAG_union_type:
f792889a 21570 this_type = read_structure_type (die, cu);
c906108c
SS
21571 break;
21572 case DW_TAG_enumeration_type:
f792889a 21573 this_type = read_enumeration_type (die, cu);
c906108c
SS
21574 break;
21575 case DW_TAG_subprogram:
21576 case DW_TAG_subroutine_type:
edb3359d 21577 case DW_TAG_inlined_subroutine:
f792889a 21578 this_type = read_subroutine_type (die, cu);
c906108c
SS
21579 break;
21580 case DW_TAG_array_type:
f792889a 21581 this_type = read_array_type (die, cu);
c906108c 21582 break;
72019c9c 21583 case DW_TAG_set_type:
f792889a 21584 this_type = read_set_type (die, cu);
72019c9c 21585 break;
c906108c 21586 case DW_TAG_pointer_type:
f792889a 21587 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21588 break;
21589 case DW_TAG_ptr_to_member_type:
f792889a 21590 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21591 break;
21592 case DW_TAG_reference_type:
4297a3f0
AV
21593 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21594 break;
21595 case DW_TAG_rvalue_reference_type:
21596 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21597 break;
21598 case DW_TAG_const_type:
f792889a 21599 this_type = read_tag_const_type (die, cu);
c906108c
SS
21600 break;
21601 case DW_TAG_volatile_type:
f792889a 21602 this_type = read_tag_volatile_type (die, cu);
c906108c 21603 break;
06d66ee9
TT
21604 case DW_TAG_restrict_type:
21605 this_type = read_tag_restrict_type (die, cu);
21606 break;
c906108c 21607 case DW_TAG_string_type:
f792889a 21608 this_type = read_tag_string_type (die, cu);
c906108c
SS
21609 break;
21610 case DW_TAG_typedef:
f792889a 21611 this_type = read_typedef (die, cu);
c906108c 21612 break;
a02abb62 21613 case DW_TAG_subrange_type:
f792889a 21614 this_type = read_subrange_type (die, cu);
a02abb62 21615 break;
c906108c 21616 case DW_TAG_base_type:
f792889a 21617 this_type = read_base_type (die, cu);
c906108c 21618 break;
81a17f79 21619 case DW_TAG_unspecified_type:
f792889a 21620 this_type = read_unspecified_type (die, cu);
81a17f79 21621 break;
0114d602
DJ
21622 case DW_TAG_namespace:
21623 this_type = read_namespace_type (die, cu);
21624 break;
f55ee35c
JK
21625 case DW_TAG_module:
21626 this_type = read_module_type (die, cu);
21627 break;
a2c2acaf
MW
21628 case DW_TAG_atomic_type:
21629 this_type = read_tag_atomic_type (die, cu);
21630 break;
c906108c 21631 default:
b98664d3 21632 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21633 dwarf_tag_name (die->tag));
c906108c
SS
21634 break;
21635 }
63d06c5c 21636
f792889a 21637 return this_type;
63d06c5c
DC
21638}
21639
abc72ce4
DE
21640/* See if we can figure out if the class lives in a namespace. We do
21641 this by looking for a member function; its demangled name will
21642 contain namespace info, if there is any.
21643 Return the computed name or NULL.
21644 Space for the result is allocated on the objfile's obstack.
21645 This is the full-die version of guess_partial_die_structure_name.
21646 In this case we know DIE has no useful parent. */
21647
43816ebc 21648static const char *
abc72ce4
DE
21649guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21650{
21651 struct die_info *spec_die;
21652 struct dwarf2_cu *spec_cu;
21653 struct die_info *child;
5e22e966 21654 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
21655
21656 spec_cu = cu;
21657 spec_die = die_specification (die, &spec_cu);
21658 if (spec_die != NULL)
21659 {
21660 die = spec_die;
21661 cu = spec_cu;
21662 }
21663
21664 for (child = die->child;
21665 child != NULL;
21666 child = child->sibling)
21667 {
21668 if (child->tag == DW_TAG_subprogram)
21669 {
73b9be8b 21670 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21671
7d45c7c3 21672 if (linkage_name != NULL)
abc72ce4 21673 {
43816ebc
TT
21674 gdb::unique_xmalloc_ptr<char> actual_name
21675 (language_class_name_from_physname (cu->language_defn,
21676 linkage_name));
21677 const char *name = NULL;
abc72ce4
DE
21678
21679 if (actual_name != NULL)
21680 {
15d034d0 21681 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21682
21683 if (die_name != NULL
43816ebc 21684 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21685 {
21686 /* Strip off the class name from the full name.
21687 We want the prefix. */
21688 int die_name_len = strlen (die_name);
43816ebc
TT
21689 int actual_name_len = strlen (actual_name.get ());
21690 const char *ptr = actual_name.get ();
abc72ce4
DE
21691
21692 /* Test for '::' as a sanity check. */
21693 if (actual_name_len > die_name_len + 2
43816ebc 21694 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21695 name = obstack_strndup (
e3b94546 21696 &objfile->per_bfd->storage_obstack,
43816ebc 21697 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21698 }
21699 }
abc72ce4
DE
21700 return name;
21701 }
21702 }
21703 }
21704
21705 return NULL;
21706}
21707
96408a79
SA
21708/* GCC might emit a nameless typedef that has a linkage name. Determine the
21709 prefix part in such case. See
21710 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21711
a121b7c1 21712static const char *
96408a79
SA
21713anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21714{
21715 struct attribute *attr;
e6a959d6 21716 const char *base;
96408a79
SA
21717
21718 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21719 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21720 return NULL;
21721
7d45c7c3 21722 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21723 return NULL;
21724
73b9be8b 21725 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21726 if (attr == NULL || DW_STRING (attr) == NULL)
21727 return NULL;
21728
21729 /* dwarf2_name had to be already called. */
21730 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21731
21732 /* Strip the base name, keep any leading namespaces/classes. */
21733 base = strrchr (DW_STRING (attr), ':');
21734 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21735 return "";
21736
5e22e966 21737 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9
TT
21738 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21739 DW_STRING (attr),
21740 &base[-1] - DW_STRING (attr));
96408a79
SA
21741}
21742
fdde2d81 21743/* Return the name of the namespace/class that DIE is defined within,
0114d602 21744 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21745
0114d602
DJ
21746 For example, if we're within the method foo() in the following
21747 code:
21748
21749 namespace N {
21750 class C {
21751 void foo () {
21752 }
21753 };
21754 }
21755
21756 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21757
0d5cff50 21758static const char *
e142c38c 21759determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21760{
5e22e966 21761 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
0114d602
DJ
21762 struct die_info *parent, *spec_die;
21763 struct dwarf2_cu *spec_cu;
21764 struct type *parent_type;
a121b7c1 21765 const char *retval;
63d06c5c 21766
9c37b5ae 21767 if (cu->language != language_cplus
c44af4eb
TT
21768 && cu->language != language_fortran && cu->language != language_d
21769 && cu->language != language_rust)
0114d602
DJ
21770 return "";
21771
96408a79
SA
21772 retval = anonymous_struct_prefix (die, cu);
21773 if (retval)
21774 return retval;
21775
0114d602
DJ
21776 /* We have to be careful in the presence of DW_AT_specification.
21777 For example, with GCC 3.4, given the code
21778
21779 namespace N {
21780 void foo() {
21781 // Definition of N::foo.
21782 }
21783 }
21784
21785 then we'll have a tree of DIEs like this:
21786
21787 1: DW_TAG_compile_unit
21788 2: DW_TAG_namespace // N
21789 3: DW_TAG_subprogram // declaration of N::foo
21790 4: DW_TAG_subprogram // definition of N::foo
21791 DW_AT_specification // refers to die #3
21792
21793 Thus, when processing die #4, we have to pretend that we're in
21794 the context of its DW_AT_specification, namely the contex of die
21795 #3. */
21796 spec_cu = cu;
21797 spec_die = die_specification (die, &spec_cu);
21798 if (spec_die == NULL)
21799 parent = die->parent;
21800 else
63d06c5c 21801 {
0114d602
DJ
21802 parent = spec_die->parent;
21803 cu = spec_cu;
63d06c5c 21804 }
0114d602
DJ
21805
21806 if (parent == NULL)
21807 return "";
98bfdba5
PA
21808 else if (parent->building_fullname)
21809 {
21810 const char *name;
21811 const char *parent_name;
21812
21813 /* It has been seen on RealView 2.2 built binaries,
21814 DW_TAG_template_type_param types actually _defined_ as
21815 children of the parent class:
21816
21817 enum E {};
21818 template class <class Enum> Class{};
21819 Class<enum E> class_e;
21820
21821 1: DW_TAG_class_type (Class)
21822 2: DW_TAG_enumeration_type (E)
21823 3: DW_TAG_enumerator (enum1:0)
21824 3: DW_TAG_enumerator (enum2:1)
21825 ...
21826 2: DW_TAG_template_type_param
21827 DW_AT_type DW_FORM_ref_udata (E)
21828
21829 Besides being broken debug info, it can put GDB into an
21830 infinite loop. Consider:
21831
21832 When we're building the full name for Class<E>, we'll start
21833 at Class, and go look over its template type parameters,
21834 finding E. We'll then try to build the full name of E, and
21835 reach here. We're now trying to build the full name of E,
21836 and look over the parent DIE for containing scope. In the
21837 broken case, if we followed the parent DIE of E, we'd again
21838 find Class, and once again go look at its template type
21839 arguments, etc., etc. Simply don't consider such parent die
21840 as source-level parent of this die (it can't be, the language
21841 doesn't allow it), and break the loop here. */
21842 name = dwarf2_name (die, cu);
21843 parent_name = dwarf2_name (parent, cu);
b98664d3 21844 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21845 name ? name : "<unknown>",
21846 parent_name ? parent_name : "<unknown>");
21847 return "";
21848 }
63d06c5c 21849 else
0114d602
DJ
21850 switch (parent->tag)
21851 {
63d06c5c 21852 case DW_TAG_namespace:
0114d602 21853 parent_type = read_type_die (parent, cu);
acebe513
UW
21854 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21855 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21856 Work around this problem here. */
21857 if (cu->language == language_cplus
7d93a1e0 21858 && strcmp (parent_type->name (), "::") == 0)
acebe513 21859 return "";
0114d602 21860 /* We give a name to even anonymous namespaces. */
7d93a1e0 21861 return parent_type->name ();
63d06c5c 21862 case DW_TAG_class_type:
680b30c7 21863 case DW_TAG_interface_type:
63d06c5c 21864 case DW_TAG_structure_type:
0114d602 21865 case DW_TAG_union_type:
f55ee35c 21866 case DW_TAG_module:
0114d602 21867 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
21868 if (parent_type->name () != NULL)
21869 return parent_type->name ();
0114d602
DJ
21870 else
21871 /* An anonymous structure is only allowed non-static data
21872 members; no typedefs, no member functions, et cetera.
21873 So it does not need a prefix. */
21874 return "";
abc72ce4 21875 case DW_TAG_compile_unit:
95554aad 21876 case DW_TAG_partial_unit:
abc72ce4
DE
21877 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21878 if (cu->language == language_cplus
5989a64e 21879 && !dwarf2_per_objfile->per_bfd->types.empty ()
abc72ce4
DE
21880 && die->child != NULL
21881 && (die->tag == DW_TAG_class_type
21882 || die->tag == DW_TAG_structure_type
21883 || die->tag == DW_TAG_union_type))
21884 {
43816ebc 21885 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21886 if (name != NULL)
21887 return name;
21888 }
21889 return "";
0a4b0913
AB
21890 case DW_TAG_subprogram:
21891 /* Nested subroutines in Fortran get a prefix with the name
21892 of the parent's subroutine. */
21893 if (cu->language == language_fortran)
21894 {
21895 if ((die->tag == DW_TAG_subprogram)
21896 && (dwarf2_name (parent, cu) != NULL))
21897 return dwarf2_name (parent, cu);
21898 }
21899 return determine_prefix (parent, cu);
3d567982
TT
21900 case DW_TAG_enumeration_type:
21901 parent_type = read_type_die (parent, cu);
21902 if (TYPE_DECLARED_CLASS (parent_type))
21903 {
7d93a1e0
SM
21904 if (parent_type->name () != NULL)
21905 return parent_type->name ();
3d567982
TT
21906 return "";
21907 }
21908 /* Fall through. */
63d06c5c 21909 default:
8176b9b8 21910 return determine_prefix (parent, cu);
63d06c5c 21911 }
63d06c5c
DC
21912}
21913
3e43a32a
MS
21914/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21915 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21916 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21917 an obconcat, otherwise allocate storage for the result. The CU argument is
21918 used to determine the language and hence, the appropriate separator. */
987504bb 21919
f55ee35c 21920#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21921
21922static char *
f55ee35c
JK
21923typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21924 int physname, struct dwarf2_cu *cu)
63d06c5c 21925{
f55ee35c 21926 const char *lead = "";
5c315b68 21927 const char *sep;
63d06c5c 21928
3e43a32a
MS
21929 if (suffix == NULL || suffix[0] == '\0'
21930 || prefix == NULL || prefix[0] == '\0')
987504bb 21931 sep = "";
45280282
IB
21932 else if (cu->language == language_d)
21933 {
21934 /* For D, the 'main' function could be defined in any module, but it
21935 should never be prefixed. */
21936 if (strcmp (suffix, "D main") == 0)
21937 {
21938 prefix = "";
21939 sep = "";
21940 }
21941 else
21942 sep = ".";
21943 }
f55ee35c
JK
21944 else if (cu->language == language_fortran && physname)
21945 {
21946 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21947 DW_AT_MIPS_linkage_name is preferred and used instead. */
21948
21949 lead = "__";
21950 sep = "_MOD_";
21951 }
987504bb
JJ
21952 else
21953 sep = "::";
63d06c5c 21954
6dd47d34
DE
21955 if (prefix == NULL)
21956 prefix = "";
21957 if (suffix == NULL)
21958 suffix = "";
21959
987504bb
JJ
21960 if (obs == NULL)
21961 {
3e43a32a 21962 char *retval
224c3ddb
SM
21963 = ((char *)
21964 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21965
f55ee35c
JK
21966 strcpy (retval, lead);
21967 strcat (retval, prefix);
6dd47d34
DE
21968 strcat (retval, sep);
21969 strcat (retval, suffix);
63d06c5c
DC
21970 return retval;
21971 }
987504bb
JJ
21972 else
21973 {
21974 /* We have an obstack. */
f55ee35c 21975 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21976 }
63d06c5c
DC
21977}
21978
71c25dea
TT
21979/* Get name of a die, return NULL if not found. */
21980
15d034d0
TT
21981static const char *
21982dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 21983 struct objfile *objfile)
71c25dea
TT
21984{
21985 if (name && cu->language == language_cplus)
21986 {
596dc4ad
TT
21987 gdb::unique_xmalloc_ptr<char> canon_name
21988 = cp_canonicalize_string (name);
71c25dea 21989
596dc4ad
TT
21990 if (canon_name != nullptr)
21991 name = objfile->intern (canon_name.get ());
71c25dea
TT
21992 }
21993
21994 return name;
c906108c
SS
21995}
21996
96553a0c
DE
21997/* Get name of a die, return NULL if not found.
21998 Anonymous namespaces are converted to their magic string. */
9219021c 21999
15d034d0 22000static const char *
e142c38c 22001dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22002{
22003 struct attribute *attr;
5e22e966 22004 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 22005
e142c38c 22006 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22007 if ((!attr || !DW_STRING (attr))
96553a0c 22008 && die->tag != DW_TAG_namespace
53832f31
TT
22009 && die->tag != DW_TAG_class_type
22010 && die->tag != DW_TAG_interface_type
22011 && die->tag != DW_TAG_structure_type
22012 && die->tag != DW_TAG_union_type)
71c25dea
TT
22013 return NULL;
22014
22015 switch (die->tag)
22016 {
22017 case DW_TAG_compile_unit:
95554aad 22018 case DW_TAG_partial_unit:
71c25dea
TT
22019 /* Compilation units have a DW_AT_name that is a filename, not
22020 a source language identifier. */
22021 case DW_TAG_enumeration_type:
22022 case DW_TAG_enumerator:
22023 /* These tags always have simple identifiers already; no need
22024 to canonicalize them. */
22025 return DW_STRING (attr);
907af001 22026
96553a0c
DE
22027 case DW_TAG_namespace:
22028 if (attr != NULL && DW_STRING (attr) != NULL)
22029 return DW_STRING (attr);
22030 return CP_ANONYMOUS_NAMESPACE_STR;
22031
907af001
UW
22032 case DW_TAG_class_type:
22033 case DW_TAG_interface_type:
22034 case DW_TAG_structure_type:
22035 case DW_TAG_union_type:
22036 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22037 structures or unions. These were of the form "._%d" in GCC 4.1,
22038 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22039 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22040 if (attr && DW_STRING (attr)
61012eef
GB
22041 && (startswith (DW_STRING (attr), "._")
22042 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22043 return NULL;
53832f31
TT
22044
22045 /* GCC might emit a nameless typedef that has a linkage name. See
22046 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22047 if (!attr || DW_STRING (attr) == NULL)
22048 {
73b9be8b 22049 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22050 if (attr == NULL || DW_STRING (attr) == NULL)
22051 return NULL;
22052
df5c6c50
JK
22053 /* Avoid demangling DW_STRING (attr) the second time on a second
22054 call for the same DIE. */
22055 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 22056 {
43816ebc
TT
22057 gdb::unique_xmalloc_ptr<char> demangled
22058 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
22059 if (demangled == nullptr)
22060 return nullptr;
43816ebc 22061
be1e3d3e 22062 DW_STRING (attr) = objfile->intern (demangled.get ());
53832f31 22063 DW_STRING_IS_CANONICAL (attr) = 1;
53832f31 22064 }
67430cd0
TT
22065
22066 /* Strip any leading namespaces/classes, keep only the base name.
22067 DW_AT_name for named DIEs does not contain the prefixes. */
22068 const char *base = strrchr (DW_STRING (attr), ':');
22069 if (base && base > DW_STRING (attr) && base[-1] == ':')
22070 return &base[1];
22071 else
22072 return DW_STRING (attr);
53832f31 22073 }
907af001
UW
22074 break;
22075
71c25dea 22076 default:
907af001
UW
22077 break;
22078 }
22079
22080 if (!DW_STRING_IS_CANONICAL (attr))
22081 {
be1e3d3e
TT
22082 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22083 objfile);
907af001 22084 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22085 }
907af001 22086 return DW_STRING (attr);
9219021c
DC
22087}
22088
22089/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22090 is none. *EXT_CU is the CU containing DIE on input, and the CU
22091 containing the return value on output. */
9219021c
DC
22092
22093static struct die_info *
f2f0e013 22094dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22095{
22096 struct attribute *attr;
9219021c 22097
f2f0e013 22098 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22099 if (attr == NULL)
22100 return NULL;
22101
f2f0e013 22102 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22103}
22104
f9aca02d 22105static void
d97bc12b 22106dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22107{
22108 unsigned int i;
22109
d97bc12b 22110 print_spaces (indent, f);
9d8780f0 22111 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22112 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22113 sect_offset_str (die->sect_off));
d97bc12b
DE
22114
22115 if (die->parent != NULL)
22116 {
22117 print_spaces (indent, f);
9d8780f0
SM
22118 fprintf_unfiltered (f, " parent at offset: %s\n",
22119 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22120 }
22121
22122 print_spaces (indent, f);
22123 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22124 dwarf_bool_name (die->child != NULL));
c906108c 22125
d97bc12b
DE
22126 print_spaces (indent, f);
22127 fprintf_unfiltered (f, " attributes:\n");
22128
c906108c
SS
22129 for (i = 0; i < die->num_attrs; ++i)
22130 {
d97bc12b
DE
22131 print_spaces (indent, f);
22132 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22133 dwarf_attr_name (die->attrs[i].name),
22134 dwarf_form_name (die->attrs[i].form));
d97bc12b 22135
c906108c
SS
22136 switch (die->attrs[i].form)
22137 {
c906108c 22138 case DW_FORM_addr:
336d760d 22139 case DW_FORM_addrx:
3019eac3 22140 case DW_FORM_GNU_addr_index:
d97bc12b 22141 fprintf_unfiltered (f, "address: ");
5af949e3 22142 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22143 break;
22144 case DW_FORM_block2:
22145 case DW_FORM_block4:
22146 case DW_FORM_block:
22147 case DW_FORM_block1:
56eb65bd
SP
22148 fprintf_unfiltered (f, "block: size %s",
22149 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22150 break;
2dc7f7b3 22151 case DW_FORM_exprloc:
56eb65bd
SP
22152 fprintf_unfiltered (f, "expression: size %s",
22153 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22154 break;
0224619f
JK
22155 case DW_FORM_data16:
22156 fprintf_unfiltered (f, "constant of 16 bytes");
22157 break;
4568ecf9
DE
22158 case DW_FORM_ref_addr:
22159 fprintf_unfiltered (f, "ref address: ");
22160 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22161 break;
36586728
TT
22162 case DW_FORM_GNU_ref_alt:
22163 fprintf_unfiltered (f, "alt ref address: ");
22164 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22165 break;
10b3939b
DJ
22166 case DW_FORM_ref1:
22167 case DW_FORM_ref2:
22168 case DW_FORM_ref4:
4568ecf9
DE
22169 case DW_FORM_ref8:
22170 case DW_FORM_ref_udata:
d97bc12b 22171 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22172 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22173 break;
c906108c
SS
22174 case DW_FORM_data1:
22175 case DW_FORM_data2:
22176 case DW_FORM_data4:
ce5d95e1 22177 case DW_FORM_data8:
c906108c
SS
22178 case DW_FORM_udata:
22179 case DW_FORM_sdata:
43bbcdc2
PH
22180 fprintf_unfiltered (f, "constant: %s",
22181 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22182 break;
2dc7f7b3
TT
22183 case DW_FORM_sec_offset:
22184 fprintf_unfiltered (f, "section offset: %s",
22185 pulongest (DW_UNSND (&die->attrs[i])));
22186 break;
55f1336d 22187 case DW_FORM_ref_sig8:
ac9ec31b
DE
22188 fprintf_unfiltered (f, "signature: %s",
22189 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22190 break;
c906108c 22191 case DW_FORM_string:
4bdf3d34 22192 case DW_FORM_strp:
43988095 22193 case DW_FORM_line_strp:
cf532bd1 22194 case DW_FORM_strx:
3019eac3 22195 case DW_FORM_GNU_str_index:
36586728 22196 case DW_FORM_GNU_strp_alt:
8285870a 22197 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22198 DW_STRING (&die->attrs[i])
8285870a
JK
22199 ? DW_STRING (&die->attrs[i]) : "",
22200 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22201 break;
22202 case DW_FORM_flag:
22203 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22204 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22205 else
d97bc12b 22206 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22207 break;
2dc7f7b3
TT
22208 case DW_FORM_flag_present:
22209 fprintf_unfiltered (f, "flag: TRUE");
22210 break;
a8329558 22211 case DW_FORM_indirect:
0963b4bd
MS
22212 /* The reader will have reduced the indirect form to
22213 the "base form" so this form should not occur. */
5f48f8f3 22214 fprintf_unfiltered (f,
3e43a32a 22215 "unexpected attribute form: DW_FORM_indirect");
a8329558 22216 break;
663c44ac
JK
22217 case DW_FORM_implicit_const:
22218 fprintf_unfiltered (f, "constant: %s",
22219 plongest (DW_SND (&die->attrs[i])));
22220 break;
c906108c 22221 default:
d97bc12b 22222 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22223 die->attrs[i].form);
d97bc12b 22224 break;
c906108c 22225 }
d97bc12b 22226 fprintf_unfiltered (f, "\n");
c906108c
SS
22227 }
22228}
22229
f9aca02d 22230static void
d97bc12b 22231dump_die_for_error (struct die_info *die)
c906108c 22232{
d97bc12b
DE
22233 dump_die_shallow (gdb_stderr, 0, die);
22234}
22235
22236static void
22237dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22238{
22239 int indent = level * 4;
22240
22241 gdb_assert (die != NULL);
22242
22243 if (level >= max_level)
22244 return;
22245
22246 dump_die_shallow (f, indent, die);
22247
22248 if (die->child != NULL)
c906108c 22249 {
d97bc12b
DE
22250 print_spaces (indent, f);
22251 fprintf_unfiltered (f, " Children:");
22252 if (level + 1 < max_level)
22253 {
22254 fprintf_unfiltered (f, "\n");
22255 dump_die_1 (f, level + 1, max_level, die->child);
22256 }
22257 else
22258 {
3e43a32a
MS
22259 fprintf_unfiltered (f,
22260 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22261 }
22262 }
22263
22264 if (die->sibling != NULL && level > 0)
22265 {
22266 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22267 }
22268}
22269
d97bc12b
DE
22270/* This is called from the pdie macro in gdbinit.in.
22271 It's not static so gcc will keep a copy callable from gdb. */
22272
22273void
22274dump_die (struct die_info *die, int max_level)
22275{
22276 dump_die_1 (gdb_stdlog, 0, max_level, die);
22277}
22278
f9aca02d 22279static void
51545339 22280store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22281{
51545339 22282 void **slot;
c906108c 22283
9c541725
PA
22284 slot = htab_find_slot_with_hash (cu->die_hash, die,
22285 to_underlying (die->sect_off),
b64f50a1 22286 INSERT);
51545339
DJ
22287
22288 *slot = die;
c906108c
SS
22289}
22290
348e048f
DE
22291/* Follow reference or signature attribute ATTR of SRC_DIE.
22292 On entry *REF_CU is the CU of SRC_DIE.
22293 On exit *REF_CU is the CU of the result. */
22294
22295static struct die_info *
ff39bb5e 22296follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22297 struct dwarf2_cu **ref_cu)
22298{
22299 struct die_info *die;
22300
cd6c91b4 22301 if (attr->form_is_ref ())
348e048f 22302 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22303 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22304 die = follow_die_sig (src_die, attr, ref_cu);
22305 else
22306 {
22307 dump_die_for_error (src_die);
22308 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 22309 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
22310 }
22311
22312 return die;
03dd20cc
DJ
22313}
22314
5c631832 22315/* Follow reference OFFSET.
673bfd45
DE
22316 On entry *REF_CU is the CU of the source die referencing OFFSET.
22317 On exit *REF_CU is the CU of the result.
22318 Returns NULL if OFFSET is invalid. */
f504f079 22319
f9aca02d 22320static struct die_info *
9c541725 22321follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22322 struct dwarf2_cu **ref_cu)
c906108c 22323{
10b3939b 22324 struct die_info temp_die;
f2f0e013 22325 struct dwarf2_cu *target_cu, *cu = *ref_cu;
5e22e966 22326 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10b3939b 22327
348e048f
DE
22328 gdb_assert (cu->per_cu != NULL);
22329
98bfdba5
PA
22330 target_cu = cu;
22331
3019eac3 22332 if (cu->per_cu->is_debug_types)
348e048f
DE
22333 {
22334 /* .debug_types CUs cannot reference anything outside their CU.
22335 If they need to, they have to reference a signatured type via
55f1336d 22336 DW_FORM_ref_sig8. */
4057dfde 22337 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22338 return NULL;
348e048f 22339 }
36586728 22340 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22341 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22342 {
22343 struct dwarf2_per_cu_data *per_cu;
9a619af0 22344
9c541725 22345 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22346 dwarf2_per_objfile);
03dd20cc
DJ
22347
22348 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 22349 if (maybe_queue_comp_unit (cu, per_cu, dwarf2_per_objfile, cu->language))
ab432490 22350 load_full_comp_unit (per_cu, dwarf2_per_objfile, false, cu->language);
03dd20cc 22351
7188ed02 22352 target_cu = dwarf2_per_objfile->get_cu (per_cu);
10b3939b 22353 }
98bfdba5
PA
22354 else if (cu->dies == NULL)
22355 {
22356 /* We're loading full DIEs during partial symbol reading. */
5989a64e 22357 gdb_assert (dwarf2_per_objfile->per_bfd->reading_partial_symbols);
ab432490
SM
22358 load_full_comp_unit (cu->per_cu, dwarf2_per_objfile, false,
22359 language_minimal);
98bfdba5 22360 }
c906108c 22361
f2f0e013 22362 *ref_cu = target_cu;
9c541725 22363 temp_die.sect_off = sect_off;
c24bdb02
KS
22364
22365 if (target_cu != cu)
22366 target_cu->ancestor = cu;
22367
9a3c8263 22368 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22369 &temp_die,
22370 to_underlying (sect_off));
5c631832 22371}
10b3939b 22372
5c631832
JK
22373/* Follow reference attribute ATTR of SRC_DIE.
22374 On entry *REF_CU is the CU of SRC_DIE.
22375 On exit *REF_CU is the CU of the result. */
22376
22377static struct die_info *
ff39bb5e 22378follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22379 struct dwarf2_cu **ref_cu)
22380{
0826b30a 22381 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
22382 struct dwarf2_cu *cu = *ref_cu;
22383 struct die_info *die;
22384
9c541725 22385 die = follow_die_offset (sect_off,
36586728
TT
22386 (attr->form == DW_FORM_GNU_ref_alt
22387 || cu->per_cu->is_dwz),
22388 ref_cu);
5c631832 22389 if (!die)
9d8780f0
SM
22390 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22391 "at %s [in module %s]"),
22392 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 22393 objfile_name (cu->per_objfile->objfile));
348e048f 22394
5c631832
JK
22395 return die;
22396}
22397
d4c9a4f8 22398/* See read.h. */
5c631832
JK
22399
22400struct dwarf2_locexpr_baton
9c541725 22401dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 22402 dwarf2_per_cu_data *per_cu,
14095eb3 22403 dwarf2_per_objfile *dwarf2_per_objfile,
8b9737bf 22404 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22405 void *baton, bool resolve_abstract_p)
5c631832 22406{
5c631832
JK
22407 struct die_info *die;
22408 struct attribute *attr;
22409 struct dwarf2_locexpr_baton retval;
12359b5e 22410 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22411
7188ed02 22412 dwarf2_cu *cu = dwarf2_per_objfile->get_cu (per_cu);
1b555f17
SM
22413 if (cu == nullptr)
22414 cu = load_cu (per_cu, dwarf2_per_objfile, false);
22415
22416 if (cu == nullptr)
cc12ce38
DE
22417 {
22418 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22419 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22420 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22421 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22422 }
918dd910 22423
9c541725 22424 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22425 if (!die)
9d8780f0
SM
22426 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22427 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22428
22429 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22430 if (!attr && resolve_abstract_p
5989a64e
SM
22431 && (dwarf2_per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22432 != dwarf2_per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65
TV
22433 {
22434 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22435 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 22436 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 22437
3360b6e7 22438 for (const auto &cand_off
5989a64e 22439 : dwarf2_per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 22440 {
3360b6e7
TV
22441 struct dwarf2_cu *cand_cu = cu;
22442 struct die_info *cand
22443 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22444 if (!cand
22445 || !cand->parent
e4a62c65
TV
22446 || cand->parent->tag != DW_TAG_subprogram)
22447 continue;
22448
22449 CORE_ADDR pc_low, pc_high;
22450 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22451 if (pc_low == ((CORE_ADDR) -1))
22452 continue;
22453 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22454 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22455 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22456 continue;
22457
22458 die = cand;
22459 attr = dwarf2_attr (die, DW_AT_location, cu);
22460 break;
22461 }
22462 }
22463
5c631832
JK
22464 if (!attr)
22465 {
e103e986
JK
22466 /* DWARF: "If there is no such attribute, then there is no effect.".
22467 DATA is ignored if SIZE is 0. */
5c631832 22468
e103e986 22469 retval.data = NULL;
5c631832
JK
22470 retval.size = 0;
22471 }
cd6c91b4 22472 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22473 {
22474 struct dwarf2_loclist_baton loclist_baton;
22475 CORE_ADDR pc = (*get_frame_pc) (baton);
22476 size_t size;
22477
22478 fill_in_loclist_baton (cu, &loclist_baton, attr);
22479
22480 retval.data = dwarf2_find_location_expression (&loclist_baton,
22481 &size, pc);
22482 retval.size = size;
22483 }
5c631832
JK
22484 else
22485 {
4fc6c0d5 22486 if (!attr->form_is_block ())
9d8780f0 22487 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22488 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22489 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22490
22491 retval.data = DW_BLOCK (attr)->data;
22492 retval.size = DW_BLOCK (attr)->size;
22493 }
a50264ba 22494 retval.per_objfile = dwarf2_per_objfile;
5c631832 22495 retval.per_cu = cu->per_cu;
918dd910 22496
7188ed02 22497 dwarf2_per_objfile->age_comp_units ();
918dd910 22498
5c631832 22499 return retval;
348e048f
DE
22500}
22501
d4c9a4f8 22502/* See read.h. */
8b9737bf
TT
22503
22504struct dwarf2_locexpr_baton
22505dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 22506 dwarf2_per_cu_data *per_cu,
14095eb3 22507 dwarf2_per_objfile *per_objfile,
8b9737bf
TT
22508 CORE_ADDR (*get_frame_pc) (void *baton),
22509 void *baton)
22510{
9c541725 22511 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22512
14095eb3
SM
22513 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22514 get_frame_pc, baton);
8b9737bf
TT
22515}
22516
b6807d98
TT
22517/* Write a constant of a given type as target-ordered bytes into
22518 OBSTACK. */
22519
22520static const gdb_byte *
22521write_constant_as_bytes (struct obstack *obstack,
22522 enum bfd_endian byte_order,
22523 struct type *type,
22524 ULONGEST value,
22525 LONGEST *len)
22526{
22527 gdb_byte *result;
22528
22529 *len = TYPE_LENGTH (type);
224c3ddb 22530 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22531 store_unsigned_integer (result, *len, byte_order, value);
22532
22533 return result;
22534}
22535
d4c9a4f8 22536/* See read.h. */
b6807d98
TT
22537
22538const gdb_byte *
9c541725 22539dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 22540 dwarf2_per_cu_data *per_cu,
14095eb3 22541 dwarf2_per_objfile *per_objfile,
d4c9a4f8 22542 obstack *obstack,
b6807d98
TT
22543 LONGEST *len)
22544{
b6807d98
TT
22545 struct die_info *die;
22546 struct attribute *attr;
22547 const gdb_byte *result = NULL;
22548 struct type *type;
22549 LONGEST value;
22550 enum bfd_endian byte_order;
14095eb3 22551 struct objfile *objfile = per_objfile->objfile;
b6807d98 22552
7188ed02 22553 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
22554 if (cu == nullptr)
22555 cu = load_cu (per_cu, per_objfile, false);
22556
22557 if (cu == nullptr)
cc12ce38
DE
22558 {
22559 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22560 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22561 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22562 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22563 }
b6807d98 22564
9c541725 22565 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22566 if (!die)
9d8780f0
SM
22567 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22568 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22569
22570 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22571 if (attr == NULL)
22572 return NULL;
22573
e3b94546 22574 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22575 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22576
22577 switch (attr->form)
22578 {
22579 case DW_FORM_addr:
336d760d 22580 case DW_FORM_addrx:
b6807d98
TT
22581 case DW_FORM_GNU_addr_index:
22582 {
22583 gdb_byte *tem;
22584
22585 *len = cu->header.addr_size;
224c3ddb 22586 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22587 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22588 result = tem;
22589 }
22590 break;
22591 case DW_FORM_string:
22592 case DW_FORM_strp:
cf532bd1 22593 case DW_FORM_strx:
b6807d98
TT
22594 case DW_FORM_GNU_str_index:
22595 case DW_FORM_GNU_strp_alt:
22596 /* DW_STRING is already allocated on the objfile obstack, point
22597 directly to it. */
22598 result = (const gdb_byte *) DW_STRING (attr);
22599 *len = strlen (DW_STRING (attr));
22600 break;
22601 case DW_FORM_block1:
22602 case DW_FORM_block2:
22603 case DW_FORM_block4:
22604 case DW_FORM_block:
22605 case DW_FORM_exprloc:
0224619f 22606 case DW_FORM_data16:
b6807d98
TT
22607 result = DW_BLOCK (attr)->data;
22608 *len = DW_BLOCK (attr)->size;
22609 break;
22610
22611 /* The DW_AT_const_value attributes are supposed to carry the
22612 symbol's value "represented as it would be on the target
22613 architecture." By the time we get here, it's already been
22614 converted to host endianness, so we just need to sign- or
22615 zero-extend it as appropriate. */
22616 case DW_FORM_data1:
22617 type = die_type (die, cu);
22618 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22619 if (result == NULL)
22620 result = write_constant_as_bytes (obstack, byte_order,
22621 type, value, len);
22622 break;
22623 case DW_FORM_data2:
22624 type = die_type (die, cu);
22625 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22626 if (result == NULL)
22627 result = write_constant_as_bytes (obstack, byte_order,
22628 type, value, len);
22629 break;
22630 case DW_FORM_data4:
22631 type = die_type (die, cu);
22632 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22633 if (result == NULL)
22634 result = write_constant_as_bytes (obstack, byte_order,
22635 type, value, len);
22636 break;
22637 case DW_FORM_data8:
22638 type = die_type (die, cu);
22639 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22640 if (result == NULL)
22641 result = write_constant_as_bytes (obstack, byte_order,
22642 type, value, len);
22643 break;
22644
22645 case DW_FORM_sdata:
663c44ac 22646 case DW_FORM_implicit_const:
b6807d98
TT
22647 type = die_type (die, cu);
22648 result = write_constant_as_bytes (obstack, byte_order,
22649 type, DW_SND (attr), len);
22650 break;
22651
22652 case DW_FORM_udata:
22653 type = die_type (die, cu);
22654 result = write_constant_as_bytes (obstack, byte_order,
22655 type, DW_UNSND (attr), len);
22656 break;
22657
22658 default:
b98664d3 22659 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22660 dwarf_form_name (attr->form));
22661 break;
22662 }
22663
22664 return result;
22665}
22666
d4c9a4f8 22667/* See read.h. */
7942e96e
AA
22668
22669struct type *
9c541725 22670dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
22671 dwarf2_per_cu_data *per_cu,
22672 dwarf2_per_objfile *per_objfile)
7942e96e 22673{
7942e96e
AA
22674 struct die_info *die;
22675
7188ed02 22676 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
22677 if (cu == nullptr)
22678 cu = load_cu (per_cu, per_objfile, false);
22679
22680 if (cu == nullptr)
22681 return nullptr;
7942e96e 22682
9c541725 22683 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22684 if (!die)
22685 return NULL;
22686
22687 return die_type (die, cu);
22688}
22689
8cb5117c 22690/* See read.h. */
8a9b8146
TT
22691
22692struct type *
b64f50a1 22693dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
22694 dwarf2_per_cu_data *per_cu,
22695 dwarf2_per_objfile *per_objfile)
8a9b8146 22696{
9c541725 22697 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 22698 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
22699}
22700
ac9ec31b 22701/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22702 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22703 On exit *REF_CU is the CU of the result.
22704 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22705
22706static struct die_info *
ac9ec31b
DE
22707follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22708 struct dwarf2_cu **ref_cu)
348e048f 22709{
348e048f 22710 struct die_info temp_die;
c24bdb02 22711 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f 22712 struct die_info *die;
120ce1b5
SM
22713 dwarf2_per_objfile *dwarf2_per_objfile = (*ref_cu)->per_objfile;
22714
348e048f 22715
ac9ec31b
DE
22716 /* While it might be nice to assert sig_type->type == NULL here,
22717 we can get here for DW_AT_imported_declaration where we need
22718 the DIE not the type. */
348e048f
DE
22719
22720 /* If necessary, add it to the queue and load its DIEs. */
22721
120ce1b5
SM
22722 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, dwarf2_per_objfile,
22723 language_minimal))
22724 read_signatured_type (sig_type, dwarf2_per_objfile);
348e048f 22725
7188ed02 22726 sig_cu = dwarf2_per_objfile->get_cu (&sig_type->per_cu);
69d751e3 22727 gdb_assert (sig_cu != NULL);
9c541725
PA
22728 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22729 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22730 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22731 to_underlying (temp_die.sect_off));
348e048f
DE
22732 if (die)
22733 {
796a7ff8
DE
22734 /* For .gdb_index version 7 keep track of included TUs.
22735 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
5989a64e
SM
22736 if (dwarf2_per_objfile->per_bfd->index_table != NULL
22737 && dwarf2_per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 22738 {
ae640021 22739 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22740 }
22741
348e048f 22742 *ref_cu = sig_cu;
c24bdb02
KS
22743 if (sig_cu != cu)
22744 sig_cu->ancestor = cu;
22745
348e048f
DE
22746 return die;
22747 }
22748
ac9ec31b
DE
22749 return NULL;
22750}
22751
22752/* Follow signatured type referenced by ATTR in SRC_DIE.
22753 On entry *REF_CU is the CU of SRC_DIE.
22754 On exit *REF_CU is the CU of the result.
22755 The result is the DIE of the type.
22756 If the referenced type cannot be found an error is thrown. */
22757
22758static struct die_info *
ff39bb5e 22759follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22760 struct dwarf2_cu **ref_cu)
22761{
22762 ULONGEST signature = DW_SIGNATURE (attr);
22763 struct signatured_type *sig_type;
22764 struct die_info *die;
22765
22766 gdb_assert (attr->form == DW_FORM_ref_sig8);
22767
a2ce51a0 22768 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22769 /* sig_type will be NULL if the signatured type is missing from
22770 the debug info. */
22771 if (sig_type == NULL)
22772 {
22773 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22774 " from DIE at %s [in module %s]"),
22775 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22776 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22777 }
22778
22779 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22780 if (die == NULL)
22781 {
22782 dump_die_for_error (src_die);
22783 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22784 " from DIE at %s [in module %s]"),
22785 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22786 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22787 }
22788
22789 return die;
22790}
22791
22792/* Get the type specified by SIGNATURE referenced in DIE/CU,
22793 reading in and processing the type unit if necessary. */
22794
22795static struct type *
22796get_signatured_type (struct die_info *die, ULONGEST signature,
22797 struct dwarf2_cu *cu)
22798{
5e22e966 22799 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ac9ec31b
DE
22800 struct signatured_type *sig_type;
22801 struct dwarf2_cu *type_cu;
22802 struct die_info *type_die;
22803 struct type *type;
22804
a2ce51a0 22805 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22806 /* sig_type will be NULL if the signatured type is missing from
22807 the debug info. */
22808 if (sig_type == NULL)
22809 {
b98664d3 22810 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22811 " from DIE at %s [in module %s]"),
22812 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22813 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22814 return build_error_marker_type (cu, die);
22815 }
22816
22817 /* If we already know the type we're done. */
e286671b
TT
22818 type = dwarf2_per_objfile->get_type_for_signatured_type (sig_type);
22819 if (type != nullptr)
22820 return type;
ac9ec31b
DE
22821
22822 type_cu = cu;
22823 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22824 if (type_die != NULL)
22825 {
22826 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22827 is created. This is important, for example, because for c++ classes
22828 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22829 type = read_type_die (type_die, type_cu);
22830 if (type == NULL)
22831 {
b98664d3 22832 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22833 " referenced from DIE at %s [in module %s]"),
22834 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22835 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22836 type = build_error_marker_type (cu, die);
22837 }
22838 }
22839 else
22840 {
b98664d3 22841 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22842 " from DIE at %s [in module %s]"),
22843 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22844 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22845 type = build_error_marker_type (cu, die);
22846 }
e286671b
TT
22847
22848 dwarf2_per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
22849
22850 return type;
22851}
22852
22853/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22854 reading in and processing the type unit if necessary. */
22855
22856static struct type *
ff39bb5e 22857get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22858 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22859{
22860 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22861 if (attr->form_is_ref ())
ac9ec31b
DE
22862 {
22863 struct dwarf2_cu *type_cu = cu;
22864 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22865
22866 return read_type_die (type_die, type_cu);
22867 }
22868 else if (attr->form == DW_FORM_ref_sig8)
22869 {
22870 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22871 }
22872 else
22873 {
5e22e966 22874 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 22875
b98664d3 22876 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22877 " at %s [in module %s]"),
22878 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 22879 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22880 return build_error_marker_type (cu, die);
22881 }
348e048f
DE
22882}
22883
e5fe5e75 22884/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22885
22886static void
ab432490
SM
22887load_full_type_unit (dwarf2_per_cu_data *per_cu,
22888 dwarf2_per_objfile *per_objfile)
348e048f 22889{
52dc124a 22890 struct signatured_type *sig_type;
348e048f 22891
f4dc4d17 22892 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22893 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22894
6721b2ec
DE
22895 /* We have the per_cu, but we need the signatured_type.
22896 Fortunately this is an easy translation. */
22897 gdb_assert (per_cu->is_debug_types);
22898 sig_type = (struct signatured_type *) per_cu;
348e048f 22899
7188ed02 22900 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 22901
ab432490 22902 read_signatured_type (sig_type, per_objfile);
348e048f 22903
7188ed02 22904 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
348e048f
DE
22905}
22906
3019eac3
DE
22907/* Read in a signatured type and build its CU and DIEs.
22908 If the type is a stub for the real type in a DWO file,
22909 read in the real type from the DWO file as well. */
dee91e82
DE
22910
22911static void
ab432490
SM
22912read_signatured_type (signatured_type *sig_type,
22913 dwarf2_per_objfile *per_objfile)
dee91e82
DE
22914{
22915 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22916
3019eac3 22917 gdb_assert (per_cu->is_debug_types);
7188ed02 22918 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 22919
2e671100 22920 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
22921
22922 if (!reader.dummy_p)
22923 {
22924 struct dwarf2_cu *cu = reader.cu;
22925 const gdb_byte *info_ptr = reader.info_ptr;
22926
22927 gdb_assert (cu->die_hash == NULL);
22928 cu->die_hash =
22929 htab_create_alloc_ex (cu->header.length / 12,
22930 die_hash,
22931 die_eq,
22932 NULL,
22933 &cu->comp_unit_obstack,
22934 hashtab_obstack_allocate,
22935 dummy_obstack_deallocate);
22936
3e225074 22937 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22938 reader.comp_unit_die->child
22939 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22940 reader.comp_unit_die);
22941 cu->dies = reader.comp_unit_die;
22942 /* comp_unit_die is not stored in die_hash, no need. */
22943
22944 /* We try not to read any attributes in this function, because
22945 not all CUs needed for references have been loaded yet, and
22946 symbol table processing isn't initialized. But we have to
22947 set the CU language, or we won't be able to build types
22948 correctly. Similarly, if we do not read the producer, we can
22949 not apply producer-specific interpretation. */
22950 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22951
22952 reader.keep ();
c0ab21c2
TT
22953 }
22954
7ee85ab1 22955 sig_type->per_cu.tu_read = 1;
c906108c
SS
22956}
22957
c906108c
SS
22958/* Decode simple location descriptions.
22959 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
22960 the location and return the value. If COMPUTED is non-null, it is
22961 set to true to indicate that decoding was successful, and false
22962 otherwise. If COMPUTED is null, then this function may emit a
22963 complaint. */
c906108c
SS
22964
22965static CORE_ADDR
7d79de9a 22966decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 22967{
5e22e966 22968 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
22969 size_t i;
22970 size_t size = blk->size;
d521ce57 22971 const gdb_byte *data = blk->data;
21ae7a4d
JK
22972 CORE_ADDR stack[64];
22973 int stacki;
22974 unsigned int bytes_read, unsnd;
22975 gdb_byte op;
c906108c 22976
7d79de9a
TT
22977 if (computed != nullptr)
22978 *computed = false;
22979
21ae7a4d
JK
22980 i = 0;
22981 stacki = 0;
22982 stack[stacki] = 0;
22983 stack[++stacki] = 0;
22984
22985 while (i < size)
22986 {
22987 op = data[i++];
22988 switch (op)
22989 {
22990 case DW_OP_lit0:
22991 case DW_OP_lit1:
22992 case DW_OP_lit2:
22993 case DW_OP_lit3:
22994 case DW_OP_lit4:
22995 case DW_OP_lit5:
22996 case DW_OP_lit6:
22997 case DW_OP_lit7:
22998 case DW_OP_lit8:
22999 case DW_OP_lit9:
23000 case DW_OP_lit10:
23001 case DW_OP_lit11:
23002 case DW_OP_lit12:
23003 case DW_OP_lit13:
23004 case DW_OP_lit14:
23005 case DW_OP_lit15:
23006 case DW_OP_lit16:
23007 case DW_OP_lit17:
23008 case DW_OP_lit18:
23009 case DW_OP_lit19:
23010 case DW_OP_lit20:
23011 case DW_OP_lit21:
23012 case DW_OP_lit22:
23013 case DW_OP_lit23:
23014 case DW_OP_lit24:
23015 case DW_OP_lit25:
23016 case DW_OP_lit26:
23017 case DW_OP_lit27:
23018 case DW_OP_lit28:
23019 case DW_OP_lit29:
23020 case DW_OP_lit30:
23021 case DW_OP_lit31:
23022 stack[++stacki] = op - DW_OP_lit0;
23023 break;
f1bea926 23024
21ae7a4d
JK
23025 case DW_OP_reg0:
23026 case DW_OP_reg1:
23027 case DW_OP_reg2:
23028 case DW_OP_reg3:
23029 case DW_OP_reg4:
23030 case DW_OP_reg5:
23031 case DW_OP_reg6:
23032 case DW_OP_reg7:
23033 case DW_OP_reg8:
23034 case DW_OP_reg9:
23035 case DW_OP_reg10:
23036 case DW_OP_reg11:
23037 case DW_OP_reg12:
23038 case DW_OP_reg13:
23039 case DW_OP_reg14:
23040 case DW_OP_reg15:
23041 case DW_OP_reg16:
23042 case DW_OP_reg17:
23043 case DW_OP_reg18:
23044 case DW_OP_reg19:
23045 case DW_OP_reg20:
23046 case DW_OP_reg21:
23047 case DW_OP_reg22:
23048 case DW_OP_reg23:
23049 case DW_OP_reg24:
23050 case DW_OP_reg25:
23051 case DW_OP_reg26:
23052 case DW_OP_reg27:
23053 case DW_OP_reg28:
23054 case DW_OP_reg29:
23055 case DW_OP_reg30:
23056 case DW_OP_reg31:
23057 stack[++stacki] = op - DW_OP_reg0;
23058 if (i < size)
7d79de9a
TT
23059 {
23060 if (computed == nullptr)
23061 dwarf2_complex_location_expr_complaint ();
23062 else
23063 return 0;
23064 }
21ae7a4d 23065 break;
c906108c 23066
21ae7a4d
JK
23067 case DW_OP_regx:
23068 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23069 i += bytes_read;
23070 stack[++stacki] = unsnd;
23071 if (i < size)
7d79de9a
TT
23072 {
23073 if (computed == nullptr)
23074 dwarf2_complex_location_expr_complaint ();
23075 else
23076 return 0;
23077 }
21ae7a4d 23078 break;
c906108c 23079
21ae7a4d 23080 case DW_OP_addr:
c8a7a66f
TT
23081 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23082 &bytes_read);
21ae7a4d
JK
23083 i += bytes_read;
23084 break;
d53d4ac5 23085
21ae7a4d
JK
23086 case DW_OP_const1u:
23087 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23088 i += 1;
23089 break;
23090
23091 case DW_OP_const1s:
23092 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23093 i += 1;
23094 break;
23095
23096 case DW_OP_const2u:
23097 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23098 i += 2;
23099 break;
23100
23101 case DW_OP_const2s:
23102 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23103 i += 2;
23104 break;
d53d4ac5 23105
21ae7a4d
JK
23106 case DW_OP_const4u:
23107 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23108 i += 4;
23109 break;
23110
23111 case DW_OP_const4s:
23112 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23113 i += 4;
23114 break;
23115
585861ea
JK
23116 case DW_OP_const8u:
23117 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23118 i += 8;
23119 break;
23120
21ae7a4d
JK
23121 case DW_OP_constu:
23122 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23123 &bytes_read);
23124 i += bytes_read;
23125 break;
23126
23127 case DW_OP_consts:
23128 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23129 i += bytes_read;
23130 break;
23131
23132 case DW_OP_dup:
23133 stack[stacki + 1] = stack[stacki];
23134 stacki++;
23135 break;
23136
23137 case DW_OP_plus:
23138 stack[stacki - 1] += stack[stacki];
23139 stacki--;
23140 break;
23141
23142 case DW_OP_plus_uconst:
23143 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23144 &bytes_read);
23145 i += bytes_read;
23146 break;
23147
23148 case DW_OP_minus:
23149 stack[stacki - 1] -= stack[stacki];
23150 stacki--;
23151 break;
23152
23153 case DW_OP_deref:
23154 /* If we're not the last op, then we definitely can't encode
23155 this using GDB's address_class enum. This is valid for partial
23156 global symbols, although the variable's address will be bogus
23157 in the psymtab. */
23158 if (i < size)
7d79de9a
TT
23159 {
23160 if (computed == nullptr)
23161 dwarf2_complex_location_expr_complaint ();
23162 else
23163 return 0;
23164 }
21ae7a4d
JK
23165 break;
23166
23167 case DW_OP_GNU_push_tls_address:
4aa4e28b 23168 case DW_OP_form_tls_address:
21ae7a4d
JK
23169 /* The top of the stack has the offset from the beginning
23170 of the thread control block at which the variable is located. */
23171 /* Nothing should follow this operator, so the top of stack would
23172 be returned. */
23173 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23174 address will be bogus in the psymtab. Make it always at least
23175 non-zero to not look as a variable garbage collected by linker
23176 which have DW_OP_addr 0. */
21ae7a4d 23177 if (i < size)
7d79de9a
TT
23178 {
23179 if (computed == nullptr)
23180 dwarf2_complex_location_expr_complaint ();
23181 else
23182 return 0;
23183 }
585861ea 23184 stack[stacki]++;
21ae7a4d
JK
23185 break;
23186
23187 case DW_OP_GNU_uninit:
7d79de9a
TT
23188 if (computed != nullptr)
23189 return 0;
21ae7a4d
JK
23190 break;
23191
336d760d 23192 case DW_OP_addrx:
3019eac3 23193 case DW_OP_GNU_addr_index:
49f6c839 23194 case DW_OP_GNU_const_index:
3019eac3
DE
23195 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23196 &bytes_read);
23197 i += bytes_read;
23198 break;
23199
21ae7a4d 23200 default:
7d79de9a
TT
23201 if (computed == nullptr)
23202 {
23203 const char *name = get_DW_OP_name (op);
21ae7a4d 23204
7d79de9a
TT
23205 if (name)
23206 complaint (_("unsupported stack op: '%s'"),
23207 name);
23208 else
23209 complaint (_("unsupported stack op: '%02x'"),
23210 op);
23211 }
21ae7a4d
JK
23212
23213 return (stack[stacki]);
d53d4ac5 23214 }
3c6e0cb3 23215
21ae7a4d
JK
23216 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23217 outside of the allocated space. Also enforce minimum>0. */
23218 if (stacki >= ARRAY_SIZE (stack) - 1)
23219 {
7d79de9a
TT
23220 if (computed == nullptr)
23221 complaint (_("location description stack overflow"));
21ae7a4d
JK
23222 return 0;
23223 }
23224
23225 if (stacki <= 0)
23226 {
7d79de9a
TT
23227 if (computed == nullptr)
23228 complaint (_("location description stack underflow"));
21ae7a4d
JK
23229 return 0;
23230 }
23231 }
7d79de9a
TT
23232
23233 if (computed != nullptr)
23234 *computed = true;
21ae7a4d 23235 return (stack[stacki]);
c906108c
SS
23236}
23237
23238/* memory allocation interface */
23239
c906108c 23240static struct dwarf_block *
7b5a2f43 23241dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23242{
8d749320 23243 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23244}
23245
c906108c 23246static struct die_info *
b60c80d6 23247dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23248{
23249 struct die_info *die;
b60c80d6
DJ
23250 size_t size = sizeof (struct die_info);
23251
23252 if (num_attrs > 1)
23253 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23254
b60c80d6 23255 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23256 memset (die, 0, sizeof (struct die_info));
23257 return (die);
23258}
2e276125
JB
23259
23260\f
a036ba48 23261
c90ec28a 23262/* Macro support. */
cf2c3c16 23263
9eac9650
TT
23264/* An overload of dwarf_decode_macros that finds the correct section
23265 and ensures it is read in before calling the other overload. */
23266
23267static void
23268dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23269 int section_is_gnu)
23270{
5e22e966 23271 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
9eac9650 23272 struct objfile *objfile = dwarf2_per_objfile->objfile;
5a0e026f 23273 const struct line_header *lh = cu->line_header;
9eac9650
TT
23274 unsigned int offset_size = cu->header.offset_size;
23275 struct dwarf2_section_info *section;
23276 const char *section_name;
23277
23278 if (cu->dwo_unit != nullptr)
23279 {
23280 if (section_is_gnu)
23281 {
23282 section = &cu->dwo_unit->dwo_file->sections.macro;
23283 section_name = ".debug_macro.dwo";
23284 }
23285 else
23286 {
23287 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23288 section_name = ".debug_macinfo.dwo";
23289 }
23290 }
23291 else
23292 {
23293 if (section_is_gnu)
23294 {
5989a64e 23295 section = &dwarf2_per_objfile->per_bfd->macro;
9eac9650
TT
23296 section_name = ".debug_macro";
23297 }
23298 else
23299 {
5989a64e 23300 section = &dwarf2_per_objfile->per_bfd->macinfo;
9eac9650
TT
23301 section_name = ".debug_macinfo";
23302 }
23303 }
23304
23305 section->read (objfile);
23306 if (section->buffer == nullptr)
23307 {
23308 complaint (_("missing %s section"), section_name);
23309 return;
23310 }
23311
23312 buildsym_compunit *builder = cu->get_builder ();
23313
23314 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23315 offset_size, offset, section_is_gnu);
23316}
23317
3019eac3
DE
23318/* Return the .debug_loc section to use for CU.
23319 For DWO files use .debug_loc.dwo. */
23320
23321static struct dwarf2_section_info *
23322cu_debug_loc_section (struct dwarf2_cu *cu)
23323{
5e22e966 23324 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 23325
3019eac3 23326 if (cu->dwo_unit)
43988095
JK
23327 {
23328 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23329
43988095
JK
23330 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23331 }
5989a64e
SM
23332 return (cu->header.version >= 5 ? &dwarf2_per_objfile->per_bfd->loclists
23333 : &dwarf2_per_objfile->per_bfd->loc);
3019eac3
DE
23334}
23335
8cf6f0b1
TT
23336/* A helper function that fills in a dwarf2_loclist_baton. */
23337
23338static void
23339fill_in_loclist_baton (struct dwarf2_cu *cu,
23340 struct dwarf2_loclist_baton *baton,
ff39bb5e 23341 const struct attribute *attr)
8cf6f0b1 23342{
5e22e966 23343 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3
DE
23344 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23345
96b79293 23346 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1 23347
a50264ba 23348 baton->per_objfile = dwarf2_per_objfile;
8cf6f0b1
TT
23349 baton->per_cu = cu->per_cu;
23350 gdb_assert (baton->per_cu);
23351 /* We don't know how long the location list is, but make sure we
23352 don't run off the edge of the section. */
3019eac3
DE
23353 baton->size = section->size - DW_UNSND (attr);
23354 baton->data = section->buffer + DW_UNSND (attr);
2b24b6e4
TT
23355 if (cu->base_address.has_value ())
23356 baton->base_address = *cu->base_address;
23357 else
23358 baton->base_address = 0;
f664829e 23359 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23360}
23361
4c2df51b 23362static void
ff39bb5e 23363dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23364 struct dwarf2_cu *cu, int is_block)
4c2df51b 23365{
5e22e966 23366 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
bb5ed363 23367 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 23368 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23369
cd6c91b4 23370 if (attr->form_is_section_offset ()
3019eac3 23371 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23372 the section. If so, fall through to the complaint in the
23373 other branch. */
2c7d5afc 23374 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 23375 {
0d53c4c4 23376 struct dwarf2_loclist_baton *baton;
4c2df51b 23377
8d749320 23378 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23379
8cf6f0b1 23380 fill_in_loclist_baton (cu, baton, attr);
be391dca 23381
2b24b6e4 23382 if (!cu->base_address.has_value ())
b98664d3 23383 complaint (_("Location list used without "
3e43a32a 23384 "specifying the CU base address."));
4c2df51b 23385
f1e6e072
TT
23386 SYMBOL_ACLASS_INDEX (sym) = (is_block
23387 ? dwarf2_loclist_block_index
23388 : dwarf2_loclist_index);
0d53c4c4
DJ
23389 SYMBOL_LOCATION_BATON (sym) = baton;
23390 }
23391 else
23392 {
23393 struct dwarf2_locexpr_baton *baton;
23394
8d749320 23395 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 23396 baton->per_objfile = dwarf2_per_objfile;
ae0d2f24
UW
23397 baton->per_cu = cu->per_cu;
23398 gdb_assert (baton->per_cu);
0d53c4c4 23399
4fc6c0d5 23400 if (attr->form_is_block ())
0d53c4c4
DJ
23401 {
23402 /* Note that we're just copying the block's data pointer
23403 here, not the actual data. We're still pointing into the
6502dd73
DJ
23404 info_buffer for SYM's objfile; right now we never release
23405 that buffer, but when we do clean up properly this may
23406 need to change. */
0d53c4c4
DJ
23407 baton->size = DW_BLOCK (attr)->size;
23408 baton->data = DW_BLOCK (attr)->data;
23409 }
23410 else
23411 {
23412 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 23413 sym->natural_name ());
0d53c4c4 23414 baton->size = 0;
0d53c4c4 23415 }
6e70227d 23416
f1e6e072
TT
23417 SYMBOL_ACLASS_INDEX (sym) = (is_block
23418 ? dwarf2_locexpr_block_index
23419 : dwarf2_locexpr_index);
0d53c4c4
DJ
23420 SYMBOL_LOCATION_BATON (sym) = baton;
23421 }
4c2df51b 23422}
6502dd73 23423
2e6a9f79 23424/* See read.h. */
96408a79 23425
2e6a9f79
SM
23426const comp_unit_head *
23427dwarf2_per_cu_data::get_header () const
96408a79 23428{
2e6a9f79
SM
23429 if (!m_header_read_in)
23430 {
23431 const gdb_byte *info_ptr
23432 = this->section->buffer + to_underlying (this->sect_off);
96408a79 23433
2e6a9f79 23434 memset (&m_header, 0, sizeof (m_header));
96408a79 23435
2e6a9f79
SM
23436 read_comp_unit_head (&m_header, info_ptr, this->section,
23437 rcuh_kind::COMPILE);
23438 }
96408a79 23439
2e6a9f79 23440 return &m_header;
96408a79
SA
23441}
23442
09ba997f 23443/* See read.h. */
ae0d2f24 23444
98714339 23445int
09ba997f 23446dwarf2_per_cu_data::addr_size () const
ae0d2f24 23447{
2e6a9f79 23448 return this->get_header ()->addr_size;
ae0d2f24
UW
23449}
23450
09ba997f 23451/* See read.h. */
9eae7c52
TT
23452
23453int
09ba997f 23454dwarf2_per_cu_data::offset_size () const
9eae7c52 23455{
2e6a9f79 23456 return this->get_header ()->offset_size;
96408a79
SA
23457}
23458
09ba997f 23459/* See read.h. */
96408a79
SA
23460
23461int
09ba997f 23462dwarf2_per_cu_data::ref_addr_size () const
96408a79 23463{
2e6a9f79 23464 const comp_unit_head *header = this->get_header ();
96408a79 23465
2e6a9f79
SM
23466 if (header->version == 2)
23467 return header->addr_size;
96408a79 23468 else
2e6a9f79 23469 return header->offset_size;
181cebd4
JK
23470}
23471
09ba997f 23472/* See read.h. */
9aa1f1e3 23473
09ba997f 23474struct type *
293e7e51 23475dwarf2_cu::addr_type () const
9a49df9d 23476{
293e7e51 23477 struct objfile *objfile = this->per_objfile->objfile;
9a49df9d
AB
23478 struct type *void_type = objfile_type (objfile)->builtin_void;
23479 struct type *addr_type = lookup_pointer_type (void_type);
293e7e51 23480 int addr_size = this->per_cu->addr_size ();
9a49df9d
AB
23481
23482 if (TYPE_LENGTH (addr_type) == addr_size)
23483 return addr_type;
23484
09ba997f 23485 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
23486 return addr_type;
23487}
23488
22b6cd70
TT
23489/* A helper function for dwarf2_find_containing_comp_unit that returns
23490 the index of the result, and that searches a vector. It will
23491 return a result even if the offset in question does not actually
23492 occur in any CU. This is separate so that it can be unit
23493 tested. */
ae038cb0 23494
22b6cd70
TT
23495static int
23496dwarf2_find_containing_comp_unit
23497 (sect_offset sect_off,
23498 unsigned int offset_in_dwz,
23499 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 23500{
ae038cb0
DJ
23501 int low, high;
23502
ae038cb0 23503 low = 0;
22b6cd70 23504 high = all_comp_units.size () - 1;
ae038cb0
DJ
23505 while (high > low)
23506 {
36586728 23507 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 23508 int mid = low + (high - low) / 2;
9a619af0 23509
22b6cd70 23510 mid_cu = all_comp_units[mid];
36586728 23511 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 23512 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 23513 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
23514 high = mid;
23515 else
23516 low = mid + 1;
23517 }
23518 gdb_assert (low == high);
22b6cd70
TT
23519 return low;
23520}
23521
23522/* Locate the .debug_info compilation unit from CU's objfile which contains
23523 the DIE at OFFSET. Raises an error on failure. */
23524
23525static struct dwarf2_per_cu_data *
23526dwarf2_find_containing_comp_unit (sect_offset sect_off,
23527 unsigned int offset_in_dwz,
23528 struct dwarf2_per_objfile *dwarf2_per_objfile)
23529{
23530 int low
23531 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
5989a64e 23532 dwarf2_per_objfile->per_bfd->all_comp_units);
22b6cd70 23533 struct dwarf2_per_cu_data *this_cu
5989a64e 23534 = dwarf2_per_objfile->per_bfd->all_comp_units[low];
22b6cd70 23535
45b8ae0c 23536 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 23537 {
36586728 23538 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 23539 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
23540 "offset %s [in module %s]"),
23541 sect_offset_str (sect_off),
ed2dc618 23542 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 23543
5989a64e 23544 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 23545 <= sect_off);
5989a64e 23546 return dwarf2_per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
23547 }
23548 else
23549 {
5989a64e 23550 if (low == dwarf2_per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 23551 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 23552 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 23553 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
23554 return this_cu;
23555 }
23556}
23557
22b6cd70
TT
23558#if GDB_SELF_TEST
23559
23560namespace selftests {
23561namespace find_containing_comp_unit {
23562
23563static void
23564run_test ()
23565{
23566 struct dwarf2_per_cu_data one {};
23567 struct dwarf2_per_cu_data two {};
23568 struct dwarf2_per_cu_data three {};
23569 struct dwarf2_per_cu_data four {};
23570
23571 one.length = 5;
23572 two.sect_off = sect_offset (one.length);
23573 two.length = 7;
23574
23575 three.length = 5;
23576 three.is_dwz = 1;
23577 four.sect_off = sect_offset (three.length);
23578 four.length = 7;
23579 four.is_dwz = 1;
23580
23581 std::vector<dwarf2_per_cu_data *> units;
23582 units.push_back (&one);
23583 units.push_back (&two);
23584 units.push_back (&three);
23585 units.push_back (&four);
23586
23587 int result;
23588
23589 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23590 SELF_CHECK (units[result] == &one);
23591 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23592 SELF_CHECK (units[result] == &one);
23593 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23594 SELF_CHECK (units[result] == &two);
23595
23596 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23597 SELF_CHECK (units[result] == &three);
23598 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23599 SELF_CHECK (units[result] == &three);
23600 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23601 SELF_CHECK (units[result] == &four);
23602}
23603
23604}
23605}
23606
23607#endif /* GDB_SELF_TEST */
23608
9e021579 23609/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 23610
9e021579
SM
23611dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23612 dwarf2_per_objfile *per_objfile)
23613 : per_cu (per_cu),
23614 per_objfile (per_objfile),
9068261f
AB
23615 mark (false),
23616 has_loclist (false),
23617 checked_producer (false),
23618 producer_is_gxx_lt_4_6 (false),
23619 producer_is_gcc_lt_4_3 (false),
eb77c9df 23620 producer_is_icc (false),
9068261f 23621 producer_is_icc_lt_14 (false),
c258c396 23622 producer_is_codewarrior (false),
9068261f 23623 processing_has_namespace_info (false)
93311388 23624{
9816fde3
JK
23625}
23626
23627/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23628
23629static void
95554aad
TT
23630prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23631 enum language pretend_language)
9816fde3
JK
23632{
23633 struct attribute *attr;
23634
23635 /* Set the language we're debugging. */
23636 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 23637 if (attr != nullptr)
9816fde3
JK
23638 set_cu_language (DW_UNSND (attr), cu);
23639 else
9cded63f 23640 {
95554aad 23641 cu->language = pretend_language;
9cded63f
TT
23642 cu->language_defn = language_def (cu->language);
23643 }
dee91e82 23644
7d45c7c3 23645 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
23646}
23647
7188ed02 23648/* See read.h. */
ae038cb0 23649
7188ed02
SM
23650dwarf2_cu *
23651dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 23652{
7188ed02
SM
23653 auto it = m_dwarf2_cus.find (per_cu);
23654 if (it == m_dwarf2_cus.end ())
23655 return nullptr;
ae038cb0 23656
7188ed02
SM
23657 return it->second;
23658}
23659
23660/* See read.h. */
23661
23662void
23663dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
23664{
23665 gdb_assert (this->get_cu (per_cu) == nullptr);
23666
23667 m_dwarf2_cus[per_cu] = cu;
23668}
23669
23670/* See read.h. */
23671
23672void
23673dwarf2_per_objfile::age_comp_units ()
23674{
23675 /* Start by clearing all marks. */
23676 for (auto pair : m_dwarf2_cus)
23677 pair.second->mark = false;
23678
23679 /* Traverse all CUs, mark them and their dependencies if used recently
23680 enough. */
23681 for (auto pair : m_dwarf2_cus)
ae038cb0 23682 {
7188ed02
SM
23683 dwarf2_cu *cu = pair.second;
23684
23685 cu->last_used++;
23686 if (cu->last_used <= dwarf_max_cache_age)
23687 dwarf2_mark (cu);
ae038cb0
DJ
23688 }
23689
7188ed02
SM
23690 /* Delete all CUs still not marked. */
23691 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
ae038cb0 23692 {
7188ed02 23693 dwarf2_cu *cu = it->second;
ae038cb0 23694
7188ed02 23695 if (!cu->mark)
ae038cb0 23696 {
7188ed02
SM
23697 delete cu;
23698 it = m_dwarf2_cus.erase (it);
ae038cb0
DJ
23699 }
23700 else
7188ed02 23701 it++;
ae038cb0
DJ
23702 }
23703}
23704
7188ed02 23705/* See read.h. */
ae038cb0 23706
7188ed02
SM
23707void
23708dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 23709{
7188ed02
SM
23710 auto it = m_dwarf2_cus.find (per_cu);
23711 if (it == m_dwarf2_cus.end ())
23712 return;
ae038cb0 23713
7188ed02 23714 delete it->second;
ae038cb0 23715
7188ed02
SM
23716 m_dwarf2_cus.erase (it);
23717}
ae038cb0 23718
7188ed02
SM
23719dwarf2_per_objfile::~dwarf2_per_objfile ()
23720{
23721 remove_all_cus ();
ae038cb0
DJ
23722}
23723
dee91e82
DE
23724/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23725 We store these in a hash table separate from the DIEs, and preserve them
23726 when the DIEs are flushed out of cache.
23727
23728 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23729 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23730 or the type may come from a DWO file. Furthermore, while it's more logical
23731 to use per_cu->section+offset, with Fission the section with the data is in
23732 the DWO file but we don't know that section at the point we need it.
23733 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23734 because we can enter the lookup routine, get_die_type_at_offset, from
23735 outside this file, and thus won't necessarily have PER_CU->cu.
23736 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23737
dee91e82 23738struct dwarf2_per_cu_offset_and_type
1c379e20 23739{
dee91e82 23740 const struct dwarf2_per_cu_data *per_cu;
9c541725 23741 sect_offset sect_off;
1c379e20
DJ
23742 struct type *type;
23743};
23744
dee91e82 23745/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23746
23747static hashval_t
dee91e82 23748per_cu_offset_and_type_hash (const void *item)
1c379e20 23749{
9a3c8263
SM
23750 const struct dwarf2_per_cu_offset_and_type *ofs
23751 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23752
9c541725 23753 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23754}
23755
dee91e82 23756/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23757
23758static int
dee91e82 23759per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23760{
9a3c8263
SM
23761 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23762 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23763 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23764 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23765
dee91e82 23766 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23767 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23768}
23769
23770/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23771 table if necessary. For convenience, return TYPE.
23772
23773 The DIEs reading must have careful ordering to:
85102364 23774 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
23775 reading current DIE.
23776 * Not trying to dereference contents of still incompletely read in types
23777 while reading in other DIEs.
23778 * Enable referencing still incompletely read in types just by a pointer to
23779 the type without accessing its fields.
23780
23781 Therefore caller should follow these rules:
23782 * Try to fetch any prerequisite types we may need to build this DIE type
23783 before building the type and calling set_die_type.
e71ec853 23784 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23785 possible before fetching more types to complete the current type.
23786 * Make the type as complete as possible before fetching more types. */
1c379e20 23787
f792889a 23788static struct type *
1c379e20
DJ
23789set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23790{
5e22e966 23791 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
dee91e82 23792 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 23793 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
23794 struct attribute *attr;
23795 struct dynamic_prop prop;
1c379e20 23796
b4ba55a1
JB
23797 /* For Ada types, make sure that the gnat-specific data is always
23798 initialized (if not already set). There are a few types where
23799 we should not be doing so, because the type-specific area is
23800 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23801 where the type-specific area is used to store the floatformat).
23802 But this is not a problem, because the gnat-specific information
23803 is actually not needed for these types. */
23804 if (need_gnat_info (cu)
78134374
SM
23805 && type->code () != TYPE_CODE_FUNC
23806 && type->code () != TYPE_CODE_FLT
23807 && type->code () != TYPE_CODE_METHODPTR
23808 && type->code () != TYPE_CODE_MEMBERPTR
23809 && type->code () != TYPE_CODE_METHOD
b4ba55a1
JB
23810 && !HAVE_GNAT_AUX_INFO (type))
23811 INIT_GNAT_SPECIFIC (type);
23812
3f2f83dd
KB
23813 /* Read DW_AT_allocated and set in type. */
23814 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 23815 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23816 {
293e7e51 23817 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 23818 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23819 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd
KB
23820 }
23821 else if (attr != NULL)
23822 {
b98664d3 23823 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 23824 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23825 sect_offset_str (die->sect_off));
3f2f83dd
KB
23826 }
23827
23828 /* Read DW_AT_associated and set in type. */
23829 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 23830 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23831 {
293e7e51 23832 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 23833 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23834 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd
KB
23835 }
23836 else if (attr != NULL)
23837 {
b98664d3 23838 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 23839 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23840 sect_offset_str (die->sect_off));
3f2f83dd
KB
23841 }
23842
3cdcd0ce
JB
23843 /* Read DW_AT_data_location and set in type. */
23844 attr = dwarf2_attr (die, DW_AT_data_location, cu);
293e7e51 23845 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 23846 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
3cdcd0ce 23847
ae090bdb
SM
23848 if (dwarf2_per_objfile->die_type_hash == NULL)
23849 dwarf2_per_objfile->die_type_hash
0335378b
TT
23850 = htab_up (htab_create_alloc (127,
23851 per_cu_offset_and_type_hash,
23852 per_cu_offset_and_type_eq,
23853 NULL, xcalloc, xfree));
1c379e20 23854
dee91e82 23855 ofs.per_cu = cu->per_cu;
9c541725 23856 ofs.sect_off = die->sect_off;
1c379e20 23857 ofs.type = type;
dee91e82 23858 slot = (struct dwarf2_per_cu_offset_and_type **)
ae090bdb 23859 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 23860 if (*slot)
b98664d3 23861 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 23862 sect_offset_str (die->sect_off));
8d749320
SM
23863 *slot = XOBNEW (&objfile->objfile_obstack,
23864 struct dwarf2_per_cu_offset_and_type);
1c379e20 23865 **slot = ofs;
f792889a 23866 return type;
1c379e20
DJ
23867}
23868
9c541725 23869/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23870 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23871
23872static struct type *
9c541725 23873get_die_type_at_offset (sect_offset sect_off,
aa66c379
SM
23874 dwarf2_per_cu_data *per_cu,
23875 dwarf2_per_objfile *dwarf2_per_objfile)
1c379e20 23876{
dee91e82 23877 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 23878
ae090bdb 23879 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23880 return NULL;
1c379e20 23881
dee91e82 23882 ofs.per_cu = per_cu;
9c541725 23883 ofs.sect_off = sect_off;
9a3c8263 23884 slot = ((struct dwarf2_per_cu_offset_and_type *)
ae090bdb 23885 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
23886 if (slot)
23887 return slot->type;
23888 else
23889 return NULL;
23890}
23891
02142a6c 23892/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23893 or return NULL if DIE does not have a saved type. */
23894
23895static struct type *
23896get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23897{
aa66c379 23898 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
23899}
23900
10b3939b
DJ
23901/* Add a dependence relationship from CU to REF_PER_CU. */
23902
23903static void
23904dwarf2_add_dependence (struct dwarf2_cu *cu,
23905 struct dwarf2_per_cu_data *ref_per_cu)
23906{
23907 void **slot;
23908
23909 if (cu->dependencies == NULL)
23910 cu->dependencies
23911 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23912 NULL, &cu->comp_unit_obstack,
23913 hashtab_obstack_allocate,
23914 dummy_obstack_deallocate);
23915
23916 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23917 if (*slot == NULL)
23918 *slot = ref_per_cu;
23919}
1c379e20 23920
f504f079
DE
23921/* Subroutine of dwarf2_mark to pass to htab_traverse.
23922 Set the mark field in every compilation unit in the
7188ed02
SM
23923 cache that we must keep because we are keeping CU.
23924
23925 DATA is the dwarf2_per_objfile object in which to look up CUs. */
ae038cb0 23926
10b3939b
DJ
23927static int
23928dwarf2_mark_helper (void **slot, void *data)
23929{
7188ed02
SM
23930 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
23931 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
23932 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
d07ed419
JK
23933
23934 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23935 reading of the chain. As such dependencies remain valid it is not much
23936 useful to track and undo them during QUIT cleanups. */
7188ed02 23937 if (cu == nullptr)
d07ed419
JK
23938 return 1;
23939
7188ed02 23940 if (cu->mark)
10b3939b 23941 return 1;
10b3939b 23942
7188ed02
SM
23943 cu->mark = true;
23944
23945 if (cu->dependencies != nullptr)
23946 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
10b3939b
DJ
23947
23948 return 1;
23949}
23950
f504f079
DE
23951/* Set the mark field in CU and in every other compilation unit in the
23952 cache that we must keep because we are keeping CU. */
23953
ae038cb0
DJ
23954static void
23955dwarf2_mark (struct dwarf2_cu *cu)
23956{
23957 if (cu->mark)
23958 return;
7188ed02 23959
9068261f 23960 cu->mark = true;
ae038cb0 23961
7188ed02
SM
23962 if (cu->dependencies != nullptr)
23963 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
72bf9492
DJ
23964}
23965
72bf9492
DJ
23966/* Trivial hash function for partial_die_info: the hash value of a DIE
23967 is its offset in .debug_info for this objfile. */
23968
23969static hashval_t
23970partial_die_hash (const void *item)
23971{
9a3c8263
SM
23972 const struct partial_die_info *part_die
23973 = (const struct partial_die_info *) item;
9a619af0 23974
9c541725 23975 return to_underlying (part_die->sect_off);
72bf9492
DJ
23976}
23977
23978/* Trivial comparison function for partial_die_info structures: two DIEs
23979 are equal if they have the same offset. */
23980
23981static int
23982partial_die_eq (const void *item_lhs, const void *item_rhs)
23983{
9a3c8263
SM
23984 const struct partial_die_info *part_die_lhs
23985 = (const struct partial_die_info *) item_lhs;
23986 const struct partial_die_info *part_die_rhs
23987 = (const struct partial_die_info *) item_rhs;
9a619af0 23988
9c541725 23989 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23990}
23991
3c3bb058
AB
23992struct cmd_list_element *set_dwarf_cmdlist;
23993struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 23994
9291a0cd 23995static void
cd4fb1b2
SM
23996show_check_physname (struct ui_file *file, int from_tty,
23997 struct cmd_list_element *c, const char *value)
9291a0cd 23998{
cd4fb1b2
SM
23999 fprintf_filtered (file,
24000 _("Whether to check \"physname\" is %s.\n"),
24001 value);
9291a0cd
TT
24002}
24003
6c265988 24004void _initialize_dwarf2_read ();
cd4fb1b2 24005void
6c265988 24006_initialize_dwarf2_read ()
9291a0cd 24007{
0743fc83 24008 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 24009Set DWARF specific variables.\n\
590042fc 24010Configure DWARF variables such as the cache size."),
0743fc83
TT
24011 &set_dwarf_cmdlist, "maintenance set dwarf ",
24012 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 24013
0743fc83 24014 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
24015Show DWARF specific variables.\n\
24016Show DWARF variables such as the cache size."),
0743fc83
TT
24017 &show_dwarf_cmdlist, "maintenance show dwarf ",
24018 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 24019
cd4fb1b2
SM
24020 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24021 &dwarf_max_cache_age, _("\
24022Set the upper bound on the age of cached DWARF compilation units."), _("\
24023Show the upper bound on the age of cached DWARF compilation units."), _("\
24024A higher limit means that cached compilation units will be stored\n\
24025in memory longer, and more total memory will be used. Zero disables\n\
24026caching, which can slow down startup."),
24027 NULL,
24028 show_dwarf_max_cache_age,
24029 &set_dwarf_cmdlist,
24030 &show_dwarf_cmdlist);
156942c7 24031
cd4fb1b2
SM
24032 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24033Set debugging of the DWARF reader."), _("\
24034Show debugging of the DWARF reader."), _("\
24035When enabled (non-zero), debugging messages are printed during DWARF\n\
24036reading and symtab expansion. A value of 1 (one) provides basic\n\
24037information. A value greater than 1 provides more verbose information."),
24038 NULL,
24039 NULL,
24040 &setdebuglist, &showdebuglist);
9291a0cd 24041
cd4fb1b2
SM
24042 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24043Set debugging of the DWARF DIE reader."), _("\
24044Show debugging of the DWARF DIE reader."), _("\
24045When enabled (non-zero), DIEs are dumped after they are read in.\n\
24046The value is the maximum depth to print."),
24047 NULL,
24048 NULL,
24049 &setdebuglist, &showdebuglist);
9291a0cd 24050
cd4fb1b2
SM
24051 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24052Set debugging of the dwarf line reader."), _("\
24053Show debugging of the dwarf line reader."), _("\
24054When enabled (non-zero), line number entries are dumped as they are read in.\n\
24055A value of 1 (one) provides basic information.\n\
24056A value greater than 1 provides more verbose information."),
24057 NULL,
24058 NULL,
24059 &setdebuglist, &showdebuglist);
437afbb8 24060
cd4fb1b2
SM
24061 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24062Set cross-checking of \"physname\" code against demangler."), _("\
24063Show cross-checking of \"physname\" code against demangler."), _("\
24064When enabled, GDB's internal \"physname\" code is checked against\n\
24065the demangler."),
24066 NULL, show_check_physname,
24067 &setdebuglist, &showdebuglist);
900e11f9 24068
e615022a
DE
24069 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24070 no_class, &use_deprecated_index_sections, _("\
24071Set whether to use deprecated gdb_index sections."), _("\
24072Show whether to use deprecated gdb_index sections."), _("\
24073When enabled, deprecated .gdb_index sections are used anyway.\n\
24074Normally they are ignored either because of a missing feature or\n\
24075performance issue.\n\
24076Warning: This option must be enabled before gdb reads the file."),
24077 NULL,
24078 NULL,
24079 &setlist, &showlist);
24080
f1e6e072
TT
24081 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24082 &dwarf2_locexpr_funcs);
24083 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24084 &dwarf2_loclist_funcs);
24085
24086 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24087 &dwarf2_block_frame_base_locexpr_funcs);
24088 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24089 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24090
24091#if GDB_SELF_TEST
24092 selftests::register_test ("dw2_expand_symtabs_matching",
24093 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
24094 selftests::register_test ("dwarf2_find_containing_comp_unit",
24095 selftests::find_containing_comp_unit::run_test);
c62446b1 24096#endif
6502dd73 24097}
This page took 5.57314 seconds and 4 git commands to generate.