gdb: Convert language la_compute_program field to a method
[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 485 /* The dwarf2_per_objfile that owns this. */
976ca316 486 dwarf2_per_objfile *per_objfile;
9e021579 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
7d00ffec
TT
974 /* Compute the name of this partial DIE. This memoizes the
975 result, so it is safe to call multiple times. */
976 const char *name (dwarf2_cu *cu);
977
72bf9492 978 /* Offset of this DIE. */
6f06d47b 979 const sect_offset sect_off;
72bf9492
DJ
980
981 /* DWARF-2 tag for this DIE. */
6f06d47b 982 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 983
72bf9492 984 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
985 const unsigned int has_children : 1;
986
72bf9492
DJ
987 unsigned int is_external : 1;
988 unsigned int is_declaration : 1;
989 unsigned int has_type : 1;
990 unsigned int has_specification : 1;
991 unsigned int has_pc_info : 1;
481860b3 992 unsigned int may_be_inlined : 1;
72bf9492 993
0c1b455e
TT
994 /* This DIE has been marked DW_AT_main_subprogram. */
995 unsigned int main_subprogram : 1;
996
72bf9492
DJ
997 /* Flag set if the SCOPE field of this structure has been
998 computed. */
999 unsigned int scope_set : 1;
1000
fa4028e9
JB
1001 /* Flag set if the DIE has a byte_size attribute. */
1002 unsigned int has_byte_size : 1;
1003
ff908ebf
AW
1004 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1005 unsigned int has_const_value : 1;
1006
98bfdba5
PA
1007 /* Flag set if any of the DIE's children are template arguments. */
1008 unsigned int has_template_arguments : 1;
1009
52356b79 1010 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1011 unsigned int fixup_called : 1;
1012
36586728
TT
1013 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1014 unsigned int is_dwz : 1;
1015
1016 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1017 unsigned int spec_is_dwz : 1;
1018
7d00ffec
TT
1019 unsigned int canonical_name : 1;
1020
72bf9492 1021 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1022 sometimes a default name for unnamed DIEs. */
7d00ffec 1023 const char *raw_name = nullptr;
72bf9492 1024
abc72ce4 1025 /* The linkage name, if present. */
6f06d47b 1026 const char *linkage_name = nullptr;
abc72ce4 1027
72bf9492
DJ
1028 /* The scope to prepend to our children. This is generally
1029 allocated on the comp_unit_obstack, so will disappear
1030 when this compilation unit leaves the cache. */
6f06d47b 1031 const char *scope = nullptr;
72bf9492 1032
95554aad
TT
1033 /* Some data associated with the partial DIE. The tag determines
1034 which field is live. */
1035 union
1036 {
1037 /* The location description associated with this DIE, if any. */
1038 struct dwarf_block *locdesc;
1039 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1040 sect_offset sect_off;
6f06d47b 1041 } d {};
72bf9492
DJ
1042
1043 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1044 CORE_ADDR lowpc = 0;
1045 CORE_ADDR highpc = 0;
72bf9492 1046
93311388 1047 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1048 DW_AT_sibling, if any. */
48fbe735
YQ
1049 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1050 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1051 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1052
1053 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1054 DW_AT_specification (or DW_AT_abstract_origin or
1055 DW_AT_extension). */
6f06d47b 1056 sect_offset spec_offset {};
72bf9492
DJ
1057
1058 /* Pointers to this DIE's parent, first child, and next sibling,
1059 if any. */
6f06d47b
YQ
1060 struct partial_die_info *die_parent = nullptr;
1061 struct partial_die_info *die_child = nullptr;
1062 struct partial_die_info *die_sibling = nullptr;
1063
1064 friend struct partial_die_info *
1065 dwarf2_cu::find_partial_die (sect_offset sect_off);
1066
1067 private:
1068 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1069 partial_die_info (sect_offset sect_off)
1070 : partial_die_info (sect_off, DW_TAG_padding, 0)
1071 {
1072 }
1073
1074 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1075 int has_children_)
1076 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1077 {
1078 is_external = 0;
1079 is_declaration = 0;
1080 has_type = 0;
1081 has_specification = 0;
1082 has_pc_info = 0;
1083 may_be_inlined = 0;
1084 main_subprogram = 0;
1085 scope_set = 0;
1086 has_byte_size = 0;
1087 has_const_value = 0;
1088 has_template_arguments = 0;
1089 fixup_called = 0;
1090 is_dwz = 0;
1091 spec_is_dwz = 0;
7d00ffec 1092 canonical_name = 0;
6f06d47b 1093 }
c906108c
SS
1094 };
1095
c906108c
SS
1096/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1097 but this would require a corresponding change in unpack_field_as_long
1098 and friends. */
1099static int bits_per_byte = 8;
1100
9c6a1327
TT
1101struct variant_part_builder;
1102
1103/* When reading a variant, we track a bit more information about the
1104 field, and store it in an object of this type. */
2ddeaf8a
TT
1105
1106struct variant_field
1107{
9c6a1327
TT
1108 int first_field = -1;
1109 int last_field = -1;
1110
1111 /* A variant can contain other variant parts. */
1112 std::vector<variant_part_builder> variant_parts;
1113
2ddeaf8a
TT
1114 /* If we see a DW_TAG_variant, then this will be set if this is the
1115 default branch. */
9c6a1327
TT
1116 bool default_branch = false;
1117 /* If we see a DW_AT_discr_value, then this will be the discriminant
1118 value. */
1119 ULONGEST discriminant_value = 0;
1120 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1121 data. */
1122 struct dwarf_block *discr_list_data = nullptr;
1123};
1124
1125/* This represents a DW_TAG_variant_part. */
1126
1127struct variant_part_builder
1128{
1129 /* The offset of the discriminant field. */
1130 sect_offset discriminant_offset {};
1131
1132 /* Variants that are direct children of this variant part. */
1133 std::vector<variant_field> variants;
1134
1135 /* True if we're currently reading a variant. */
1136 bool processing_variant = false;
2ddeaf8a
TT
1137};
1138
52059ffd
TT
1139struct nextfield
1140{
be2daae6
TT
1141 int accessibility = 0;
1142 int virtuality = 0;
9c6a1327
TT
1143 /* Variant parts need to find the discriminant, which is a DIE
1144 reference. We track the section offset of each field to make
1145 this link. */
1146 sect_offset offset;
be2daae6 1147 struct field field {};
52059ffd
TT
1148};
1149
1150struct fnfieldlist
1151{
be2daae6
TT
1152 const char *name = nullptr;
1153 std::vector<struct fn_field> fnfields;
52059ffd
TT
1154};
1155
c906108c
SS
1156/* The routines that read and process dies for a C struct or C++ class
1157 pass lists of data member fields and lists of member function fields
1158 in an instance of a field_info structure, as defined below. */
1159struct field_info
c5aa993b 1160 {
0963b4bd 1161 /* List of data member and baseclasses fields. */
be2daae6
TT
1162 std::vector<struct nextfield> fields;
1163 std::vector<struct nextfield> baseclasses;
c906108c 1164
85102364 1165 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1166 int non_public_fields = 0;
c906108c 1167
c5aa993b
JM
1168 /* Member function fieldlist array, contains name of possibly overloaded
1169 member function, number of overloaded member functions and a pointer
1170 to the head of the member function field chain. */
be2daae6 1171 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1172
1173 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1174 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1175 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1176
1177 /* Nested types defined by this class and the number of elements in this
1178 list. */
be2daae6 1179 std::vector<struct decl_field> nested_types_list;
317f7127 1180
9c6a1327
TT
1181 /* If non-null, this is the variant part we are currently
1182 reading. */
1183 variant_part_builder *current_variant_part = nullptr;
1184 /* This holds all the top-level variant parts attached to the type
1185 we're reading. */
1186 std::vector<variant_part_builder> variant_parts;
1187
317f7127
TT
1188 /* Return the total number of fields (including baseclasses). */
1189 int nfields () const
1190 {
1191 return fields.size () + baseclasses.size ();
1192 }
c5aa993b 1193 };
c906108c 1194
ae038cb0
DJ
1195/* Loaded secondary compilation units are kept in memory until they
1196 have not been referenced for the processing of this many
1197 compilation units. Set this to zero to disable caching. Cache
1198 sizes of up to at least twenty will improve startup time for
1199 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1200static int dwarf_max_cache_age = 5;
920d2a44 1201static void
b4f54984
DE
1202show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1203 struct cmd_list_element *c, const char *value)
920d2a44 1204{
3e43a32a 1205 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1206 "DWARF compilation units is %s.\n"),
920d2a44
AC
1207 value);
1208}
4390d890 1209\f
c906108c
SS
1210/* local function prototypes */
1211
918dd910
JK
1212static void dwarf2_find_base_address (struct die_info *die,
1213 struct dwarf2_cu *cu);
1214
891813be 1215static dwarf2_psymtab *create_partial_symtab
7aa104c4
SM
1216 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1217 const char *name);
0018ea6f 1218
f1902523
JK
1219static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1220 const gdb_byte *info_ptr,
3e225074 1221 struct die_info *type_unit_die);
f1902523 1222
976ca316 1223static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
c906108c 1224
72bf9492
DJ
1225static void scan_partial_symbols (struct partial_die_info *,
1226 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1227 int, struct dwarf2_cu *);
c906108c 1228
72bf9492
DJ
1229static void add_partial_symbol (struct partial_die_info *,
1230 struct dwarf2_cu *);
63d06c5c 1231
72bf9492
DJ
1232static void add_partial_namespace (struct partial_die_info *pdi,
1233 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1234 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1235
5d7cb8df 1236static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1237 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1238 struct dwarf2_cu *cu);
1239
72bf9492
DJ
1240static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1241 struct dwarf2_cu *cu);
91c24f0a 1242
bc30ff58
JB
1243static void add_partial_subprogram (struct partial_die_info *pdi,
1244 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1245 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1246
d521ce57 1247static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1248
dee91e82 1249static struct partial_die_info *load_partial_dies
d521ce57 1250 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1251
fb816e8b
TV
1252/* A pair of partial_die_info and compilation unit. */
1253struct cu_partial_die_info
1254{
1255 /* The compilation unit of the partial_die_info. */
1256 struct dwarf2_cu *cu;
1257 /* A partial_die_info. */
1258 struct partial_die_info *pdi;
122cf0f2
AB
1259
1260 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1261 : cu (cu),
1262 pdi (pdi)
405feb71 1263 { /* Nothing. */ }
122cf0f2
AB
1264
1265private:
1266 cu_partial_die_info () = delete;
fb816e8b
TV
1267};
1268
122cf0f2
AB
1269static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1270 struct dwarf2_cu *);
72bf9492 1271
d521ce57
TT
1272static const gdb_byte *read_attribute (const struct die_reader_specs *,
1273 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1274 const gdb_byte *, bool *need_reprocess);
1275
1276static void read_attribute_reprocess (const struct die_reader_specs *reader,
1277 struct attribute *attr);
1278
1279static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1280
976ca316
SM
1281static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1282 dwarf2_section_info *, sect_offset);
f4dc4d17 1283
ed2dc618 1284static const char *read_indirect_string
976ca316 1285 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
ed2dc618 1286 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1287
ed2dc618 1288static const char *read_indirect_string_at_offset
976ca316 1289 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
927aa2e7 1290
d521ce57
TT
1291static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1292 const gdb_byte *,
3019eac3
DE
1293 unsigned int *);
1294
18a8505e
AT
1295static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1296 ULONGEST str_index);
1297
1298static const char *read_stub_str_index (struct dwarf2_cu *cu,
1299 ULONGEST str_index);
3019eac3 1300
e142c38c 1301static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1302
e142c38c
DJ
1303static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1304 struct dwarf2_cu *);
c906108c 1305
7d45c7c3
KB
1306static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1307 struct dwarf2_cu *cu);
1308
a084a2a6
AT
1309static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1310
05cf31d1
JB
1311static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1312 struct dwarf2_cu *cu);
1313
e142c38c 1314static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1315
e142c38c 1316static struct die_info *die_specification (struct die_info *die,
f2f0e013 1317 struct dwarf2_cu **);
63d06c5c 1318
9c541725 1319static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1320 struct dwarf2_cu *cu);
debd256d 1321
f3f5162e 1322static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1323 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1324 CORE_ADDR, int decode_mapping);
c906108c 1325
804d2729
TT
1326static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1327 const char *);
c906108c 1328
a14ed312 1329static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1330 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1331
ff39bb5e 1332static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1333 struct dwarf2_cu *);
c906108c 1334
ff39bb5e 1335static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1336 struct type *type,
1337 const char *name,
1338 struct obstack *obstack,
12df843f 1339 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1340 const gdb_byte **bytes,
98bfdba5 1341 struct dwarf2_locexpr_baton **baton);
2df3850c 1342
e7c27a73 1343static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1344
b4ba55a1
JB
1345static int need_gnat_info (struct dwarf2_cu *);
1346
3e43a32a
MS
1347static struct type *die_descriptive_type (struct die_info *,
1348 struct dwarf2_cu *);
b4ba55a1
JB
1349
1350static void set_descriptive_type (struct type *, struct die_info *,
1351 struct dwarf2_cu *);
1352
e7c27a73
DJ
1353static struct type *die_containing_type (struct die_info *,
1354 struct dwarf2_cu *);
c906108c 1355
ff39bb5e 1356static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1357 struct dwarf2_cu *);
c906108c 1358
f792889a 1359static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1360
673bfd45
DE
1361static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1362
0d5cff50 1363static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1364
6e70227d 1365static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1366 const char *suffix, int physname,
1367 struct dwarf2_cu *cu);
63d06c5c 1368
e7c27a73 1369static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1370
348e048f
DE
1371static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1372
e7c27a73 1373static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1374
e7c27a73 1375static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1376
96408a79
SA
1377static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1378
71a3c369
TT
1379static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1380
ff013f42 1381static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1382 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1383
41144253 1384/* Return the .debug_loclists section to use for cu. */
1385static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1386
3a2b436a 1387/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1388 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1389enum pc_bounds_kind
1390{
e385593e 1391 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1392 PC_BOUNDS_NOT_PRESENT,
1393
e385593e
JK
1394 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1395 were present but they do not form a valid range of PC addresses. */
1396 PC_BOUNDS_INVALID,
1397
3a2b436a
JK
1398 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1399 PC_BOUNDS_RANGES,
1400
1401 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1402 PC_BOUNDS_HIGH_LOW,
1403};
1404
1405static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1406 CORE_ADDR *, CORE_ADDR *,
1407 struct dwarf2_cu *,
891813be 1408 dwarf2_psymtab *);
c906108c 1409
fae299cd
DC
1410static void get_scope_pc_bounds (struct die_info *,
1411 CORE_ADDR *, CORE_ADDR *,
1412 struct dwarf2_cu *);
1413
801e3a5b
JB
1414static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1415 CORE_ADDR, struct dwarf2_cu *);
1416
a14ed312 1417static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1418 struct dwarf2_cu *);
c906108c 1419
a14ed312 1420static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1421 struct type *, struct dwarf2_cu *);
c906108c 1422
a14ed312 1423static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1424 struct die_info *, struct type *,
e7c27a73 1425 struct dwarf2_cu *);
c906108c 1426
a14ed312 1427static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1428 struct type *,
1429 struct dwarf2_cu *);
c906108c 1430
134d01f1 1431static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1432
e7c27a73 1433static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1434
e7c27a73 1435static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1436
5d7cb8df
JK
1437static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1438
804d2729 1439static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1440
27aa8d6a
SW
1441static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1442
74921315
KS
1443static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1444
f55ee35c
JK
1445static struct type *read_module_type (struct die_info *die,
1446 struct dwarf2_cu *cu);
1447
38d518c9 1448static const char *namespace_name (struct die_info *die,
e142c38c 1449 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1450
134d01f1 1451static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1452
7d79de9a
TT
1453static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1454 bool * = nullptr);
c906108c 1455
6e70227d 1456static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1457 struct dwarf2_cu *);
1458
bf6af496 1459static struct die_info *read_die_and_siblings_1
d521ce57 1460 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1461 struct die_info *);
639d11d3 1462
dee91e82 1463static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1464 const gdb_byte *info_ptr,
1465 const gdb_byte **new_info_ptr,
639d11d3
DC
1466 struct die_info *parent);
1467
d521ce57
TT
1468static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1469 struct die_info **, const gdb_byte *,
3e225074 1470 int);
3019eac3 1471
d521ce57 1472static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1473 struct die_info **, const gdb_byte *);
93311388 1474
e7c27a73 1475static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1476
15d034d0 1477static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1478 struct objfile *);
71c25dea 1479
15d034d0 1480static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1481
15d034d0 1482static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1483 struct die_info *die,
1484 struct dwarf2_cu *cu);
1485
ca69b9e6
DE
1486static const char *dwarf2_physname (const char *name, struct die_info *die,
1487 struct dwarf2_cu *cu);
1488
e142c38c 1489static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1490 struct dwarf2_cu **);
9219021c 1491
d97bc12b
DE
1492static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1493
1494static void dump_die_for_error (struct die_info *);
1495
1496static void dump_die_1 (struct ui_file *, int level, int max_level,
1497 struct die_info *);
c906108c 1498
d97bc12b 1499/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1500
51545339 1501static void store_in_ref_table (struct die_info *,
10b3939b 1502 struct dwarf2_cu *);
c906108c 1503
348e048f 1504static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1505 const struct attribute *,
348e048f
DE
1506 struct dwarf2_cu **);
1507
10b3939b 1508static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1509 const struct attribute *,
f2f0e013 1510 struct dwarf2_cu **);
c906108c 1511
348e048f 1512static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1513 const struct attribute *,
348e048f
DE
1514 struct dwarf2_cu **);
1515
ac9ec31b
DE
1516static struct type *get_signatured_type (struct die_info *, ULONGEST,
1517 struct dwarf2_cu *);
1518
1519static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1520 const struct attribute *,
ac9ec31b
DE
1521 struct dwarf2_cu *);
1522
ab432490
SM
1523static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1524 dwarf2_per_objfile *per_objfile);
348e048f 1525
ab432490
SM
1526static void read_signatured_type (signatured_type *sig_type,
1527 dwarf2_per_objfile *per_objfile);
348e048f 1528
63e43d3a
PMR
1529static int attr_to_dynamic_prop (const struct attribute *attr,
1530 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1531 struct dynamic_prop *prop, struct type *type);
63e43d3a 1532
c906108c
SS
1533/* memory allocation interface */
1534
7b5a2f43 1535static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1536
b60c80d6 1537static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1538
43f3e411 1539static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1540
8cf6f0b1
TT
1541static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1542 struct dwarf2_loclist_baton *baton,
ff39bb5e 1543 const struct attribute *attr);
8cf6f0b1 1544
ff39bb5e 1545static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1546 struct symbol *sym,
f1e6e072
TT
1547 struct dwarf2_cu *cu,
1548 int is_block);
4c2df51b 1549
d521ce57
TT
1550static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1551 const gdb_byte *info_ptr,
1552 struct abbrev_info *abbrev);
4bb7a0a7 1553
72bf9492
DJ
1554static hashval_t partial_die_hash (const void *item);
1555
1556static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1557
ae038cb0 1558static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618 1559 (sect_offset sect_off, unsigned int offset_in_dwz,
976ca316 1560 dwarf2_per_objfile *per_objfile);
ae038cb0 1561
9816fde3 1562static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1563 struct die_info *comp_unit_die,
1564 enum language pretend_language);
93311388 1565
f792889a
DJ
1566static struct type *set_die_type (struct die_info *, struct type *,
1567 struct dwarf2_cu *);
1c379e20 1568
976ca316 1569static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
ae038cb0 1570
976ca316 1571static int create_all_type_units (dwarf2_per_objfile *per_objfile);
1fd400ff 1572
ab432490
SM
1573static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1574 dwarf2_per_objfile *per_objfile,
1575 bool skip_partial,
1576 enum language pretend_language);
10b3939b 1577
8fc0b21d 1578static void process_full_comp_unit (dwarf2_cu *cu,
47b14e86 1579 enum language pretend_language);
10b3939b 1580
8fc0b21d 1581static void process_full_type_unit (dwarf2_cu *cu,
47b14e86 1582 enum language pretend_language);
f4dc4d17 1583
10b3939b
DJ
1584static void dwarf2_add_dependence (struct dwarf2_cu *,
1585 struct dwarf2_per_cu_data *);
1586
ae038cb0
DJ
1587static void dwarf2_mark (struct dwarf2_cu *);
1588
b64f50a1 1589static struct type *get_die_type_at_offset (sect_offset,
aa66c379
SM
1590 dwarf2_per_cu_data *per_cu,
1591 dwarf2_per_objfile *per_objfile);
673bfd45 1592
f792889a 1593static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1594
120ce1b5
SM
1595static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1596 dwarf2_per_objfile *per_objfile,
95554aad
TT
1597 enum language pretend_language);
1598
976ca316 1599static void process_queue (dwarf2_per_objfile *per_objfile);
9291a0cd 1600
b303c6f6
AB
1601/* Class, the destructor of which frees all allocated queue entries. This
1602 will only have work to do if an error was thrown while processing the
1603 dwarf. If no error was thrown then the queue entries should have all
1604 been processed, and freed, as we went along. */
1605
1606class dwarf2_queue_guard
1607{
1608public:
39856def
TT
1609 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1610 : m_per_objfile (per_objfile)
1611 {
1612 }
b303c6f6
AB
1613
1614 /* Free any entries remaining on the queue. There should only be
1615 entries left if we hit an error while processing the dwarf. */
1616 ~dwarf2_queue_guard ()
1617 {
39856def
TT
1618 /* Ensure that no memory is allocated by the queue. */
1619 std::queue<dwarf2_queue_item> empty;
5989a64e 1620 std::swap (m_per_objfile->per_bfd->queue, empty);
39856def 1621 }
b303c6f6 1622
39856def 1623 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1624
39856def
TT
1625private:
1626 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1627};
1628
39856def
TT
1629dwarf2_queue_item::~dwarf2_queue_item ()
1630{
1631 /* Anything still marked queued is likely to be in an
1632 inconsistent state, so discard it. */
1633 if (per_cu->queued)
1634 {
7188ed02 1635 per_objfile->remove_cu (per_cu);
39856def
TT
1636 per_cu->queued = 0;
1637 }
1638}
1639
d721ba37
PA
1640/* The return type of find_file_and_directory. Note, the enclosed
1641 string pointers are only valid while this object is valid. */
1642
1643struct file_and_directory
1644{
1645 /* The filename. This is never NULL. */
1646 const char *name;
1647
1648 /* The compilation directory. NULL if not known. If we needed to
1649 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1650 points directly to the DW_AT_comp_dir string attribute owned by
1651 the obstack that owns the DIE. */
1652 const char *comp_dir;
1653
1654 /* If we needed to build a new string for comp_dir, this is what
1655 owns the storage. */
1656 std::string comp_dir_storage;
1657};
1658
1659static file_and_directory find_file_and_directory (struct die_info *die,
1660 struct dwarf2_cu *cu);
9291a0cd 1661
298e9637 1662static htab_up allocate_signatured_type_table ();
1fd400ff 1663
298e9637 1664static htab_up allocate_dwo_unit_table ();
3019eac3 1665
57d63ce2 1666static struct dwo_unit *lookup_dwo_unit_in_dwp
976ca316
SM
1667 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1668 const char *comp_dir, ULONGEST signature, int is_debug_types);
a2ce51a0 1669
976ca316 1670static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
a2ce51a0 1671
3019eac3 1672static struct dwo_unit *lookup_dwo_comp_unit
4ab09049
SM
1673 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1674 ULONGEST signature);
3019eac3
DE
1675
1676static struct dwo_unit *lookup_dwo_type_unit
4ab09049 1677 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
3019eac3 1678
1b555f17 1679static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
89e63ee4 1680
263db9a1
TT
1681/* A unique pointer to a dwo_file. */
1682
51ac9db5 1683typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1684
976ca316 1685static void process_cu_includes (dwarf2_per_objfile *per_objfile);
95554aad 1686
1b80a9fa 1687static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1688
1689static void free_line_header_voidp (void *arg);
4390d890
DE
1690\f
1691/* Various complaints about symbol reading that don't abort the process. */
1692
4390d890
DE
1693static void
1694dwarf2_debug_line_missing_file_complaint (void)
1695{
b98664d3 1696 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1697}
1698
1699static void
1700dwarf2_debug_line_missing_end_sequence_complaint (void)
1701{
b98664d3 1702 complaint (_(".debug_line section has line "
4390d890
DE
1703 "program sequence without an end"));
1704}
1705
1706static void
1707dwarf2_complex_location_expr_complaint (void)
1708{
b98664d3 1709 complaint (_("location expression too complex"));
4390d890
DE
1710}
1711
1712static void
1713dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1714 int arg3)
1715{
b98664d3 1716 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1717 arg1, arg2, arg3);
1718}
1719
4390d890
DE
1720static void
1721dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1722{
b98664d3 1723 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1724 arg1, arg2);
1725}
527f3840
JK
1726
1727/* Hash function for line_header_hash. */
1728
1729static hashval_t
1730line_header_hash (const struct line_header *ofs)
1731{
9c541725 1732 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1733}
1734
1735/* Hash function for htab_create_alloc_ex for line_header_hash. */
1736
1737static hashval_t
1738line_header_hash_voidp (const void *item)
1739{
9a3c8263 1740 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1741
1742 return line_header_hash (ofs);
1743}
1744
1745/* Equality function for line_header_hash. */
1746
1747static int
1748line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1749{
9a3c8263
SM
1750 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1751 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1752
9c541725 1753 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1754 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1755}
1756
4390d890 1757\f
9291a0cd 1758
330cdd98
PA
1759/* See declaration. */
1760
5989a64e
SM
1761dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1762 bool can_copy_)
c3699833
SM
1763 : obfd (obfd),
1764 can_copy (can_copy_)
330cdd98
PA
1765{
1766 if (names == NULL)
1767 names = &dwarf2_elf_names;
1768
330cdd98
PA
1769 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1770 locate_sections (obfd, sec, *names);
1771}
1772
5989a64e 1773dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98 1774{
b76e467d 1775 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1776 per_cu->imported_symtabs_free ();
fc8e7e75 1777
b2bdb8cf 1778 for (signatured_type *sig_type : all_type_units)
ae640021 1779 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1780
5989a64e 1781 /* Everything else should be on this->obstack. */
330cdd98
PA
1782}
1783
7188ed02 1784/* See read.h. */
330cdd98
PA
1785
1786void
7188ed02 1787dwarf2_per_objfile::remove_all_cus ()
330cdd98 1788{
7188ed02
SM
1789 for (auto pair : m_dwarf2_cus)
1790 delete pair.second;
330cdd98 1791
7188ed02 1792 m_dwarf2_cus.clear ();
330cdd98
PA
1793}
1794
11ed8cad
TT
1795/* A helper class that calls free_cached_comp_units on
1796 destruction. */
1797
1798class free_cached_comp_units
1799{
1800public:
1801
1802 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1803 : m_per_objfile (per_objfile)
1804 {
1805 }
1806
1807 ~free_cached_comp_units ()
1808 {
7188ed02 1809 m_per_objfile->remove_all_cus ();
11ed8cad
TT
1810 }
1811
1812 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1813
1814private:
1815
1816 dwarf2_per_objfile *m_per_objfile;
1817};
1818
af758d11
SM
1819/* See read.h. */
1820
1821bool
1822dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1823{
1824 gdb_assert (per_cu->index < this->m_symtabs.size ());
1825
1826 return this->m_symtabs[per_cu->index] != nullptr;
1827}
1828
1829/* See read.h. */
1830
1831compunit_symtab *
1832dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1833{
1834 gdb_assert (per_cu->index < this->m_symtabs.size ());
1835
1836 return this->m_symtabs[per_cu->index];
1837}
1838
1839/* See read.h. */
1840
1841void
1842dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1843 compunit_symtab *symtab)
1844{
1845 gdb_assert (per_cu->index < this->m_symtabs.size ());
1846 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1847
1848 this->m_symtabs[per_cu->index] = symtab;
1849}
1850
c906108c 1851/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1852 information and return true if we have enough to do something.
1853 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1854 ELF names are used. CAN_COPY is true for formats where symbol
1855 interposition is possible and so symbol values must follow copy
1856 relocation rules. */
c906108c
SS
1857
1858int
251d32d9 1859dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1860 const struct dwarf2_debug_sections *names,
1861 bool can_copy)
c906108c 1862{
97cbe998
SDJ
1863 if (objfile->flags & OBJF_READNEVER)
1864 return 0;
1865
976ca316 1866 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 1867
976ca316 1868 if (per_objfile == NULL)
5989a64e 1869 {
17ee85fc
TT
1870 dwarf2_per_bfd *per_bfd;
1871
1872 /* We can share a "dwarf2_per_bfd" with other objfiles if the BFD
1873 doesn't require relocations and if there aren't partial symbols
1874 from some other reader. */
1875 if (!objfile_has_partial_symbols (objfile)
1876 && !gdb_bfd_requires_relocations (objfile->obfd))
1877 {
1878 /* See if one has been created for this BFD yet. */
1879 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1880
1881 if (per_bfd == nullptr)
1882 {
1883 /* No, create it now. */
1884 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1885 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1886 }
1887 }
1888 else
1889 {
1890 /* No sharing possible, create one specifically for this objfile. */
1891 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1892 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1893 }
5989a64e 1894
976ca316 1895 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
5989a64e 1896 }
5bfd760d 1897
976ca316
SM
1898 return (!per_objfile->per_bfd->info.is_virtual
1899 && per_objfile->per_bfd->info.s.section != NULL
1900 && !per_objfile->per_bfd->abbrev.is_virtual
1901 && per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1902}
1903
251d32d9
TG
1904/* When loading sections, we look either for uncompressed section or for
1905 compressed section names. */
233a11ab
CS
1906
1907static int
251d32d9
TG
1908section_is_p (const char *section_name,
1909 const struct dwarf2_section_names *names)
233a11ab 1910{
251d32d9
TG
1911 if (names->normal != NULL
1912 && strcmp (section_name, names->normal) == 0)
1913 return 1;
1914 if (names->compressed != NULL
1915 && strcmp (section_name, names->compressed) == 0)
1916 return 1;
1917 return 0;
233a11ab
CS
1918}
1919
330cdd98 1920/* See declaration. */
c906108c 1921
330cdd98 1922void
5989a64e
SM
1923dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1924 const dwarf2_debug_sections &names)
c906108c 1925{
fd361982 1926 flagword aflag = bfd_section_flags (sectp);
251d32d9 1927
dc7650b8
JK
1928 if ((aflag & SEC_HAS_CONTENTS) == 0)
1929 {
1930 }
950b7495
KS
1931 else if (elf_section_data (sectp)->this_hdr.sh_size
1932 > bfd_get_file_size (abfd))
1933 {
1934 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1935 warning (_("Discarding section %s which has a section size (%s"
1936 ") larger than the file size [in module %s]"),
1937 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1938 bfd_get_filename (abfd));
1939 }
330cdd98 1940 else if (section_is_p (sectp->name, &names.info))
c906108c 1941 {
330cdd98 1942 this->info.s.section = sectp;
fd361982 1943 this->info.size = bfd_section_size (sectp);
c906108c 1944 }
330cdd98 1945 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1946 {
330cdd98 1947 this->abbrev.s.section = sectp;
fd361982 1948 this->abbrev.size = bfd_section_size (sectp);
c906108c 1949 }
330cdd98 1950 else if (section_is_p (sectp->name, &names.line))
c906108c 1951 {
330cdd98 1952 this->line.s.section = sectp;
fd361982 1953 this->line.size = bfd_section_size (sectp);
c906108c 1954 }
330cdd98 1955 else if (section_is_p (sectp->name, &names.loc))
c906108c 1956 {
330cdd98 1957 this->loc.s.section = sectp;
fd361982 1958 this->loc.size = bfd_section_size (sectp);
c906108c 1959 }
330cdd98 1960 else if (section_is_p (sectp->name, &names.loclists))
43988095 1961 {
330cdd98 1962 this->loclists.s.section = sectp;
fd361982 1963 this->loclists.size = bfd_section_size (sectp);
43988095 1964 }
330cdd98 1965 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1966 {
330cdd98 1967 this->macinfo.s.section = sectp;
fd361982 1968 this->macinfo.size = bfd_section_size (sectp);
c906108c 1969 }
330cdd98 1970 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1971 {
330cdd98 1972 this->macro.s.section = sectp;
fd361982 1973 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1974 }
330cdd98 1975 else if (section_is_p (sectp->name, &names.str))
c906108c 1976 {
330cdd98 1977 this->str.s.section = sectp;
fd361982 1978 this->str.size = bfd_section_size (sectp);
c906108c 1979 }
18a8505e
AT
1980 else if (section_is_p (sectp->name, &names.str_offsets))
1981 {
1982 this->str_offsets.s.section = sectp;
1983 this->str_offsets.size = bfd_section_size (sectp);
1984 }
330cdd98 1985 else if (section_is_p (sectp->name, &names.line_str))
43988095 1986 {
330cdd98 1987 this->line_str.s.section = sectp;
fd361982 1988 this->line_str.size = bfd_section_size (sectp);
43988095 1989 }
330cdd98 1990 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1991 {
330cdd98 1992 this->addr.s.section = sectp;
fd361982 1993 this->addr.size = bfd_section_size (sectp);
3019eac3 1994 }
330cdd98 1995 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1996 {
330cdd98 1997 this->frame.s.section = sectp;
fd361982 1998 this->frame.size = bfd_section_size (sectp);
b6af0555 1999 }
330cdd98 2000 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2001 {
330cdd98 2002 this->eh_frame.s.section = sectp;
fd361982 2003 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2004 }
330cdd98 2005 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2006 {
330cdd98 2007 this->ranges.s.section = sectp;
fd361982 2008 this->ranges.size = bfd_section_size (sectp);
af34e669 2009 }
330cdd98 2010 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2011 {
330cdd98 2012 this->rnglists.s.section = sectp;
fd361982 2013 this->rnglists.size = bfd_section_size (sectp);
43988095 2014 }
330cdd98 2015 else if (section_is_p (sectp->name, &names.types))
348e048f 2016 {
8b70b953
TT
2017 struct dwarf2_section_info type_section;
2018
2019 memset (&type_section, 0, sizeof (type_section));
049412e3 2020 type_section.s.section = sectp;
fd361982 2021 type_section.size = bfd_section_size (sectp);
8b70b953 2022
fd5866f6 2023 this->types.push_back (type_section);
348e048f 2024 }
330cdd98 2025 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2026 {
330cdd98 2027 this->gdb_index.s.section = sectp;
fd361982 2028 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2029 }
927aa2e7
JK
2030 else if (section_is_p (sectp->name, &names.debug_names))
2031 {
2032 this->debug_names.s.section = sectp;
fd361982 2033 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2034 }
2035 else if (section_is_p (sectp->name, &names.debug_aranges))
2036 {
2037 this->debug_aranges.s.section = sectp;
fd361982 2038 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2039 }
dce234bc 2040
fd361982
AM
2041 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2042 && bfd_section_vma (sectp) == 0)
330cdd98 2043 this->has_section_at_zero = true;
c906108c
SS
2044}
2045
dce234bc 2046/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2047 SECTION_NAME. */
af34e669 2048
dce234bc 2049void
3017a003
TG
2050dwarf2_get_section_info (struct objfile *objfile,
2051 enum dwarf2_section_enum sect,
d521ce57 2052 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2053 bfd_size_type *sizep)
2054{
976ca316 2055 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
dce234bc 2056 struct dwarf2_section_info *info;
a3b2a86b
TT
2057
2058 /* We may see an objfile without any DWARF, in which case we just
2059 return nothing. */
976ca316 2060 if (per_objfile == NULL)
a3b2a86b
TT
2061 {
2062 *sectp = NULL;
2063 *bufp = NULL;
2064 *sizep = 0;
2065 return;
2066 }
3017a003
TG
2067 switch (sect)
2068 {
2069 case DWARF2_DEBUG_FRAME:
976ca316 2070 info = &per_objfile->per_bfd->frame;
3017a003
TG
2071 break;
2072 case DWARF2_EH_FRAME:
976ca316 2073 info = &per_objfile->per_bfd->eh_frame;
3017a003
TG
2074 break;
2075 default:
2076 gdb_assert_not_reached ("unexpected section");
2077 }
dce234bc 2078
96b79293 2079 info->read (objfile);
dce234bc 2080
96b79293 2081 *sectp = info->get_bfd_section ();
dce234bc
PP
2082 *bufp = info->buffer;
2083 *sizep = info->size;
2084}
2085
36586728
TT
2086/* A helper function to find the sections for a .dwz file. */
2087
2088static void
2089locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2090{
9a3c8263 2091 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2092
2093 /* Note that we only support the standard ELF names, because .dwz
2094 is ELF-only (at the time of writing). */
2095 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2096 {
049412e3 2097 dwz_file->abbrev.s.section = sectp;
fd361982 2098 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2099 }
2100 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2101 {
049412e3 2102 dwz_file->info.s.section = sectp;
fd361982 2103 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2104 }
2105 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2106 {
049412e3 2107 dwz_file->str.s.section = sectp;
fd361982 2108 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2109 }
2110 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2111 {
049412e3 2112 dwz_file->line.s.section = sectp;
fd361982 2113 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2114 }
2115 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2116 {
049412e3 2117 dwz_file->macro.s.section = sectp;
fd361982 2118 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2119 }
2ec9a5e0
TT
2120 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2121 {
049412e3 2122 dwz_file->gdb_index.s.section = sectp;
fd361982 2123 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2124 }
927aa2e7
JK
2125 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2126 {
2127 dwz_file->debug_names.s.section = sectp;
fd361982 2128 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2129 }
36586728
TT
2130}
2131
c4973306 2132/* See dwarf2read.h. */
36586728 2133
c4973306 2134struct dwz_file *
c3699833 2135dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
36586728 2136{
36586728 2137 const char *filename;
acd13123 2138 bfd_size_type buildid_len_arg;
dc294be5
TT
2139 size_t buildid_len;
2140 bfd_byte *buildid;
36586728 2141
c3699833
SM
2142 if (per_bfd->dwz_file != NULL)
2143 return per_bfd->dwz_file.get ();
36586728 2144
4db1a1dc 2145 bfd_set_error (bfd_error_no_error);
791afaa2 2146 gdb::unique_xmalloc_ptr<char> data
c3699833 2147 (bfd_get_alt_debug_link_info (per_bfd->obfd,
791afaa2 2148 &buildid_len_arg, &buildid));
4db1a1dc
TT
2149 if (data == NULL)
2150 {
2151 if (bfd_get_error () == bfd_error_no_error)
2152 return NULL;
2153 error (_("could not read '.gnu_debugaltlink' section: %s"),
2154 bfd_errmsg (bfd_get_error ()));
2155 }
791afaa2
TT
2156
2157 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2158
acd13123
TT
2159 buildid_len = (size_t) buildid_len_arg;
2160
791afaa2 2161 filename = data.get ();
d721ba37
PA
2162
2163 std::string abs_storage;
36586728
TT
2164 if (!IS_ABSOLUTE_PATH (filename))
2165 {
14278e1f 2166 gdb::unique_xmalloc_ptr<char> abs
c3699833 2167 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
36586728 2168
14278e1f 2169 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2170 filename = abs_storage.c_str ();
36586728
TT
2171 }
2172
dc294be5
TT
2173 /* First try the file name given in the section. If that doesn't
2174 work, try to use the build-id instead. */
ad80db5b 2175 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
dc294be5 2176 if (dwz_bfd != NULL)
36586728 2177 {
192b62ce 2178 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2179 dwz_bfd.reset (nullptr);
36586728
TT
2180 }
2181
dc294be5
TT
2182 if (dwz_bfd == NULL)
2183 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2184
0d79cdc4
AM
2185 if (dwz_bfd == nullptr)
2186 {
2187 gdb::unique_xmalloc_ptr<char> alt_filename;
c3699833 2188 const char *origname = bfd_get_filename (per_bfd->obfd);
0d79cdc4
AM
2189
2190 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2191 buildid_len,
2192 origname,
2193 &alt_filename));
2194
2195 if (fd.get () >= 0)
2196 {
2197 /* File successfully retrieved from server. */
ad80db5b 2198 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
0d79cdc4
AM
2199
2200 if (dwz_bfd == nullptr)
2201 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2202 alt_filename.get ());
2203 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2204 dwz_bfd.reset (nullptr);
2205 }
2206 }
2207
dc294be5
TT
2208 if (dwz_bfd == NULL)
2209 error (_("could not find '.gnu_debugaltlink' file for %s"),
c3699833 2210 bfd_get_filename (per_bfd->obfd));
dc294be5 2211
7ff8cb8c
TT
2212 std::unique_ptr<struct dwz_file> result
2213 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2214
7ff8cb8c
TT
2215 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2216 result.get ());
36586728 2217
c3699833
SM
2218 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2219 per_bfd->dwz_file = std::move (result);
2220 return per_bfd->dwz_file.get ();
36586728 2221}
9291a0cd 2222\f
7b9f3c50
DE
2223/* DWARF quick_symbols_functions support. */
2224
2225/* TUs can share .debug_line entries, and there can be a lot more TUs than
2226 unique line tables, so we maintain a separate table of all .debug_line
2227 derived entries to support the sharing.
2228 All the quick functions need is the list of file names. We discard the
2229 line_header when we're done and don't need to record it here. */
2230struct quick_file_names
2231{
094b34ac
DE
2232 /* The data used to construct the hash key. */
2233 struct stmt_list_hash hash;
7b9f3c50
DE
2234
2235 /* The number of entries in file_names, real_names. */
2236 unsigned int num_file_names;
2237
2238 /* The file names from the line table, after being run through
2239 file_full_name. */
2240 const char **file_names;
2241
2242 /* The file names from the line table after being run through
2243 gdb_realpath. These are computed lazily. */
2244 const char **real_names;
2245};
2246
2247/* When using the index (and thus not using psymtabs), each CU has an
2248 object of this type. This is used to hold information needed by
2249 the various "quick" methods. */
2250struct dwarf2_per_cu_quick_data
2251{
2252 /* The file table. This can be NULL if there was no file table
2253 or it's currently not read in.
5989a64e 2254 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2255 struct quick_file_names *file_names;
2256
7b9f3c50
DE
2257 /* A temporary mark bit used when iterating over all CUs in
2258 expand_symtabs_matching. */
2259 unsigned int mark : 1;
2260
2261 /* True if we've tried to read the file table and found there isn't one.
2262 There will be no point in trying to read it again next time. */
2263 unsigned int no_file_data : 1;
2264};
2265
094b34ac
DE
2266/* Utility hash function for a stmt_list_hash. */
2267
2268static hashval_t
2269hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2270{
2271 hashval_t v = 0;
2272
2273 if (stmt_list_hash->dwo_unit != NULL)
2274 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2275 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2276 return v;
2277}
2278
2279/* Utility equality function for a stmt_list_hash. */
2280
2281static int
2282eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2283 const struct stmt_list_hash *rhs)
2284{
2285 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2286 return 0;
2287 if (lhs->dwo_unit != NULL
2288 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2289 return 0;
2290
9c541725 2291 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2292}
2293
7b9f3c50
DE
2294/* Hash function for a quick_file_names. */
2295
2296static hashval_t
2297hash_file_name_entry (const void *e)
2298{
9a3c8263
SM
2299 const struct quick_file_names *file_data
2300 = (const struct quick_file_names *) e;
7b9f3c50 2301
094b34ac 2302 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2303}
2304
2305/* Equality function for a quick_file_names. */
2306
2307static int
2308eq_file_name_entry (const void *a, const void *b)
2309{
9a3c8263
SM
2310 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2311 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2312
094b34ac 2313 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2314}
2315
2316/* Delete function for a quick_file_names. */
2317
2318static void
2319delete_file_name_entry (void *e)
2320{
9a3c8263 2321 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2322 int i;
2323
2324 for (i = 0; i < file_data->num_file_names; ++i)
2325 {
2326 xfree ((void*) file_data->file_names[i]);
2327 if (file_data->real_names)
2328 xfree ((void*) file_data->real_names[i]);
2329 }
2330
45940949
TT
2331 /* The space for the struct itself lives on the obstack, so we don't
2332 free it here. */
7b9f3c50
DE
2333}
2334
2335/* Create a quick_file_names hash table. */
2336
5895093f 2337static htab_up
7b9f3c50
DE
2338create_quick_file_names_table (unsigned int nr_initial_entries)
2339{
5895093f
TT
2340 return htab_up (htab_create_alloc (nr_initial_entries,
2341 hash_file_name_entry, eq_file_name_entry,
2342 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2343}
9291a0cd 2344
ab432490
SM
2345/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2346 function is unrelated to symtabs, symtab would have to be created afterwards.
2347 You should call age_cached_comp_units after processing the CU. */
918dd910 2348
1b555f17 2349static dwarf2_cu *
ab432490
SM
2350load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2351 bool skip_partial)
918dd910 2352{
3019eac3 2353 if (per_cu->is_debug_types)
ab432490 2354 load_full_type_unit (per_cu, per_objfile);
918dd910 2355 else
ab432490 2356 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
918dd910 2357
7188ed02
SM
2358 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2359 if (cu == nullptr)
1b555f17 2360 return nullptr; /* Dummy CU. */
2dc860c0 2361
7188ed02 2362 dwarf2_find_base_address (cu->dies, cu);
1b555f17 2363
7188ed02 2364 return cu;
918dd910
JK
2365}
2366
97a1449a 2367/* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2fdf6df6 2368
9291a0cd 2369static void
97a1449a 2370dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2371 dwarf2_per_objfile *per_objfile, bool skip_partial)
9291a0cd 2372{
f4dc4d17
DE
2373 /* Skip type_unit_groups, reading the type units they contain
2374 is handled elsewhere. */
197400e8 2375 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2376 return;
2377
b303c6f6
AB
2378 /* The destructor of dwarf2_queue_guard frees any entries left on
2379 the queue. After this point we're guaranteed to leave this function
2380 with the dwarf queue empty. */
39856def 2381 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2382
976ca316 2383 if (!per_objfile->symtab_set_p (per_cu))
95554aad 2384 {
976ca316
SM
2385 queue_comp_unit (per_cu, per_objfile, language_minimal);
2386 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
89e63ee4
DE
2387
2388 /* If we just loaded a CU from a DWO, and we're working with an index
2389 that may badly handle TUs, load all the TUs in that DWO as well.
2390 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2391 if (!per_cu->is_debug_types
1b555f17
SM
2392 && cu != NULL
2393 && cu->dwo_unit != NULL
976ca316
SM
2394 && per_objfile->per_bfd->index_table != NULL
2395 && per_objfile->per_bfd->index_table->version <= 7
89e63ee4 2396 /* DWP files aren't supported yet. */
976ca316 2397 && get_dwp_file (per_objfile) == NULL)
1b555f17 2398 queue_and_load_all_dwo_tus (cu);
95554aad 2399 }
9291a0cd 2400
976ca316 2401 process_queue (per_objfile);
9291a0cd
TT
2402
2403 /* Age the cache, releasing compilation units that have not
2404 been used recently. */
976ca316 2405 per_objfile->age_comp_units ();
9291a0cd
TT
2406}
2407
97a1449a
SM
2408/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2409 the per-objfile for which this symtab is instantiated.
2410
2411 Returns the resulting symbol table. */
2fdf6df6 2412
43f3e411 2413static struct compunit_symtab *
97a1449a 2414dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2415 dwarf2_per_objfile *per_objfile,
97a1449a 2416 bool skip_partial)
9291a0cd 2417{
976ca316 2418 gdb_assert (per_objfile->per_bfd->using_index);
af758d11 2419
976ca316 2420 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd 2421 {
976ca316 2422 free_cached_comp_units freer (per_objfile);
c83dd867 2423 scoped_restore decrementer = increment_reading_symtab ();
976ca316
SM
2424 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2425 process_cu_includes (per_objfile);
9291a0cd 2426 }
f194fefb 2427
976ca316 2428 return per_objfile->get_symtab (per_cu);
9291a0cd
TT
2429}
2430
ff4c9fec 2431/* See declaration. */
f4dc4d17 2432
ff4c9fec 2433dwarf2_per_cu_data *
5989a64e 2434dwarf2_per_bfd::get_cutu (int index)
ff4c9fec 2435{
b76e467d 2436 if (index >= this->all_comp_units.size ())
ff4c9fec 2437 {
b76e467d 2438 index -= this->all_comp_units.size ();
b2bdb8cf 2439 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2440 return &this->all_type_units[index]->per_cu;
2441 }
f4dc4d17 2442
ff4c9fec
SM
2443 return this->all_comp_units[index];
2444}
f4dc4d17 2445
ff4c9fec 2446/* See declaration. */
2fdf6df6 2447
ff4c9fec 2448dwarf2_per_cu_data *
5989a64e 2449dwarf2_per_bfd::get_cu (int index)
1fd400ff 2450{
b76e467d 2451 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2452
ff4c9fec 2453 return this->all_comp_units[index];
f4dc4d17
DE
2454}
2455
ff4c9fec 2456/* See declaration. */
f4dc4d17 2457
ff4c9fec 2458signatured_type *
5989a64e 2459dwarf2_per_bfd::get_tu (int index)
f4dc4d17 2460{
b2bdb8cf 2461 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2462
ff4c9fec 2463 return this->all_type_units[index];
1fd400ff
TT
2464}
2465
d3473f0c
TT
2466/* See read.h. */
2467
2468dwarf2_per_cu_data *
5989a64e 2469dwarf2_per_bfd::allocate_per_cu ()
d3473f0c
TT
2470{
2471 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
1859c670 2472 result->per_bfd = this;
d3473f0c
TT
2473 result->index = m_num_psymtabs++;
2474 return result;
2475}
2476
2477/* See read.h. */
2478
2479signatured_type *
5989a64e 2480dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c
TT
2481{
2482 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
1859c670 2483 result->per_cu.per_bfd = this;
d3473f0c
TT
2484 result->per_cu.index = m_num_psymtabs++;
2485 return result;
2486}
2487
168c9250 2488/* Return a new dwarf2_per_cu_data allocated on the per-bfd
45940949 2489 obstack, and constructed with the specified field values. */
4b514bc8
JK
2490
2491static dwarf2_per_cu_data *
168c9250
SM
2492create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2493 struct dwarf2_section_info *section,
2494 int is_dwz,
2495 sect_offset sect_off, ULONGEST length)
4b514bc8 2496{
168c9250 2497 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
4b514bc8
JK
2498 the_cu->sect_off = sect_off;
2499 the_cu->length = length;
4b514bc8 2500 the_cu->section = section;
168c9250 2501 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 2502 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2503 the_cu->is_dwz = is_dwz;
2504 return the_cu;
2505}
2506
2ec9a5e0
TT
2507/* A helper for create_cus_from_index that handles a given list of
2508 CUs. */
2fdf6df6 2509
74a0d9f6 2510static void
168c9250 2511create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2512 const gdb_byte *cu_list, offset_type n_elements,
2513 struct dwarf2_section_info *section,
b76e467d 2514 int is_dwz)
9291a0cd 2515{
12359b5e 2516 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2517 {
74a0d9f6 2518 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2519
2520 sect_offset sect_off
2521 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2522 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2523 cu_list += 2 * 8;
2524
b76e467d 2525 dwarf2_per_cu_data *per_cu
168c9250
SM
2526 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2527 length);
2528 per_bfd->all_comp_units.push_back (per_cu);
9291a0cd 2529 }
9291a0cd
TT
2530}
2531
2ec9a5e0 2532/* Read the CU list from the mapped index, and use it to create all
168c9250 2533 the CU objects for PER_BFD. */
2ec9a5e0 2534
74a0d9f6 2535static void
168c9250 2536create_cus_from_index (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2537 const gdb_byte *cu_list, offset_type cu_list_elements,
2538 const gdb_byte *dwz_list, offset_type dwz_elements)
2539{
168c9250
SM
2540 gdb_assert (per_bfd->all_comp_units.empty ());
2541 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2542
168c9250
SM
2543 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2544 &per_bfd->info, 0);
2ec9a5e0
TT
2545
2546 if (dwz_elements == 0)
74a0d9f6 2547 return;
2ec9a5e0 2548
168c9250
SM
2549 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2550 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
b76e467d 2551 &dwz->info, 1);
2ec9a5e0
TT
2552}
2553
1fd400ff 2554/* Create the signatured type hash table from the index. */
673bfd45 2555
74a0d9f6 2556static void
12359b5e 2557create_signatured_type_table_from_index
168c9250
SM
2558 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2559 const gdb_byte *bytes, offset_type elements)
1fd400ff 2560{
168c9250
SM
2561 gdb_assert (per_bfd->all_type_units.empty ());
2562 per_bfd->all_type_units.reserve (elements / 3);
1fd400ff 2563
298e9637 2564 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2565
12359b5e 2566 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2567 {
52dc124a 2568 struct signatured_type *sig_type;
9c541725 2569 ULONGEST signature;
1fd400ff 2570 void **slot;
9c541725 2571 cu_offset type_offset_in_tu;
1fd400ff 2572
74a0d9f6 2573 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2574 sect_offset sect_off
2575 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2576 type_offset_in_tu
2577 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2578 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2579 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2580 bytes += 3 * 8;
2581
168c9250 2582 sig_type = per_bfd->allocate_signatured_type ();
52dc124a 2583 sig_type->signature = signature;
9c541725 2584 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2585 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2586 sig_type->per_cu.section = section;
9c541725 2587 sig_type->per_cu.sect_off = sect_off;
52dc124a 2588 sig_type->per_cu.v.quick
168c9250 2589 = OBSTACK_ZALLOC (&per_bfd->obstack,
1fd400ff
TT
2590 struct dwarf2_per_cu_quick_data);
2591
b0b6a987 2592 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2593 *slot = sig_type;
1fd400ff 2594
168c9250 2595 per_bfd->all_type_units.push_back (sig_type);
1fd400ff
TT
2596 }
2597
168c9250 2598 per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2599}
2600
927aa2e7
JK
2601/* Create the signatured type hash table from .debug_names. */
2602
2603static void
2604create_signatured_type_table_from_debug_names
976ca316 2605 (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2606 const mapped_debug_names &map,
2607 struct dwarf2_section_info *section,
2608 struct dwarf2_section_info *abbrev_section)
2609{
976ca316 2610 struct objfile *objfile = per_objfile->objfile;
ed2dc618 2611
96b79293
TT
2612 section->read (objfile);
2613 abbrev_section->read (objfile);
927aa2e7 2614
976ca316
SM
2615 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2616 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
927aa2e7 2617
298e9637 2618 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2619
2620 for (uint32_t i = 0; i < map.tu_count; ++i)
2621 {
2622 struct signatured_type *sig_type;
927aa2e7 2623 void **slot;
927aa2e7
JK
2624
2625 sect_offset sect_off
2626 = (sect_offset) (extract_unsigned_integer
2627 (map.tu_table_reordered + i * map.offset_size,
2628 map.offset_size,
2629 map.dwarf5_byte_order));
2630
2631 comp_unit_head cu_header;
976ca316 2632 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618 2633 abbrev_section,
927aa2e7
JK
2634 section->buffer + to_underlying (sect_off),
2635 rcuh_kind::TYPE);
2636
976ca316 2637 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2638 sig_type->signature = cu_header.signature;
2639 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2640 sig_type->per_cu.is_debug_types = 1;
2641 sig_type->per_cu.section = section;
2642 sig_type->per_cu.sect_off = sect_off;
927aa2e7 2643 sig_type->per_cu.v.quick
976ca316 2644 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
927aa2e7
JK
2645 struct dwarf2_per_cu_quick_data);
2646
b0b6a987 2647 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2648 *slot = sig_type;
2649
976ca316 2650 per_objfile->per_bfd->all_type_units.push_back (sig_type);
927aa2e7
JK
2651 }
2652
976ca316 2653 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2654}
2655
9291a0cd
TT
2656/* Read the address map data from the mapped index, and use it to
2657 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2658
9291a0cd 2659static void
976ca316 2660create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
ed2dc618 2661 struct mapped_index *index)
9291a0cd 2662{
976ca316 2663 struct objfile *objfile = per_objfile->objfile;
08feed99 2664 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2665 const gdb_byte *iter, *end;
9291a0cd 2666 struct addrmap *mutable_map;
9291a0cd
TT
2667 CORE_ADDR baseaddr;
2668
8268c778
PA
2669 auto_obstack temp_obstack;
2670
9291a0cd
TT
2671 mutable_map = addrmap_create_mutable (&temp_obstack);
2672
f00a2de2
PA
2673 iter = index->address_table.data ();
2674 end = iter + index->address_table.size ();
9291a0cd 2675
b3b3bada 2676 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2677
2678 while (iter < end)
2679 {
2680 ULONGEST hi, lo, cu_index;
2681 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2682 iter += 8;
2683 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2684 iter += 8;
2685 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2686 iter += 4;
f652bce2 2687
24a55014 2688 if (lo > hi)
f652bce2 2689 {
b98664d3 2690 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2691 hex_string (lo), hex_string (hi));
24a55014 2692 continue;
f652bce2 2693 }
24a55014 2694
976ca316 2695 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
f652bce2 2696 {
b98664d3 2697 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2698 (unsigned) cu_index);
24a55014 2699 continue;
f652bce2 2700 }
24a55014 2701
79748972
TT
2702 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2703 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2704 addrmap_set_empty (mutable_map, lo, hi - 1,
976ca316 2705 per_objfile->per_bfd->get_cu (cu_index));
9291a0cd
TT
2706 }
2707
d320c2b5 2708 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2709 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2710}
2711
927aa2e7
JK
2712/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2713 populate the objfile's psymtabs_addrmap. */
2714
2715static void
976ca316 2716create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2717 struct dwarf2_section_info *section)
2718{
976ca316 2719 struct objfile *objfile = per_objfile->objfile;
927aa2e7 2720 bfd *abfd = objfile->obfd;
08feed99 2721 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2722 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2723
2724 auto_obstack temp_obstack;
2725 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2726
2727 std::unordered_map<sect_offset,
2728 dwarf2_per_cu_data *,
2729 gdb::hash_enum<sect_offset>>
2730 debug_info_offset_to_per_cu;
976ca316 2731 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 2732 {
927aa2e7
JK
2733 const auto insertpair
2734 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2735 if (!insertpair.second)
2736 {
2737 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2738 "debug_info_offset %s, ignoring .debug_aranges."),
2739 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2740 return;
2741 }
2742 }
2743
96b79293 2744 section->read (objfile);
927aa2e7
JK
2745
2746 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2747
2748 const gdb_byte *addr = section->buffer;
2749
2750 while (addr < section->buffer + section->size)
2751 {
2752 const gdb_byte *const entry_addr = addr;
2753 unsigned int bytes_read;
2754
2755 const LONGEST entry_length = read_initial_length (abfd, addr,
2756 &bytes_read);
2757 addr += bytes_read;
2758
2759 const gdb_byte *const entry_end = addr + entry_length;
2760 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2761 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2762 if (addr + entry_length > section->buffer + section->size)
2763 {
47e3f474 2764 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2765 "length %s exceeds section length %s, "
2766 "ignoring .debug_aranges."),
47e3f474
TV
2767 objfile_name (objfile),
2768 plongest (entry_addr - section->buffer),
927aa2e7
JK
2769 plongest (bytes_read + entry_length),
2770 pulongest (section->size));
2771 return;
2772 }
2773
2774 /* The version number. */
2775 const uint16_t version = read_2_bytes (abfd, addr);
2776 addr += 2;
2777 if (version != 2)
2778 {
47e3f474 2779 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2780 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2781 objfile_name (objfile),
2782 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2783 return;
2784 }
2785
2786 const uint64_t debug_info_offset
2787 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2788 addr += offset_size;
2789 const auto per_cu_it
2790 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2791 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2792 {
47e3f474 2793 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2794 "debug_info_offset %s does not exists, "
2795 "ignoring .debug_aranges."),
47e3f474
TV
2796 objfile_name (objfile),
2797 plongest (entry_addr - section->buffer),
927aa2e7
JK
2798 pulongest (debug_info_offset));
2799 return;
2800 }
2801 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2802
2803 const uint8_t address_size = *addr++;
2804 if (address_size < 1 || address_size > 8)
2805 {
47e3f474 2806 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2807 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2808 objfile_name (objfile),
2809 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2810 return;
2811 }
2812
2813 const uint8_t segment_selector_size = *addr++;
2814 if (segment_selector_size != 0)
2815 {
47e3f474 2816 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2817 "segment_selector_size %u is not supported, "
2818 "ignoring .debug_aranges."),
47e3f474
TV
2819 objfile_name (objfile),
2820 plongest (entry_addr - section->buffer),
927aa2e7
JK
2821 segment_selector_size);
2822 return;
2823 }
2824
2825 /* Must pad to an alignment boundary that is twice the address
2826 size. It is undocumented by the DWARF standard but GCC does
2827 use it. */
2828 for (size_t padding = ((-(addr - section->buffer))
2829 & (2 * address_size - 1));
2830 padding > 0; padding--)
2831 if (*addr++ != 0)
2832 {
47e3f474 2833 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2834 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2835 objfile_name (objfile),
2836 plongest (entry_addr - section->buffer));
927aa2e7
JK
2837 return;
2838 }
2839
2840 for (;;)
2841 {
2842 if (addr + 2 * address_size > entry_end)
2843 {
47e3f474 2844 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2845 "address list is not properly terminated, "
2846 "ignoring .debug_aranges."),
47e3f474
TV
2847 objfile_name (objfile),
2848 plongest (entry_addr - section->buffer));
927aa2e7
JK
2849 return;
2850 }
2851 ULONGEST start = extract_unsigned_integer (addr, address_size,
2852 dwarf5_byte_order);
2853 addr += address_size;
2854 ULONGEST length = extract_unsigned_integer (addr, address_size,
2855 dwarf5_byte_order);
2856 addr += address_size;
2857 if (start == 0 && length == 0)
2858 break;
976ca316 2859 if (start == 0 && !per_objfile->per_bfd->has_section_at_zero)
927aa2e7
JK
2860 {
2861 /* Symbol was eliminated due to a COMDAT group. */
2862 continue;
2863 }
2864 ULONGEST end = start + length;
79748972
TT
2865 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2866 - baseaddr);
2867 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2868 - baseaddr);
927aa2e7
JK
2869 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2870 }
2871 }
2872
d320c2b5 2873 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2874 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2875}
2876
9291a0cd
TT
2877/* Find a slot in the mapped index INDEX for the object named NAME.
2878 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2879 constant pool and return true. If NAME cannot be found, return
2880 false. */
2fdf6df6 2881
109483d9 2882static bool
9291a0cd
TT
2883find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2884 offset_type **vec_out)
2885{
0cf03b49 2886 offset_type hash;
9291a0cd 2887 offset_type slot, step;
559a7a62 2888 int (*cmp) (const char *, const char *);
9291a0cd 2889
791afaa2 2890 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2891 if (current_language->la_language == language_cplus
45280282
IB
2892 || current_language->la_language == language_fortran
2893 || current_language->la_language == language_d)
0cf03b49
JK
2894 {
2895 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2896 not contain any. */
a8719064 2897
72998fb3 2898 if (strchr (name, '(') != NULL)
0cf03b49 2899 {
109483d9 2900 without_params = cp_remove_params (name);
0cf03b49 2901
72998fb3 2902 if (without_params != NULL)
791afaa2 2903 name = without_params.get ();
0cf03b49
JK
2904 }
2905 }
2906
559a7a62 2907 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2908 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2909 simulate our NAME being searched is also lowercased. */
2910 hash = mapped_index_string_hash ((index->version == 4
2911 && case_sensitivity == case_sensitive_off
2912 ? 5 : index->version),
2913 name);
2914
f00a2de2
PA
2915 slot = hash & (index->symbol_table.size () - 1);
2916 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2917 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2918
2919 for (;;)
2920 {
9291a0cd 2921 const char *str;
f00a2de2
PA
2922
2923 const auto &bucket = index->symbol_table[slot];
2924 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2925 return false;
9291a0cd 2926
f00a2de2 2927 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2928 if (!cmp (name, str))
9291a0cd
TT
2929 {
2930 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2931 + MAYBE_SWAP (bucket.vec));
109483d9 2932 return true;
9291a0cd
TT
2933 }
2934
f00a2de2 2935 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2936 }
2937}
2938
4485a1c1
SM
2939/* A helper function that reads the .gdb_index from BUFFER and fills
2940 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2941 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2942 ok to use deprecated sections.
2943
2944 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2945 out parameters that are filled in with information about the CU and
2946 TU lists in the section.
2947
4485a1c1 2948 Returns true if all went well, false otherwise. */
2fdf6df6 2949
d33bc52e 2950static bool
3810f182 2951read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2952 bool deprecated_ok,
2953 gdb::array_view<const gdb_byte> buffer,
2954 struct mapped_index *map,
2955 const gdb_byte **cu_list,
2956 offset_type *cu_list_elements,
2957 const gdb_byte **types_list,
2958 offset_type *types_list_elements)
2959{
2960 const gdb_byte *addr = &buffer[0];
82430852 2961
9291a0cd 2962 /* Version check. */
4485a1c1 2963 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2964 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2965 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2966 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2967 indices. */
831adc1f 2968 if (version < 4)
481860b3
GB
2969 {
2970 static int warning_printed = 0;
2971 if (!warning_printed)
2972 {
2973 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2974 filename);
481860b3
GB
2975 warning_printed = 1;
2976 }
2977 return 0;
2978 }
2979 /* Index version 4 uses a different hash function than index version
2980 5 and later.
2981
2982 Versions earlier than 6 did not emit psymbols for inlined
2983 functions. Using these files will cause GDB not to be able to
2984 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2985 indices unless the user has done
2986 "set use-deprecated-index-sections on". */
2ec9a5e0 2987 if (version < 6 && !deprecated_ok)
481860b3
GB
2988 {
2989 static int warning_printed = 0;
2990 if (!warning_printed)
2991 {
e615022a
DE
2992 warning (_("\
2993Skipping deprecated .gdb_index section in %s.\n\
2994Do \"set use-deprecated-index-sections on\" before the file is read\n\
2995to use the section anyway."),
2ec9a5e0 2996 filename);
481860b3
GB
2997 warning_printed = 1;
2998 }
2999 return 0;
3000 }
796a7ff8 3001 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3002 of the TU (for symbols coming from TUs),
3003 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3004 Plus gold-generated indices can have duplicate entries for global symbols,
3005 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3006 These are just performance bugs, and we can't distinguish gdb-generated
3007 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3008
481860b3 3009 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3010 longer backward compatible. */
796a7ff8 3011 if (version > 8)
594e8718 3012 return 0;
9291a0cd 3013
559a7a62 3014 map->version = version;
9291a0cd 3015
4485a1c1 3016 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3017
4485a1c1 3018 int i = 0;
2ec9a5e0
TT
3019 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3020 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3021 / 8);
1fd400ff
TT
3022 ++i;
3023
2ec9a5e0
TT
3024 *types_list = addr + MAYBE_SWAP (metadata[i]);
3025 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3026 - MAYBE_SWAP (metadata[i]))
3027 / 8);
987d643c 3028 ++i;
1fd400ff 3029
f00a2de2
PA
3030 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3031 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3032 map->address_table
3033 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3034 ++i;
3035
f00a2de2
PA
3036 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3037 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3038 map->symbol_table
3039 = gdb::array_view<mapped_index::symbol_table_slot>
3040 ((mapped_index::symbol_table_slot *) symbol_table,
3041 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3042
f00a2de2 3043 ++i;
f9d83a0b 3044 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3045
2ec9a5e0
TT
3046 return 1;
3047}
3048
4485a1c1
SM
3049/* Callback types for dwarf2_read_gdb_index. */
3050
3051typedef gdb::function_view
5989a64e 3052 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
3053 get_gdb_index_contents_ftype;
3054typedef gdb::function_view
3055 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3056 get_gdb_index_contents_dwz_ftype;
3057
927aa2e7 3058/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3059 elements of all the CUs and return 1. Otherwise, return 0. */
3060
3061static int
4485a1c1 3062dwarf2_read_gdb_index
976ca316 3063 (dwarf2_per_objfile *per_objfile,
4485a1c1
SM
3064 get_gdb_index_contents_ftype get_gdb_index_contents,
3065 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3066{
2ec9a5e0
TT
3067 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3068 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3069 struct dwz_file *dwz;
976ca316 3070 struct objfile *objfile = per_objfile->objfile;
f8c41851 3071 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2ec9a5e0 3072
4485a1c1 3073 gdb::array_view<const gdb_byte> main_index_contents
f8c41851 3074 = get_gdb_index_contents (objfile, per_bfd);
4485a1c1
SM
3075
3076 if (main_index_contents.empty ())
3077 return 0;
3078
3063847f 3079 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3080 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3081 use_deprecated_index_sections,
3082 main_index_contents, map.get (), &cu_list,
3083 &cu_list_elements, &types_list,
3084 &types_list_elements))
2ec9a5e0
TT
3085 return 0;
3086
0fefef59 3087 /* Don't use the index if it's empty. */
3063847f 3088 if (map->symbol_table.empty ())
0fefef59
DE
3089 return 0;
3090
2ec9a5e0
TT
3091 /* If there is a .dwz file, read it so we can get its CU list as
3092 well. */
f8c41851 3093 dwz = dwarf2_get_dwz_file (per_bfd);
4db1a1dc 3094 if (dwz != NULL)
2ec9a5e0 3095 {
2ec9a5e0
TT
3096 struct mapped_index dwz_map;
3097 const gdb_byte *dwz_types_ignore;
3098 offset_type dwz_types_elements_ignore;
3099
4485a1c1
SM
3100 gdb::array_view<const gdb_byte> dwz_index_content
3101 = get_gdb_index_contents_dwz (objfile, dwz);
3102
3103 if (dwz_index_content.empty ())
3104 return 0;
3105
3810f182 3106 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3107 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3108 &dwz_list, &dwz_list_elements,
3109 &dwz_types_ignore,
3110 &dwz_types_elements_ignore))
2ec9a5e0
TT
3111 {
3112 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3113 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3114 return 0;
3115 }
3116 }
3117
f8c41851
SM
3118 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3119 dwz_list_elements);
1fd400ff 3120
8b70b953
TT
3121 if (types_list_elements)
3122 {
8b70b953
TT
3123 /* We can only handle a single .debug_types when we have an
3124 index. */
f8c41851 3125 if (per_bfd->types.size () != 1)
8b70b953
TT
3126 return 0;
3127
f8c41851 3128 dwarf2_section_info *section = &per_bfd->types[0];
8b70b953 3129
f8c41851 3130 create_signatured_type_table_from_index (per_bfd, section, types_list,
168c9250 3131 types_list_elements);
8b70b953 3132 }
9291a0cd 3133
976ca316 3134 create_addrmap_from_index (per_objfile, map.get ());
9291a0cd 3135
f8c41851
SM
3136 per_bfd->index_table = std::move (map);
3137 per_bfd->using_index = 1;
3138 per_bfd->quick_file_names_table =
3139 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3140
3141 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
3142 objfiles using the same BFD. */
3143 gdb_assert (per_bfd->partial_symtabs == nullptr);
3144 per_bfd->partial_symtabs = objfile->partial_symtabs;
9291a0cd
TT
3145
3146 return 1;
3147}
3148
dee91e82 3149/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3150
dee91e82
DE
3151static void
3152dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3153 const gdb_byte *info_ptr,
3e225074 3154 struct die_info *comp_unit_die)
9291a0cd 3155{
dee91e82 3156 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3157 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
976ca316 3158 dwarf2_per_objfile *per_objfile = cu->per_objfile;
094b34ac 3159 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3160 struct attribute *attr;
7b9f3c50
DE
3161 void **slot;
3162 struct quick_file_names *qfn;
9291a0cd 3163
0186c6a7
DE
3164 gdb_assert (! this_cu->is_debug_types);
3165
07261596
TT
3166 /* Our callers never want to match partial units -- instead they
3167 will match the enclosing full CU. */
3168 if (comp_unit_die->tag == DW_TAG_partial_unit)
3169 {
3170 this_cu->v.quick->no_file_data = 1;
3171 return;
3172 }
3173
0186c6a7 3174 lh_cu = this_cu;
7b9f3c50 3175 slot = NULL;
dee91e82 3176
fff8551c 3177 line_header_up lh;
9c541725 3178 sect_offset line_offset {};
fff8551c 3179
dee91e82 3180 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3181 if (attr != nullptr)
9291a0cd 3182 {
7b9f3c50
DE
3183 struct quick_file_names find_entry;
3184
9c541725 3185 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3186
3187 /* We may have already read in this line header (TU line header sharing).
3188 If we have we're done. */
094b34ac 3189 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3190 find_entry.hash.line_sect_off = line_offset;
976ca316 3191 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3192 &find_entry, INSERT);
3193 if (*slot != NULL)
3194 {
9a3c8263 3195 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3196 return;
7b9f3c50
DE
3197 }
3198
3019eac3 3199 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3200 }
3201 if (lh == NULL)
3202 {
094b34ac 3203 lh_cu->v.quick->no_file_data = 1;
dee91e82 3204 return;
9291a0cd
TT
3205 }
3206
976ca316 3207 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3208 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3209 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3210 gdb_assert (slot != NULL);
3211 *slot = qfn;
9291a0cd 3212
d721ba37 3213 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3214
aa391654
TT
3215 int offset = 0;
3216 if (strcmp (fnd.name, "<unknown>") != 0)
3217 ++offset;
3218
7ba99d21 3219 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3220 qfn->file_names =
976ca316 3221 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
45940949 3222 qfn->num_file_names);
aa391654
TT
3223 if (offset != 0)
3224 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3225 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3226 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3227 fnd.comp_dir).release ();
7b9f3c50 3228 qfn->real_names = NULL;
9291a0cd 3229
094b34ac 3230 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3231}
3232
3233/* A helper for the "quick" functions which attempts to read the line
3234 table for THIS_CU. */
3235
3236static struct quick_file_names *
ab432490
SM
3237dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3238 dwarf2_per_objfile *per_objfile)
dee91e82 3239{
0186c6a7
DE
3240 /* This should never be called for TUs. */
3241 gdb_assert (! this_cu->is_debug_types);
3242 /* Nor type unit groups. */
197400e8 3243 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3244
dee91e82
DE
3245 if (this_cu->v.quick->file_names != NULL)
3246 return this_cu->v.quick->file_names;
3247 /* If we know there is no line data, no point in looking again. */
3248 if (this_cu->v.quick->no_file_data)
3249 return NULL;
3250
ab432490 3251 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3252 if (!reader.dummy_p)
3e225074 3253 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3254
3255 if (this_cu->v.quick->no_file_data)
3256 return NULL;
3257 return this_cu->v.quick->file_names;
9291a0cd
TT
3258}
3259
3260/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3261 real path for a given file name from the line table. */
2fdf6df6 3262
9291a0cd 3263static const char *
976ca316 3264dw2_get_real_path (dwarf2_per_objfile *per_objfile,
7b9f3c50 3265 struct quick_file_names *qfn, int index)
9291a0cd 3266{
7b9f3c50 3267 if (qfn->real_names == NULL)
976ca316 3268 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
26f2dc30 3269 qfn->num_file_names, const char *);
9291a0cd 3270
7b9f3c50 3271 if (qfn->real_names[index] == NULL)
14278e1f 3272 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3273
7b9f3c50 3274 return qfn->real_names[index];
9291a0cd
TT
3275}
3276
3277static struct symtab *
3278dw2_find_last_source_symtab (struct objfile *objfile)
3279{
976ca316
SM
3280 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3281 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3282 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
ae2de4f8 3283
43f3e411
DE
3284 if (cust == NULL)
3285 return NULL;
ed2dc618 3286
43f3e411 3287 return compunit_primary_filetab (cust);
9291a0cd
TT
3288}
3289
7b9f3c50
DE
3290/* Traversal function for dw2_forget_cached_source_info. */
3291
3292static int
3293dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3294{
7b9f3c50 3295 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3296
7b9f3c50 3297 if (file_data->real_names)
9291a0cd 3298 {
7b9f3c50 3299 int i;
9291a0cd 3300
7b9f3c50 3301 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3302 {
7b9f3c50
DE
3303 xfree ((void*) file_data->real_names[i]);
3304 file_data->real_names[i] = NULL;
9291a0cd
TT
3305 }
3306 }
7b9f3c50
DE
3307
3308 return 1;
3309}
3310
3311static void
3312dw2_forget_cached_source_info (struct objfile *objfile)
3313{
976ca316 3314 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
7b9f3c50 3315
976ca316 3316 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3317 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3318}
3319
f8eba3c6
TT
3320/* Helper function for dw2_map_symtabs_matching_filename that expands
3321 the symtabs and calls the iterator. */
3322
3323static int
3324dw2_map_expand_apply (struct objfile *objfile,
3325 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3326 const char *name, const char *real_path,
14bc53a8 3327 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3328{
43f3e411 3329 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3330
3331 /* Don't visit already-expanded CUs. */
af758d11
SM
3332 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3333 if (per_objfile->symtab_set_p (per_cu))
f8eba3c6
TT
3334 return 0;
3335
3336 /* This may expand more than one symtab, and we want to iterate over
3337 all of them. */
97a1449a 3338 dw2_instantiate_symtab (per_cu, per_objfile, false);
f8eba3c6 3339
14bc53a8
PA
3340 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3341 last_made, callback);
f8eba3c6
TT
3342}
3343
3344/* Implementation of the map_symtabs_matching_filename method. */
3345
14bc53a8
PA
3346static bool
3347dw2_map_symtabs_matching_filename
3348 (struct objfile *objfile, const char *name, const char *real_path,
3349 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3350{
c011a4f4 3351 const char *name_basename = lbasename (name);
976ca316 3352 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 3353
848e3e78
DE
3354 /* The rule is CUs specify all the files, including those used by
3355 any TU, so there's no need to scan TUs here. */
f4dc4d17 3356
976ca316 3357 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3358 {
3d7bb9d9 3359 /* We only need to look at symtabs not already expanded. */
976ca316 3360 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3361 continue;
3362
976ca316 3363 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3364 if (file_data == NULL)
9291a0cd
TT
3365 continue;
3366
b76e467d 3367 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3368 {
7b9f3c50 3369 const char *this_name = file_data->file_names[j];
da235a7c 3370 const char *this_real_name;
9291a0cd 3371
af529f8f 3372 if (compare_filenames_for_search (this_name, name))
9291a0cd 3373 {
f5b95b50 3374 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3375 callback))
3376 return true;
288e77a7 3377 continue;
4aac40c8 3378 }
9291a0cd 3379
c011a4f4
DE
3380 /* Before we invoke realpath, which can get expensive when many
3381 files are involved, do a quick comparison of the basenames. */
3382 if (! basenames_may_differ
3383 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3384 continue;
3385
976ca316 3386 this_real_name = dw2_get_real_path (per_objfile, 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 3418 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
976ca316 3419 dwarf2_per_objfile *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
e5f3ece2 3439/* Initialize the index symtab iterator ITER, common part. */
2fdf6df6 3440
9291a0cd 3441static void
e5f3ece2
TV
3442dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3443 dwarf2_per_objfile *per_objfile,
3444 gdb::optional<block_enum> block_index,
3445 domain_enum domain)
da51c347 3446{
976ca316 3447 iter->per_objfile = per_objfile;
da51c347
DE
3448 iter->block_index = block_index;
3449 iter->domain = domain;
3450 iter->next = 0;
8943b874 3451 iter->global_seen = 0;
e5f3ece2
TV
3452 iter->vec = NULL;
3453 iter->length = 0;
3454}
da51c347 3455
e5f3ece2
TV
3456/* Initialize the index symtab iterator ITER, const char *NAME variant. */
3457
3458static void
3459dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3460 dwarf2_per_objfile *per_objfile,
3461 gdb::optional<block_enum> block_index,
3462 domain_enum domain,
3463 const char *name)
3464{
3465 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
ed2dc618 3466
e5f3ece2 3467 mapped_index *index = per_objfile->per_bfd->index_table.get ();
ed2dc618 3468 /* index is NULL if OBJF_READNOW. */
e5f3ece2
TV
3469 if (index == NULL)
3470 return;
3471
3472 if (find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347 3473 iter->length = MAYBE_SWAP (*iter->vec);
e5f3ece2
TV
3474}
3475
3476/* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3477
3478static void
3479dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3480 dwarf2_per_objfile *per_objfile,
3481 gdb::optional<block_enum> block_index,
3482 domain_enum domain, offset_type namei)
3483{
3484 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3485
3486 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3487 /* index is NULL if OBJF_READNOW. */
3488 if (index == NULL)
3489 return;
3490
3491 gdb_assert (!index->symbol_name_slot_invalid (namei));
3492 const auto &bucket = index->symbol_table[namei];
3493
3494 iter->vec = (offset_type *) (index->constant_pool
3495 + MAYBE_SWAP (bucket.vec));
3496 iter->length = MAYBE_SWAP (*iter->vec);
da51c347
DE
3497}
3498
3499/* Return the next matching CU or NULL if there are no more. */
3500
3501static struct dwarf2_per_cu_data *
3502dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3503{
976ca316 3504 dwarf2_per_objfile *per_objfile = iter->per_objfile;
ed2dc618 3505
da51c347
DE
3506 for ( ; iter->next < iter->length; ++iter->next)
3507 {
3508 offset_type cu_index_and_attrs =
3509 MAYBE_SWAP (iter->vec[iter->next + 1]);
3510 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3511 gdb_index_symbol_kind symbol_kind =
3512 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3513 /* Only check the symbol attributes if they're present.
3514 Indices prior to version 7 don't record them,
3515 and indices >= 7 may elide them for certain symbols
3516 (gold does this). */
3517 int attrs_valid =
976ca316 3518 (per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3519 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3520
3190f0c6 3521 /* Don't crash on bad data. */
976ca316
SM
3522 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3523 + per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3524 {
b98664d3 3525 complaint (_(".gdb_index entry has bad CU index"
976ca316 3526 " [in module %s]"), objfile_name (per_objfile->objfile));
3190f0c6
DE
3527 continue;
3528 }
3529
976ca316 3530 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3531
da51c347 3532 /* Skip if already read in. */
976ca316 3533 if (per_objfile->symtab_set_p (per_cu))
da51c347
DE
3534 continue;
3535
8943b874
DE
3536 /* Check static vs global. */
3537 if (attrs_valid)
3538 {
2b79f376
SM
3539 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3540
3541 if (iter->block_index.has_value ())
3542 {
3543 bool want_static = *iter->block_index == STATIC_BLOCK;
3544
3545 if (is_static != want_static)
3546 continue;
3547 }
3548
8943b874 3549 /* Work around gold/15646. */
f030440d
TV
3550 if (!is_static
3551 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3552 {
3553 if (iter->global_seen)
3554 continue;
3555
3556 iter->global_seen = 1;
3557 }
8943b874 3558 }
da51c347
DE
3559
3560 /* Only check the symbol's kind if it has one. */
3561 if (attrs_valid)
3562 {
3563 switch (iter->domain)
3564 {
3565 case VAR_DOMAIN:
3566 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3567 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3568 /* Some types are also in VAR_DOMAIN. */
3569 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3570 continue;
3571 break;
3572 case STRUCT_DOMAIN:
3573 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3574 continue;
3575 break;
3576 case LABEL_DOMAIN:
3577 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3578 continue;
3579 break;
59c35742
AB
3580 case MODULE_DOMAIN:
3581 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3582 continue;
3583 break;
da51c347
DE
3584 default:
3585 break;
3586 }
3587 }
3588
3589 ++iter->next;
3590 return per_cu;
3591 }
3592
3593 return NULL;
3594}
3595
43f3e411 3596static struct compunit_symtab *
c7f839cb 3597dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3598 const char *name, domain_enum domain)
9291a0cd 3599{
43f3e411 3600 struct compunit_symtab *stab_best = NULL;
976ca316 3601 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 3602
b5ec771e
PA
3603 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3604
ed2dc618
SM
3605 struct dw2_symtab_iterator iter;
3606 struct dwarf2_per_cu_data *per_cu;
da51c347 3607
976ca316 3608 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
9291a0cd 3609
ed2dc618
SM
3610 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3611 {
3612 struct symbol *sym, *with_opaque = NULL;
97a1449a 3613 struct compunit_symtab *stab
976ca316 3614 = dw2_instantiate_symtab (per_cu, per_objfile, false);
ed2dc618 3615 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3616 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3617
ed2dc618
SM
3618 sym = block_find_symbol (block, name, domain,
3619 block_find_non_opaque_type_preferred,
3620 &with_opaque);
b2e2f908 3621
ed2dc618
SM
3622 /* Some caution must be observed with overloaded functions
3623 and methods, since the index will not contain any overload
3624 information (but NAME might contain it). */
da51c347 3625
ed2dc618
SM
3626 if (sym != NULL
3627 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3628 return stab;
3629 if (with_opaque != NULL
3630 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3631 stab_best = stab;
da51c347 3632
ed2dc618 3633 /* Keep looking through other CUs. */
9291a0cd 3634 }
9291a0cd 3635
da51c347 3636 return stab_best;
9291a0cd
TT
3637}
3638
3639static void
3640dw2_print_stats (struct objfile *objfile)
3641{
976ca316
SM
3642 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3643 int total = (per_objfile->per_bfd->all_comp_units.size ()
3644 + per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3645 int count = 0;
9291a0cd 3646
ed2dc618 3647 for (int i = 0; i < total; ++i)
9291a0cd 3648 {
976ca316 3649 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3650
976ca316 3651 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3652 ++count;
3653 }
e4a48d9d 3654 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3655 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3656}
3657
779bd270
DE
3658/* This dumps minimal information about the index.
3659 It is called via "mt print objfiles".
3660 One use is to verify .gdb_index has been loaded by the
3661 gdb.dwarf2/gdb-index.exp testcase. */
3662
9291a0cd
TT
3663static void
3664dw2_dump (struct objfile *objfile)
3665{
976ca316 3666 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 3667
976ca316 3668 gdb_assert (per_objfile->per_bfd->using_index);
779bd270 3669 printf_filtered (".gdb_index:");
976ca316 3670 if (per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3671 {
3672 printf_filtered (" version %d\n",
976ca316 3673 per_objfile->per_bfd->index_table->version);
779bd270
DE
3674 }
3675 else
3676 printf_filtered (" faked for \"readnow\"\n");
3677 printf_filtered ("\n");
9291a0cd
TT
3678}
3679
9291a0cd
TT
3680static void
3681dw2_expand_symtabs_for_function (struct objfile *objfile,
3682 const char *func_name)
3683{
976ca316 3684 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
da51c347 3685
ed2dc618
SM
3686 struct dw2_symtab_iterator iter;
3687 struct dwarf2_per_cu_data *per_cu;
da51c347 3688
976ca316 3689 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3690
ed2dc618 3691 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3692 dw2_instantiate_symtab (per_cu, per_objfile, false);
da51c347 3693
9291a0cd
TT
3694}
3695
3696static void
3697dw2_expand_all_symtabs (struct objfile *objfile)
3698{
976ca316
SM
3699 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3700 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3701 + per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3702
ed2dc618 3703 for (int i = 0; i < total_units; ++i)
9291a0cd 3704 {
976ca316 3705 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3706
58f0c718
TT
3707 /* We don't want to directly expand a partial CU, because if we
3708 read it with the wrong language, then assertion failures can
3709 be triggered later on. See PR symtab/23010. So, tell
3710 dw2_instantiate_symtab to skip partial CUs -- any important
3711 partial CU will be read via DW_TAG_imported_unit anyway. */
976ca316 3712 dw2_instantiate_symtab (per_cu, per_objfile, true);
9291a0cd
TT
3713 }
3714}
3715
3716static void
652a8996
JK
3717dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3718 const char *fullname)
9291a0cd 3719{
976ca316 3720 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3721
3722 /* We don't need to consider type units here.
3723 This is only called for examining code, e.g. expand_line_sal.
3724 There can be an order of magnitude (or more) more type units
3725 than comp units, and we avoid them if we can. */
3726
976ca316 3727 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3728 {
3d7bb9d9 3729 /* We only need to look at symtabs not already expanded. */
976ca316 3730 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3731 continue;
3732
976ca316 3733 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3734 if (file_data == NULL)
9291a0cd
TT
3735 continue;
3736
b76e467d 3737 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3738 {
652a8996
JK
3739 const char *this_fullname = file_data->file_names[j];
3740
3741 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3742 {
976ca316 3743 dw2_instantiate_symtab (per_cu, per_objfile, false);
9291a0cd
TT
3744 break;
3745 }
3746 }
3747 }
3748}
3749
9a0bacfb
TV
3750static void
3751dw2_expand_symtabs_matching_symbol
3752 (mapped_index_base &index,
3753 const lookup_name_info &lookup_name_in,
3754 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3755 enum search_domain kind,
fcf23d5b
SM
3756 gdb::function_view<bool (offset_type)> match_callback,
3757 dwarf2_per_objfile *per_objfile);
9a0bacfb
TV
3758
3759static void
3760dw2_expand_symtabs_matching_one
97a1449a
SM
3761 (dwarf2_per_cu_data *per_cu,
3762 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3763 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3764 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3765
9291a0cd 3766static void
199b4314
TT
3767dw2_map_matching_symbols
3768 (struct objfile *objfile,
b054970d 3769 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3770 int global,
3771 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3772 symbol_compare_ftype *ordered_compare)
9291a0cd 3773{
1aa98955 3774 /* Used for Ada. */
976ca316 3775 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1aa98955 3776
9a0bacfb
TV
3777 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3778
976ca316 3779 if (per_objfile->per_bfd->index_table != nullptr)
1aa98955 3780 {
976ca316 3781 mapped_index &index = *per_objfile->per_bfd->index_table;
1aa98955 3782
9a0bacfb
TV
3783 const char *match_name = name.ada ().lookup_name ().c_str ();
3784 auto matcher = [&] (const char *symname)
3785 {
3786 if (ordered_compare == nullptr)
3787 return true;
3788 return ordered_compare (symname, match_name) == 0;
3789 };
3790
3791 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3792 [&] (offset_type namei)
3793 {
3794 struct dw2_symtab_iterator iter;
3795 struct dwarf2_per_cu_data *per_cu;
3796
976ca316 3797 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
e5f3ece2 3798 namei);
9a0bacfb 3799 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3800 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 3801 nullptr);
9a0bacfb 3802 return true;
976ca316 3803 }, per_objfile);
9a0bacfb
TV
3804 }
3805 else
3806 {
3807 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3808 proceed assuming all symtabs have been read in. */
3809 }
1aa98955
TV
3810
3811 for (compunit_symtab *cust : objfile->compunits ())
3812 {
3813 const struct block *block;
3814
3815 if (cust == NULL)
3816 continue;
3817 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3818 if (!iterate_over_symbols_terminated (block, name,
3819 domain, callback))
3820 return;
3821 }
9291a0cd
TT
3822}
3823
e1ef7d7a
PA
3824/* Starting from a search name, return the string that finds the upper
3825 bound of all strings that start with SEARCH_NAME in a sorted name
3826 list. Returns the empty string to indicate that the upper bound is
3827 the end of the list. */
3828
3829static std::string
3830make_sort_after_prefix_name (const char *search_name)
3831{
3832 /* When looking to complete "func", we find the upper bound of all
3833 symbols that start with "func" by looking for where we'd insert
3834 the closest string that would follow "func" in lexicographical
3835 order. Usually, that's "func"-with-last-character-incremented,
3836 i.e. "fund". Mind non-ASCII characters, though. Usually those
3837 will be UTF-8 multi-byte sequences, but we can't be certain.
3838 Especially mind the 0xff character, which is a valid character in
3839 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3840 rule out compilers allowing it in identifiers. Note that
3841 conveniently, strcmp/strcasecmp are specified to compare
3842 characters interpreted as unsigned char. So what we do is treat
3843 the whole string as a base 256 number composed of a sequence of
3844 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3845 to 0, and carries 1 to the following more-significant position.
3846 If the very first character in SEARCH_NAME ends up incremented
3847 and carries/overflows, then the upper bound is the end of the
3848 list. The string after the empty string is also the empty
3849 string.
3850
3851 Some examples of this operation:
3852
3853 SEARCH_NAME => "+1" RESULT
3854
3855 "abc" => "abd"
3856 "ab\xff" => "ac"
3857 "\xff" "a" "\xff" => "\xff" "b"
3858 "\xff" => ""
3859 "\xff\xff" => ""
3860 "" => ""
3861
3862 Then, with these symbols for example:
3863
3864 func
3865 func1
3866 fund
3867
3868 completing "func" looks for symbols between "func" and
3869 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3870 which finds "func" and "func1", but not "fund".
3871
3872 And with:
3873
3874 funcÿ (Latin1 'ÿ' [0xff])
3875 funcÿ1
3876 fund
3877
3878 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3879 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3880
3881 And with:
3882
3883 ÿÿ (Latin1 'ÿ' [0xff])
3884 ÿÿ1
3885
3886 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3887 the end of the list.
3888 */
3889 std::string after = search_name;
3890 while (!after.empty () && (unsigned char) after.back () == 0xff)
3891 after.pop_back ();
3892 if (!after.empty ())
3893 after.back () = (unsigned char) after.back () + 1;
3894 return after;
3895}
3896
5c58de74 3897/* See declaration. */
61d96d7e 3898
5c58de74
PA
3899std::pair<std::vector<name_component>::const_iterator,
3900 std::vector<name_component>::const_iterator>
44ed8f3e 3901mapped_index_base::find_name_components_bounds
fcf23d5b
SM
3902 (const lookup_name_info &lookup_name_without_params, language lang,
3903 dwarf2_per_objfile *per_objfile) const
3f563c84 3904{
5c58de74
PA
3905 auto *name_cmp
3906 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3907
3b00ef10 3908 const char *lang_name
e0802d59 3909 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3910
3f563c84
PA
3911 /* Comparison function object for lower_bound that matches against a
3912 given symbol name. */
3913 auto lookup_compare_lower = [&] (const name_component &elem,
3914 const char *name)
3915 {
fcf23d5b 3916 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3917 const char *elem_name = elem_qualified + elem.name_offset;
3918 return name_cmp (elem_name, name) < 0;
3919 };
3920
3921 /* Comparison function object for upper_bound that matches against a
3922 given symbol name. */
3923 auto lookup_compare_upper = [&] (const char *name,
3924 const name_component &elem)
3925 {
fcf23d5b 3926 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3927 const char *elem_name = elem_qualified + elem.name_offset;
3928 return name_cmp (name, elem_name) < 0;
3929 };
3930
5c58de74
PA
3931 auto begin = this->name_components.begin ();
3932 auto end = this->name_components.end ();
3f563c84
PA
3933
3934 /* Find the lower bound. */
3935 auto lower = [&] ()
3936 {
3b00ef10 3937 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3938 return begin;
3939 else
3b00ef10 3940 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3941 } ();
3942
3943 /* Find the upper bound. */
3944 auto upper = [&] ()
3945 {
5c58de74 3946 if (lookup_name_without_params.completion_mode ())
3f563c84 3947 {
e1ef7d7a
PA
3948 /* In completion mode, we want UPPER to point past all
3949 symbols names that have the same prefix. I.e., with
3950 these symbols, and completing "func":
3951
3952 function << lower bound
3953 function1
3954 other_function << upper bound
3955
3956 We find the upper bound by looking for the insertion
3957 point of "func"-with-last-character-incremented,
3958 i.e. "fund". */
3b00ef10 3959 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3960 if (after.empty ())
3f563c84 3961 return end;
e6b2f5ef
PA
3962 return std::lower_bound (lower, end, after.c_str (),
3963 lookup_compare_lower);
3f563c84
PA
3964 }
3965 else
3b00ef10 3966 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3967 } ();
3968
5c58de74
PA
3969 return {lower, upper};
3970}
3971
3972/* See declaration. */
3973
3974void
fcf23d5b 3975mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
5c58de74
PA
3976{
3977 if (!this->name_components.empty ())
3978 return;
3979
3980 this->name_components_casing = case_sensitivity;
3981 auto *name_cmp
3982 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3983
3984 /* The code below only knows how to break apart components of C++
3985 symbol names (and other languages that use '::' as
3b00ef10 3986 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3987 auto count = this->symbol_name_count ();
3988 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3989 {
44ed8f3e 3990 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3991 continue;
3992
fcf23d5b 3993 const char *name = this->symbol_name_at (idx, per_objfile);
5c58de74
PA
3994
3995 /* Add each name component to the name component table. */
3996 unsigned int previous_len = 0;
3b00ef10
TT
3997
3998 if (strstr (name, "::") != nullptr)
3999 {
4000 for (unsigned int current_len = cp_find_first_component (name);
4001 name[current_len] != '\0';
4002 current_len += cp_find_first_component (name + current_len))
4003 {
4004 gdb_assert (name[current_len] == ':');
4005 this->name_components.push_back ({previous_len, idx});
4006 /* Skip the '::'. */
4007 current_len += 2;
4008 previous_len = current_len;
4009 }
4010 }
4011 else
5c58de74 4012 {
3b00ef10
TT
4013 /* Handle the Ada encoded (aka mangled) form here. */
4014 for (const char *iter = strstr (name, "__");
4015 iter != nullptr;
4016 iter = strstr (iter, "__"))
4017 {
4018 this->name_components.push_back ({previous_len, idx});
4019 iter += 2;
4020 previous_len = iter - name;
4021 }
5c58de74 4022 }
3b00ef10 4023
5c58de74
PA
4024 this->name_components.push_back ({previous_len, idx});
4025 }
4026
4027 /* Sort name_components elements by name. */
4028 auto name_comp_compare = [&] (const name_component &left,
4029 const name_component &right)
4030 {
fcf23d5b
SM
4031 const char *left_qualified
4032 = this->symbol_name_at (left.idx, per_objfile);
4033 const char *right_qualified
4034 = this->symbol_name_at (right.idx, per_objfile);
5c58de74
PA
4035
4036 const char *left_name = left_qualified + left.name_offset;
4037 const char *right_name = right_qualified + right.name_offset;
4038
4039 return name_cmp (left_name, right_name) < 0;
4040 };
4041
4042 std::sort (this->name_components.begin (),
4043 this->name_components.end (),
4044 name_comp_compare);
4045}
4046
4047/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4048 mapped_index_base instead of the containing objfile. This is split
4049 to a separate function in order to be able to unit test the
4050 name_components matching using a mock mapped_index_base. For each
5c58de74 4051 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4052 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4053
4054static void
4055dw2_expand_symtabs_matching_symbol
44ed8f3e 4056 (mapped_index_base &index,
5c58de74
PA
4057 const lookup_name_info &lookup_name_in,
4058 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4059 enum search_domain kind,
fcf23d5b
SM
4060 gdb::function_view<bool (offset_type)> match_callback,
4061 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4062{
4063 lookup_name_info lookup_name_without_params
4064 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4065
4066 /* Build the symbol name component sorted vector, if we haven't
4067 yet. */
fcf23d5b 4068 index.build_name_components (per_objfile);
5c58de74 4069
3f563c84
PA
4070 /* The same symbol may appear more than once in the range though.
4071 E.g., if we're looking for symbols that complete "w", and we have
4072 a symbol named "w1::w2", we'll find the two name components for
4073 that same symbol in the range. To be sure we only call the
4074 callback once per symbol, we first collect the symbol name
4075 indexes that matched in a temporary vector and ignore
4076 duplicates. */
4077 std::vector<offset_type> matches;
3f563c84 4078
3b00ef10
TT
4079 struct name_and_matcher
4080 {
4081 symbol_name_matcher_ftype *matcher;
ecc6c606 4082 const char *name;
3b00ef10
TT
4083
4084 bool operator== (const name_and_matcher &other) const
3f563c84 4085 {
ecc6c606 4086 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
4087 }
4088 };
4089
4090 /* A vector holding all the different symbol name matchers, for all
4091 languages. */
4092 std::vector<name_and_matcher> matchers;
4093
4094 for (int i = 0; i < nr_languages; i++)
4095 {
4096 enum language lang_e = (enum language) i;
4097
4098 const language_defn *lang = language_def (lang_e);
4099 symbol_name_matcher_ftype *name_matcher
4100 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4101
3b00ef10
TT
4102 name_and_matcher key {
4103 name_matcher,
4104 lookup_name_without_params.language_lookup_name (lang_e)
4105 };
4106
4107 /* Don't insert the same comparison routine more than once.
4108 Note that we do this linear walk. This is not a problem in
4109 practice because the number of supported languages is
4110 low. */
4111 if (std::find (matchers.begin (), matchers.end (), key)
4112 != matchers.end ())
9291a0cd 4113 continue;
3b00ef10
TT
4114 matchers.push_back (std::move (key));
4115
4116 auto bounds
4117 = index.find_name_components_bounds (lookup_name_without_params,
fcf23d5b 4118 lang_e, per_objfile);
3b00ef10
TT
4119
4120 /* Now for each symbol name in range, check to see if we have a name
4121 match, and if so, call the MATCH_CALLBACK callback. */
4122
4123 for (; bounds.first != bounds.second; ++bounds.first)
4124 {
fcf23d5b
SM
4125 const char *qualified
4126 = index.symbol_name_at (bounds.first->idx, per_objfile);
3b00ef10
TT
4127
4128 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4129 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4130 continue;
9291a0cd 4131
3b00ef10
TT
4132 matches.push_back (bounds.first->idx);
4133 }
3f563c84
PA
4134 }
4135
4136 std::sort (matches.begin (), matches.end ());
4137
4138 /* Finally call the callback, once per match. */
4139 ULONGEST prev = -1;
4140 for (offset_type idx : matches)
4141 {
4142 if (prev != idx)
4143 {
3b00ef10
TT
4144 if (!match_callback (idx))
4145 break;
3f563c84
PA
4146 prev = idx;
4147 }
4148 }
4149
4150 /* Above we use a type wider than idx's for 'prev', since 0 and
4151 (offset_type)-1 are both possible values. */
4152 static_assert (sizeof (prev) > sizeof (offset_type), "");
4153}
4154
c62446b1
PA
4155#if GDB_SELF_TEST
4156
4157namespace selftests { namespace dw2_expand_symtabs_matching {
4158
a3c5fafd
PA
4159/* A mock .gdb_index/.debug_names-like name index table, enough to
4160 exercise dw2_expand_symtabs_matching_symbol, which works with the
4161 mapped_index_base interface. Builds an index from the symbol list
4162 passed as parameter to the constructor. */
4163class mock_mapped_index : public mapped_index_base
c62446b1
PA
4164{
4165public:
a3c5fafd
PA
4166 mock_mapped_index (gdb::array_view<const char *> symbols)
4167 : m_symbol_table (symbols)
c62446b1
PA
4168 {}
4169
a3c5fafd 4170 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4171
a3c5fafd 4172 /* Return the number of names in the symbol table. */
632e107b 4173 size_t symbol_name_count () const override
c62446b1 4174 {
a3c5fafd 4175 return m_symbol_table.size ();
c62446b1
PA
4176 }
4177
a3c5fafd 4178 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
4179 const char *symbol_name_at
4180 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
a3c5fafd
PA
4181 {
4182 return m_symbol_table[idx];
4183 }
c62446b1 4184
a3c5fafd
PA
4185private:
4186 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4187};
4188
4189/* Convenience function that converts a NULL pointer to a "<null>"
4190 string, to pass to print routines. */
4191
4192static const char *
4193string_or_null (const char *str)
4194{
4195 return str != NULL ? str : "<null>";
4196}
4197
4198/* Check if a lookup_name_info built from
4199 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4200 index. EXPECTED_LIST is the list of expected matches, in expected
4201 matching order. If no match expected, then an empty list is
4202 specified. Returns true on success. On failure prints a warning
4203 indicating the file:line that failed, and returns false. */
4204
4205static bool
4206check_match (const char *file, int line,
4207 mock_mapped_index &mock_index,
4208 const char *name, symbol_name_match_type match_type,
4209 bool completion_mode,
fcf23d5b
SM
4210 std::initializer_list<const char *> expected_list,
4211 dwarf2_per_objfile *per_objfile)
c62446b1
PA
4212{
4213 lookup_name_info lookup_name (name, match_type, completion_mode);
4214
4215 bool matched = true;
4216
4217 auto mismatch = [&] (const char *expected_str,
4218 const char *got)
4219 {
4220 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4221 "expected=\"%s\", got=\"%s\"\n"),
4222 file, line,
4223 (match_type == symbol_name_match_type::FULL
4224 ? "FULL" : "WILD"),
4225 name, string_or_null (expected_str), string_or_null (got));
4226 matched = false;
4227 };
4228
4229 auto expected_it = expected_list.begin ();
4230 auto expected_end = expected_list.end ();
4231
a3c5fafd 4232 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4233 NULL, ALL_DOMAIN,
4234 [&] (offset_type idx)
4235 {
fcf23d5b 4236 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
c62446b1
PA
4237 const char *expected_str
4238 = expected_it == expected_end ? NULL : *expected_it++;
4239
4240 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4241 mismatch (expected_str, matched_name);
3b00ef10 4242 return true;
fcf23d5b 4243 }, per_objfile);
c62446b1
PA
4244
4245 const char *expected_str
4246 = expected_it == expected_end ? NULL : *expected_it++;
4247 if (expected_str != NULL)
4248 mismatch (expected_str, NULL);
4249
4250 return matched;
4251}
4252
4253/* The symbols added to the mock mapped_index for testing (in
4254 canonical form). */
4255static const char *test_symbols[] = {
4256 "function",
4257 "std::bar",
4258 "std::zfunction",
4259 "std::zfunction2",
4260 "w1::w2",
4261 "ns::foo<char*>",
4262 "ns::foo<int>",
4263 "ns::foo<long>",
a20714ff
PA
4264 "ns2::tmpl<int>::foo2",
4265 "(anonymous namespace)::A::B::C",
c62446b1 4266
e1ef7d7a
PA
4267 /* These are used to check that the increment-last-char in the
4268 matching algorithm for completion doesn't match "t1_fund" when
4269 completing "t1_func". */
4270 "t1_func",
4271 "t1_func1",
4272 "t1_fund",
4273 "t1_fund1",
4274
4275 /* A UTF-8 name with multi-byte sequences to make sure that
4276 cp-name-parser understands this as a single identifier ("função"
4277 is "function" in PT). */
4278 u8"u8função",
4279
4280 /* \377 (0xff) is Latin1 'ÿ'. */
4281 "yfunc\377",
4282
4283 /* \377 (0xff) is Latin1 'ÿ'. */
4284 "\377",
4285 "\377\377123",
4286
c62446b1
PA
4287 /* A name with all sorts of complications. Starts with "z" to make
4288 it easier for the completion tests below. */
4289#define Z_SYM_NAME \
4290 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4291 "::tuple<(anonymous namespace)::ui*, " \
4292 "std::default_delete<(anonymous namespace)::ui>, void>"
4293
4294 Z_SYM_NAME
4295};
4296
a3c5fafd
PA
4297/* Returns true if the mapped_index_base::find_name_component_bounds
4298 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4299 in completion mode. */
5c58de74
PA
4300
4301static bool
a3c5fafd 4302check_find_bounds_finds (mapped_index_base &index,
5c58de74 4303 const char *search_name,
fcf23d5b
SM
4304 gdb::array_view<const char *> expected_syms,
4305 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4306{
4307 lookup_name_info lookup_name (search_name,
4308 symbol_name_match_type::FULL, true);
4309
3b00ef10 4310 auto bounds = index.find_name_components_bounds (lookup_name,
fcf23d5b
SM
4311 language_cplus,
4312 per_objfile);
5c58de74
PA
4313
4314 size_t distance = std::distance (bounds.first, bounds.second);
4315 if (distance != expected_syms.size ())
4316 return false;
4317
4318 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4319 {
4320 auto nc_elem = bounds.first + exp_elem;
fcf23d5b 4321 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
5c58de74
PA
4322 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4323 return false;
4324 }
4325
4326 return true;
4327}
4328
4329/* Test the lower-level mapped_index::find_name_component_bounds
4330 method. */
4331
c62446b1 4332static void
5c58de74
PA
4333test_mapped_index_find_name_component_bounds ()
4334{
4335 mock_mapped_index mock_index (test_symbols);
4336
fcf23d5b 4337 mock_index.build_name_components (NULL /* per_objfile */);
5c58de74
PA
4338
4339 /* Test the lower-level mapped_index::find_name_component_bounds
4340 method in completion mode. */
4341 {
4342 static const char *expected_syms[] = {
4343 "t1_func",
4344 "t1_func1",
5c58de74
PA
4345 };
4346
fcf23d5b
SM
4347 SELF_CHECK (check_find_bounds_finds
4348 (mock_index, "t1_func", expected_syms,
4349 NULL /* per_objfile */));
5c58de74
PA
4350 }
4351
4352 /* Check that the increment-last-char in the name matching algorithm
4353 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4354 {
4355 static const char *expected_syms1[] = {
4356 "\377",
4357 "\377\377123",
4358 };
fcf23d5b
SM
4359 SELF_CHECK (check_find_bounds_finds
4360 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
5c58de74
PA
4361
4362 static const char *expected_syms2[] = {
4363 "\377\377123",
4364 };
fcf23d5b
SM
4365 SELF_CHECK (check_find_bounds_finds
4366 (mock_index, "\377\377", expected_syms2,
4367 NULL /* per_objfile */));
5c58de74
PA
4368 }
4369}
4370
4371/* Test dw2_expand_symtabs_matching_symbol. */
4372
4373static void
4374test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4375{
4376 mock_mapped_index mock_index (test_symbols);
4377
4378 /* We let all tests run until the end even if some fails, for debug
4379 convenience. */
4380 bool any_mismatch = false;
4381
4382 /* Create the expected symbols list (an initializer_list). Needed
4383 because lists have commas, and we need to pass them to CHECK,
4384 which is a macro. */
4385#define EXPECT(...) { __VA_ARGS__ }
4386
4387 /* Wrapper for check_match that passes down the current
4388 __FILE__/__LINE__. */
4389#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4390 any_mismatch |= !check_match (__FILE__, __LINE__, \
4391 mock_index, \
4392 NAME, MATCH_TYPE, COMPLETION_MODE, \
fcf23d5b 4393 EXPECTED_LIST, NULL)
c62446b1
PA
4394
4395 /* Identity checks. */
4396 for (const char *sym : test_symbols)
4397 {
4398 /* Should be able to match all existing symbols. */
4399 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4400 EXPECT (sym));
4401
4402 /* Should be able to match all existing symbols with
4403 parameters. */
4404 std::string with_params = std::string (sym) + "(int)";
4405 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4406 EXPECT (sym));
4407
4408 /* Should be able to match all existing symbols with
4409 parameters and qualifiers. */
4410 with_params = std::string (sym) + " ( int ) const";
4411 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4412 EXPECT (sym));
4413
4414 /* This should really find sym, but cp-name-parser.y doesn't
4415 know about lvalue/rvalue qualifiers yet. */
4416 with_params = std::string (sym) + " ( int ) &&";
4417 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4418 {});
4419 }
4420
e1ef7d7a
PA
4421 /* Check that the name matching algorithm for completion doesn't get
4422 confused with Latin1 'ÿ' / 0xff. */
4423 {
4424 static const char str[] = "\377";
4425 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4426 EXPECT ("\377", "\377\377123"));
4427 }
4428
4429 /* Check that the increment-last-char in the matching algorithm for
4430 completion doesn't match "t1_fund" when completing "t1_func". */
4431 {
4432 static const char str[] = "t1_func";
4433 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4434 EXPECT ("t1_func", "t1_func1"));
4435 }
4436
c62446b1
PA
4437 /* Check that completion mode works at each prefix of the expected
4438 symbol name. */
4439 {
4440 static const char str[] = "function(int)";
4441 size_t len = strlen (str);
4442 std::string lookup;
4443
4444 for (size_t i = 1; i < len; i++)
4445 {
4446 lookup.assign (str, i);
4447 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4448 EXPECT ("function"));
4449 }
4450 }
4451
4452 /* While "w" is a prefix of both components, the match function
4453 should still only be called once. */
4454 {
4455 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4456 EXPECT ("w1::w2"));
a20714ff
PA
4457 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4458 EXPECT ("w1::w2"));
c62446b1
PA
4459 }
4460
4461 /* Same, with a "complicated" symbol. */
4462 {
4463 static const char str[] = Z_SYM_NAME;
4464 size_t len = strlen (str);
4465 std::string lookup;
4466
4467 for (size_t i = 1; i < len; i++)
4468 {
4469 lookup.assign (str, i);
4470 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4471 EXPECT (Z_SYM_NAME));
4472 }
4473 }
4474
4475 /* In FULL mode, an incomplete symbol doesn't match. */
4476 {
4477 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4478 {});
4479 }
4480
4481 /* A complete symbol with parameters matches any overload, since the
4482 index has no overload info. */
4483 {
4484 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4485 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4486 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4487 EXPECT ("std::zfunction", "std::zfunction2"));
4488 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4489 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4490 }
4491
4492 /* Check that whitespace is ignored appropriately. A symbol with a
4493 template argument list. */
4494 {
4495 static const char expected[] = "ns::foo<int>";
4496 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4497 EXPECT (expected));
a20714ff
PA
4498 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4499 EXPECT (expected));
c62446b1
PA
4500 }
4501
4502 /* Check that whitespace is ignored appropriately. A symbol with a
4503 template argument list that includes a pointer. */
4504 {
4505 static const char expected[] = "ns::foo<char*>";
4506 /* Try both completion and non-completion modes. */
4507 static const bool completion_mode[2] = {false, true};
4508 for (size_t i = 0; i < 2; i++)
4509 {
4510 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4511 completion_mode[i], EXPECT (expected));
a20714ff
PA
4512 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4513 completion_mode[i], EXPECT (expected));
c62446b1
PA
4514
4515 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4516 completion_mode[i], EXPECT (expected));
a20714ff
PA
4517 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4518 completion_mode[i], EXPECT (expected));
c62446b1
PA
4519 }
4520 }
4521
4522 {
4523 /* Check method qualifiers are ignored. */
4524 static const char expected[] = "ns::foo<char*>";
4525 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4526 symbol_name_match_type::FULL, true, EXPECT (expected));
4527 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4528 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4529 CHECK_MATCH ("foo < char * > ( int ) const",
4530 symbol_name_match_type::WILD, true, EXPECT (expected));
4531 CHECK_MATCH ("foo < char * > ( int ) &&",
4532 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4533 }
4534
4535 /* Test lookup names that don't match anything. */
4536 {
a20714ff
PA
4537 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4538 {});
4539
c62446b1
PA
4540 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4541 {});
4542 }
4543
a20714ff
PA
4544 /* Some wild matching tests, exercising "(anonymous namespace)",
4545 which should not be confused with a parameter list. */
4546 {
4547 static const char *syms[] = {
4548 "A::B::C",
4549 "B::C",
4550 "C",
4551 "A :: B :: C ( int )",
4552 "B :: C ( int )",
4553 "C ( int )",
4554 };
4555
4556 for (const char *s : syms)
4557 {
4558 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4559 EXPECT ("(anonymous namespace)::A::B::C"));
4560 }
4561 }
4562
4563 {
4564 static const char expected[] = "ns2::tmpl<int>::foo2";
4565 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4566 EXPECT (expected));
4567 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4568 EXPECT (expected));
4569 }
4570
c62446b1
PA
4571 SELF_CHECK (!any_mismatch);
4572
4573#undef EXPECT
4574#undef CHECK_MATCH
4575}
4576
5c58de74
PA
4577static void
4578run_test ()
4579{
4580 test_mapped_index_find_name_component_bounds ();
4581 test_dw2_expand_symtabs_matching_symbol ();
4582}
4583
c62446b1
PA
4584}} // namespace selftests::dw2_expand_symtabs_matching
4585
4586#endif /* GDB_SELF_TEST */
4587
4b514bc8
JK
4588/* If FILE_MATCHER is NULL or if PER_CU has
4589 dwarf2_per_cu_quick_data::MARK set (see
4590 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4591 EXPANSION_NOTIFY on it. */
4592
4593static void
4594dw2_expand_symtabs_matching_one
97a1449a
SM
4595 (dwarf2_per_cu_data *per_cu,
4596 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4597 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4598 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4599{
4600 if (file_matcher == NULL || per_cu->v.quick->mark)
4601 {
af758d11 4602 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4603
97a1449a
SM
4604 compunit_symtab *symtab
4605 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4606 gdb_assert (symtab != nullptr);
4b514bc8 4607
af758d11
SM
4608 if (expansion_notify != NULL && symtab_was_null)
4609 expansion_notify (symtab);
4b514bc8
JK
4610 }
4611}
4612
3f563c84
PA
4613/* Helper for dw2_expand_matching symtabs. Called on each symbol
4614 matched, to expand corresponding CUs that were marked. IDX is the
4615 index of the symbol name that matched. */
4616
4617static void
4618dw2_expand_marked_cus
976ca316 4619 (dwarf2_per_objfile *per_objfile, offset_type idx,
3f563c84
PA
4620 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4621 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4622 search_domain kind)
4623{
3f563c84
PA
4624 offset_type *vec, vec_len, vec_idx;
4625 bool global_seen = false;
976ca316 4626 mapped_index &index = *per_objfile->per_bfd->index_table;
3f563c84 4627
61920122 4628 vec = (offset_type *) (index.constant_pool
f00a2de2 4629 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4630 vec_len = MAYBE_SWAP (vec[0]);
4631 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4632 {
61920122
PA
4633 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4634 /* This value is only valid for index versions >= 7. */
4635 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4636 gdb_index_symbol_kind symbol_kind =
4637 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4638 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4639 /* Only check the symbol attributes if they're present.
4640 Indices prior to version 7 don't record them,
4641 and indices >= 7 may elide them for certain symbols
4642 (gold does this). */
4643 int attrs_valid =
4644 (index.version >= 7
4645 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4646
4647 /* Work around gold/15646. */
f030440d
TV
4648 if (attrs_valid
4649 && !is_static
4650 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
9291a0cd 4651 {
f030440d 4652 if (global_seen)
61920122 4653 continue;
f030440d
TV
4654
4655 global_seen = true;
61920122 4656 }
3190f0c6 4657
61920122
PA
4658 /* Only check the symbol's kind if it has one. */
4659 if (attrs_valid)
4660 {
4661 switch (kind)
8943b874 4662 {
61920122
PA
4663 case VARIABLES_DOMAIN:
4664 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4665 continue;
4666 break;
4667 case FUNCTIONS_DOMAIN:
4668 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4669 continue;
61920122
PA
4670 break;
4671 case TYPES_DOMAIN:
4672 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4673 continue;
4674 break;
59c35742
AB
4675 case MODULES_DOMAIN:
4676 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4677 continue;
4678 break;
61920122
PA
4679 default:
4680 break;
8943b874 4681 }
61920122 4682 }
8943b874 4683
61920122 4684 /* Don't crash on bad data. */
976ca316
SM
4685 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4686 + per_objfile->per_bfd->all_type_units.size ()))
61920122 4687 {
b98664d3 4688 complaint (_(".gdb_index entry has bad CU index"
976ca316 4689 " [in module %s]"), objfile_name (per_objfile->objfile));
61920122
PA
4690 continue;
4691 }
4692
976ca316
SM
4693 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4694 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4b514bc8 4695 expansion_notify);
61920122
PA
4696 }
4697}
4698
4b514bc8
JK
4699/* If FILE_MATCHER is non-NULL, set all the
4700 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4701 that match FILE_MATCHER. */
4702
61920122 4703static void
4b514bc8 4704dw_expand_symtabs_matching_file_matcher
976ca316 4705 (dwarf2_per_objfile *per_objfile,
ed2dc618 4706 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4707{
4b514bc8 4708 if (file_matcher == NULL)
61920122
PA
4709 return;
4710
4b514bc8
JK
4711 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4712 htab_eq_pointer,
4713 NULL, xcalloc, xfree));
4714 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4715 htab_eq_pointer,
4716 NULL, xcalloc, xfree));
61920122 4717
4b514bc8
JK
4718 /* The rule is CUs specify all the files, including those used by
4719 any TU, so there's no need to scan TUs here. */
61920122 4720
976ca316 4721 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4722 {
927aa2e7
JK
4723 QUIT;
4724
4725 per_cu->v.quick->mark = 0;
4726
4727 /* We only need to look at symtabs not already expanded. */
976ca316 4728 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4729 continue;
4730
976ca316 4731 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4732 if (file_data == NULL)
4733 continue;
4734
4735 if (htab_find (visited_not_found.get (), file_data) != NULL)
4736 continue;
4737 else if (htab_find (visited_found.get (), file_data) != NULL)
4738 {
4739 per_cu->v.quick->mark = 1;
4740 continue;
4741 }
4742
b76e467d 4743 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4744 {
4745 const char *this_real_name;
4746
4747 if (file_matcher (file_data->file_names[j], false))
4748 {
4749 per_cu->v.quick->mark = 1;
4750 break;
4751 }
4752
4753 /* Before we invoke realpath, which can get expensive when many
4754 files are involved, do a quick comparison of the basenames. */
4755 if (!basenames_may_differ
4756 && !file_matcher (lbasename (file_data->file_names[j]),
4757 true))
4758 continue;
4759
976ca316 4760 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
927aa2e7
JK
4761 if (file_matcher (this_real_name, false))
4762 {
4763 per_cu->v.quick->mark = 1;
4764 break;
4765 }
4766 }
4767
b76e467d
SM
4768 void **slot = htab_find_slot (per_cu->v.quick->mark
4769 ? visited_found.get ()
4770 : visited_not_found.get (),
4771 file_data, INSERT);
927aa2e7
JK
4772 *slot = file_data;
4773 }
4774}
4775
4776static void
4777dw2_expand_symtabs_matching
4778 (struct objfile *objfile,
4779 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4780 const lookup_name_info *lookup_name,
927aa2e7
JK
4781 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4782 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4783 enum search_domain kind)
4784{
976ca316 4785 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4786
4787 /* index_table is NULL if OBJF_READNOW. */
976ca316 4788 if (!per_objfile->per_bfd->index_table)
927aa2e7
JK
4789 return;
4790
976ca316 4791 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
927aa2e7 4792
c1a66c06
TV
4793 if (symbol_matcher == NULL && lookup_name == NULL)
4794 {
976ca316 4795 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4796 {
4797 QUIT;
4798
976ca316 4799 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
97a1449a 4800 file_matcher, expansion_notify);
c1a66c06
TV
4801 }
4802 return;
4803 }
4804
976ca316 4805 mapped_index &index = *per_objfile->per_bfd->index_table;
927aa2e7 4806
c1a66c06 4807 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4808 symbol_matcher,
4809 kind, [&] (offset_type idx)
4810 {
976ca316
SM
4811 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4812 kind);
3b00ef10 4813 return true;
976ca316 4814 }, per_objfile);
927aa2e7
JK
4815}
4816
4817/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4818 symtab. */
4819
4820static struct compunit_symtab *
4821recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4822 CORE_ADDR pc)
4823{
4824 int i;
4825
4826 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4827 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4828 return cust;
4829
4830 if (cust->includes == NULL)
4831 return NULL;
4832
4833 for (i = 0; cust->includes[i]; ++i)
4834 {
4835 struct compunit_symtab *s = cust->includes[i];
4836
4837 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4838 if (s != NULL)
4839 return s;
4840 }
4841
4842 return NULL;
4843}
4844
4845static struct compunit_symtab *
4846dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4847 struct bound_minimal_symbol msymbol,
4848 CORE_ADDR pc,
4849 struct obj_section *section,
4850 int warn_if_readin)
4851{
4852 struct dwarf2_per_cu_data *data;
4853 struct compunit_symtab *result;
4854
d320c2b5 4855 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4856 return NULL;
4857
b3b3bada 4858 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4859 data = (struct dwarf2_per_cu_data *) addrmap_find
4860 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4861 if (!data)
4862 return NULL;
4863
af758d11
SM
4864 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4865 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4866 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4867 paddress (objfile->arch (), pc));
927aa2e7 4868
97a1449a
SM
4869 result = recursively_find_pc_sect_compunit_symtab
4870 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4871
927aa2e7
JK
4872 gdb_assert (result != NULL);
4873 return result;
4874}
4875
4876static void
4877dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4878 void *data, int need_fullname)
4879{
976ca316 4880 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7 4881
976ca316 4882 if (!per_objfile->per_bfd->filenames_cache)
927aa2e7 4883 {
976ca316 4884 per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
4885
4886 htab_up visited (htab_create_alloc (10,
4887 htab_hash_pointer, htab_eq_pointer,
4888 NULL, xcalloc, xfree));
4889
4890 /* The rule is CUs specify all the files, including those used
4891 by any TU, so there's no need to scan TUs here. We can
4892 ignore file names coming from already-expanded CUs. */
4893
976ca316 4894 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4895 {
976ca316 4896 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4897 {
4898 void **slot = htab_find_slot (visited.get (),
4899 per_cu->v.quick->file_names,
4900 INSERT);
4901
4902 *slot = per_cu->v.quick->file_names;
4903 }
4904 }
4905
976ca316 4906 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4907 {
927aa2e7 4908 /* We only need to look at symtabs not already expanded. */
976ca316 4909 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4910 continue;
4911
ab432490 4912 quick_file_names *file_data
976ca316 4913 = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4914 if (file_data == NULL)
4915 continue;
4916
b76e467d 4917 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4918 if (*slot)
4919 {
4920 /* Already visited. */
4921 continue;
4922 }
4923 *slot = file_data;
4924
4925 for (int j = 0; j < file_data->num_file_names; ++j)
4926 {
4927 const char *filename = file_data->file_names[j];
976ca316 4928 per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
4929 }
4930 }
4931 }
4932
976ca316 4933 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
4934 {
4935 gdb::unique_xmalloc_ptr<char> this_real_name;
4936
4937 if (need_fullname)
4938 this_real_name = gdb_realpath (filename);
4939 (*fun) (filename, this_real_name.get (), data);
4940 });
4941}
4942
4943static int
4944dw2_has_symbols (struct objfile *objfile)
4945{
4946 return 1;
4947}
4948
4949const struct quick_symbol_functions dwarf2_gdb_index_functions =
4950{
4951 dw2_has_symbols,
4952 dw2_find_last_source_symtab,
4953 dw2_forget_cached_source_info,
4954 dw2_map_symtabs_matching_filename,
4955 dw2_lookup_symbol,
d3214198 4956 NULL,
927aa2e7
JK
4957 dw2_print_stats,
4958 dw2_dump,
927aa2e7
JK
4959 dw2_expand_symtabs_for_function,
4960 dw2_expand_all_symtabs,
4961 dw2_expand_symtabs_with_fullname,
4962 dw2_map_matching_symbols,
4963 dw2_expand_symtabs_matching,
4964 dw2_find_pc_sect_compunit_symtab,
4965 NULL,
4966 dw2_map_symbol_filenames
4967};
4968
4969/* DWARF-5 debug_names reader. */
4970
4971/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4972static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4973
4974/* A helper function that reads the .debug_names section in SECTION
4975 and fills in MAP. FILENAME is the name of the file containing the
4976 section; it is used for error reporting.
4977
4978 Returns true if all went well, false otherwise. */
4979
4980static bool
4981read_debug_names_from_section (struct objfile *objfile,
4982 const char *filename,
4983 struct dwarf2_section_info *section,
4984 mapped_debug_names &map)
4985{
96b79293 4986 if (section->empty ())
927aa2e7
JK
4987 return false;
4988
4989 /* Older elfutils strip versions could keep the section in the main
4990 executable while splitting it for the separate debug info file. */
96b79293 4991 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4992 return false;
4993
96b79293 4994 section->read (objfile);
927aa2e7 4995
08feed99 4996 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4997
4998 const gdb_byte *addr = section->buffer;
4999
96b79293 5000 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
5001
5002 unsigned int bytes_read;
5003 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5004 addr += bytes_read;
5005
5006 map.dwarf5_is_dwarf64 = bytes_read != 4;
5007 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5008 if (bytes_read + length != section->size)
5009 {
5010 /* There may be multiple per-CU indices. */
5011 warning (_("Section .debug_names in %s length %s does not match "
5012 "section length %s, ignoring .debug_names."),
5013 filename, plongest (bytes_read + length),
5014 pulongest (section->size));
5015 return false;
5016 }
5017
5018 /* The version number. */
5019 uint16_t version = read_2_bytes (abfd, addr);
5020 addr += 2;
5021 if (version != 5)
5022 {
5023 warning (_("Section .debug_names in %s has unsupported version %d, "
5024 "ignoring .debug_names."),
5025 filename, version);
5026 return false;
5027 }
5028
5029 /* Padding. */
5030 uint16_t padding = read_2_bytes (abfd, addr);
5031 addr += 2;
5032 if (padding != 0)
5033 {
5034 warning (_("Section .debug_names in %s has unsupported padding %d, "
5035 "ignoring .debug_names."),
5036 filename, padding);
5037 return false;
5038 }
5039
5040 /* comp_unit_count - The number of CUs in the CU list. */
5041 map.cu_count = read_4_bytes (abfd, addr);
5042 addr += 4;
5043
5044 /* local_type_unit_count - The number of TUs in the local TU
5045 list. */
5046 map.tu_count = read_4_bytes (abfd, addr);
5047 addr += 4;
5048
5049 /* foreign_type_unit_count - The number of TUs in the foreign TU
5050 list. */
5051 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5052 addr += 4;
5053 if (foreign_tu_count != 0)
5054 {
5055 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5056 "ignoring .debug_names."),
5057 filename, static_cast<unsigned long> (foreign_tu_count));
5058 return false;
5059 }
5060
5061 /* bucket_count - The number of hash buckets in the hash lookup
5062 table. */
5063 map.bucket_count = read_4_bytes (abfd, addr);
5064 addr += 4;
5065
5066 /* name_count - The number of unique names in the index. */
5067 map.name_count = read_4_bytes (abfd, addr);
5068 addr += 4;
5069
5070 /* abbrev_table_size - The size in bytes of the abbreviations
5071 table. */
5072 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5073 addr += 4;
5074
5075 /* augmentation_string_size - The size in bytes of the augmentation
5076 string. This value is rounded up to a multiple of 4. */
5077 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5078 addr += 4;
5079 map.augmentation_is_gdb = ((augmentation_string_size
5080 == sizeof (dwarf5_augmentation))
5081 && memcmp (addr, dwarf5_augmentation,
5082 sizeof (dwarf5_augmentation)) == 0);
5083 augmentation_string_size += (-augmentation_string_size) & 3;
5084 addr += augmentation_string_size;
5085
5086 /* List of CUs */
5087 map.cu_table_reordered = addr;
5088 addr += map.cu_count * map.offset_size;
5089
5090 /* List of Local TUs */
5091 map.tu_table_reordered = addr;
5092 addr += map.tu_count * map.offset_size;
5093
5094 /* Hash Lookup Table */
5095 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5096 addr += map.bucket_count * 4;
5097 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5098 addr += map.name_count * 4;
5099
5100 /* Name Table */
5101 map.name_table_string_offs_reordered = addr;
5102 addr += map.name_count * map.offset_size;
5103 map.name_table_entry_offs_reordered = addr;
5104 addr += map.name_count * map.offset_size;
5105
5106 const gdb_byte *abbrev_table_start = addr;
5107 for (;;)
5108 {
927aa2e7
JK
5109 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5110 addr += bytes_read;
5111 if (index_num == 0)
5112 break;
5113
5114 const auto insertpair
5115 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5116 if (!insertpair.second)
5117 {
5118 warning (_("Section .debug_names in %s has duplicate index %s, "
5119 "ignoring .debug_names."),
5120 filename, pulongest (index_num));
5121 return false;
5122 }
5123 mapped_debug_names::index_val &indexval = insertpair.first->second;
5124 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5125 addr += bytes_read;
5126
5127 for (;;)
5128 {
5129 mapped_debug_names::index_val::attr attr;
5130 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5131 addr += bytes_read;
5132 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5133 addr += bytes_read;
5134 if (attr.form == DW_FORM_implicit_const)
5135 {
5136 attr.implicit_const = read_signed_leb128 (abfd, addr,
5137 &bytes_read);
5138 addr += bytes_read;
5139 }
5140 if (attr.dw_idx == 0 && attr.form == 0)
5141 break;
5142 indexval.attr_vec.push_back (std::move (attr));
5143 }
5144 }
5145 if (addr != abbrev_table_start + abbrev_table_size)
5146 {
5147 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5148 "of size %s vs. written as %u, ignoring .debug_names."),
5149 filename, plongest (addr - abbrev_table_start),
5150 abbrev_table_size);
927aa2e7
JK
5151 return false;
5152 }
5153 map.entry_pool = addr;
5154
5155 return true;
5156}
5157
5158/* A helper for create_cus_from_debug_names that handles the MAP's CU
5159 list. */
5160
5161static void
168c9250 5162create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5163 const mapped_debug_names &map,
5164 dwarf2_section_info &section,
b76e467d 5165 bool is_dwz)
927aa2e7 5166{
3ee6bb11
TV
5167 if (!map.augmentation_is_gdb)
5168 {
5169 for (uint32_t i = 0; i < map.cu_count; ++i)
5170 {
5171 sect_offset sect_off
5172 = (sect_offset) (extract_unsigned_integer
5173 (map.cu_table_reordered + i * map.offset_size,
5174 map.offset_size,
5175 map.dwarf5_byte_order));
5176 /* We don't know the length of the CU, because the CU list in a
5177 .debug_names index can be incomplete, so we can't use the start of
5178 the next CU as end of this CU. We create the CUs here with length 0,
5179 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5180 dwarf2_per_cu_data *per_cu
168c9250
SM
5181 = create_cu_from_index_list (per_bfd, &section, is_dwz, sect_off, 0);
5182 per_bfd->all_comp_units.push_back (per_cu);
3ee6bb11
TV
5183 }
5184 }
5185
927aa2e7
JK
5186 sect_offset sect_off_prev;
5187 for (uint32_t i = 0; i <= map.cu_count; ++i)
5188 {
5189 sect_offset sect_off_next;
5190 if (i < map.cu_count)
5191 {
5192 sect_off_next
5193 = (sect_offset) (extract_unsigned_integer
5194 (map.cu_table_reordered + i * map.offset_size,
5195 map.offset_size,
5196 map.dwarf5_byte_order));
5197 }
5198 else
5199 sect_off_next = (sect_offset) section.size;
5200 if (i >= 1)
5201 {
5202 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5203 dwarf2_per_cu_data *per_cu
168c9250 5204 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 5205 sect_off_prev, length);
168c9250 5206 per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5207 }
5208 sect_off_prev = sect_off_next;
5209 }
5210}
5211
5212/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5213 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5214
5215static void
168c9250 5216create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5217 const mapped_debug_names &map,
5218 const mapped_debug_names &dwz_map)
5219{
168c9250
SM
5220 gdb_assert (per_bfd->all_comp_units.empty ());
5221 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5222
168c9250 5223 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 5224 false /* is_dwz */);
927aa2e7
JK
5225
5226 if (dwz_map.cu_count == 0)
5227 return;
5228
168c9250
SM
5229 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5230 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 5231 true /* is_dwz */);
927aa2e7
JK
5232}
5233
5234/* Read .debug_names. If everything went ok, initialize the "quick"
5235 elements of all the CUs and return true. Otherwise, return false. */
5236
5237static bool
976ca316 5238dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
927aa2e7 5239{
fcf23d5b
SM
5240 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5241 mapped_debug_names dwz_map;
976ca316 5242 struct objfile *objfile = per_objfile->objfile;
f8c41851 5243 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
5244
5245 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
976ca316 5246 &per_objfile->per_bfd->debug_names, *map))
927aa2e7
JK
5247 return false;
5248
5249 /* Don't use the index if it's empty. */
22ca247e 5250 if (map->name_count == 0)
927aa2e7
JK
5251 return false;
5252
5253 /* If there is a .dwz file, read it so we can get its CU list as
5254 well. */
f8c41851 5255 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
927aa2e7
JK
5256 if (dwz != NULL)
5257 {
5258 if (!read_debug_names_from_section (objfile,
00f93c44 5259 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5260 &dwz->debug_names, dwz_map))
5261 {
5262 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5263 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5264 return false;
5265 }
5266 }
5267
f8c41851 5268 create_cus_from_debug_names (per_bfd, *map, dwz_map);
927aa2e7 5269
22ca247e 5270 if (map->tu_count != 0)
927aa2e7
JK
5271 {
5272 /* We can only handle a single .debug_types when we have an
5273 index. */
f8c41851 5274 if (per_bfd->types.size () != 1)
927aa2e7
JK
5275 return false;
5276
f8c41851 5277 dwarf2_section_info *section = &per_bfd->types[0];
927aa2e7
JK
5278
5279 create_signatured_type_table_from_debug_names
f8c41851 5280 (per_objfile, *map, section, &per_bfd->abbrev);
927aa2e7
JK
5281 }
5282
f8c41851 5283 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
927aa2e7 5284
f8c41851
SM
5285 per_bfd->debug_names_table = std::move (map);
5286 per_bfd->using_index = 1;
5287 per_bfd->quick_file_names_table =
976ca316 5288 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
927aa2e7 5289
f8c41851
SM
5290 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
5291 objfiles using the same BFD. */
5292 gdb_assert (per_bfd->partial_symtabs == nullptr);
5293 per_bfd->partial_symtabs = objfile->partial_symtabs;
5294
927aa2e7
JK
5295 return true;
5296}
5297
927aa2e7
JK
5298/* Type used to manage iterating over all CUs looking for a symbol for
5299 .debug_names. */
5300
5301class dw2_debug_names_iterator
5302{
5303public:
927aa2e7 5304 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5305 gdb::optional<block_enum> block_index,
5306 domain_enum domain,
fcf23d5b 5307 const char *name, dwarf2_per_objfile *per_objfile)
2b79f376 5308 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5309 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5310 m_per_objfile (per_objfile)
927aa2e7
JK
5311 {}
5312
5313 dw2_debug_names_iterator (const mapped_debug_names &map,
fcf23d5b 5314 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5315 : m_map (map),
5316 m_search (search),
fcf23d5b
SM
5317 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5318 m_per_objfile (per_objfile)
927aa2e7
JK
5319 {}
5320
3b00ef10
TT
5321 dw2_debug_names_iterator (const mapped_debug_names &map,
5322 block_enum block_index, domain_enum domain,
fcf23d5b 5323 uint32_t namei, dwarf2_per_objfile *per_objfile)
3b00ef10 5324 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5325 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5326 m_per_objfile (per_objfile)
3b00ef10
TT
5327 {}
5328
927aa2e7
JK
5329 /* Return the next matching CU or NULL if there are no more. */
5330 dwarf2_per_cu_data *next ();
5331
5332private:
5333 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5334 const char *name,
5335 dwarf2_per_objfile *per_objfile);
927aa2e7 5336 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5337 uint32_t namei,
5338 dwarf2_per_objfile *per_objfile);
927aa2e7
JK
5339
5340 /* The internalized form of .debug_names. */
5341 const mapped_debug_names &m_map;
5342
2b79f376
SM
5343 /* If set, only look for symbols that match that block. Valid values are
5344 GLOBAL_BLOCK and STATIC_BLOCK. */
5345 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5346
5347 /* The kind of symbol we're looking for. */
5348 const domain_enum m_domain = UNDEF_DOMAIN;
5349 const search_domain m_search = ALL_DOMAIN;
5350
5351 /* The list of CUs from the index entry of the symbol, or NULL if
5352 not found. */
5353 const gdb_byte *m_addr;
fcf23d5b
SM
5354
5355 dwarf2_per_objfile *m_per_objfile;
927aa2e7
JK
5356};
5357
5358const char *
fcf23d5b 5359mapped_debug_names::namei_to_name
976ca316 5360 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
927aa2e7
JK
5361{
5362 const ULONGEST namei_string_offs
5363 = extract_unsigned_integer ((name_table_string_offs_reordered
5364 + namei * offset_size),
5365 offset_size,
5366 dwarf5_byte_order);
976ca316 5367 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
927aa2e7
JK
5368}
5369
5370/* Find a slot in .debug_names for the object named NAME. If NAME is
5371 found, return pointer to its pool data. If NAME cannot be found,
5372 return NULL. */
5373
5374const gdb_byte *
5375dw2_debug_names_iterator::find_vec_in_debug_names
976ca316
SM
5376 (const mapped_debug_names &map, const char *name,
5377 dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5378{
5379 int (*cmp) (const char *, const char *);
5380
54ee4252 5381 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5382 if (current_language->la_language == language_cplus
5383 || current_language->la_language == language_fortran
5384 || current_language->la_language == language_d)
5385 {
5386 /* NAME is already canonical. Drop any qualifiers as
5387 .debug_names does not contain any. */
5388
5389 if (strchr (name, '(') != NULL)
5390 {
54ee4252 5391 without_params = cp_remove_params (name);
927aa2e7 5392 if (without_params != NULL)
54ee4252 5393 name = without_params.get ();
927aa2e7
JK
5394 }
5395 }
5396
5397 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5398
5399 const uint32_t full_hash = dwarf5_djb_hash (name);
5400 uint32_t namei
5401 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5402 (map.bucket_table_reordered
5403 + (full_hash % map.bucket_count)), 4,
5404 map.dwarf5_byte_order);
5405 if (namei == 0)
5406 return NULL;
5407 --namei;
5408 if (namei >= map.name_count)
5409 {
b98664d3 5410 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5411 "[in module %s]"),
5412 namei, map.name_count,
fcf23d5b 5413 objfile_name (per_objfile->objfile));
927aa2e7
JK
5414 return NULL;
5415 }
5416
5417 for (;;)
5418 {
5419 const uint32_t namei_full_hash
5420 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5421 (map.hash_table_reordered + namei), 4,
5422 map.dwarf5_byte_order);
5423 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5424 return NULL;
5425
5426 if (full_hash == namei_full_hash)
5427 {
fcf23d5b 5428 const char *const namei_string = map.namei_to_name (namei, per_objfile);
927aa2e7
JK
5429
5430#if 0 /* An expensive sanity check. */
5431 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5432 {
b98664d3 5433 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5434 "[in module %s]"),
5435 namei, objfile_name (dwarf2_per_objfile->objfile));
5436 return NULL;
5437 }
5438#endif
5439
5440 if (cmp (namei_string, name) == 0)
5441 {
5442 const ULONGEST namei_entry_offs
5443 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5444 + namei * map.offset_size),
5445 map.offset_size, map.dwarf5_byte_order);
5446 return map.entry_pool + namei_entry_offs;
5447 }
5448 }
5449
5450 ++namei;
5451 if (namei >= map.name_count)
5452 return NULL;
5453 }
5454}
5455
5456const gdb_byte *
5457dw2_debug_names_iterator::find_vec_in_debug_names
fcf23d5b 5458 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5459{
5460 if (namei >= map.name_count)
5461 {
b98664d3 5462 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5463 "[in module %s]"),
5464 namei, map.name_count,
fcf23d5b 5465 objfile_name (per_objfile->objfile));
927aa2e7
JK
5466 return NULL;
5467 }
5468
5469 const ULONGEST namei_entry_offs
5470 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5471 + namei * map.offset_size),
5472 map.offset_size, map.dwarf5_byte_order);
5473 return map.entry_pool + namei_entry_offs;
5474}
5475
5476/* See dw2_debug_names_iterator. */
5477
5478dwarf2_per_cu_data *
5479dw2_debug_names_iterator::next ()
5480{
5481 if (m_addr == NULL)
5482 return NULL;
5483
fcf23d5b
SM
5484 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5485 struct objfile *objfile = m_per_objfile->objfile;
ed2dc618 5486 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5487
5488 again:
5489
5490 unsigned int bytes_read;
5491 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5492 m_addr += bytes_read;
5493 if (abbrev == 0)
5494 return NULL;
5495
5496 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5497 if (indexval_it == m_map.abbrev_map.cend ())
5498 {
b98664d3 5499 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5500 "[in module %s]"),
ed2dc618 5501 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5502 return NULL;
5503 }
5504 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5505 enum class symbol_linkage {
5506 unknown,
5507 static_,
5508 extern_,
23c13d42 5509 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5510 dwarf2_per_cu_data *per_cu = NULL;
5511 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5512 {
5513 ULONGEST ull;
5514 switch (attr.form)
5515 {
5516 case DW_FORM_implicit_const:
5517 ull = attr.implicit_const;
5518 break;
5519 case DW_FORM_flag_present:
5520 ull = 1;
5521 break;
5522 case DW_FORM_udata:
5523 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5524 m_addr += bytes_read;
5525 break;
6dc55ce9 5526 case DW_FORM_ref4:
5527 ull = read_4_bytes (abfd, m_addr);
5528 m_addr += 4;
5529 break;
5530 case DW_FORM_ref8:
5531 ull = read_8_bytes (abfd, m_addr);
5532 m_addr += 8;
5533 break;
5534 case DW_FORM_ref_sig8:
5535 ull = read_8_bytes (abfd, m_addr);
5536 m_addr += 8;
5537 break;
927aa2e7 5538 default:
b98664d3 5539 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5540 dwarf_form_name (attr.form),
ed2dc618 5541 objfile_name (objfile));
927aa2e7
JK
5542 return NULL;
5543 }
5544 switch (attr.dw_idx)
5545 {
5546 case DW_IDX_compile_unit:
5547 /* Don't crash on bad data. */
fcf23d5b 5548 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
927aa2e7 5549 {
b98664d3 5550 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5551 " [in module %s]"),
5552 pulongest (ull),
fcf23d5b 5553 objfile_name (objfile));
927aa2e7
JK
5554 continue;
5555 }
fcf23d5b 5556 per_cu = per_bfd->get_cutu (ull);
927aa2e7 5557 break;
8af5c486
JK
5558 case DW_IDX_type_unit:
5559 /* Don't crash on bad data. */
fcf23d5b 5560 if (ull >= per_bfd->all_type_units.size ())
8af5c486 5561 {
b98664d3 5562 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5563 " [in module %s]"),
5564 pulongest (ull),
fcf23d5b 5565 objfile_name (objfile));
8af5c486
JK
5566 continue;
5567 }
fcf23d5b 5568 per_cu = &per_bfd->get_tu (ull)->per_cu;
8af5c486 5569 break;
6dc55ce9 5570 case DW_IDX_die_offset:
5571 /* In a per-CU index (as opposed to a per-module index), index
5572 entries without CU attribute implicitly refer to the single CU. */
5573 if (per_cu == NULL)
fcf23d5b 5574 per_cu = per_bfd->get_cu (0);
6dc55ce9 5575 break;
927aa2e7
JK
5576 case DW_IDX_GNU_internal:
5577 if (!m_map.augmentation_is_gdb)
5578 break;
23c13d42 5579 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5580 break;
5581 case DW_IDX_GNU_external:
5582 if (!m_map.augmentation_is_gdb)
5583 break;
23c13d42 5584 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5585 break;
5586 }
5587 }
5588
5589 /* Skip if already read in. */
fcf23d5b 5590 if (m_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5591 goto again;
5592
5593 /* Check static vs global. */
23c13d42 5594 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5595 {
2b79f376 5596 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5597 const bool symbol_is_static =
5598 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5599 if (want_static != symbol_is_static)
2b79f376 5600 goto again;
927aa2e7
JK
5601 }
5602
5603 /* Match dw2_symtab_iter_next, symbol_kind
5604 and debug_names::psymbol_tag. */
5605 switch (m_domain)
5606 {
5607 case VAR_DOMAIN:
5608 switch (indexval.dwarf_tag)
5609 {
5610 case DW_TAG_variable:
5611 case DW_TAG_subprogram:
5612 /* Some types are also in VAR_DOMAIN. */
5613 case DW_TAG_typedef:
5614 case DW_TAG_structure_type:
5615 break;
5616 default:
5617 goto again;
5618 }
5619 break;
5620 case STRUCT_DOMAIN:
5621 switch (indexval.dwarf_tag)
5622 {
5623 case DW_TAG_typedef:
5624 case DW_TAG_structure_type:
5625 break;
5626 default:
5627 goto again;
5628 }
5629 break;
5630 case LABEL_DOMAIN:
5631 switch (indexval.dwarf_tag)
5632 {
5633 case 0:
5634 case DW_TAG_variable:
5635 break;
5636 default:
5637 goto again;
5638 }
5639 break;
59c35742
AB
5640 case MODULE_DOMAIN:
5641 switch (indexval.dwarf_tag)
5642 {
5643 case DW_TAG_module:
5644 break;
5645 default:
5646 goto again;
5647 }
5648 break;
927aa2e7
JK
5649 default:
5650 break;
5651 }
5652
5653 /* Match dw2_expand_symtabs_matching, symbol_kind and
5654 debug_names::psymbol_tag. */
5655 switch (m_search)
4b514bc8 5656 {
927aa2e7
JK
5657 case VARIABLES_DOMAIN:
5658 switch (indexval.dwarf_tag)
4b514bc8 5659 {
927aa2e7
JK
5660 case DW_TAG_variable:
5661 break;
5662 default:
5663 goto again;
4b514bc8 5664 }
927aa2e7
JK
5665 break;
5666 case FUNCTIONS_DOMAIN:
5667 switch (indexval.dwarf_tag)
4b514bc8 5668 {
927aa2e7
JK
5669 case DW_TAG_subprogram:
5670 break;
5671 default:
5672 goto again;
4b514bc8 5673 }
927aa2e7
JK
5674 break;
5675 case TYPES_DOMAIN:
5676 switch (indexval.dwarf_tag)
5677 {
5678 case DW_TAG_typedef:
5679 case DW_TAG_structure_type:
5680 break;
5681 default:
5682 goto again;
5683 }
5684 break;
59c35742
AB
5685 case MODULES_DOMAIN:
5686 switch (indexval.dwarf_tag)
5687 {
5688 case DW_TAG_module:
5689 break;
5690 default:
5691 goto again;
5692 }
927aa2e7
JK
5693 default:
5694 break;
4b514bc8 5695 }
927aa2e7
JK
5696
5697 return per_cu;
4b514bc8 5698}
61920122 5699
927aa2e7 5700static struct compunit_symtab *
c7f839cb 5701dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5702 const char *name, domain_enum domain)
4b514bc8 5703{
976ca316 5704 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
61920122 5705
976ca316 5706 const auto &mapp = per_objfile->per_bfd->debug_names_table;
927aa2e7 5707 if (!mapp)
61920122 5708 {
927aa2e7
JK
5709 /* index is NULL if OBJF_READNOW. */
5710 return NULL;
5711 }
5712 const auto &map = *mapp;
9291a0cd 5713
976ca316 5714 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
9703b513 5715
927aa2e7
JK
5716 struct compunit_symtab *stab_best = NULL;
5717 struct dwarf2_per_cu_data *per_cu;
5718 while ((per_cu = iter.next ()) != NULL)
5719 {
5720 struct symbol *sym, *with_opaque = NULL;
97a1449a 5721 compunit_symtab *stab
976ca316 5722 = dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7 5723 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5724 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5725
927aa2e7
JK
5726 sym = block_find_symbol (block, name, domain,
5727 block_find_non_opaque_type_preferred,
5728 &with_opaque);
9703b513 5729
927aa2e7
JK
5730 /* Some caution must be observed with overloaded functions and
5731 methods, since the index will not contain any overload
5732 information (but NAME might contain it). */
a3ec0bb1 5733
927aa2e7 5734 if (sym != NULL
987012b8 5735 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5736 return stab;
5737 if (with_opaque != NULL
987012b8 5738 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5739 stab_best = stab;
9703b513 5740
927aa2e7 5741 /* Keep looking through other CUs. */
9703b513
TT
5742 }
5743
927aa2e7 5744 return stab_best;
9703b513
TT
5745}
5746
927aa2e7
JK
5747/* This dumps minimal information about .debug_names. It is called
5748 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5749 uses this to verify that .debug_names has been loaded. */
9291a0cd 5750
927aa2e7
JK
5751static void
5752dw2_debug_names_dump (struct objfile *objfile)
5753{
976ca316 5754 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 5755
976ca316 5756 gdb_assert (per_objfile->per_bfd->using_index);
927aa2e7 5757 printf_filtered (".debug_names:");
976ca316 5758 if (per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5759 printf_filtered (" exists\n");
5760 else
5761 printf_filtered (" faked for \"readnow\"\n");
5762 printf_filtered ("\n");
9291a0cd
TT
5763}
5764
9291a0cd 5765static void
927aa2e7
JK
5766dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5767 const char *func_name)
9291a0cd 5768{
976ca316 5769 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 5770
976ca316
SM
5771 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5772 if (per_objfile->per_bfd->debug_names_table)
24c79950 5773 {
976ca316 5774 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
24c79950 5775
fcf23d5b 5776 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
976ca316 5777 per_objfile);
24c79950 5778
927aa2e7
JK
5779 struct dwarf2_per_cu_data *per_cu;
5780 while ((per_cu = iter.next ()) != NULL)
976ca316 5781 dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7
JK
5782 }
5783}
24c79950 5784
3b00ef10
TT
5785static void
5786dw2_debug_names_map_matching_symbols
5787 (struct objfile *objfile,
5788 const lookup_name_info &name, domain_enum domain,
5789 int global,
5790 gdb::function_view<symbol_found_callback_ftype> callback,
5791 symbol_compare_ftype *ordered_compare)
5792{
976ca316 5793 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3b00ef10
TT
5794
5795 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5796 if (!per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5797 return;
5798
976ca316 5799 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
3b00ef10
TT
5800 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5801
5802 const char *match_name = name.ada ().lookup_name ().c_str ();
5803 auto matcher = [&] (const char *symname)
5804 {
5805 if (ordered_compare == nullptr)
5806 return true;
5807 return ordered_compare (symname, match_name) == 0;
5808 };
5809
5810 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5811 [&] (offset_type namei)
5812 {
5813 /* The name was matched, now expand corresponding CUs that were
5814 marked. */
fcf23d5b 5815 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
976ca316 5816 per_objfile);
3b00ef10
TT
5817
5818 struct dwarf2_per_cu_data *per_cu;
5819 while ((per_cu = iter.next ()) != NULL)
976ca316 5820 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 5821 nullptr);
3b00ef10 5822 return true;
976ca316 5823 }, per_objfile);
3b00ef10
TT
5824
5825 /* It's a shame we couldn't do this inside the
5826 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5827 that have already been expanded. Instead, this loop matches what
5828 the psymtab code does. */
976ca316 5829 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3b00ef10 5830 {
976ca316 5831 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
af758d11 5832 if (symtab != nullptr)
3b00ef10
TT
5833 {
5834 const struct block *block
af758d11 5835 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
3b00ef10
TT
5836 if (!iterate_over_symbols_terminated (block, name,
5837 domain, callback))
5838 break;
5839 }
5840 }
5841}
5842
927aa2e7
JK
5843static void
5844dw2_debug_names_expand_symtabs_matching
5845 (struct objfile *objfile,
5846 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5847 const lookup_name_info *lookup_name,
927aa2e7
JK
5848 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5849 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5850 enum search_domain kind)
5851{
976ca316 5852 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 5853
927aa2e7 5854 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5855 if (!per_objfile->per_bfd->debug_names_table)
927aa2e7 5856 return;
9291a0cd 5857
976ca316 5858 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
24c79950 5859
c1a66c06
TV
5860 if (symbol_matcher == NULL && lookup_name == NULL)
5861 {
976ca316 5862 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5863 {
5864 QUIT;
5865
976ca316
SM
5866 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5867 expansion_notify);
c1a66c06
TV
5868 }
5869 return;
5870 }
5871
976ca316 5872 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
bbf2f4df 5873
c1a66c06 5874 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
5875 symbol_matcher,
5876 kind, [&] (offset_type namei)
927aa2e7 5877 {
927aa2e7
JK
5878 /* The name was matched, now expand corresponding CUs that were
5879 marked. */
976ca316 5880 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
bbf2f4df 5881
927aa2e7
JK
5882 struct dwarf2_per_cu_data *per_cu;
5883 while ((per_cu = iter.next ()) != NULL)
976ca316
SM
5884 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5885 expansion_notify);
3b00ef10 5886 return true;
976ca316 5887 }, per_objfile);
9291a0cd
TT
5888}
5889
927aa2e7 5890const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5891{
5892 dw2_has_symbols,
5893 dw2_find_last_source_symtab,
5894 dw2_forget_cached_source_info,
f8eba3c6 5895 dw2_map_symtabs_matching_filename,
927aa2e7 5896 dw2_debug_names_lookup_symbol,
d3214198 5897 NULL,
9291a0cd 5898 dw2_print_stats,
927aa2e7 5899 dw2_debug_names_dump,
927aa2e7 5900 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5901 dw2_expand_all_symtabs,
652a8996 5902 dw2_expand_symtabs_with_fullname,
3b00ef10 5903 dw2_debug_names_map_matching_symbols,
927aa2e7 5904 dw2_debug_names_expand_symtabs_matching,
43f3e411 5905 dw2_find_pc_sect_compunit_symtab,
71a3c369 5906 NULL,
9291a0cd
TT
5907 dw2_map_symbol_filenames
5908};
5909
4485a1c1 5910/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5911 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5912
5913template <typename T>
5914static gdb::array_view<const gdb_byte>
5915get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5916{
5917 dwarf2_section_info *section = &section_owner->gdb_index;
5918
96b79293 5919 if (section->empty ())
4485a1c1
SM
5920 return {};
5921
5922 /* Older elfutils strip versions could keep the section in the main
5923 executable while splitting it for the separate debug info file. */
96b79293 5924 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5925 return {};
5926
96b79293 5927 section->read (obj);
4485a1c1 5928
8bebfcda
PA
5929 /* dwarf2_section_info::size is a bfd_size_type, while
5930 gdb::array_view works with size_t. On 32-bit hosts, with
5931 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5932 is 32-bit. So we need an explicit narrowing conversion here.
5933 This is fine, because it's impossible to allocate or mmap an
5934 array/buffer larger than what size_t can represent. */
5935 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5936}
5937
87d6a7aa
SM
5938/* Lookup the index cache for the contents of the index associated to
5939 DWARF2_OBJ. */
5940
5941static gdb::array_view<const gdb_byte>
5989a64e 5942get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5943{
5944 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5945 if (build_id == nullptr)
5946 return {};
5947
5948 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5949 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5950}
5951
5952/* Same as the above, but for DWZ. */
5953
5954static gdb::array_view<const gdb_byte>
5955get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5956{
5957 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5958 if (build_id == nullptr)
5959 return {};
5960
5961 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5962}
5963
3c0aa29a 5964/* See symfile.h. */
9291a0cd 5965
3c0aa29a
PA
5966bool
5967dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5968{
976ca316
SM
5969 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5970 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
ed2dc618 5971
9291a0cd
TT
5972 /* If we're about to read full symbols, don't bother with the
5973 indices. In this case we also don't care if some other debug
5974 format is making psymtabs, because they are all about to be
5975 expanded anyway. */
5976 if ((objfile->flags & OBJF_READNOW))
5977 {
17ee85fc
TT
5978 /* When using READNOW, the using_index flag (set below) indicates that
5979 PER_BFD was already initialized, when we loaded some other objfile. */
5980 if (per_bfd->using_index)
5981 {
5982 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 5983 per_objfile->resize_symtabs ();
17ee85fc
TT
5984 return true;
5985 }
5986
5987 per_bfd->using_index = 1;
976ca316
SM
5988 create_all_comp_units (per_objfile);
5989 create_all_type_units (per_objfile);
17ee85fc
TT
5990 per_bfd->quick_file_names_table
5991 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
976ca316 5992 per_objfile->resize_symtabs ();
9291a0cd 5993
17ee85fc
TT
5994 for (int i = 0; i < (per_bfd->all_comp_units.size ()
5995 + per_bfd->all_type_units.size ()); ++i)
9291a0cd 5996 {
17ee85fc 5997 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
9291a0cd 5998
17ee85fc 5999 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
e254ef6a 6000 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6001 }
6002
6003 /* Return 1 so that gdb sees the "quick" functions. However,
6004 these functions will be no-ops because we will have expanded
6005 all symtabs. */
3c0aa29a
PA
6006 *index_kind = dw_index_kind::GDB_INDEX;
6007 return true;
9291a0cd
TT
6008 }
6009
17ee85fc
TT
6010 /* Was a debug names index already read when we processed an objfile sharing
6011 PER_BFD? */
6012 if (per_bfd->debug_names_table != nullptr)
6013 {
6014 *index_kind = dw_index_kind::DEBUG_NAMES;
f8c41851 6015 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6016 per_objfile->resize_symtabs ();
17ee85fc
TT
6017 return true;
6018 }
6019
6020 /* Was a GDB index already read when we processed an objfile sharing
6021 PER_BFD? */
6022 if (per_bfd->index_table != nullptr)
6023 {
6024 *index_kind = dw_index_kind::GDB_INDEX;
f8c41851 6025 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6026 per_objfile->resize_symtabs ();
17ee85fc
TT
6027 return true;
6028 }
6029
976ca316 6030 if (dwarf2_read_debug_names (per_objfile))
3c0aa29a
PA
6031 {
6032 *index_kind = dw_index_kind::DEBUG_NAMES;
976ca316 6033 per_objfile->resize_symtabs ();
3c0aa29a
PA
6034 return true;
6035 }
927aa2e7 6036
976ca316 6037 if (dwarf2_read_gdb_index (per_objfile,
5989a64e 6038 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 6039 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6040 {
6041 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6042 per_objfile->resize_symtabs ();
3c0aa29a
PA
6043 return true;
6044 }
9291a0cd 6045
87d6a7aa 6046 /* ... otherwise, try to find the index in the index cache. */
976ca316 6047 if (dwarf2_read_gdb_index (per_objfile,
87d6a7aa
SM
6048 get_gdb_index_contents_from_cache,
6049 get_gdb_index_contents_from_cache_dwz))
6050 {
6051 global_index_cache.hit ();
6052 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6053 per_objfile->resize_symtabs ();
87d6a7aa
SM
6054 return true;
6055 }
6056
6057 global_index_cache.miss ();
3c0aa29a 6058 return false;
9291a0cd
TT
6059}
6060
6061\f
6062
dce234bc
PP
6063/* Build a partial symbol table. */
6064
6065void
f29dff0a 6066dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6067{
976ca316
SM
6068 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6069 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
17ee85fc
TT
6070
6071 if (per_bfd->partial_symtabs != nullptr)
6072 {
6073 /* Partial symbols were already read, so now we can simply
6074 attach them. */
6075 objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6076 per_objfile->resize_symtabs ();
17ee85fc
TT
6077 return;
6078 }
c9bf0622 6079
6eee24ce 6080 init_psymbol_list (objfile, 1024);
c906108c 6081
a70b8144 6082 try
c9bf0622
TT
6083 {
6084 /* This isn't really ideal: all the data we allocate on the
6085 objfile's obstack is still uselessly kept around. However,
6086 freeing it seems unsafe. */
906768f9 6087 psymtab_discarder psymtabs (objfile);
976ca316 6088 dwarf2_build_psymtabs_hard (per_objfile);
906768f9 6089 psymtabs.keep ();
87d6a7aa 6090
976ca316 6091 per_objfile->resize_symtabs ();
af758d11 6092
87d6a7aa 6093 /* (maybe) store an index in the cache. */
976ca316 6094 global_index_cache.store (per_objfile);
c9bf0622 6095 }
230d2906 6096 catch (const gdb_exception_error &except)
492d29ea
PA
6097 {
6098 exception_print (gdb_stderr, except);
6099 }
17ee85fc
TT
6100
6101 /* Finish by setting the local reference to partial symtabs, so that
6102 we don't try to read them again if reading another objfile with the same
6103 BFD. If we can't in fact share, this won't make a difference anyway as
6104 the dwarf2_per_bfd object won't be shared. */
6105 per_bfd->partial_symtabs = objfile->partial_symtabs;
c906108c 6106}
c906108c 6107
3b80fe9b
DE
6108/* Find the base address of the compilation unit for range lists and
6109 location lists. It will normally be specified by DW_AT_low_pc.
6110 In DWARF-3 draft 4, the base address could be overridden by
6111 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6112 compilation units with discontinuous ranges. */
6113
6114static void
6115dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6116{
6117 struct attribute *attr;
6118
2b24b6e4 6119 cu->base_address.reset ();
3b80fe9b
DE
6120
6121 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6122 if (attr != nullptr)
2b24b6e4 6123 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6124 else
6125 {
6126 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6127 if (attr != nullptr)
2b24b6e4 6128 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6129 }
6130}
6131
36586728
TT
6132/* Helper function that returns the proper abbrev section for
6133 THIS_CU. */
6134
6135static struct dwarf2_section_info *
6136get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6137{
6138 struct dwarf2_section_info *abbrev;
c3699833 6139 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
6140
6141 if (this_cu->is_dwz)
c3699833 6142 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
36586728 6143 else
c3699833 6144 abbrev = &per_bfd->abbrev;
36586728
TT
6145
6146 return abbrev;
6147}
6148
f4dc4d17
DE
6149/* Fetch the abbreviation table offset from a comp or type unit header. */
6150
6151static sect_offset
976ca316 6152read_abbrev_offset (dwarf2_per_objfile *per_objfile,
ed2dc618 6153 struct dwarf2_section_info *section,
9c541725 6154 sect_offset sect_off)
f4dc4d17 6155{
96b79293 6156 bfd *abfd = section->get_bfd_owner ();
d521ce57 6157 const gdb_byte *info_ptr;
ac298888 6158 unsigned int initial_length_size, offset_size;
43988095 6159 uint16_t version;
f4dc4d17 6160
976ca316 6161 section->read (per_objfile->objfile);
9c541725 6162 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6163 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6164 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6165 info_ptr += initial_length_size;
6166
6167 version = read_2_bytes (abfd, info_ptr);
6168 info_ptr += 2;
6169 if (version >= 5)
6170 {
6171 /* Skip unit type and address size. */
6172 info_ptr += 2;
6173 }
6174
24aa364d 6175 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6176}
6177
b83470bf
TT
6178/* A partial symtab that is used only for include files. */
6179struct dwarf2_include_psymtab : public partial_symtab
6180{
6181 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6182 : partial_symtab (filename, objfile)
6183 {
6184 }
6185
6186 void read_symtab (struct objfile *objfile) override
6187 {
194d088f
TV
6188 /* It's an include file, no symbols to read for it.
6189 Everything is in the includer symtab. */
6190
6191 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6192 expansion of the includer psymtab. We use the dependencies[0] field to
6193 model the includer. But if we go the regular route of calling
6194 expand_psymtab here, and having expand_psymtab call expand_dependencies
6195 to expand the includer, we'll only use expand_psymtab on the includer
6196 (making it a non-toplevel psymtab), while if we expand the includer via
6197 another path, we'll use read_symtab (making it a toplevel psymtab).
6198 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6199 psymtab, and trigger read_symtab on the includer here directly. */
6200 includer ()->read_symtab (objfile);
b83470bf
TT
6201 }
6202
6203 void expand_psymtab (struct objfile *objfile) override
6204 {
194d088f
TV
6205 /* This is not called by read_symtab, and should not be called by any
6206 expand_dependencies. */
6207 gdb_assert (false);
b83470bf
TT
6208 }
6209
5717c425 6210 bool readin_p (struct objfile *objfile) const override
b83470bf 6211 {
5717c425 6212 return includer ()->readin_p (objfile);
b83470bf
TT
6213 }
6214
5717c425 6215 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6216 {
6217 return nullptr;
6218 }
6219
6220private:
194d088f
TV
6221 partial_symtab *includer () const
6222 {
6223 /* An include psymtab has exactly one dependency: the psymtab that
6224 includes it. */
6225 gdb_assert (this->number_of_dependencies == 1);
6226 return this->dependencies[0];
6227 }
b83470bf
TT
6228};
6229
aaa75496
JB
6230/* Allocate a new partial symtab for file named NAME and mark this new
6231 partial symtab as being an include of PST. */
6232
6233static void
891813be 6234dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
6235 struct objfile *objfile)
6236{
b83470bf 6237 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6238
fbd9ab74 6239 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6240 subpst->dirname = pst->dirname;
fbd9ab74 6241
a9342b62 6242 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6243 subpst->dependencies[0] = pst;
6244 subpst->number_of_dependencies = 1;
aaa75496
JB
6245}
6246
6247/* Read the Line Number Program data and extract the list of files
6248 included by the source file represented by PST. Build an include
d85a05f0 6249 partial symtab for each of these included files. */
aaa75496
JB
6250
6251static void
6252dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6253 struct die_info *die,
891813be 6254 dwarf2_psymtab *pst)
aaa75496 6255{
fff8551c 6256 line_header_up lh;
d85a05f0 6257 struct attribute *attr;
aaa75496 6258
d85a05f0 6259 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6260 if (attr != nullptr)
9c541725 6261 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6262 if (lh == NULL)
6263 return; /* No linetable, so no includes. */
6264
79748972
TT
6265 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6266 that we pass in the raw text_low here; that is ok because we're
6267 only decoding the line table to make include partial symtabs, and
6268 so the addresses aren't really used. */
4ae976d1 6269 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6270 pst->raw_text_low (), 1);
aaa75496
JB
6271}
6272
348e048f 6273static hashval_t
52dc124a 6274hash_signatured_type (const void *item)
348e048f 6275{
9a3c8263
SM
6276 const struct signatured_type *sig_type
6277 = (const struct signatured_type *) item;
9a619af0 6278
348e048f 6279 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6280 return sig_type->signature;
348e048f
DE
6281}
6282
6283static int
52dc124a 6284eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6285{
9a3c8263
SM
6286 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6287 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6288
348e048f
DE
6289 return lhs->signature == rhs->signature;
6290}
6291
1fd400ff
TT
6292/* Allocate a hash table for signatured types. */
6293
b0b6a987 6294static htab_up
298e9637 6295allocate_signatured_type_table ()
1fd400ff 6296{
b0b6a987
TT
6297 return htab_up (htab_create_alloc (41,
6298 hash_signatured_type,
6299 eq_signatured_type,
6300 NULL, xcalloc, xfree));
1fd400ff
TT
6301}
6302
d467dd73 6303/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6304
6305static int
d467dd73 6306add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6307{
9a3c8263 6308 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6309 std::vector<signatured_type *> *all_type_units
6310 = (std::vector<signatured_type *> *) datum;
1fd400ff 6311
b2bdb8cf 6312 all_type_units->push_back (sigt);
1fd400ff
TT
6313
6314 return 1;
6315}
6316
78d4d2c5 6317/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6318 and fill them into TYPES_HTAB. It will process only type units,
6319 therefore DW_UT_type. */
c88ee1f0 6320
78d4d2c5 6321static void
976ca316 6322create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6323 struct dwo_file *dwo_file,
b0b6a987 6324 dwarf2_section_info *section, htab_up &types_htab,
43988095 6325 rcuh_kind section_kind)
348e048f 6326{
976ca316 6327 struct objfile *objfile = per_objfile->objfile;
4bdcc0c1 6328 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6329 bfd *abfd;
6330 const gdb_byte *info_ptr, *end_ptr;
348e048f 6331
4bdcc0c1
DE
6332 abbrev_section = (dwo_file != NULL
6333 ? &dwo_file->sections.abbrev
976ca316 6334 : &per_objfile->per_bfd->abbrev);
4bdcc0c1 6335
b4f54984 6336 if (dwarf_read_debug)
43988095 6337 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6338 section->get_name (),
6339 abbrev_section->get_file_name ());
09406207 6340
96b79293 6341 section->read (objfile);
78d4d2c5 6342 info_ptr = section->buffer;
348e048f 6343
78d4d2c5
JK
6344 if (info_ptr == NULL)
6345 return;
348e048f 6346
78d4d2c5
JK
6347 /* We can't set abfd until now because the section may be empty or
6348 not present, in which case the bfd is unknown. */
96b79293 6349 abfd = section->get_bfd_owner ();
348e048f 6350
c0ab21c2
TT
6351 /* We don't use cutu_reader here because we don't need to read
6352 any dies: the signature is in the header. */
3019eac3 6353
78d4d2c5
JK
6354 end_ptr = info_ptr + section->size;
6355 while (info_ptr < end_ptr)
6356 {
78d4d2c5
JK
6357 struct signatured_type *sig_type;
6358 struct dwo_unit *dwo_tu;
6359 void **slot;
6360 const gdb_byte *ptr = info_ptr;
6361 struct comp_unit_head header;
6362 unsigned int length;
8b70b953 6363
9c541725 6364 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6365
a49dd8dd
JK
6366 /* Initialize it due to a false compiler warning. */
6367 header.signature = -1;
9c541725 6368 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6369
78d4d2c5
JK
6370 /* We need to read the type's signature in order to build the hash
6371 table, but we don't need anything else just yet. */
348e048f 6372
976ca316 6373 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
43988095 6374 abbrev_section, ptr, section_kind);
348e048f 6375
4057dfde 6376 length = header.get_length ();
6caca83c 6377
78d4d2c5
JK
6378 /* Skip dummy type units. */
6379 if (ptr >= info_ptr + length
43988095
JK
6380 || peek_abbrev_code (abfd, ptr) == 0
6381 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6382 {
6383 info_ptr += length;
6384 continue;
6385 }
dee91e82 6386
78d4d2c5
JK
6387 if (types_htab == NULL)
6388 {
6389 if (dwo_file)
298e9637 6390 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6391 else
298e9637 6392 types_htab = allocate_signatured_type_table ();
78d4d2c5 6393 }
8b70b953 6394
78d4d2c5
JK
6395 if (dwo_file)
6396 {
6397 sig_type = NULL;
976ca316 6398 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
78d4d2c5 6399 dwo_tu->dwo_file = dwo_file;
43988095 6400 dwo_tu->signature = header.signature;
9c541725 6401 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6402 dwo_tu->section = section;
9c541725 6403 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6404 dwo_tu->length = length;
6405 }
6406 else
6407 {
6408 /* N.B.: type_offset is not usable if this type uses a DWO file.
6409 The real type_offset is in the DWO file. */
6410 dwo_tu = NULL;
976ca316 6411 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
43988095 6412 sig_type->signature = header.signature;
9c541725 6413 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
6414 sig_type->per_cu.is_debug_types = 1;
6415 sig_type->per_cu.section = section;
9c541725 6416 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6417 sig_type->per_cu.length = length;
6418 }
6419
b0b6a987 6420 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6421 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6422 INSERT);
6423 gdb_assert (slot != NULL);
6424 if (*slot != NULL)
6425 {
9c541725 6426 sect_offset dup_sect_off;
0349ea22 6427
3019eac3
DE
6428 if (dwo_file)
6429 {
78d4d2c5
JK
6430 const struct dwo_unit *dup_tu
6431 = (const struct dwo_unit *) *slot;
6432
9c541725 6433 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6434 }
6435 else
6436 {
78d4d2c5
JK
6437 const struct signatured_type *dup_tu
6438 = (const struct signatured_type *) *slot;
6439
9c541725 6440 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6441 }
8b70b953 6442
b98664d3 6443 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6444 " the entry at offset %s, signature %s"),
6445 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6446 hex_string (header.signature));
78d4d2c5
JK
6447 }
6448 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6449
78d4d2c5 6450 if (dwarf_read_debug > 1)
9d8780f0
SM
6451 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6452 sect_offset_str (sect_off),
43988095 6453 hex_string (header.signature));
3019eac3 6454
78d4d2c5
JK
6455 info_ptr += length;
6456 }
6457}
3019eac3 6458
78d4d2c5
JK
6459/* Create the hash table of all entries in the .debug_types
6460 (or .debug_types.dwo) section(s).
6461 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6462 otherwise it is NULL.
b3c8eb43 6463
78d4d2c5 6464 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6465
78d4d2c5 6466 Note: This function processes DWO files only, not DWP files. */
348e048f 6467
78d4d2c5 6468static void
976ca316 6469create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6470 struct dwo_file *dwo_file,
fd5866f6 6471 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6472 htab_up &types_htab)
78d4d2c5 6473{
fd5866f6 6474 for (dwarf2_section_info &section : type_sections)
976ca316
SM
6475 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6476 rcuh_kind::TYPE);
3019eac3
DE
6477}
6478
6479/* Create the hash table of all entries in the .debug_types section,
6480 and initialize all_type_units.
6481 The result is zero if there is an error (e.g. missing .debug_types section),
6482 otherwise non-zero. */
6483
6484static int
976ca316 6485create_all_type_units (dwarf2_per_objfile *per_objfile)
3019eac3 6486{
b0b6a987 6487 htab_up types_htab;
3019eac3 6488
976ca316
SM
6489 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6490 types_htab, rcuh_kind::COMPILE);
6491 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6492 types_htab);
3019eac3
DE
6493 if (types_htab == NULL)
6494 {
976ca316 6495 per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6496 return 0;
6497 }
6498
976ca316 6499 per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6500
976ca316
SM
6501 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6502 per_objfile->per_bfd->all_type_units.reserve
6503 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6504
976ca316 6505 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6506 add_signatured_type_cu_to_table,
976ca316 6507 &per_objfile->per_bfd->all_type_units);
1fd400ff 6508
348e048f
DE
6509 return 1;
6510}
6511
5989a64e 6512/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6513 If SLOT is non-NULL, it is the entry to use in the hash table.
6514 Otherwise we find one. */
6515
6516static struct signatured_type *
976ca316 6517add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6aa5f3a6 6518{
976ca316
SM
6519 if (per_objfile->per_bfd->all_type_units.size ()
6520 == per_objfile->per_bfd->all_type_units.capacity ())
6521 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6522
976ca316 6523 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6524
976ca316 6525 per_objfile->resize_symtabs ();
af758d11 6526
976ca316 6527 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6528 sig_type->signature = sig;
6529 sig_type->per_cu.is_debug_types = 1;
976ca316 6530 if (per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6531 {
6532 sig_type->per_cu.v.quick =
976ca316 6533 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6534 struct dwarf2_per_cu_quick_data);
6535 }
6536
6537 if (slot == NULL)
6538 {
976ca316 6539 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6540 sig_type, INSERT);
6541 }
6542 gdb_assert (*slot == NULL);
6543 *slot = sig_type;
6544 /* The rest of sig_type must be filled in by the caller. */
6545 return sig_type;
6546}
6547
a2ce51a0
DE
6548/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6549 Fill in SIG_ENTRY with DWO_ENTRY. */
6550
6551static void
976ca316 6552fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
a2ce51a0
DE
6553 struct signatured_type *sig_entry,
6554 struct dwo_unit *dwo_entry)
6555{
976ca316 6556 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1859c670 6557
7ee85ab1 6558 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0 6559 gdb_assert (! sig_entry->per_cu.queued);
976ca316 6560 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
1859c670 6561 if (per_bfd->using_index)
6aa5f3a6
DE
6562 {
6563 gdb_assert (sig_entry->per_cu.v.quick != NULL);
976ca316 6564 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6565 }
6566 else
6567 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6568 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6569 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6570 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6571 gdb_assert (sig_entry->dwo_unit == NULL);
6572
6573 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6574 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6575 sig_entry->per_cu.length = dwo_entry->length;
6576 sig_entry->per_cu.reading_dwo_directly = 1;
1859c670 6577 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6578 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6579 sig_entry->dwo_unit = dwo_entry;
6580}
6581
6582/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6583 If we haven't read the TU yet, create the signatured_type data structure
6584 for a TU to be read in directly from a DWO file, bypassing the stub.
6585 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6586 using .gdb_index, then when reading a CU we want to stay in the DWO file
6587 containing that CU. Otherwise we could end up reading several other DWO
6588 files (due to comdat folding) to process the transitive closure of all the
6589 mentioned TUs, and that can be slow. The current DWO file will have every
6590 type signature that it needs.
a2ce51a0
DE
6591 We only do this for .gdb_index because in the psymtab case we already have
6592 to read all the DWOs to build the type unit groups. */
6593
6594static struct signatured_type *
6595lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6596{
976ca316 6597 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a2ce51a0
DE
6598 struct dwo_file *dwo_file;
6599 struct dwo_unit find_dwo_entry, *dwo_entry;
6600 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6601 void **slot;
a2ce51a0 6602
976ca316 6603 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0 6604
6aa5f3a6
DE
6605 /* If TU skeletons have been removed then we may not have read in any
6606 TUs yet. */
976ca316
SM
6607 if (per_objfile->per_bfd->signatured_types == NULL)
6608 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6609
6610 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6611 Use the global signatured_types array to do our own comdat-folding
6612 of types. If this is the first time we're reading this TU, and
6613 the TU has an entry in .gdb_index, replace the recorded data from
6614 .gdb_index with this TU. */
a2ce51a0 6615
a2ce51a0 6616 find_sig_entry.signature = sig;
976ca316 6617 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6618 &find_sig_entry, INSERT);
9a3c8263 6619 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6620
6621 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6622 read. Don't reassign the global entry to point to this DWO if that's
6623 the case. Also note that if the TU is already being read, it may not
6624 have come from a DWO, the program may be a mix of Fission-compiled
6625 code and non-Fission-compiled code. */
6626
6627 /* Have we already tried to read this TU?
6628 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6629 needn't exist in the global table yet). */
6630 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6631 return sig_entry;
6632
6aa5f3a6
DE
6633 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6634 dwo_unit of the TU itself. */
6635 dwo_file = cu->dwo_unit->dwo_file;
6636
a2ce51a0
DE
6637 /* Ok, this is the first time we're reading this TU. */
6638 if (dwo_file->tus == NULL)
6639 return NULL;
6640 find_dwo_entry.signature = sig;
b0b6a987
TT
6641 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6642 &find_dwo_entry);
a2ce51a0
DE
6643 if (dwo_entry == NULL)
6644 return NULL;
6645
6aa5f3a6
DE
6646 /* If the global table doesn't have an entry for this TU, add one. */
6647 if (sig_entry == NULL)
976ca316 6648 sig_entry = add_type_unit (per_objfile, sig, slot);
6aa5f3a6 6649
976ca316 6650 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
89e63ee4 6651 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6652 return sig_entry;
6653}
6654
a2ce51a0
DE
6655/* Subroutine of lookup_signatured_type.
6656 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6657 then try the DWP file. If the TU stub (skeleton) has been removed then
6658 it won't be in .gdb_index. */
a2ce51a0
DE
6659
6660static struct signatured_type *
6661lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6662{
976ca316
SM
6663 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6664 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
a2ce51a0
DE
6665 struct dwo_unit *dwo_entry;
6666 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6667 void **slot;
a2ce51a0 6668
976ca316 6669 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0
DE
6670 gdb_assert (dwp_file != NULL);
6671
6aa5f3a6
DE
6672 /* If TU skeletons have been removed then we may not have read in any
6673 TUs yet. */
976ca316
SM
6674 if (per_objfile->per_bfd->signatured_types == NULL)
6675 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6676
6aa5f3a6 6677 find_sig_entry.signature = sig;
976ca316 6678 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6679 &find_sig_entry, INSERT);
9a3c8263 6680 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6681
6682 /* Have we already tried to read this TU?
6683 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6684 needn't exist in the global table yet). */
6685 if (sig_entry != NULL)
6686 return sig_entry;
6687
a2ce51a0
DE
6688 if (dwp_file->tus == NULL)
6689 return NULL;
976ca316
SM
6690 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6691 1 /* is_debug_types */);
a2ce51a0
DE
6692 if (dwo_entry == NULL)
6693 return NULL;
6694
976ca316
SM
6695 sig_entry = add_type_unit (per_objfile, sig, slot);
6696 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
a2ce51a0 6697
a2ce51a0
DE
6698 return sig_entry;
6699}
6700
380bca97 6701/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6702 Returns NULL if signature SIG is not present in the table.
6703 It is up to the caller to complain about this. */
348e048f
DE
6704
6705static struct signatured_type *
a2ce51a0 6706lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6707{
976ca316 6708 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 6709
976ca316 6710 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
a2ce51a0
DE
6711 {
6712 /* We're in a DWO/DWP file, and we're using .gdb_index.
6713 These cases require special processing. */
976ca316 6714 if (get_dwp_file (per_objfile) == NULL)
a2ce51a0
DE
6715 return lookup_dwo_signatured_type (cu, sig);
6716 else
6717 return lookup_dwp_signatured_type (cu, sig);
6718 }
6719 else
6720 {
6721 struct signatured_type find_entry, *entry;
348e048f 6722
976ca316 6723 if (per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6724 return NULL;
6725 find_entry.signature = sig;
9a3c8263 6726 entry = ((struct signatured_type *)
976ca316 6727 htab_find (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6728 &find_entry));
a2ce51a0
DE
6729 return entry;
6730 }
348e048f 6731}
18a8505e 6732
42e7ad6c 6733/* Low level DIE reading support. */
348e048f 6734
d85a05f0
DJ
6735/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6736
6737static void
6738init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6739 struct dwarf2_cu *cu,
3019eac3 6740 struct dwarf2_section_info *section,
685af9cd
TT
6741 struct dwo_file *dwo_file,
6742 struct abbrev_table *abbrev_table)
d85a05f0 6743{
fceca515 6744 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6745 reader->abfd = section->get_bfd_owner ();
d85a05f0 6746 reader->cu = cu;
3019eac3 6747 reader->dwo_file = dwo_file;
dee91e82
DE
6748 reader->die_section = section;
6749 reader->buffer = section->buffer;
f664829e 6750 reader->buffer_end = section->buffer + section->size;
685af9cd 6751 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6752}
6753
c0ab21c2 6754/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6755 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6756 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6757 already.
6758
6759 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6760 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6761 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6762 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6763 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6764 STUB_COMP_DIR may be non-NULL.
3e225074 6765 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6766 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6767 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6768 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6769 kept around for at least as long as *RESULT_READER.
6770
b0c7bfa9
DE
6771 The result is non-zero if a valid (non-dummy) DIE was found. */
6772
6773static int
4ab09049 6774read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6775 struct dwo_unit *dwo_unit,
b0c7bfa9 6776 struct die_info *stub_comp_unit_die,
a2ce51a0 6777 const char *stub_comp_dir,
b0c7bfa9 6778 struct die_reader_specs *result_reader,
d521ce57 6779 const gdb_byte **result_info_ptr,
b0c7bfa9 6780 struct die_info **result_comp_unit_die,
685af9cd 6781 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6782{
976ca316 6783 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4ab09049 6784 dwarf2_per_cu_data *per_cu = cu->per_cu;
976ca316 6785 struct objfile *objfile = per_objfile->objfile;
b0c7bfa9 6786 bfd *abfd;
d521ce57 6787 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6788 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6789 int i,num_extra_attrs;
6790 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6791 struct die_info *comp_unit_die;
6792
b0aeadb3
DE
6793 /* At most one of these may be provided. */
6794 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6795
b0c7bfa9
DE
6796 /* These attributes aren't processed until later:
6797 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6798 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6799 referenced later. However, these attributes are found in the stub
6800 which we won't have later. In order to not impose this complication
6801 on the rest of the code, we read them here and copy them to the
6802 DWO CU/TU die. */
b0c7bfa9
DE
6803
6804 stmt_list = NULL;
6805 low_pc = NULL;
6806 high_pc = NULL;
6807 ranges = NULL;
6808 comp_dir = NULL;
6809
6810 if (stub_comp_unit_die != NULL)
6811 {
6812 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6813 DWO file. */
4ab09049 6814 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6815 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6816 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6817 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6818 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6819 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6820
a39fdb41 6821 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6822
18a8505e
AT
6823 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6824 here (if needed). We need the value before we can process
6825 DW_AT_ranges. */
a39fdb41 6826 cu->ranges_base = stub_comp_unit_die->ranges_base ();
b0c7bfa9 6827 }
a2ce51a0
DE
6828 else if (stub_comp_dir != NULL)
6829 {
6830 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6831 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6832 comp_dir->name = DW_AT_comp_dir;
6833 comp_dir->form = DW_FORM_string;
6834 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6835 DW_STRING (comp_dir) = stub_comp_dir;
6836 }
b0c7bfa9
DE
6837
6838 /* Set up for reading the DWO CU/TU. */
6839 cu->dwo_unit = dwo_unit;
685af9cd 6840 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6841 section->read (objfile);
6842 abfd = section->get_bfd_owner ();
9c541725
PA
6843 begin_info_ptr = info_ptr = (section->buffer
6844 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6845 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6846
4ab09049 6847 if (per_cu->is_debug_types)
b0c7bfa9 6848 {
4ab09049 6849 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6850
976ca316
SM
6851 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6852 section, dwo_abbrev_section,
43988095 6853 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6854 /* This is not an assert because it can be caused by bad debug info. */
43988095 6855 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6856 {
6857 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6858 " TU at offset %s [in module %s]"),
a2ce51a0 6859 hex_string (sig_type->signature),
43988095 6860 hex_string (cu->header.signature),
9d8780f0 6861 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6862 bfd_get_filename (abfd));
6863 }
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 nor the type's offset in the TU until now. */
4057dfde 6867 dwo_unit->length = cu->header.get_length ();
9c541725 6868 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6869
6870 /* Establish the type offset that can be used to lookup the type.
6871 For DWO files, we don't know it until now. */
9c541725
PA
6872 sig_type->type_offset_in_section
6873 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6874 }
6875 else
6876 {
976ca316
SM
6877 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6878 section, dwo_abbrev_section,
43988095 6879 info_ptr, rcuh_kind::COMPILE);
9c541725 6880 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6881 /* For DWOs coming from DWP files, we don't know the CU length
6882 until now. */
4057dfde 6883 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6884 }
6885
685af9cd 6886 *result_dwo_abbrev_table
86de1d91
TT
6887 = abbrev_table::read (objfile, dwo_abbrev_section,
6888 cu->header.abbrev_sect_off);
685af9cd
TT
6889 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6890 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6891
6892 /* Read in the die, but leave space to copy over the attributes
6893 from the stub. This has the benefit of simplifying the rest of
6894 the code - all the work to maintain the illusion of a single
6895 DW_TAG_{compile,type}_unit DIE is done here. */
6896 num_extra_attrs = ((stmt_list != NULL)
6897 + (low_pc != NULL)
6898 + (high_pc != NULL)
6899 + (ranges != NULL)
6900 + (comp_dir != NULL));
6901 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6902 num_extra_attrs);
b0c7bfa9
DE
6903
6904 /* Copy over the attributes from the stub to the DIE we just read in. */
6905 comp_unit_die = *result_comp_unit_die;
6906 i = comp_unit_die->num_attrs;
6907 if (stmt_list != NULL)
6908 comp_unit_die->attrs[i++] = *stmt_list;
6909 if (low_pc != NULL)
6910 comp_unit_die->attrs[i++] = *low_pc;
6911 if (high_pc != NULL)
6912 comp_unit_die->attrs[i++] = *high_pc;
6913 if (ranges != NULL)
6914 comp_unit_die->attrs[i++] = *ranges;
6915 if (comp_dir != NULL)
6916 comp_unit_die->attrs[i++] = *comp_dir;
6917 comp_unit_die->num_attrs += num_extra_attrs;
6918
b4f54984 6919 if (dwarf_die_debug)
bf6af496
DE
6920 {
6921 fprintf_unfiltered (gdb_stdlog,
6922 "Read die from %s@0x%x of %s:\n",
96b79293 6923 section->get_name (),
bf6af496
DE
6924 (unsigned) (begin_info_ptr - section->buffer),
6925 bfd_get_filename (abfd));
b4f54984 6926 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6927 }
6928
b0c7bfa9
DE
6929 /* Skip dummy compilation units. */
6930 if (info_ptr >= begin_info_ptr + dwo_unit->length
6931 || peek_abbrev_code (abfd, info_ptr) == 0)
6932 return 0;
6933
6934 *result_info_ptr = info_ptr;
6935 return 1;
6936}
6937
a084a2a6
AT
6938/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6939 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6940 signature is part of the header. */
6941static gdb::optional<ULONGEST>
6942lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6943{
6944 if (cu->header.version >= 5)
6945 return cu->header.signature;
6946 struct attribute *attr;
6947 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6948 if (attr == nullptr)
6949 return gdb::optional<ULONGEST> ();
6950 return DW_UNSND (attr);
6951}
6952
c0ab21c2 6953/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6954 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6955 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6956
6957static struct dwo_unit *
4ab09049 6958lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 6959{
4ab09049 6960 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6961 struct dwo_unit *dwo_unit;
c0ab21c2 6962 const char *comp_dir;
b0c7bfa9 6963
a2ce51a0
DE
6964 gdb_assert (cu != NULL);
6965
b0c7bfa9 6966 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6967 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6968 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 6969
4ab09049
SM
6970 if (per_cu->is_debug_types)
6971 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
6972 else
6973 {
a084a2a6 6974 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 6975
a084a2a6 6976 if (!signature.has_value ())
b0c7bfa9
DE
6977 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6978 " [in module %s]"),
4ab09049
SM
6979 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6980
6981 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
6982 }
6983
b0c7bfa9
DE
6984 return dwo_unit;
6985}
6986
c0ab21c2 6987/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6988 See it for a description of the parameters.
fcd3b13d 6989 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6990
c0ab21c2 6991void
9e021579
SM
6992cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6993 dwarf2_per_objfile *per_objfile,
2e671100 6994 dwarf2_cu *existing_cu)
a2ce51a0 6995{
a2ce51a0 6996 struct signatured_type *sig_type;
a2ce51a0
DE
6997
6998 /* Verify we can do the following downcast, and that we have the
6999 data we need. */
7000 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7001 sig_type = (struct signatured_type *) this_cu;
7002 gdb_assert (sig_type->dwo_unit != NULL);
7003
2e671100
SM
7004 dwarf2_cu *cu;
7005
7006 if (existing_cu != nullptr)
6aa5f3a6 7007 {
2e671100
SM
7008 cu = existing_cu;
7009 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 7010 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 7011 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
7012 }
7013 else
7014 {
7188ed02
SM
7015 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7016 in per_objfile yet. */
7017 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
9e021579 7018 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
2e671100 7019 cu = m_new_cu.get ();
6aa5f3a6
DE
7020 }
7021
7022 /* A future optimization, if needed, would be to use an existing
7023 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7024 could share abbrev tables. */
a2ce51a0 7025
2e671100 7026 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
a2ce51a0
DE
7027 NULL /* stub_comp_unit_die */,
7028 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 7029 this, &info_ptr,
3e225074 7030 &comp_unit_die,
c0ab21c2 7031 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
7032 {
7033 /* Dummy die. */
c0ab21c2 7034 dummy_p = true;
a2ce51a0 7035 }
a2ce51a0
DE
7036}
7037
fd820528 7038/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7039 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7040
f4dc4d17
DE
7041 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7042 Otherwise the table specified in the comp unit header is read in and used.
7043 This is an optimization for when we already have the abbrev table.
7044
2e671100
SM
7045 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7046 allocated. */
aaa75496 7047
ab432490 7048cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7049 dwarf2_per_objfile *per_objfile,
c0ab21c2 7050 struct abbrev_table *abbrev_table,
2e671100 7051 dwarf2_cu *existing_cu,
c0ab21c2
TT
7052 bool skip_partial)
7053 : die_reader_specs {},
6751ebae 7054 m_this_cu (this_cu)
c906108c 7055{
976ca316 7056 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7057 struct dwarf2_section_info *section = this_cu->section;
96b79293 7058 bfd *abfd = section->get_bfd_owner ();
c0ab21c2 7059 const gdb_byte *begin_info_ptr;
dee91e82 7060 struct signatured_type *sig_type = NULL;
4bdcc0c1 7061 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7062 /* Non-zero if CU currently points to a DWO file and we need to
7063 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7064 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7065 int rereading_dwo_cu = 0;
c906108c 7066
b4f54984 7067 if (dwarf_die_debug)
9d8780f0 7068 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7069 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7070 sect_offset_str (this_cu->sect_off));
09406207 7071
a2ce51a0
DE
7072 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7073 file (instead of going through the stub), short-circuit all of this. */
7074 if (this_cu->reading_dwo_directly)
7075 {
7076 /* Narrow down the scope of possibilities to have to understand. */
7077 gdb_assert (this_cu->is_debug_types);
7078 gdb_assert (abbrev_table == NULL);
976ca316 7079 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
a2ce51a0
DE
7080 return;
7081 }
7082
dee91e82 7083 /* This is cheap if the section is already read in. */
96b79293 7084 section->read (objfile);
dee91e82 7085
9c541725 7086 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7087
7088 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7089
2e671100
SM
7090 dwarf2_cu *cu;
7091
7092 if (existing_cu != nullptr)
dee91e82 7093 {
2e671100 7094 cu = existing_cu;
42e7ad6c
DE
7095 /* If this CU is from a DWO file we need to start over, we need to
7096 refetch the attributes from the skeleton CU.
7097 This could be optimized by retrieving those attributes from when we
7098 were here the first time: the previous comp_unit_die was stored in
7099 comp_unit_obstack. But there's no data yet that we need this
7100 optimization. */
7101 if (cu->dwo_unit != NULL)
7102 rereading_dwo_cu = 1;
dee91e82
DE
7103 }
7104 else
7105 {
7188ed02
SM
7106 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7107 in per_objfile yet. */
976ca316
SM
7108 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7109 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
c0ab21c2 7110 cu = m_new_cu.get ();
42e7ad6c 7111 }
dee91e82 7112
b0c7bfa9 7113 /* Get the header. */
9c541725 7114 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7115 {
7116 /* We already have the header, there's no need to read it in again. */
9c541725 7117 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7118 }
7119 else
7120 {
3019eac3 7121 if (this_cu->is_debug_types)
dee91e82 7122 {
976ca316
SM
7123 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7124 section, abbrev_section,
7125 info_ptr, rcuh_kind::TYPE);
dee91e82 7126
42e7ad6c
DE
7127 /* Since per_cu is the first member of struct signatured_type,
7128 we can go from a pointer to one to a pointer to the other. */
7129 sig_type = (struct signatured_type *) this_cu;
43988095 7130 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7131 gdb_assert (sig_type->type_offset_in_tu
7132 == cu->header.type_cu_offset_in_tu);
7133 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7134
42e7ad6c
DE
7135 /* LENGTH has not been set yet for type units if we're
7136 using .gdb_index. */
4057dfde 7137 this_cu->length = cu->header.get_length ();
3019eac3
DE
7138
7139 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7140 sig_type->type_offset_in_section =
7141 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7142
7143 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7144 }
7145 else
7146 {
976ca316
SM
7147 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7148 section, abbrev_section,
43988095
JK
7149 info_ptr,
7150 rcuh_kind::COMPILE);
dee91e82 7151
9c541725 7152 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
7153 if (this_cu->length == 0)
7154 this_cu->length = cu->header.get_length ();
7155 else
7156 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7157 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7158 }
7159 }
10b3939b 7160
6caca83c 7161 /* Skip dummy compilation units. */
dee91e82 7162 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7163 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7164 {
7165 dummy_p = true;
7166 return;
7167 }
6caca83c 7168
433df2d4
DE
7169 /* If we don't have them yet, read the abbrevs for this compilation unit.
7170 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7171 done. */
f4dc4d17 7172 if (abbrev_table != NULL)
685af9cd
TT
7173 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7174 else
f4dc4d17 7175 {
c0ab21c2 7176 m_abbrev_table_holder
86de1d91
TT
7177 = abbrev_table::read (objfile, abbrev_section,
7178 cu->header.abbrev_sect_off);
c0ab21c2 7179 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7180 }
af703f96 7181
dee91e82 7182 /* Read the top level CU/TU die. */
c0ab21c2 7183 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7184 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7185
58f0c718 7186 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7187 {
7188 dummy_p = true;
7189 return;
7190 }
58f0c718 7191
b0c7bfa9 7192 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7193 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7194 table from the DWO file and pass the ownership over to us. It will be
7195 referenced from READER, so we must make sure to free it after we're done
7196 with READER.
7197
b0c7bfa9
DE
7198 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7199 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7200 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7201 if (dwo_name != nullptr)
3019eac3 7202 {
3019eac3 7203 struct dwo_unit *dwo_unit;
b0c7bfa9 7204 struct die_info *dwo_comp_unit_die;
3019eac3 7205
3e225074 7206 if (comp_unit_die->has_children)
6a506a2d 7207 {
b98664d3 7208 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7209 " has children (offset %s) [in module %s]"),
7210 sect_offset_str (this_cu->sect_off),
7211 bfd_get_filename (abfd));
6a506a2d 7212 }
4ab09049 7213 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 7214 if (dwo_unit != NULL)
3019eac3 7215 {
4ab09049 7216 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 7217 comp_unit_die, NULL,
c0ab21c2 7218 this, &info_ptr,
3e225074 7219 &dwo_comp_unit_die,
c0ab21c2 7220 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7221 {
7222 /* Dummy die. */
c0ab21c2 7223 dummy_p = true;
6a506a2d
DE
7224 return;
7225 }
7226 comp_unit_die = dwo_comp_unit_die;
7227 }
7228 else
7229 {
7230 /* Yikes, we couldn't find the rest of the DIE, we only have
7231 the stub. A complaint has already been logged. There's
7232 not much more we can do except pass on the stub DIE to
7233 die_reader_func. We don't want to throw an error on bad
7234 debug info. */
3019eac3
DE
7235 }
7236 }
c0ab21c2 7237}
3019eac3 7238
6751ebae
TT
7239void
7240cutu_reader::keep ()
c0ab21c2 7241{
b0c7bfa9 7242 /* Done, clean up. */
6751ebae
TT
7243 gdb_assert (!dummy_p);
7244 if (m_new_cu != NULL)
348e048f 7245 {
7188ed02
SM
7246 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7247 now. */
7248 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7249 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
348e048f 7250 }
dee91e82
DE
7251}
7252
18a8505e
AT
7253/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7254 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7255 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7256
7257 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7258 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7259
7260 We fill in THIS_CU->length.
7261
dee91e82 7262 THIS_CU->cu is always freed when done.
3019eac3 7263 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7264 to care whether it refers to the "main" CU or the DWO CU.
7265
7266 When parent_cu is passed, it is used to provide a default value for
7267 str_offsets_base and addr_base from the parent. */
dee91e82 7268
ab432490 7269cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7270 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
7271 struct dwarf2_cu *parent_cu,
7272 struct dwo_file *dwo_file)
7273 : die_reader_specs {},
7274 m_this_cu (this_cu)
dee91e82 7275{
976ca316 7276 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7277 struct dwarf2_section_info *section = this_cu->section;
96b79293 7278 bfd *abfd = section->get_bfd_owner ();
33e80786 7279 struct dwarf2_section_info *abbrev_section;
d521ce57 7280 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7281
b4f54984 7282 if (dwarf_die_debug)
9d8780f0 7283 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7284 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7285 sect_offset_str (this_cu->sect_off));
09406207 7286
976ca316 7287 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
dee91e82 7288
33e80786
DE
7289 abbrev_section = (dwo_file != NULL
7290 ? &dwo_file->sections.abbrev
7291 : get_abbrev_section_for_cu (this_cu));
7292
dee91e82 7293 /* This is cheap if the section is already read in. */
96b79293 7294 section->read (objfile);
dee91e82 7295
976ca316 7296 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
dee91e82 7297
9c541725 7298 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
976ca316
SM
7299 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7300 section, abbrev_section, info_ptr,
43988095
JK
7301 (this_cu->is_debug_types
7302 ? rcuh_kind::TYPE
7303 : rcuh_kind::COMPILE));
dee91e82 7304
18a8505e
AT
7305 if (parent_cu != nullptr)
7306 {
c0ab21c2
TT
7307 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7308 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7309 }
4057dfde 7310 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7311
7312 /* Skip dummy compilation units. */
7313 if (info_ptr >= begin_info_ptr + this_cu->length
7314 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7315 {
7316 dummy_p = true;
7317 return;
7318 }
72bf9492 7319
c0ab21c2 7320 m_abbrev_table_holder
86de1d91
TT
7321 = abbrev_table::read (objfile, abbrev_section,
7322 m_new_cu->header.abbrev_sect_off);
dee91e82 7323
c0ab21c2
TT
7324 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7325 m_abbrev_table_holder.get ());
3e225074 7326 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7327}
7328
0018ea6f
DE
7329\f
7330/* Type Unit Groups.
dee91e82 7331
0018ea6f
DE
7332 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7333 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7334 so that all types coming from the same compilation (.o file) are grouped
7335 together. A future step could be to put the types in the same symtab as
7336 the CU the types ultimately came from. */
ff013f42 7337
f4dc4d17
DE
7338static hashval_t
7339hash_type_unit_group (const void *item)
7340{
9a3c8263
SM
7341 const struct type_unit_group *tu_group
7342 = (const struct type_unit_group *) item;
f4dc4d17 7343
094b34ac 7344 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7345}
348e048f
DE
7346
7347static int
f4dc4d17 7348eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7349{
9a3c8263
SM
7350 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7351 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7352
094b34ac 7353 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7354}
348e048f 7355
f4dc4d17
DE
7356/* Allocate a hash table for type unit groups. */
7357
eaa5fa8b 7358static htab_up
298e9637 7359allocate_type_unit_groups_table ()
f4dc4d17 7360{
eaa5fa8b
TT
7361 return htab_up (htab_create_alloc (3,
7362 hash_type_unit_group,
7363 eq_type_unit_group,
7364 NULL, xcalloc, xfree));
f4dc4d17 7365}
dee91e82 7366
f4dc4d17
DE
7367/* Type units that don't have DW_AT_stmt_list are grouped into their own
7368 partial symtabs. We combine several TUs per psymtab to not let the size
7369 of any one psymtab grow too big. */
7370#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7371#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7372
094b34ac 7373/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7374 Create the type_unit_group object used to hold one or more TUs. */
7375
7376static struct type_unit_group *
094b34ac 7377create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7378{
976ca316
SM
7379 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7380 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
094b34ac 7381 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7382 struct type_unit_group *tu_group;
f4dc4d17 7383
976ca316 7384 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
094b34ac 7385 per_cu = &tu_group->per_cu;
1859c670 7386 per_cu->per_bfd = per_bfd;
f4dc4d17 7387
1859c670 7388 if (per_bfd->using_index)
094b34ac 7389 {
1859c670 7390 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7391 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7392 }
7393 else
7394 {
9c541725 7395 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7396 dwarf2_psymtab *pst;
528e1572 7397 std::string name;
094b34ac
DE
7398
7399 /* Give the symtab a useful name for debug purposes. */
7400 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7401 name = string_printf ("<type_units_%d>",
7402 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7403 else
528e1572 7404 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7405
976ca316 7406 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
6d94535f 7407 pst->anonymous = true;
094b34ac 7408 }
f4dc4d17 7409
094b34ac 7410 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7411 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7412
7413 return tu_group;
7414}
7415
094b34ac
DE
7416/* Look up the type_unit_group for type unit CU, and create it if necessary.
7417 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7418
7419static struct type_unit_group *
ff39bb5e 7420get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7421{
976ca316
SM
7422 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7423 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7424 struct type_unit_group *tu_group;
7425 void **slot;
7426 unsigned int line_offset;
7427 struct type_unit_group type_unit_group_for_lookup;
7428
976ca316
SM
7429 if (per_objfile->per_bfd->type_unit_groups == NULL)
7430 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7431
7432 /* Do we need to create a new group, or can we use an existing one? */
7433
7434 if (stmt_list)
7435 {
7436 line_offset = DW_UNSND (stmt_list);
7437 ++tu_stats->nr_symtab_sharers;
7438 }
7439 else
7440 {
7441 /* Ugh, no stmt_list. Rare, but we have to handle it.
7442 We can do various things here like create one group per TU or
7443 spread them over multiple groups to split up the expansion work.
7444 To avoid worst case scenarios (too many groups or too large groups)
7445 we, umm, group them in bunches. */
7446 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7447 | (tu_stats->nr_stmt_less_type_units
7448 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7449 ++tu_stats->nr_stmt_less_type_units;
7450 }
7451
094b34ac 7452 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7453 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
976ca316 7454 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7455 &type_unit_group_for_lookup, INSERT);
7456 if (*slot != NULL)
7457 {
9a3c8263 7458 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7459 gdb_assert (tu_group != NULL);
7460 }
7461 else
7462 {
9c541725 7463 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7464 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7465 *slot = tu_group;
7466 ++tu_stats->nr_symtabs;
7467 }
7468
7469 return tu_group;
7470}
0018ea6f
DE
7471\f
7472/* Partial symbol tables. */
7473
7474/* Create a psymtab named NAME and assign it to PER_CU.
7475
7476 The caller must fill in the following details:
7477 dirname, textlow, texthigh. */
7478
891813be 7479static dwarf2_psymtab *
7aa104c4
SM
7480create_partial_symtab (dwarf2_per_cu_data *per_cu,
7481 dwarf2_per_objfile *per_objfile,
7482 const char *name)
0018ea6f 7483{
7aa104c4 7484 struct objfile *objfile = per_objfile->objfile;
891813be 7485 dwarf2_psymtab *pst;
0018ea6f 7486
9f4e76a4 7487 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7488
6d94535f 7489 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7490
7491 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7492 per_cu->v.psymtab = pst;
7493
7494 return pst;
7495}
7496
c0ab21c2 7497/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7498
7499static void
7500process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7501 const gdb_byte *info_ptr,
0018ea6f 7502 struct die_info *comp_unit_die,
c0ab21c2 7503 enum language pretend_language)
0018ea6f
DE
7504{
7505 struct dwarf2_cu *cu = reader->cu;
7aa104c4
SM
7506 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7507 struct objfile *objfile = per_objfile->objfile;
08feed99 7508 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7509 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7510 CORE_ADDR baseaddr;
7511 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7512 dwarf2_psymtab *pst;
3a2b436a 7513 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7514 const char *filename;
0018ea6f 7515
0018ea6f
DE
7516 gdb_assert (! per_cu->is_debug_types);
7517
c0ab21c2 7518 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7519
0018ea6f 7520 /* Allocate a new partial symbol table structure. */
2e927613
TV
7521 gdb::unique_xmalloc_ptr<char> debug_filename;
7522 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7523 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7524 if (filename == NULL)
0018ea6f 7525 filename = "";
2e927613
TV
7526 else if (strcmp (filename, artificial) == 0)
7527 {
7528 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7529 sect_offset_str (per_cu->sect_off),
7530 (char *) NULL));
2e927613
TV
7531 filename = debug_filename.get ();
7532 }
0018ea6f 7533
7aa104c4 7534 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7535
7536 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7537 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7538
b3b3bada 7539 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7540
7541 dwarf2_find_base_address (comp_unit_die, cu);
7542
7543 /* Possibly set the default values of LOWPC and HIGHPC from
7544 `DW_AT_ranges'. */
3a2b436a
JK
7545 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7546 &best_highpc, cu, pst);
7547 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7548 {
7549 CORE_ADDR low
7550 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7551 - baseaddr);
7552 CORE_ADDR high
7553 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7554 - baseaddr - 1);
7555 /* Store the contiguous range if it is not empty; it can be
7556 empty for CUs with no code. */
d320c2b5
TT
7557 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7558 low, high, pst);
79748972 7559 }
0018ea6f
DE
7560
7561 /* Check if comp unit has_children.
7562 If so, read the rest of the partial symbols from this comp unit.
7563 If not, there's no more debug_info for this comp unit. */
3e225074 7564 if (comp_unit_die->has_children)
0018ea6f
DE
7565 {
7566 struct partial_die_info *first_die;
7567 CORE_ADDR lowpc, highpc;
7568
7569 lowpc = ((CORE_ADDR) -1);
7570 highpc = ((CORE_ADDR) 0);
7571
7572 first_die = load_partial_dies (reader, info_ptr, 1);
7573
7574 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7575 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7576
7577 /* If we didn't find a lowpc, set it to highpc to avoid
7578 complaints from `maint check'. */
7579 if (lowpc == ((CORE_ADDR) -1))
7580 lowpc = highpc;
7581
7582 /* If the compilation unit didn't have an explicit address range,
7583 then use the information extracted from its child dies. */
e385593e 7584 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7585 {
7586 best_lowpc = lowpc;
7587 best_highpc = highpc;
7588 }
7589 }
4ae976d1 7590 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7591 best_lowpc + baseaddr)
7592 - baseaddr);
4ae976d1 7593 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7594 best_highpc + baseaddr)
7595 - baseaddr);
0018ea6f 7596
8763cede 7597 end_psymtab_common (objfile, pst);
0018ea6f 7598
ae640021 7599 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7600 {
7601 int i;
ae640021 7602 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7603
7604 /* Fill in 'dependencies' here; we fill in 'users' in a
7605 post-pass. */
7606 pst->number_of_dependencies = len;
a9342b62
TT
7607 pst->dependencies
7608 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7609 for (i = 0; i < len; ++i)
7610 {
7611 pst->dependencies[i]
7612 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7613 }
0018ea6f 7614
ae640021 7615 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7616 }
7617
7618 /* Get the list of files included in the current compilation unit,
7619 and build a psymtab for each of them. */
7620 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7621
b4f54984 7622 if (dwarf_read_debug)
b926417a
TT
7623 fprintf_unfiltered (gdb_stdlog,
7624 "Psymtab for %s unit @%s: %s - %s"
7625 ", %d global, %d static syms\n",
7626 per_cu->is_debug_types ? "type" : "comp",
7627 sect_offset_str (per_cu->sect_off),
7628 paddress (gdbarch, pst->text_low (objfile)),
7629 paddress (gdbarch, pst->text_high (objfile)),
7630 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7631}
7632
7633/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7634 Process compilation unit THIS_CU for a psymtab. */
7635
7636static void
ab432490
SM
7637process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7638 dwarf2_per_objfile *per_objfile,
135f5437 7639 bool want_partial_unit,
b93601f3 7640 enum language pretend_language)
0018ea6f
DE
7641{
7642 /* If this compilation unit was already read in, free the
7643 cached copy in order to read it in again. This is
7644 necessary because we skipped some symbols when we first
7645 read in the compilation unit (see load_partial_dies).
7646 This problem could be avoided, but the benefit is unclear. */
7188ed02 7647 per_objfile->remove_cu (this_cu);
0018ea6f 7648
2e671100 7649 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2 7650
58990295
TV
7651 switch (reader.comp_unit_die->tag)
7652 {
7653 case DW_TAG_compile_unit:
7654 this_cu->unit_type = DW_UT_compile;
7655 break;
7656 case DW_TAG_partial_unit:
7657 this_cu->unit_type = DW_UT_partial;
7658 break;
7659 default:
7660 abort ();
7661 }
7662
c0ab21c2 7663 if (reader.dummy_p)
f1902523 7664 {
c0ab21c2 7665 /* Nothing. */
f1902523 7666 }
c0ab21c2 7667 else if (this_cu->is_debug_types)
3e225074
TT
7668 build_type_psymtabs_reader (&reader, reader.info_ptr,
7669 reader.comp_unit_die);
135f5437
TT
7670 else if (want_partial_unit
7671 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7672 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7673 reader.comp_unit_die,
c0ab21c2 7674 pretend_language);
0018ea6f 7675
7188ed02 7676 this_cu->lang = reader.cu->language;
58990295 7677
0018ea6f 7678 /* Age out any secondary CUs. */
7188ed02 7679 per_objfile->age_comp_units ();
0018ea6f 7680}
f4dc4d17
DE
7681
7682/* Reader function for build_type_psymtabs. */
7683
7684static void
7685build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7686 const gdb_byte *info_ptr,
3e225074 7687 struct die_info *type_unit_die)
f4dc4d17 7688{
976ca316
SM
7689 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7690 struct objfile *objfile = per_objfile->objfile;
f4dc4d17
DE
7691 struct dwarf2_cu *cu = reader->cu;
7692 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7693 struct signatured_type *sig_type;
f4dc4d17
DE
7694 struct type_unit_group *tu_group;
7695 struct attribute *attr;
7696 struct partial_die_info *first_die;
7697 CORE_ADDR lowpc, highpc;
891813be 7698 dwarf2_psymtab *pst;
f4dc4d17 7699
0186c6a7
DE
7700 gdb_assert (per_cu->is_debug_types);
7701 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7702
3e225074 7703 if (! type_unit_die->has_children)
f4dc4d17
DE
7704 return;
7705
052c8bb8 7706 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7707 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7708
df07e2c7 7709 if (tu_group->tus == nullptr)
a8b3b8e9 7710 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7711 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7712
7713 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
976ca316 7714 pst = create_partial_symtab (per_cu, per_objfile, "");
6d94535f 7715 pst->anonymous = true;
f4dc4d17
DE
7716
7717 first_die = load_partial_dies (reader, info_ptr, 1);
7718
7719 lowpc = (CORE_ADDR) -1;
7720 highpc = (CORE_ADDR) 0;
7721 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7722
8763cede 7723 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7724}
7725
73051182
DE
7726/* Struct used to sort TUs by their abbreviation table offset. */
7727
7728struct tu_abbrev_offset
7729{
b2bdb8cf
SM
7730 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7731 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7732 {}
7733
7734 signatured_type *sig_type;
73051182
DE
7735 sect_offset abbrev_offset;
7736};
7737
484cf504 7738/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7739
484cf504
TT
7740static bool
7741sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7742 const struct tu_abbrev_offset &b)
73051182 7743{
484cf504 7744 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7745}
7746
7747/* Efficiently read all the type units.
7748 This does the bulk of the work for build_type_psymtabs.
7749
7750 The efficiency is because we sort TUs by the abbrev table they use and
7751 only read each abbrev table once. In one program there are 200K TUs
7752 sharing 8K abbrev tables.
7753
7754 The main purpose of this function is to support building the
5989a64e 7755 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7756 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7757 can collapse the search space by grouping them by stmt_list.
7758 The savings can be significant, in the same program from above the 200K TUs
7759 share 8K stmt_list tables.
7760
7761 FUNC is expected to call get_type_unit_group, which will create the
7762 struct type_unit_group if necessary and add it to
5989a64e 7763 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7764
7765static void
976ca316 7766build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
73051182 7767{
976ca316 7768 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
685af9cd 7769 abbrev_table_up abbrev_table;
73051182 7770 sect_offset abbrev_offset;
73051182
DE
7771
7772 /* It's up to the caller to not call us multiple times. */
976ca316 7773 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7774
976ca316 7775 if (per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7776 return;
7777
7778 /* TUs typically share abbrev tables, and there can be way more TUs than
7779 abbrev tables. Sort by abbrev table to reduce the number of times we
7780 read each abbrev table in.
7781 Alternatives are to punt or to maintain a cache of abbrev tables.
7782 This is simpler and efficient enough for now.
7783
7784 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7785 symtab to use). Typically TUs with the same abbrev offset have the same
7786 stmt_list value too so in practice this should work well.
7787
7788 The basic algorithm here is:
7789
7790 sort TUs by abbrev table
7791 for each TU with same abbrev table:
7792 read abbrev table if first user
7793 read TU top level DIE
7794 [IWBN if DWO skeletons had DW_AT_stmt_list]
7795 call FUNC */
7796
b4f54984 7797 if (dwarf_read_debug)
73051182
DE
7798 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7799
7800 /* Sort in a separate table to maintain the order of all_type_units
7801 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7802 std::vector<tu_abbrev_offset> sorted_by_abbrev;
976ca316 7803 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7804
976ca316 7805 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
b2bdb8cf 7806 sorted_by_abbrev.emplace_back
976ca316 7807 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
b2bdb8cf 7808 sig_type->per_cu.sect_off));
73051182 7809
484cf504
TT
7810 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7811 sort_tu_by_abbrev_offset);
73051182 7812
9c541725 7813 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7814
b2bdb8cf 7815 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7816 {
73051182
DE
7817 /* Switch to the next abbrev table if necessary. */
7818 if (abbrev_table == NULL
b2bdb8cf 7819 || tu.abbrev_offset != abbrev_offset)
73051182 7820 {
b2bdb8cf 7821 abbrev_offset = tu.abbrev_offset;
73051182 7822 abbrev_table =
976ca316
SM
7823 abbrev_table::read (per_objfile->objfile,
7824 &per_objfile->per_bfd->abbrev, abbrev_offset);
73051182
DE
7825 ++tu_stats->nr_uniq_abbrev_tables;
7826 }
7827
976ca316 7828 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
2e671100 7829 abbrev_table.get (), nullptr, false);
c0ab21c2
TT
7830 if (!reader.dummy_p)
7831 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7832 reader.comp_unit_die);
73051182 7833 }
6aa5f3a6 7834}
73051182 7835
6aa5f3a6
DE
7836/* Print collected type unit statistics. */
7837
7838static void
976ca316 7839print_tu_stats (dwarf2_per_objfile *per_objfile)
6aa5f3a6 7840{
976ca316 7841 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6aa5f3a6
DE
7842
7843 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf 7844 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
976ca316 7845 per_objfile->per_bfd->all_type_units.size ());
6aa5f3a6
DE
7846 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7847 tu_stats->nr_uniq_abbrev_tables);
7848 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7849 tu_stats->nr_symtabs);
7850 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7851 tu_stats->nr_symtab_sharers);
7852 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7853 tu_stats->nr_stmt_less_type_units);
7854 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7855 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7856}
7857
f4dc4d17
DE
7858/* Traversal function for build_type_psymtabs. */
7859
7860static int
7861build_type_psymtab_dependencies (void **slot, void *info)
7862{
976ca316
SM
7863 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7864 struct objfile *objfile = per_objfile->objfile;
f4dc4d17 7865 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7866 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7867 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7868 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7869 int i;
7870
7871 gdb_assert (len > 0);
197400e8 7872 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7873
7874 pst->number_of_dependencies = len;
a9342b62 7875 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7876 for (i = 0; i < len; ++i)
f4dc4d17 7877 {
df07e2c7 7878 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7879 gdb_assert (iter->per_cu.is_debug_types);
7880 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7881 iter->type_unit_group = tu_group;
f4dc4d17
DE
7882 }
7883
df07e2c7
AB
7884 delete tu_group->tus;
7885 tu_group->tus = nullptr;
348e048f
DE
7886
7887 return 1;
7888}
7889
7890/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7891 Build partial symbol tables for the .debug_types comp-units. */
7892
7893static void
976ca316 7894build_type_psymtabs (dwarf2_per_objfile *per_objfile)
348e048f 7895{
976ca316 7896 if (! create_all_type_units (per_objfile))
348e048f
DE
7897 return;
7898
976ca316 7899 build_type_psymtabs_1 (per_objfile);
6aa5f3a6 7900}
f4dc4d17 7901
6aa5f3a6
DE
7902/* Traversal function for process_skeletonless_type_unit.
7903 Read a TU in a DWO file and build partial symbols for it. */
7904
7905static int
7906process_skeletonless_type_unit (void **slot, void *info)
7907{
7908 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
976ca316 7909 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
6aa5f3a6
DE
7910 struct signatured_type find_entry, *entry;
7911
7912 /* If this TU doesn't exist in the global table, add it and read it in. */
7913
976ca316
SM
7914 if (per_objfile->per_bfd->signatured_types == NULL)
7915 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7916
7917 find_entry.signature = dwo_unit->signature;
976ca316 7918 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7919 &find_entry, INSERT);
6aa5f3a6
DE
7920 /* If we've already seen this type there's nothing to do. What's happening
7921 is we're doing our own version of comdat-folding here. */
7922 if (*slot != NULL)
7923 return 1;
7924
7925 /* This does the job that create_all_type_units would have done for
7926 this TU. */
976ca316
SM
7927 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7928 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7929 *slot = entry;
7930
7931 /* This does the job that build_type_psymtabs_1 would have done. */
976ca316 7932 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
7933 if (!reader.dummy_p)
7934 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7935 reader.comp_unit_die);
6aa5f3a6
DE
7936
7937 return 1;
7938}
7939
7940/* Traversal function for process_skeletonless_type_units. */
7941
7942static int
7943process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7944{
7945 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7946
7947 if (dwo_file->tus != NULL)
b0b6a987
TT
7948 htab_traverse_noresize (dwo_file->tus.get (),
7949 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7950
7951 return 1;
7952}
7953
7954/* Scan all TUs of DWO files, verifying we've processed them.
7955 This is needed in case a TU was emitted without its skeleton.
7956 Note: This can't be done until we know what all the DWO files are. */
7957
7958static void
976ca316 7959process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
6aa5f3a6
DE
7960{
7961 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
976ca316
SM
7962 if (get_dwp_file (per_objfile) == NULL
7963 && per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 7964 {
976ca316 7965 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 7966 process_dwo_file_for_skeletonless_type_units,
976ca316 7967 per_objfile);
6aa5f3a6 7968 }
348e048f
DE
7969}
7970
ed2dc618 7971/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7972
7973static void
976ca316 7974set_partial_user (dwarf2_per_objfile *per_objfile)
95554aad 7975{
976ca316 7976 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
95554aad 7977 {
891813be 7978 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7979
36586728
TT
7980 if (pst == NULL)
7981 continue;
7982
b76e467d 7983 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7984 {
7985 /* Set the 'user' field only if it is not already set. */
7986 if (pst->dependencies[j]->user == NULL)
7987 pst->dependencies[j]->user = pst;
7988 }
7989 }
7990}
7991
93311388
DE
7992/* Build the partial symbol table by doing a quick pass through the
7993 .debug_info and .debug_abbrev sections. */
72bf9492 7994
93311388 7995static void
976ca316 7996dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
93311388 7997{
976ca316 7998 struct objfile *objfile = per_objfile->objfile;
93311388 7999
b4f54984 8000 if (dwarf_read_debug)
45cfd468
DE
8001 {
8002 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8003 objfile_name (objfile));
45cfd468
DE
8004 }
8005
76935768 8006 scoped_restore restore_reading_psyms
976ca316 8007 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
76935768 8008 true);
98bfdba5 8009
976ca316 8010 per_objfile->per_bfd->info.read (objfile);
91c24f0a 8011
93311388
DE
8012 /* Any cached compilation units will be linked by the per-objfile
8013 read_in_chain. Make sure to free them when we're done. */
976ca316 8014 free_cached_comp_units freer (per_objfile);
72bf9492 8015
976ca316 8016 build_type_psymtabs (per_objfile);
348e048f 8017
976ca316 8018 create_all_comp_units (per_objfile);
c906108c 8019
60606b2c
TT
8020 /* Create a temporary address map on a temporary obstack. We later
8021 copy this to the final obstack. */
8268c778 8022 auto_obstack temp_obstack;
791afaa2
TT
8023
8024 scoped_restore save_psymtabs_addrmap
d320c2b5 8025 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8026 addrmap_create_mutable (&temp_obstack));
72bf9492 8027
976ca316 8028 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3d5afab3
TV
8029 {
8030 if (per_cu->v.psymtab != NULL)
8031 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8032 continue;
976ca316 8033 process_psymtab_comp_unit (per_cu, per_objfile, false,
ab432490 8034 language_minimal);
3d5afab3 8035 }
ff013f42 8036
6aa5f3a6 8037 /* This has to wait until we read the CUs, we need the list of DWOs. */
976ca316 8038 process_skeletonless_type_units (per_objfile);
6aa5f3a6
DE
8039
8040 /* Now that all TUs have been processed we can fill in the dependencies. */
976ca316 8041 if (per_objfile->per_bfd->type_unit_groups != NULL)
6aa5f3a6 8042 {
976ca316
SM
8043 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8044 build_type_psymtab_dependencies, per_objfile);
6aa5f3a6
DE
8045 }
8046
b4f54984 8047 if (dwarf_read_debug)
976ca316 8048 print_tu_stats (per_objfile);
6aa5f3a6 8049
976ca316 8050 set_partial_user (per_objfile);
95554aad 8051
d320c2b5
TT
8052 objfile->partial_symtabs->psymtabs_addrmap
8053 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8054 objfile->partial_symtabs->obstack ());
791afaa2
TT
8055 /* At this point we want to keep the address map. */
8056 save_psymtabs_addrmap.release ();
ff013f42 8057
b4f54984 8058 if (dwarf_read_debug)
45cfd468 8059 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8060 objfile_name (objfile));
ae038cb0
DJ
8061}
8062
dee91e82
DE
8063/* Load the partial DIEs for a secondary CU into memory.
8064 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8065
dee91e82 8066static void
ab432490 8067load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
2e671100
SM
8068 dwarf2_per_objfile *per_objfile,
8069 dwarf2_cu *existing_cu)
dee91e82 8070{
2e671100 8071 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
c0ab21c2
TT
8072
8073 if (!reader.dummy_p)
8074 {
8075 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8076 language_minimal);
8077
8078 /* Check if comp unit has_children.
8079 If so, read the rest of the partial symbols from this comp unit.
8080 If not, there's no more debug_info for this comp unit. */
3e225074 8081 if (reader.comp_unit_die->has_children)
c0ab21c2 8082 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
8083
8084 reader.keep ();
c0ab21c2 8085 }
ae038cb0
DJ
8086}
8087
ae038cb0 8088static void
976ca316 8089read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
36586728 8090 struct dwarf2_section_info *section,
f1902523 8091 struct dwarf2_section_info *abbrev_section,
b76e467d 8092 unsigned int is_dwz)
ae038cb0 8093{
d521ce57 8094 const gdb_byte *info_ptr;
976ca316 8095 struct objfile *objfile = per_objfile->objfile;
be391dca 8096
b4f54984 8097 if (dwarf_read_debug)
bf6af496 8098 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
8099 section->get_name (),
8100 section->get_file_name ());
bf6af496 8101
96b79293 8102 section->read (objfile);
ae038cb0 8103
36586728 8104 info_ptr = section->buffer;
6e70227d 8105
36586728 8106 while (info_ptr < section->buffer + section->size)
ae038cb0 8107 {
ae038cb0 8108 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8109
9c541725 8110 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8111
f1902523 8112 comp_unit_head cu_header;
976ca316 8113 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618
SM
8114 abbrev_section, info_ptr,
8115 rcuh_kind::COMPILE);
ae038cb0
DJ
8116
8117 /* Save the compilation unit for later lookup. */
f1902523 8118 if (cu_header.unit_type != DW_UT_type)
976ca316 8119 this_cu = per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
8120 else
8121 {
976ca316 8122 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
8123 sig_type->signature = cu_header.signature;
8124 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8125 this_cu = &sig_type->per_cu;
8126 }
8127 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8128 this_cu->sect_off = sect_off;
f1902523 8129 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8130 this_cu->is_dwz = is_dwz;
8a0459fd 8131 this_cu->section = section;
ae038cb0 8132
976ca316 8133 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8134
8135 info_ptr = info_ptr + this_cu->length;
8136 }
36586728
TT
8137}
8138
8139/* Create a list of all compilation units in OBJFILE.
8140 This is only done for -readnow and building partial symtabs. */
8141
8142static void
976ca316 8143create_all_comp_units (dwarf2_per_objfile *per_objfile)
36586728 8144{
976ca316
SM
8145 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8146 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8147 &per_objfile->per_bfd->abbrev, 0);
36586728 8148
976ca316 8149 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
4db1a1dc 8150 if (dwz != NULL)
976ca316 8151 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
c906108c
SS
8152}
8153
5734ee8b 8154/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8155 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8156 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8157 DW_AT_ranges). See the comments of add_partial_subprogram on how
8158 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8159
72bf9492
DJ
8160static void
8161scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8162 CORE_ADDR *highpc, int set_addrmap,
8163 struct dwarf2_cu *cu)
c906108c 8164{
72bf9492 8165 struct partial_die_info *pdi;
c906108c 8166
91c24f0a
DC
8167 /* Now, march along the PDI's, descending into ones which have
8168 interesting children but skipping the children of the other ones,
8169 until we reach the end of the compilation unit. */
c906108c 8170
72bf9492 8171 pdi = first_die;
91c24f0a 8172
72bf9492
DJ
8173 while (pdi != NULL)
8174 {
52356b79 8175 pdi->fixup (cu);
c906108c 8176
f55ee35c 8177 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8178 children, so we need to look at them. Ditto for anonymous
8179 enums. */
933c6fe4 8180
7d00ffec 8181 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8182 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8183 || pdi->tag == DW_TAG_imported_unit
8184 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8185 {
72bf9492 8186 switch (pdi->tag)
c906108c
SS
8187 {
8188 case DW_TAG_subprogram:
b1dc1806 8189 case DW_TAG_inlined_subroutine:
cdc07690 8190 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
f9b5d5ea
TV
8191 if (cu->language == language_cplus)
8192 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8193 set_addrmap, cu);
c906108c 8194 break;
72929c62 8195 case DW_TAG_constant:
c906108c
SS
8196 case DW_TAG_variable:
8197 case DW_TAG_typedef:
91c24f0a 8198 case DW_TAG_union_type:
317d2668
TV
8199 if (!pdi->is_declaration
8200 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8201 {
72bf9492 8202 add_partial_symbol (pdi, cu);
63d06c5c
DC
8203 }
8204 break;
c906108c 8205 case DW_TAG_class_type:
680b30c7 8206 case DW_TAG_interface_type:
c906108c 8207 case DW_TAG_structure_type:
72bf9492 8208 if (!pdi->is_declaration)
c906108c 8209 {
72bf9492 8210 add_partial_symbol (pdi, cu);
c906108c 8211 }
b7fee5a3
KS
8212 if ((cu->language == language_rust
8213 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8214 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8215 set_addrmap, cu);
c906108c 8216 break;
91c24f0a 8217 case DW_TAG_enumeration_type:
72bf9492
DJ
8218 if (!pdi->is_declaration)
8219 add_partial_enumeration (pdi, cu);
c906108c
SS
8220 break;
8221 case DW_TAG_base_type:
a02abb62 8222 case DW_TAG_subrange_type:
c906108c 8223 /* File scope base type definitions are added to the partial
c5aa993b 8224 symbol table. */
72bf9492 8225 add_partial_symbol (pdi, cu);
c906108c 8226 break;
d9fa45fe 8227 case DW_TAG_namespace:
cdc07690 8228 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8229 break;
5d7cb8df 8230 case DW_TAG_module:
59c35742
AB
8231 if (!pdi->is_declaration)
8232 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8233 break;
95554aad
TT
8234 case DW_TAG_imported_unit:
8235 {
8236 struct dwarf2_per_cu_data *per_cu;
8237
f4dc4d17
DE
8238 /* For now we don't handle imported units in type units. */
8239 if (cu->per_cu->is_debug_types)
8240 {
8241 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8242 " supported in type units [in module %s]"),
5e22e966 8243 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
8244 }
8245
e3b94546 8246 per_cu = dwarf2_find_containing_comp_unit
5e22e966 8247 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
8248
8249 /* Go read the partial unit, if needed. */
8250 if (per_cu->v.psymtab == NULL)
ab432490
SM
8251 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8252 cu->language);
95554aad 8253
ae640021 8254 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8255 }
8256 break;
74921315
KS
8257 case DW_TAG_imported_declaration:
8258 add_partial_symbol (pdi, cu);
8259 break;
c906108c
SS
8260 default:
8261 break;
8262 }
8263 }
8264
72bf9492
DJ
8265 /* If the die has a sibling, skip to the sibling. */
8266
8267 pdi = pdi->die_sibling;
8268 }
8269}
8270
8271/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8272
72bf9492 8273 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8274 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8275 Enumerators are an exception; they use the scope of their parent
8276 enumeration type, i.e. the name of the enumeration type is not
8277 prepended to the enumerator.
91c24f0a 8278
72bf9492
DJ
8279 There are two complexities. One is DW_AT_specification; in this
8280 case "parent" means the parent of the target of the specification,
8281 instead of the direct parent of the DIE. The other is compilers
8282 which do not emit DW_TAG_namespace; in this case we try to guess
8283 the fully qualified name of structure types from their members'
8284 linkage names. This must be done using the DIE's children rather
8285 than the children of any DW_AT_specification target. We only need
8286 to do this for structures at the top level, i.e. if the target of
8287 any DW_AT_specification (if any; otherwise the DIE itself) does not
8288 have a parent. */
8289
8290/* Compute the scope prefix associated with PDI's parent, in
8291 compilation unit CU. The result will be allocated on CU's
8292 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8293 field. NULL is returned if no prefix is necessary. */
15d034d0 8294static const char *
72bf9492
DJ
8295partial_die_parent_scope (struct partial_die_info *pdi,
8296 struct dwarf2_cu *cu)
8297{
15d034d0 8298 const char *grandparent_scope;
72bf9492 8299 struct partial_die_info *parent, *real_pdi;
91c24f0a 8300
72bf9492
DJ
8301 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8302 then this means the parent of the specification DIE. */
8303
8304 real_pdi = pdi;
72bf9492 8305 while (real_pdi->has_specification)
fb816e8b 8306 {
122cf0f2
AB
8307 auto res = find_partial_die (real_pdi->spec_offset,
8308 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8309 real_pdi = res.pdi;
8310 cu = res.cu;
8311 }
72bf9492
DJ
8312
8313 parent = real_pdi->die_parent;
8314 if (parent == NULL)
8315 return NULL;
8316
8317 if (parent->scope_set)
8318 return parent->scope;
8319
52356b79 8320 parent->fixup (cu);
72bf9492 8321
10b3939b 8322 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8323
acebe513
UW
8324 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8325 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8326 Work around this problem here. */
8327 if (cu->language == language_cplus
6e70227d 8328 && parent->tag == DW_TAG_namespace
7d00ffec 8329 && strcmp (parent->name (cu), "::") == 0
acebe513
UW
8330 && grandparent_scope == NULL)
8331 {
8332 parent->scope = NULL;
8333 parent->scope_set = 1;
8334 return NULL;
8335 }
8336
0a4b0913 8337 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8338 if (pdi->tag == DW_TAG_enumerator)
8339 /* Enumerators should not get the name of the enumeration as a prefix. */
8340 parent->scope = grandparent_scope;
8341 else if (parent->tag == DW_TAG_namespace
f55ee35c 8342 || parent->tag == DW_TAG_module
72bf9492
DJ
8343 || parent->tag == DW_TAG_structure_type
8344 || parent->tag == DW_TAG_class_type
680b30c7 8345 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8346 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8347 || parent->tag == DW_TAG_enumeration_type
8348 || (cu->language == language_fortran
8349 && parent->tag == DW_TAG_subprogram
8350 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8351 {
8352 if (grandparent_scope == NULL)
7d00ffec 8353 parent->scope = parent->name (cu);
72bf9492 8354 else
3e43a32a
MS
8355 parent->scope = typename_concat (&cu->comp_unit_obstack,
8356 grandparent_scope,
7d00ffec 8357 parent->name (cu), 0, cu);
72bf9492 8358 }
72bf9492
DJ
8359 else
8360 {
8361 /* FIXME drow/2004-04-01: What should we be doing with
8362 function-local names? For partial symbols, we should probably be
8363 ignoring them. */
fa9c3fa0
TT
8364 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8365 dwarf_tag_name (parent->tag),
8366 sect_offset_str (pdi->sect_off));
72bf9492 8367 parent->scope = grandparent_scope;
c906108c
SS
8368 }
8369
72bf9492
DJ
8370 parent->scope_set = 1;
8371 return parent->scope;
8372}
8373
8374/* Return the fully scoped name associated with PDI, from compilation unit
8375 CU. The result will be allocated with malloc. */
4568ecf9 8376
43816ebc 8377static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8378partial_die_full_name (struct partial_die_info *pdi,
8379 struct dwarf2_cu *cu)
8380{
15d034d0 8381 const char *parent_scope;
72bf9492 8382
98bfdba5
PA
8383 /* If this is a template instantiation, we can not work out the
8384 template arguments from partial DIEs. So, unfortunately, we have
8385 to go through the full DIEs. At least any work we do building
8386 types here will be reused if full symbols are loaded later. */
8387 if (pdi->has_template_arguments)
8388 {
52356b79 8389 pdi->fixup (cu);
98bfdba5 8390
7d00ffec 8391 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
98bfdba5
PA
8392 {
8393 struct die_info *die;
8394 struct attribute attr;
8395 struct dwarf2_cu *ref_cu = cu;
8396
b64f50a1 8397 /* DW_FORM_ref_addr is using section offset. */
b4069958 8398 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8399 attr.form = DW_FORM_ref_addr;
9c541725 8400 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8401 die = follow_die_ref (NULL, &attr, &ref_cu);
8402
43816ebc 8403 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8404 }
8405 }
8406
72bf9492
DJ
8407 parent_scope = partial_die_parent_scope (pdi, cu);
8408 if (parent_scope == NULL)
8409 return NULL;
8410 else
43816ebc 8411 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7d00ffec
TT
8412 pdi->name (cu),
8413 0, cu));
c906108c
SS
8414}
8415
8416static void
72bf9492 8417add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8418{
976ca316
SM
8419 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8420 struct objfile *objfile = per_objfile->objfile;
08feed99 8421 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8422 CORE_ADDR addr = 0;
15d034d0 8423 const char *actual_name = NULL;
e142c38c
DJ
8424 CORE_ADDR baseaddr;
8425
b3b3bada 8426 baseaddr = objfile->text_section_offset ();
c906108c 8427
43816ebc
TT
8428 gdb::unique_xmalloc_ptr<char> built_actual_name
8429 = partial_die_full_name (pdi, cu);
15d034d0 8430 if (built_actual_name != NULL)
43816ebc 8431 actual_name = built_actual_name.get ();
63d06c5c 8432
72bf9492 8433 if (actual_name == NULL)
7d00ffec 8434 actual_name = pdi->name (cu);
72bf9492 8435
76e288d1
TT
8436 partial_symbol psymbol;
8437 memset (&psymbol, 0, sizeof (psymbol));
8438 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8439 psymbol.ginfo.section = -1;
8440
8441 /* The code below indicates that the psymbol should be installed by
8442 setting this. */
8443 gdb::optional<psymbol_placement> where;
8444
c906108c
SS
8445 switch (pdi->tag)
8446 {
b1dc1806 8447 case DW_TAG_inlined_subroutine:
c906108c 8448 case DW_TAG_subprogram:
79748972
TT
8449 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8450 - baseaddr);
0a4b0913
AB
8451 if (pdi->is_external
8452 || cu->language == language_ada
8453 || (cu->language == language_fortran
8454 && pdi->die_parent != NULL
8455 && pdi->die_parent->tag == DW_TAG_subprogram))
8456 {
8457 /* Normally, only "external" DIEs are part of the global scope.
8458 But in Ada and Fortran, we want to be able to access nested
8459 procedures globally. So all Ada and Fortran subprograms are
8460 stored in the global scope. */
76e288d1 8461 where = psymbol_placement::GLOBAL;
c906108c
SS
8462 }
8463 else
76e288d1
TT
8464 where = psymbol_placement::STATIC;
8465
8466 psymbol.domain = VAR_DOMAIN;
8467 psymbol.aclass = LOC_BLOCK;
8468 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8469 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8470
8471 if (pdi->main_subprogram && actual_name != NULL)
8472 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8473 break;
72929c62 8474 case DW_TAG_constant:
76e288d1
TT
8475 psymbol.domain = VAR_DOMAIN;
8476 psymbol.aclass = LOC_STATIC;
8477 where = (pdi->is_external
8478 ? psymbol_placement::GLOBAL
8479 : psymbol_placement::STATIC);
72929c62 8480 break;
c906108c 8481 case DW_TAG_variable:
95554aad
TT
8482 if (pdi->d.locdesc)
8483 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8484
95554aad 8485 if (pdi->d.locdesc
caac4577 8486 && addr == 0
976ca316 8487 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8488 {
8489 /* A global or static variable may also have been stripped
8490 out by the linker if unused, in which case its address
8491 will be nullified; do not add such variables into partial
8492 symbol table then. */
8493 }
8494 else if (pdi->is_external)
c906108c
SS
8495 {
8496 /* Global Variable.
8497 Don't enter into the minimal symbol tables as there is
8498 a minimal symbol table entry from the ELF symbols already.
8499 Enter into partial symbol table if it has a location
8500 descriptor or a type.
8501 If the location descriptor is missing, new_symbol will create
8502 a LOC_UNRESOLVED symbol, the address of the variable will then
8503 be determined from the minimal symbol table whenever the variable
8504 is referenced.
8505 The address for the partial symbol table entry is not
8506 used by GDB, but it comes in handy for debugging partial symbol
8507 table building. */
8508
95554aad 8509 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8510 {
8511 psymbol.domain = VAR_DOMAIN;
8512 psymbol.aclass = LOC_STATIC;
8513 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8514 psymbol.ginfo.value.address = addr;
8515 where = psymbol_placement::GLOBAL;
8516 }
c906108c
SS
8517 }
8518 else
8519 {
ff908ebf
AW
8520 int has_loc = pdi->d.locdesc != NULL;
8521
8522 /* Static Variable. Skip symbols whose value we cannot know (those
8523 without location descriptors or constant values). */
8524 if (!has_loc && !pdi->has_const_value)
43816ebc 8525 return;
ff908ebf 8526
76e288d1
TT
8527 psymbol.domain = VAR_DOMAIN;
8528 psymbol.aclass = LOC_STATIC;
8529 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8530 if (has_loc)
8531 psymbol.ginfo.value.address = addr;
8532 where = psymbol_placement::STATIC;
c906108c
SS
8533 }
8534 break;
8535 case DW_TAG_typedef:
8536 case DW_TAG_base_type:
a02abb62 8537 case DW_TAG_subrange_type:
76e288d1
TT
8538 psymbol.domain = VAR_DOMAIN;
8539 psymbol.aclass = LOC_TYPEDEF;
8540 where = psymbol_placement::STATIC;
c906108c 8541 break;
74921315 8542 case DW_TAG_imported_declaration:
72bf9492 8543 case DW_TAG_namespace:
76e288d1
TT
8544 psymbol.domain = VAR_DOMAIN;
8545 psymbol.aclass = LOC_TYPEDEF;
8546 where = psymbol_placement::GLOBAL;
72bf9492 8547 break;
530e8392 8548 case DW_TAG_module:
a5fd13a9
BH
8549 /* With Fortran 77 there might be a "BLOCK DATA" module
8550 available without any name. If so, we skip the module as it
8551 doesn't bring any value. */
8552 if (actual_name != nullptr)
76e288d1
TT
8553 {
8554 psymbol.domain = MODULE_DOMAIN;
8555 psymbol.aclass = LOC_TYPEDEF;
8556 where = psymbol_placement::GLOBAL;
8557 }
530e8392 8558 break;
c906108c 8559 case DW_TAG_class_type:
680b30c7 8560 case DW_TAG_interface_type:
c906108c
SS
8561 case DW_TAG_structure_type:
8562 case DW_TAG_union_type:
8563 case DW_TAG_enumeration_type:
fa4028e9
JB
8564 /* Skip external references. The DWARF standard says in the section
8565 about "Structure, Union, and Class Type Entries": "An incomplete
8566 structure, union or class type is represented by a structure,
8567 union or class entry that does not have a byte size attribute
8568 and that has a DW_AT_declaration attribute." */
8569 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8570 return;
fa4028e9 8571
63d06c5c
DC
8572 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8573 static vs. global. */
76e288d1
TT
8574 psymbol.domain = STRUCT_DOMAIN;
8575 psymbol.aclass = LOC_TYPEDEF;
8576 where = (cu->language == language_cplus
8577 ? psymbol_placement::GLOBAL
8578 : psymbol_placement::STATIC);
c906108c
SS
8579 break;
8580 case DW_TAG_enumerator:
76e288d1
TT
8581 psymbol.domain = VAR_DOMAIN;
8582 psymbol.aclass = LOC_CONST;
8583 where = (cu->language == language_cplus
8584 ? psymbol_placement::GLOBAL
8585 : psymbol_placement::STATIC);
c906108c
SS
8586 break;
8587 default:
8588 break;
8589 }
76e288d1
TT
8590
8591 if (where.has_value ())
8592 {
f049a313
TT
8593 if (built_actual_name != nullptr)
8594 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8595 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8596 psymbol.ginfo.set_linkage_name (actual_name);
8597 else
8598 {
8599 psymbol.ginfo.set_demangled_name (actual_name,
8600 &objfile->objfile_obstack);
8601 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8602 }
76e288d1
TT
8603 add_psymbol_to_list (psymbol, *where, objfile);
8604 }
c906108c
SS
8605}
8606
5c4e30ca
DC
8607/* Read a partial die corresponding to a namespace; also, add a symbol
8608 corresponding to that namespace to the symbol table. NAMESPACE is
8609 the name of the enclosing namespace. */
91c24f0a 8610
72bf9492
DJ
8611static void
8612add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8613 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8614 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8615{
72bf9492 8616 /* Add a symbol for the namespace. */
e7c27a73 8617
72bf9492 8618 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8619
8620 /* Now scan partial symbols in that namespace. */
8621
91c24f0a 8622 if (pdi->has_children)
cdc07690 8623 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8624}
8625
5d7cb8df
JK
8626/* Read a partial die corresponding to a Fortran module. */
8627
8628static void
8629add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8630 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8631{
530e8392
KB
8632 /* Add a symbol for the namespace. */
8633
8634 add_partial_symbol (pdi, cu);
8635
f55ee35c 8636 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8637
8638 if (pdi->has_children)
cdc07690 8639 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8640}
8641
b1dc1806
XR
8642/* Read a partial die corresponding to a subprogram or an inlined
8643 subprogram and create a partial symbol for that subprogram.
8644 When the CU language allows it, this routine also defines a partial
8645 symbol for each nested subprogram that this subprogram contains.
8646 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8647 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8648
cdc07690
YQ
8649 PDI may also be a lexical block, in which case we simply search
8650 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8651 Again, this is only performed when the CU language allows this
8652 type of definitions. */
8653
8654static void
8655add_partial_subprogram (struct partial_die_info *pdi,
8656 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8657 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8658{
b1dc1806 8659 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8660 {
8661 if (pdi->has_pc_info)
8662 {
8663 if (pdi->lowpc < *lowpc)
8664 *lowpc = pdi->lowpc;
8665 if (pdi->highpc > *highpc)
8666 *highpc = pdi->highpc;
cdc07690 8667 if (set_addrmap)
5734ee8b 8668 {
5e22e966 8669 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 8670 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8671 CORE_ADDR baseaddr;
b926417a
TT
8672 CORE_ADDR this_highpc;
8673 CORE_ADDR this_lowpc;
5734ee8b 8674
b3b3bada 8675 baseaddr = objfile->text_section_offset ();
b926417a
TT
8676 this_lowpc
8677 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8678 pdi->lowpc + baseaddr)
8679 - baseaddr);
8680 this_highpc
8681 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8682 pdi->highpc + baseaddr)
8683 - baseaddr);
d320c2b5 8684 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8685 this_lowpc, this_highpc - 1,
9291a0cd 8686 cu->per_cu->v.psymtab);
5734ee8b 8687 }
481860b3
GB
8688 }
8689
8690 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8691 {
bc30ff58 8692 if (!pdi->is_declaration)
e8d05480
JB
8693 /* Ignore subprogram DIEs that do not have a name, they are
8694 illegal. Do not emit a complaint at this point, we will
8695 do so when we convert this psymtab into a symtab. */
7d00ffec 8696 if (pdi->name (cu))
e8d05480 8697 add_partial_symbol (pdi, cu);
bc30ff58
JB
8698 }
8699 }
6e70227d 8700
bc30ff58
JB
8701 if (! pdi->has_children)
8702 return;
8703
0a4b0913 8704 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8705 {
8706 pdi = pdi->die_child;
8707 while (pdi != NULL)
8708 {
52356b79 8709 pdi->fixup (cu);
bc30ff58 8710 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8711 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8712 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8713 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8714 pdi = pdi->die_sibling;
8715 }
8716 }
8717}
8718
91c24f0a
DC
8719/* Read a partial die corresponding to an enumeration type. */
8720
72bf9492
DJ
8721static void
8722add_partial_enumeration (struct partial_die_info *enum_pdi,
8723 struct dwarf2_cu *cu)
91c24f0a 8724{
72bf9492 8725 struct partial_die_info *pdi;
91c24f0a 8726
7d00ffec 8727 if (enum_pdi->name (cu) != NULL)
72bf9492
DJ
8728 add_partial_symbol (enum_pdi, cu);
8729
8730 pdi = enum_pdi->die_child;
8731 while (pdi)
91c24f0a 8732 {
7d00ffec 8733 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
b98664d3 8734 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8735 else
72bf9492
DJ
8736 add_partial_symbol (pdi, cu);
8737 pdi = pdi->die_sibling;
91c24f0a 8738 }
91c24f0a
DC
8739}
8740
6caca83c
CC
8741/* Return the initial uleb128 in the die at INFO_PTR. */
8742
8743static unsigned int
d521ce57 8744peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8745{
8746 unsigned int bytes_read;
8747
8748 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8749}
8750
685af9cd
TT
8751/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8752 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8753
4bb7a0a7
DJ
8754 Return the corresponding abbrev, or NULL if the number is zero (indicating
8755 an empty DIE). In either case *BYTES_READ will be set to the length of
8756 the initial number. */
8757
8758static struct abbrev_info *
685af9cd
TT
8759peek_die_abbrev (const die_reader_specs &reader,
8760 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8761{
685af9cd 8762 dwarf2_cu *cu = reader.cu;
5e22e966 8763 bfd *abfd = cu->per_objfile->objfile->obfd;
685af9cd
TT
8764 unsigned int abbrev_number
8765 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8766
8767 if (abbrev_number == 0)
8768 return NULL;
8769
685af9cd 8770 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8771 if (!abbrev)
8772 {
422b9917 8773 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8774 " at offset %s [in module %s]"),
422b9917 8775 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8776 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8777 }
8778
8779 return abbrev;
8780}
8781
93311388
DE
8782/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8783 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8784 DIE. Any children of the skipped DIEs will also be skipped. */
8785
d521ce57
TT
8786static const gdb_byte *
8787skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8788{
4bb7a0a7
DJ
8789 while (1)
8790 {
685af9cd
TT
8791 unsigned int bytes_read;
8792 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8793
4bb7a0a7
DJ
8794 if (abbrev == NULL)
8795 return info_ptr + bytes_read;
8796 else
dee91e82 8797 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8798 }
8799}
8800
93311388
DE
8801/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8802 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8803 abbrev corresponding to that skipped uleb128 should be passed in
8804 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8805 children. */
8806
d521ce57
TT
8807static const gdb_byte *
8808skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8809 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8810{
8811 unsigned int bytes_read;
8812 struct attribute attr;
dee91e82
DE
8813 bfd *abfd = reader->abfd;
8814 struct dwarf2_cu *cu = reader->cu;
d521ce57 8815 const gdb_byte *buffer = reader->buffer;
f664829e 8816 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8817 unsigned int form, i;
8818
8819 for (i = 0; i < abbrev->num_attrs; i++)
8820 {
8821 /* The only abbrev we care about is DW_AT_sibling. */
8822 if (abbrev->attrs[i].name == DW_AT_sibling)
8823 {
18a8505e
AT
8824 bool ignored;
8825 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8826 &ignored);
4bb7a0a7 8827 if (attr.form == DW_FORM_ref_addr)
b98664d3 8828 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8829 else
b9502d3f 8830 {
0826b30a 8831 sect_offset off = attr.get_ref_die_offset ();
9c541725 8832 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8833
8834 if (sibling_ptr < info_ptr)
b98664d3 8835 complaint (_("DW_AT_sibling points backwards"));
22869d73 8836 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8837 reader->die_section->overflow_complaint ();
b9502d3f
WN
8838 else
8839 return sibling_ptr;
8840 }
4bb7a0a7
DJ
8841 }
8842
8843 /* If it isn't DW_AT_sibling, skip this attribute. */
8844 form = abbrev->attrs[i].form;
8845 skip_attribute:
8846 switch (form)
8847 {
4bb7a0a7 8848 case DW_FORM_ref_addr:
ae411497
TT
8849 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8850 and later it is offset sized. */
8851 if (cu->header.version == 2)
8852 info_ptr += cu->header.addr_size;
8853 else
8854 info_ptr += cu->header.offset_size;
8855 break;
36586728
TT
8856 case DW_FORM_GNU_ref_alt:
8857 info_ptr += cu->header.offset_size;
8858 break;
ae411497 8859 case DW_FORM_addr:
4bb7a0a7
DJ
8860 info_ptr += cu->header.addr_size;
8861 break;
8862 case DW_FORM_data1:
8863 case DW_FORM_ref1:
8864 case DW_FORM_flag:
8fe0f950 8865 case DW_FORM_strx1:
4bb7a0a7
DJ
8866 info_ptr += 1;
8867 break;
2dc7f7b3 8868 case DW_FORM_flag_present:
43988095 8869 case DW_FORM_implicit_const:
2dc7f7b3 8870 break;
4bb7a0a7
DJ
8871 case DW_FORM_data2:
8872 case DW_FORM_ref2:
8fe0f950 8873 case DW_FORM_strx2:
4bb7a0a7
DJ
8874 info_ptr += 2;
8875 break;
8fe0f950
AT
8876 case DW_FORM_strx3:
8877 info_ptr += 3;
8878 break;
4bb7a0a7
DJ
8879 case DW_FORM_data4:
8880 case DW_FORM_ref4:
8fe0f950 8881 case DW_FORM_strx4:
4bb7a0a7
DJ
8882 info_ptr += 4;
8883 break;
8884 case DW_FORM_data8:
8885 case DW_FORM_ref8:
55f1336d 8886 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8887 info_ptr += 8;
8888 break;
0224619f
JK
8889 case DW_FORM_data16:
8890 info_ptr += 16;
8891 break;
4bb7a0a7 8892 case DW_FORM_string:
9b1c24c8 8893 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8894 info_ptr += bytes_read;
8895 break;
2dc7f7b3 8896 case DW_FORM_sec_offset:
4bb7a0a7 8897 case DW_FORM_strp:
36586728 8898 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8899 info_ptr += cu->header.offset_size;
8900 break;
2dc7f7b3 8901 case DW_FORM_exprloc:
4bb7a0a7
DJ
8902 case DW_FORM_block:
8903 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8904 info_ptr += bytes_read;
8905 break;
8906 case DW_FORM_block1:
8907 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8908 break;
8909 case DW_FORM_block2:
8910 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8911 break;
8912 case DW_FORM_block4:
8913 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8914 break;
336d760d 8915 case DW_FORM_addrx:
cf532bd1 8916 case DW_FORM_strx:
4bb7a0a7
DJ
8917 case DW_FORM_sdata:
8918 case DW_FORM_udata:
8919 case DW_FORM_ref_udata:
3019eac3
DE
8920 case DW_FORM_GNU_addr_index:
8921 case DW_FORM_GNU_str_index:
18a8505e 8922 case DW_FORM_rnglistx:
41144253 8923 case DW_FORM_loclistx:
d521ce57 8924 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8925 break;
8926 case DW_FORM_indirect:
8927 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8928 info_ptr += bytes_read;
8929 /* We need to continue parsing from here, so just go back to
8930 the top. */
8931 goto skip_attribute;
8932
8933 default:
3e43a32a
MS
8934 error (_("Dwarf Error: Cannot handle %s "
8935 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8936 dwarf_form_name (form),
8937 bfd_get_filename (abfd));
8938 }
8939 }
8940
8941 if (abbrev->has_children)
dee91e82 8942 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8943 else
8944 return info_ptr;
8945}
8946
93311388 8947/* Locate ORIG_PDI's sibling.
dee91e82 8948 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8949
d521ce57 8950static const gdb_byte *
dee91e82
DE
8951locate_pdi_sibling (const struct die_reader_specs *reader,
8952 struct partial_die_info *orig_pdi,
d521ce57 8953 const gdb_byte *info_ptr)
91c24f0a
DC
8954{
8955 /* Do we know the sibling already? */
72bf9492 8956
91c24f0a
DC
8957 if (orig_pdi->sibling)
8958 return orig_pdi->sibling;
8959
8960 /* Are there any children to deal with? */
8961
8962 if (!orig_pdi->has_children)
8963 return info_ptr;
8964
4bb7a0a7 8965 /* Skip the children the long way. */
91c24f0a 8966
dee91e82 8967 return skip_children (reader, info_ptr);
91c24f0a
DC
8968}
8969
257e7a09 8970/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8971 not NULL. */
c906108c 8972
891813be
TT
8973void
8974dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8975{
976ca316 8976 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 8977
976ca316 8978 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
af758d11 8979
077cbab2
TT
8980 /* If this psymtab is constructed from a debug-only objfile, the
8981 has_section_at_zero flag will not necessarily be correct. We
8982 can get the correct value for this flag by looking at the data
8983 associated with the (presumably stripped) associated objfile. */
8984 if (objfile->separate_debug_objfile_backlink)
c906108c 8985 {
976ca316 8986 dwarf2_per_objfile *per_objfile_backlink
077cbab2 8987 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8988
976ca316
SM
8989 per_objfile->per_bfd->has_section_at_zero
8990 = per_objfile_backlink->per_bfd->has_section_at_zero;
077cbab2 8991 }
98bfdba5 8992
8566b89b 8993 expand_psymtab (objfile);
95554aad 8994
976ca316 8995 process_cu_includes (per_objfile);
c906108c 8996}
9cdd5dbd
DE
8997\f
8998/* Reading in full CUs. */
c906108c 8999
10b3939b
DJ
9000/* Add PER_CU to the queue. */
9001
9002static void
120ce1b5
SM
9003queue_comp_unit (dwarf2_per_cu_data *per_cu,
9004 dwarf2_per_objfile *per_objfile,
95554aad 9005 enum language pretend_language)
10b3939b 9006{
10b3939b 9007 per_cu->queued = 1;
120ce1b5 9008 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
9009}
9010
89e63ee4
DE
9011/* If PER_CU is not yet queued, add it to the queue.
9012 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9013 dependency.
0907af0c 9014 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9015 meaning either PER_CU is already queued or it is already loaded.
9016
9017 N.B. There is an invariant here that if a CU is queued then it is loaded.
9018 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9019
9020static int
89e63ee4 9021maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
9022 dwarf2_per_cu_data *per_cu,
9023 dwarf2_per_objfile *per_objfile,
0907af0c
DE
9024 enum language pretend_language)
9025{
9026 /* We may arrive here during partial symbol reading, if we need full
9027 DIEs to process an unusual case (e.g. template arguments). Do
9028 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 9029 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c 9030 {
7188ed02
SM
9031 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9032
9033 if (cu == NULL || cu->dies == NULL)
0907af0c
DE
9034 return 1;
9035 return 0;
9036 }
9037
9038 /* Mark the dependence relation so that we don't flush PER_CU
9039 too early. */
89e63ee4
DE
9040 if (dependent_cu != NULL)
9041 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9042
9043 /* If it's already on the queue, we have nothing to do. */
9044 if (per_cu->queued)
9045 return 0;
9046
9047 /* If the compilation unit is already loaded, just mark it as
9048 used. */
7188ed02
SM
9049 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9050 if (cu != nullptr)
0907af0c 9051 {
7188ed02 9052 cu->last_used = 0;
0907af0c
DE
9053 return 0;
9054 }
9055
9056 /* Add it to the queue. */
120ce1b5 9057 queue_comp_unit (per_cu, per_objfile, pretend_language);
0907af0c
DE
9058
9059 return 1;
9060}
9061
10b3939b
DJ
9062/* Process the queue. */
9063
9064static void
976ca316 9065process_queue (dwarf2_per_objfile *per_objfile)
10b3939b 9066{
b4f54984 9067 if (dwarf_read_debug)
45cfd468
DE
9068 {
9069 fprintf_unfiltered (gdb_stdlog,
9070 "Expanding one or more symtabs of objfile %s ...\n",
976ca316 9071 objfile_name (per_objfile->objfile));
45cfd468
DE
9072 }
9073
03dd20cc
DJ
9074 /* The queue starts out with one item, but following a DIE reference
9075 may load a new CU, adding it to the end of the queue. */
976ca316 9076 while (!per_objfile->per_bfd->queue.empty ())
10b3939b 9077 {
976ca316 9078 dwarf2_queue_item &item = per_objfile->per_bfd->queue.front ();
7188ed02 9079 dwarf2_per_cu_data *per_cu = item.per_cu;
39856def 9080
976ca316 9081 if (!per_objfile->symtab_set_p (per_cu))
f4dc4d17 9082 {
976ca316 9083 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
f4dc4d17 9084
7188ed02
SM
9085 /* Skip dummy CUs. */
9086 if (cu != nullptr)
73be47f5 9087 {
7188ed02
SM
9088 unsigned int debug_print_threshold;
9089 char buf[100];
9090
9091 if (per_cu->is_debug_types)
9092 {
9093 struct signatured_type *sig_type =
9094 (struct signatured_type *) per_cu;
9095
9096 sprintf (buf, "TU %s at offset %s",
9097 hex_string (sig_type->signature),
9098 sect_offset_str (per_cu->sect_off));
9099 /* There can be 100s of TUs.
9100 Only print them in verbose mode. */
9101 debug_print_threshold = 2;
9102 }
9103 else
9104 {
9105 sprintf (buf, "CU at offset %s",
9106 sect_offset_str (per_cu->sect_off));
9107 debug_print_threshold = 1;
9108 }
247f5c4f 9109
7188ed02
SM
9110 if (dwarf_read_debug >= debug_print_threshold)
9111 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17 9112
7188ed02
SM
9113 if (per_cu->is_debug_types)
9114 process_full_type_unit (cu, item.pretend_language);
9115 else
9116 process_full_comp_unit (cu, item.pretend_language);
f4dc4d17 9117
7188ed02
SM
9118 if (dwarf_read_debug >= debug_print_threshold)
9119 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9120 }
f4dc4d17 9121 }
10b3939b 9122
7188ed02 9123 per_cu->queued = 0;
976ca316 9124 per_objfile->per_bfd->queue.pop ();
10b3939b
DJ
9125 }
9126
b4f54984 9127 if (dwarf_read_debug)
45cfd468
DE
9128 {
9129 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
976ca316 9130 objfile_name (per_objfile->objfile));
45cfd468 9131 }
10b3939b
DJ
9132}
9133
10b3939b
DJ
9134/* Read in full symbols for PST, and anything it depends on. */
9135
8566b89b
TT
9136void
9137dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9138{
af758d11 9139 gdb_assert (!readin_p (objfile));
95554aad 9140
17ee85fc
TT
9141 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9142 free_cached_comp_units freer (per_objfile);
48993951 9143 expand_dependencies (objfile);
aaa75496 9144
97a1449a 9145 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 9146 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
9147}
9148
af758d11
SM
9149/* See psympriv.h. */
9150
9151bool
9152dwarf2_psymtab::readin_p (struct objfile *objfile) const
9153{
9154 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9155 return per_objfile->symtab_set_p (per_cu_data);
9156}
9157
9158/* See psympriv.h. */
9159
9160compunit_symtab *
9161dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9162{
9163 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9164 return per_objfile->get_symtab (per_cu_data);
9165}
9166
dee91e82
DE
9167/* Trivial hash function for die_info: the hash value of a DIE
9168 is its offset in .debug_info for this objfile. */
10b3939b 9169
dee91e82
DE
9170static hashval_t
9171die_hash (const void *item)
10b3939b 9172{
9a3c8263 9173 const struct die_info *die = (const struct die_info *) item;
6502dd73 9174
9c541725 9175 return to_underlying (die->sect_off);
dee91e82 9176}
63d06c5c 9177
dee91e82
DE
9178/* Trivial comparison function for die_info structures: two DIEs
9179 are equal if they have the same offset. */
98bfdba5 9180
dee91e82
DE
9181static int
9182die_eq (const void *item_lhs, const void *item_rhs)
9183{
9a3c8263
SM
9184 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9185 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9186
9c541725 9187 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9188}
c906108c 9189
c0ab21c2 9190/* Load the DIEs associated with PER_CU into memory. */
c906108c 9191
dee91e82 9192static void
ab432490
SM
9193load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9194 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
9195 bool skip_partial,
9196 enum language pretend_language)
dee91e82 9197{
c0ab21c2
TT
9198 gdb_assert (! this_cu->is_debug_types);
9199
7188ed02
SM
9200 dwarf2_cu *existing_cu = per_objfile->get_cu (this_cu);
9201 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
c0ab21c2
TT
9202 if (reader.dummy_p)
9203 return;
9204
9205 struct dwarf2_cu *cu = reader.cu;
9206 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9207
dee91e82
DE
9208 gdb_assert (cu->die_hash == NULL);
9209 cu->die_hash =
9210 htab_create_alloc_ex (cu->header.length / 12,
9211 die_hash,
9212 die_eq,
9213 NULL,
9214 &cu->comp_unit_obstack,
9215 hashtab_obstack_allocate,
9216 dummy_obstack_deallocate);
e142c38c 9217
3e225074 9218 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9219 reader.comp_unit_die->child
9220 = read_die_and_siblings (&reader, reader.info_ptr,
9221 &info_ptr, reader.comp_unit_die);
9222 cu->dies = reader.comp_unit_die;
dee91e82 9223 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9224
9225 /* We try not to read any attributes in this function, because not
9cdd5dbd 9226 all CUs needed for references have been loaded yet, and symbol
10b3939b 9227 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9228 or we won't be able to build types correctly.
9229 Similarly, if we do not read the producer, we can not apply
9230 producer-specific interpretation. */
c0ab21c2 9231 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9232
9233 reader.keep ();
10b3939b
DJ
9234}
9235
3da10d80
KS
9236/* Add a DIE to the delayed physname list. */
9237
9238static void
9239add_to_method_list (struct type *type, int fnfield_index, int index,
9240 const char *name, struct die_info *die,
9241 struct dwarf2_cu *cu)
9242{
9243 struct delayed_method_info mi;
9244 mi.type = type;
9245 mi.fnfield_index = fnfield_index;
9246 mi.index = index;
9247 mi.name = name;
9248 mi.die = die;
c89b44cd 9249 cu->method_list.push_back (mi);
3da10d80
KS
9250}
9251
3693fdb3
PA
9252/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9253 "const" / "volatile". If so, decrements LEN by the length of the
9254 modifier and return true. Otherwise return false. */
9255
9256template<size_t N>
9257static bool
9258check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9259{
9260 size_t mod_len = sizeof (mod) - 1;
9261 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9262 {
9263 len -= mod_len;
9264 return true;
9265 }
9266 return false;
9267}
9268
3da10d80
KS
9269/* Compute the physnames of any methods on the CU's method list.
9270
9271 The computation of method physnames is delayed in order to avoid the
9272 (bad) condition that one of the method's formal parameters is of an as yet
9273 incomplete type. */
9274
9275static void
9276compute_delayed_physnames (struct dwarf2_cu *cu)
9277{
3693fdb3 9278 /* Only C++ delays computing physnames. */
c89b44cd 9279 if (cu->method_list.empty ())
3693fdb3
PA
9280 return;
9281 gdb_assert (cu->language == language_cplus);
9282
52941706 9283 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9284 {
1d06ead6 9285 const char *physname;
3da10d80 9286 struct fn_fieldlist *fn_flp
c89b44cd
TT
9287 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9288 physname = dwarf2_physname (mi.name, mi.die, cu);
9289 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9290 = physname ? physname : "";
3693fdb3
PA
9291
9292 /* Since there's no tag to indicate whether a method is a
9293 const/volatile overload, extract that information out of the
9294 demangled name. */
9295 if (physname != NULL)
9296 {
9297 size_t len = strlen (physname);
9298
9299 while (1)
9300 {
9301 if (physname[len] == ')') /* shortcut */
9302 break;
9303 else if (check_modifier (physname, len, " const"))
c89b44cd 9304 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9305 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9306 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9307 else
9308 break;
9309 }
9310 }
3da10d80 9311 }
c89b44cd
TT
9312
9313 /* The list is no longer needed. */
9314 cu->method_list.clear ();
3da10d80
KS
9315}
9316
a766d390
DE
9317/* Go objects should be embedded in a DW_TAG_module DIE,
9318 and it's not clear if/how imported objects will appear.
9319 To keep Go support simple until that's worked out,
9320 go back through what we've read and create something usable.
9321 We could do this while processing each DIE, and feels kinda cleaner,
9322 but that way is more invasive.
9323 This is to, for example, allow the user to type "p var" or "b main"
9324 without having to specify the package name, and allow lookups
9325 of module.object to work in contexts that use the expression
9326 parser. */
9327
9328static void
9329fixup_go_packaging (struct dwarf2_cu *cu)
9330{
421d1616 9331 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9332 struct pending *list;
9333 int i;
9334
c24bdb02 9335 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9336 list != NULL;
9337 list = list->next)
a766d390
DE
9338 {
9339 for (i = 0; i < list->nsyms; ++i)
9340 {
9341 struct symbol *sym = list->symbol[i];
9342
c1b5c1eb 9343 if (sym->language () == language_go
a766d390
DE
9344 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9345 {
421d1616
TT
9346 gdb::unique_xmalloc_ptr<char> this_package_name
9347 (go_symbol_package_name (sym));
a766d390
DE
9348
9349 if (this_package_name == NULL)
9350 continue;
9351 if (package_name == NULL)
421d1616 9352 package_name = std::move (this_package_name);
a766d390
DE
9353 else
9354 {
5e22e966 9355 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9356 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9357 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9358 (symbol_symtab (sym) != NULL
9359 ? symtab_to_filename_for_display
9360 (symbol_symtab (sym))
e3b94546 9361 : objfile_name (objfile)),
421d1616 9362 this_package_name.get (), package_name.get ());
a766d390
DE
9363 }
9364 }
9365 }
9366 }
9367
9368 if (package_name != NULL)
9369 {
5e22e966 9370 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9371 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9372 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9373 saved_package_name);
a766d390
DE
9374 struct symbol *sym;
9375
8c14c3a3 9376 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9377 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9378 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9379 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9380 e.g., "main" finds the "main" module and not C's main(). */
9381 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9382 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9383 SYMBOL_TYPE (sym) = type;
9384
c24bdb02 9385 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9386 }
9387}
9388
c9317f21
TT
9389/* Allocate a fully-qualified name consisting of the two parts on the
9390 obstack. */
9391
9392static const char *
9393rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9394{
9395 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9396}
9397
9c6a1327
TT
9398/* A helper that allocates a variant part to attach to a Rust enum
9399 type. OBSTACK is where the results should be allocated. TYPE is
9400 the type we're processing. DISCRIMINANT_INDEX is the index of the
9401 discriminant. It must be the index of one of the fields of TYPE.
9402 DEFAULT_INDEX is the index of the default field; or -1 if there is
9403 no default. RANGES is indexed by "effective" field number (the
9404 field index, but omitting the discriminant and default fields) and
9405 must hold the discriminant values used by the variants. Note that
9406 RANGES must have a lifetime at least as long as OBSTACK -- either
9407 already allocated on it, or static. */
c9317f21 9408
9c6a1327
TT
9409static void
9410alloc_rust_variant (struct obstack *obstack, struct type *type,
9411 int discriminant_index, int default_index,
9412 gdb::array_view<discriminant_range> ranges)
9413{
9414 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9415 must be handled by the caller. */
9416 gdb_assert (discriminant_index >= 0
1f704f76 9417 && discriminant_index < type->num_fields ());
c9317f21 9418 gdb_assert (default_index == -1
1f704f76 9419 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9420
9c6a1327 9421 /* We have one variant for each non-discriminant field. */
1f704f76 9422 int n_variants = type->num_fields () - 1;
c9317f21 9423
9c6a1327
TT
9424 variant *variants = new (obstack) variant[n_variants];
9425 int var_idx = 0;
9426 int range_idx = 0;
1f704f76 9427 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9428 {
9429 if (i == discriminant_index)
9430 continue;
c9317f21 9431
9c6a1327
TT
9432 variants[var_idx].first_field = i;
9433 variants[var_idx].last_field = i + 1;
9434
9435 /* The default field does not need a range, but other fields do.
9436 We skipped the discriminant above. */
9437 if (i != default_index)
9438 {
9439 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9440 ++range_idx;
9441 }
c9317f21 9442
9c6a1327
TT
9443 ++var_idx;
9444 }
9445
9446 gdb_assert (range_idx == ranges.size ());
9447 gdb_assert (var_idx == n_variants);
9448
9449 variant_part *part = new (obstack) variant_part;
9450 part->discriminant_index = discriminant_index;
940da03e 9451 part->is_unsigned = TYPE_UNSIGNED (type->field (discriminant_index).type ());
9c6a1327
TT
9452 part->variants = gdb::array_view<variant> (variants, n_variants);
9453
9454 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9455 gdb::array_view<variant_part> *prop_value
9456 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9457
9c6a1327
TT
9458 struct dynamic_prop prop;
9459 prop.kind = PROP_VARIANT_PARTS;
9460 prop.data.variant_parts = prop_value;
9461
5c54719c 9462 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9463}
9464
9465/* Some versions of rustc emitted enums in an unusual way.
9466
9467 Ordinary enums were emitted as unions. The first element of each
9468 structure in the union was named "RUST$ENUM$DISR". This element
9469 held the discriminant.
9470
9471 These versions of Rust also implemented the "non-zero"
9472 optimization. When the enum had two values, and one is empty and
9473 the other holds a pointer that cannot be zero, the pointer is used
9474 as the discriminant, with a zero value meaning the empty variant.
9475 Here, the union's first member is of the form
9476 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9477 where the fieldnos are the indices of the fields that should be
9478 traversed in order to find the field (which may be several fields deep)
9479 and the variantname is the name of the variant of the case when the
9480 field is zero.
9481
9482 This function recognizes whether TYPE is of one of these forms,
9483 and, if so, smashes it to be a variant type. */
9484
9485static void
9486quirk_rust_enum (struct type *type, struct objfile *objfile)
9487{
78134374 9488 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9489
9490 /* We don't need to deal with empty enums. */
1f704f76 9491 if (type->num_fields () == 0)
c9317f21
TT
9492 return;
9493
9494#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9495 if (type->num_fields () == 1
c9317f21
TT
9496 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9497 {
9498 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9499
9500 /* Decode the field name to find the offset of the
9501 discriminant. */
9502 ULONGEST bit_offset = 0;
940da03e 9503 struct type *field_type = type->field (0).type ();
c9317f21
TT
9504 while (name[0] >= '0' && name[0] <= '9')
9505 {
9506 char *tail;
9507 unsigned long index = strtoul (name, &tail, 10);
9508 name = tail;
9509 if (*name != '$'
1f704f76 9510 || index >= field_type->num_fields ()
c9317f21
TT
9511 || (TYPE_FIELD_LOC_KIND (field_type, index)
9512 != FIELD_LOC_KIND_BITPOS))
9513 {
b98664d3 9514 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9515 "[in module %s]"),
9516 TYPE_FIELD_NAME (type, 0),
9517 objfile_name (objfile));
9518 return;
9519 }
9520 ++name;
9521
9522 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
940da03e 9523 field_type = field_type->field (index).type ();
c9317f21
TT
9524 }
9525
9c6a1327
TT
9526 /* Smash this type to be a structure type. We have to do this
9527 because the type has already been recorded. */
67607e24 9528 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9529 type->set_num_fields (3);
9c6a1327 9530 /* Save the field we care about. */
ceacbf6e 9531 struct field saved_field = type->field (0);
3cabb6b0
SM
9532 type->set_fields
9533 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9534
9c6a1327 9535 /* Put the discriminant at index 0. */
5d14b6e5 9536 type->field (0).set_type (field_type);
9c6a1327
TT
9537 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9538 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9539 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9540
9541 /* The order of fields doesn't really matter, so put the real
9542 field at index 1 and the data-less field at index 2. */
ceacbf6e 9543 type->field (1) = saved_field;
9c6a1327 9544 TYPE_FIELD_NAME (type, 1)
940da03e
SM
9545 = rust_last_path_segment (type->field (1).type ()->name ());
9546 type->field (1).type ()->set_name
7d93a1e0 9547 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9548 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9549
9550 const char *dataless_name
7d93a1e0 9551 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9552 name);
9553 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9554 dataless_name);
5d14b6e5 9555 type->field (2).set_type (dataless_type);
c9317f21
TT
9556 /* NAME points into the original discriminant name, which
9557 already has the correct lifetime. */
9c6a1327 9558 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9559 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9560
9c6a1327
TT
9561 /* Indicate that this is a variant type. */
9562 static discriminant_range ranges[1] = { { 0, 0 } };
9563 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9564 }
77c2dba3
TT
9565 /* A union with a single anonymous field is probably an old-style
9566 univariant enum. */
1f704f76 9567 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9568 {
c9317f21
TT
9569 /* Smash this type to be a structure type. We have to do this
9570 because the type has already been recorded. */
67607e24 9571 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9572
940da03e 9573 struct type *field_type = type->field (0).type ();
c9317f21 9574 const char *variant_name
7d93a1e0 9575 = rust_last_path_segment (field_type->name ());
9c6a1327 9576 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9577 field_type->set_name
9578 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9579 type->name (), variant_name));
c9317f21
TT
9580 }
9581 else
9582 {
9583 struct type *disr_type = nullptr;
1f704f76 9584 for (int i = 0; i < type->num_fields (); ++i)
c9317f21 9585 {
940da03e 9586 disr_type = type->field (i).type ();
c9317f21 9587
78134374 9588 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9589 {
9590 /* All fields of a true enum will be structs. */
9591 return;
9592 }
1f704f76 9593 else if (disr_type->num_fields () == 0)
c9317f21
TT
9594 {
9595 /* Could be data-less variant, so keep going. */
a037790e 9596 disr_type = nullptr;
c9317f21
TT
9597 }
9598 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9599 "RUST$ENUM$DISR") != 0)
9600 {
9601 /* Not a Rust enum. */
9602 return;
9603 }
9604 else
9605 {
9606 /* Found one. */
9607 break;
9608 }
9609 }
9610
9611 /* If we got here without a discriminant, then it's probably
9612 just a union. */
9613 if (disr_type == nullptr)
9614 return;
9615
9616 /* Smash this type to be a structure type. We have to do this
9617 because the type has already been recorded. */
67607e24 9618 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9619
9c6a1327 9620 /* Make space for the discriminant field. */
ceacbf6e 9621 struct field *disr_field = &disr_type->field (0);
9c6a1327 9622 field *new_fields
1f704f76 9623 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9624 * sizeof (struct field)));
80fc5e77 9625 memcpy (new_fields + 1, type->fields (),
1f704f76 9626 type->num_fields () * sizeof (struct field));
3cabb6b0 9627 type->set_fields (new_fields);
1f704f76 9628 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9629
9630 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9631 type->field (0) = *disr_field;
9c6a1327
TT
9632 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9633 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9634
9635 /* We need a way to find the correct discriminant given a
9636 variant name. For convenience we build a map here. */
b6cdac4b 9637 struct type *enum_type = disr_field->type ();
c9317f21 9638 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9639 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9640 {
9641 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9642 {
9643 const char *name
9644 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9645 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9646 }
9647 }
9648
1f704f76 9649 int n_fields = type->num_fields ();
9c6a1327
TT
9650 /* We don't need a range entry for the discriminant, but we do
9651 need one for every other field, as there is no default
9652 variant. */
9653 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9654 discriminant_range,
9655 n_fields - 1);
c9317f21
TT
9656 /* Skip the discriminant here. */
9657 for (int i = 1; i < n_fields; ++i)
9658 {
9659 /* Find the final word in the name of this variant's type.
9660 That name can be used to look up the correct
9661 discriminant. */
9662 const char *variant_name
940da03e 9663 = rust_last_path_segment (type->field (i).type ()->name ());
c9317f21
TT
9664
9665 auto iter = discriminant_map.find (variant_name);
9666 if (iter != discriminant_map.end ())
9c6a1327
TT
9667 {
9668 ranges[i].low = iter->second;
9669 ranges[i].high = iter->second;
9670 }
c9317f21 9671
bedda9ac 9672 /* Remove the discriminant field, if it exists. */
940da03e 9673 struct type *sub_type = type->field (i).type ();
1f704f76 9674 if (sub_type->num_fields () > 0)
bedda9ac 9675 {
5e33d5f4 9676 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9677 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9678 }
9c6a1327 9679 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9680 sub_type->set_name
9681 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9682 type->name (), variant_name));
c9317f21 9683 }
9c6a1327
TT
9684
9685 /* Indicate that this is a variant type. */
9686 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9687 gdb::array_view<discriminant_range> (ranges,
9688 n_fields - 1));
c9317f21
TT
9689 }
9690}
9691
9692/* Rewrite some Rust unions to be structures with variants parts. */
9693
9694static void
9695rust_union_quirks (struct dwarf2_cu *cu)
9696{
9697 gdb_assert (cu->language == language_rust);
52941706 9698 for (type *type_ : cu->rust_unions)
5e22e966 9699 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9700 /* We don't need this any more. */
9701 cu->rust_unions.clear ();
c9317f21
TT
9702}
9703
8adb8487
TT
9704/* See read.h. */
9705
9706type_unit_group_unshareable *
9707dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9708{
9709 auto iter = this->m_type_units.find (tu_group);
9710 if (iter != this->m_type_units.end ())
9711 return iter->second.get ();
9712
9713 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9714 type_unit_group_unshareable *result = uniq.get ();
9715 this->m_type_units[tu_group] = std::move (uniq);
9716 return result;
9717}
9718
e286671b
TT
9719struct type *
9720dwarf2_per_objfile::get_type_for_signatured_type
9721 (signatured_type *sig_type) const
9722{
9723 auto iter = this->m_type_map.find (sig_type);
9724 if (iter == this->m_type_map.end ())
9725 return nullptr;
9726
9727 return iter->second;
9728}
9729
9730void dwarf2_per_objfile::set_type_for_signatured_type
9731 (signatured_type *sig_type, struct type *type)
9732{
9733 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9734
9735 this->m_type_map[sig_type] = type;
9736}
9737
95554aad
TT
9738/* A helper function for computing the list of all symbol tables
9739 included by PER_CU. */
9740
9741static void
4c39bc03 9742recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9743 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9744 dwarf2_per_cu_data *per_cu,
9745 dwarf2_per_objfile *per_objfile,
43f3e411 9746 struct compunit_symtab *immediate_parent)
95554aad 9747{
af758d11 9748 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9749 if (*slot != NULL)
9750 {
9751 /* This inclusion and its children have been processed. */
9752 return;
9753 }
9754
9755 *slot = per_cu;
af758d11 9756
95554aad 9757 /* Only add a CU if it has a symbol table. */
43182c09 9758 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9759 if (cust != NULL)
ec94af83
DE
9760 {
9761 /* If this is a type unit only add its symbol table if we haven't
9762 seen it yet (type unit per_cu's can share symtabs). */
9763 if (per_cu->is_debug_types)
9764 {
43f3e411 9765 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9766 if (*slot == NULL)
9767 {
43f3e411 9768 *slot = cust;
4c39bc03 9769 result->push_back (cust);
43f3e411
DE
9770 if (cust->user == NULL)
9771 cust->user = immediate_parent;
ec94af83
DE
9772 }
9773 }
9774 else
f9125b6c 9775 {
4c39bc03 9776 result->push_back (cust);
43f3e411
DE
9777 if (cust->user == NULL)
9778 cust->user = immediate_parent;
f9125b6c 9779 }
ec94af83 9780 }
95554aad 9781
ae640021
AB
9782 if (!per_cu->imported_symtabs_empty ())
9783 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9784 {
9785 recursively_compute_inclusions (result, all_children,
43182c09
SM
9786 all_type_symtabs, ptr, per_objfile,
9787 cust);
ae640021 9788 }
95554aad
TT
9789}
9790
43f3e411 9791/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9792 PER_CU. */
9793
9794static void
43182c09
SM
9795compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9796 dwarf2_per_objfile *per_objfile)
95554aad 9797{
f4dc4d17
DE
9798 gdb_assert (! per_cu->is_debug_types);
9799
ae640021 9800 if (!per_cu->imported_symtabs_empty ())
95554aad 9801 {
ae640021 9802 int len;
4c39bc03 9803 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9804 htab_t all_children, all_type_symtabs;
43182c09 9805 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9806
9807 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9808 if (cust == NULL)
95554aad
TT
9809 return;
9810
9811 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9812 NULL, xcalloc, xfree);
ec94af83
DE
9813 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9814 NULL, xcalloc, xfree);
95554aad 9815
ae640021 9816 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9817 {
9818 recursively_compute_inclusions (&result_symtabs, all_children,
43182c09
SM
9819 all_type_symtabs, ptr, per_objfile,
9820 cust);
ec94af83 9821 }
95554aad 9822
ec94af83 9823 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9824 len = result_symtabs.size ();
43f3e411 9825 cust->includes
f6e649dd 9826 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9827 struct compunit_symtab *, len + 1);
4c39bc03
TT
9828 memcpy (cust->includes, result_symtabs.data (),
9829 len * sizeof (compunit_symtab *));
43f3e411 9830 cust->includes[len] = NULL;
95554aad 9831
95554aad 9832 htab_delete (all_children);
ec94af83 9833 htab_delete (all_type_symtabs);
95554aad
TT
9834 }
9835}
9836
9837/* Compute the 'includes' field for the symtabs of all the CUs we just
9838 read. */
9839
9840static void
976ca316 9841process_cu_includes (dwarf2_per_objfile *per_objfile)
95554aad 9842{
976ca316 9843 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9844 {
9845 if (! iter->is_debug_types)
976ca316 9846 compute_compunit_symtab_includes (iter, per_objfile);
f4dc4d17 9847 }
95554aad 9848
976ca316 9849 per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9850}
9851
8fc0b21d 9852/* Generate full symbol information for CU, whose DIEs have
10b3939b
DJ
9853 already been loaded into memory. */
9854
9855static void
8fc0b21d 9856process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10b3939b 9857{
976ca316
SM
9858 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9859 struct objfile *objfile = per_objfile->objfile;
08feed99 9860 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9861 CORE_ADDR lowpc, highpc;
43f3e411 9862 struct compunit_symtab *cust;
10b3939b 9863 CORE_ADDR baseaddr;
4359dff1 9864 struct block *static_block;
3e29f34a 9865 CORE_ADDR addr;
10b3939b 9866
b3b3bada 9867 baseaddr = objfile->text_section_offset ();
10b3939b 9868
c89b44cd
TT
9869 /* Clear the list here in case something was left over. */
9870 cu->method_list.clear ();
10b3939b 9871
95554aad
TT
9872 cu->language = pretend_language;
9873 cu->language_defn = language_def (cu->language);
9874
c906108c 9875 /* Do line number decoding in read_file_scope () */
10b3939b 9876 process_die (cu->dies, cu);
c906108c 9877
a766d390
DE
9878 /* For now fudge the Go package. */
9879 if (cu->language == language_go)
9880 fixup_go_packaging (cu);
9881
5f48f8f3 9882 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9883 should be complete, and it should now be safe to compute all of the
9884 physnames. */
9885 compute_delayed_physnames (cu);
3da10d80 9886
c9317f21
TT
9887 if (cu->language == language_rust)
9888 rust_union_quirks (cu);
9889
fae299cd
DC
9890 /* Some compilers don't define a DW_AT_high_pc attribute for the
9891 compilation unit. If the DW_AT_high_pc is missing, synthesize
9892 it, by scanning the DIE's below the compilation unit. */
10b3939b 9893 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9894
3e29f34a 9895 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9896 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9897
9898 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9899 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9900 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9901 addrmap to help ensure it has an accurate map of pc values belonging to
9902 this comp unit. */
9903 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9904
c24bdb02 9905 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9906 SECT_OFF_TEXT (objfile),
9907 0);
c906108c 9908
43f3e411 9909 if (cust != NULL)
c906108c 9910 {
df15bd07 9911 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9912
8be455d7
JK
9913 /* Set symtab language to language from DW_AT_language. If the
9914 compilation is from a C file generated by language preprocessors, do
9915 not set the language if it was already deduced by start_subfile. */
43f3e411 9916 if (!(cu->language == language_c
40e3ad0e 9917 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9918 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9919
9920 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9921 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9922 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9923 there were bugs in prologue debug info, fixed later in GCC-4.5
9924 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9925
9926 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9927 needed, it would be wrong due to missing DW_AT_producer there.
9928
9929 Still one can confuse GDB by using non-standard GCC compilation
9930 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9931 */
ab260dad 9932 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9933 cust->locations_valid = 1;
e0d00bc7
JK
9934
9935 if (gcc_4_minor >= 5)
43f3e411 9936 cust->epilogue_unwind_valid = 1;
96408a79 9937
43f3e411 9938 cust->call_site_htab = cu->call_site_htab;
c906108c 9939 }
9291a0cd 9940
976ca316 9941 per_objfile->set_symtab (cu->per_cu, cust);
c906108c 9942
95554aad 9943 /* Push it for inclusion processing later. */
976ca316 9944 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
804d2729
TT
9945
9946 /* Not needed any more. */
c24bdb02 9947 cu->reset_builder ();
f4dc4d17 9948}
45cfd468 9949
8fc0b21d 9950/* Generate full symbol information for type unit CU, whose DIEs have
f4dc4d17
DE
9951 already been loaded into memory. */
9952
9953static void
8fc0b21d 9954process_full_type_unit (dwarf2_cu *cu,
f4dc4d17
DE
9955 enum language pretend_language)
9956{
976ca316
SM
9957 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9958 struct objfile *objfile = per_objfile->objfile;
43f3e411 9959 struct compunit_symtab *cust;
0186c6a7
DE
9960 struct signatured_type *sig_type;
9961
8fc0b21d
SM
9962 gdb_assert (cu->per_cu->is_debug_types);
9963 sig_type = (struct signatured_type *) cu->per_cu;
f4dc4d17 9964
c89b44cd
TT
9965 /* Clear the list here in case something was left over. */
9966 cu->method_list.clear ();
f4dc4d17 9967
f4dc4d17
DE
9968 cu->language = pretend_language;
9969 cu->language_defn = language_def (cu->language);
9970
9971 /* The symbol tables are set up in read_type_unit_scope. */
9972 process_die (cu->dies, cu);
9973
9974 /* For now fudge the Go package. */
9975 if (cu->language == language_go)
9976 fixup_go_packaging (cu);
9977
5f48f8f3 9978 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9979 should be complete, and it should now be safe to compute all of the
9980 physnames. */
9981 compute_delayed_physnames (cu);
f4dc4d17 9982
c9317f21
TT
9983 if (cu->language == language_rust)
9984 rust_union_quirks (cu);
9985
f4dc4d17
DE
9986 /* TUs share symbol tables.
9987 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9988 of it with end_expandable_symtab. Otherwise, complete the addition of
9989 this TU's symbols to the existing symtab. */
8adb8487 9990 type_unit_group_unshareable *tug_unshare =
976ca316 9991 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8adb8487 9992 if (tug_unshare->compunit_symtab == NULL)
45cfd468 9993 {
c24bdb02
KS
9994 buildsym_compunit *builder = cu->get_builder ();
9995 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 9996 tug_unshare->compunit_symtab = cust;
f4dc4d17 9997
43f3e411 9998 if (cust != NULL)
f4dc4d17
DE
9999 {
10000 /* Set symtab language to language from DW_AT_language. If the
10001 compilation is from a C file generated by language preprocessors,
10002 do not set the language if it was already deduced by
10003 start_subfile. */
43f3e411
DE
10004 if (!(cu->language == language_c
10005 && COMPUNIT_FILETABS (cust)->language != language_c))
10006 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10007 }
10008 }
10009 else
10010 {
c24bdb02 10011 cu->get_builder ()->augment_type_symtab ();
8adb8487 10012 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
10013 }
10014
976ca316 10015 per_objfile->set_symtab (cu->per_cu, cust);
804d2729
TT
10016
10017 /* Not needed any more. */
c24bdb02 10018 cu->reset_builder ();
c906108c
SS
10019}
10020
95554aad
TT
10021/* Process an imported unit DIE. */
10022
10023static void
10024process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10025{
10026 struct attribute *attr;
10027
f4dc4d17
DE
10028 /* For now we don't handle imported units in type units. */
10029 if (cu->per_cu->is_debug_types)
10030 {
10031 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10032 " supported in type units [in module %s]"),
5e22e966 10033 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
10034 }
10035
95554aad
TT
10036 attr = dwarf2_attr (die, DW_AT_import, cu);
10037 if (attr != NULL)
10038 {
0826b30a 10039 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 10040 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 10041 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 10042 dwarf2_per_cu_data *per_cu
ab432490 10043 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 10044
58990295
TV
10045 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10046 into another compilation unit, at root level. Regard this as a hint,
10047 and ignore it. */
10048 if (die->parent && die->parent->parent == NULL
10049 && per_cu->unit_type == DW_UT_compile
10050 && per_cu->lang == language_cplus)
10051 return;
10052
69d751e3 10053 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 10054 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
ab432490 10055 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
95554aad 10056
ae640021 10057 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10058 }
10059}
10060
4c8aa72d
PA
10061/* RAII object that represents a process_die scope: i.e.,
10062 starts/finishes processing a DIE. */
10063class process_die_scope
adde2bff 10064{
4c8aa72d
PA
10065public:
10066 process_die_scope (die_info *die, dwarf2_cu *cu)
10067 : m_die (die), m_cu (cu)
10068 {
10069 /* We should only be processing DIEs not already in process. */
10070 gdb_assert (!m_die->in_process);
10071 m_die->in_process = true;
10072 }
8c3cb9fa 10073
4c8aa72d
PA
10074 ~process_die_scope ()
10075 {
10076 m_die->in_process = false;
10077
10078 /* If we're done processing the DIE for the CU that owns the line
10079 header, we don't need the line header anymore. */
10080 if (m_cu->line_header_die_owner == m_die)
10081 {
10082 delete m_cu->line_header;
10083 m_cu->line_header = NULL;
10084 m_cu->line_header_die_owner = NULL;
10085 }
10086 }
10087
10088private:
10089 die_info *m_die;
10090 dwarf2_cu *m_cu;
10091};
adde2bff 10092
c906108c
SS
10093/* Process a die and its children. */
10094
10095static void
e7c27a73 10096process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10097{
4c8aa72d 10098 process_die_scope scope (die, cu);
adde2bff 10099
c906108c
SS
10100 switch (die->tag)
10101 {
10102 case DW_TAG_padding:
10103 break;
10104 case DW_TAG_compile_unit:
95554aad 10105 case DW_TAG_partial_unit:
e7c27a73 10106 read_file_scope (die, cu);
c906108c 10107 break;
348e048f
DE
10108 case DW_TAG_type_unit:
10109 read_type_unit_scope (die, cu);
10110 break;
c906108c 10111 case DW_TAG_subprogram:
0a4b0913
AB
10112 /* Nested subprograms in Fortran get a prefix. */
10113 if (cu->language == language_fortran
10114 && die->parent != NULL
10115 && die->parent->tag == DW_TAG_subprogram)
10116 cu->processing_has_namespace_info = true;
10117 /* Fall through. */
c906108c 10118 case DW_TAG_inlined_subroutine:
edb3359d 10119 read_func_scope (die, cu);
c906108c
SS
10120 break;
10121 case DW_TAG_lexical_block:
14898363
L
10122 case DW_TAG_try_block:
10123 case DW_TAG_catch_block:
e7c27a73 10124 read_lexical_block_scope (die, cu);
c906108c 10125 break;
216f72a1 10126 case DW_TAG_call_site:
96408a79
SA
10127 case DW_TAG_GNU_call_site:
10128 read_call_site_scope (die, cu);
10129 break;
c906108c 10130 case DW_TAG_class_type:
680b30c7 10131 case DW_TAG_interface_type:
c906108c
SS
10132 case DW_TAG_structure_type:
10133 case DW_TAG_union_type:
134d01f1 10134 process_structure_scope (die, cu);
c906108c
SS
10135 break;
10136 case DW_TAG_enumeration_type:
134d01f1 10137 process_enumeration_scope (die, cu);
c906108c 10138 break;
134d01f1 10139
f792889a
DJ
10140 /* These dies have a type, but processing them does not create
10141 a symbol or recurse to process the children. Therefore we can
10142 read them on-demand through read_type_die. */
c906108c 10143 case DW_TAG_subroutine_type:
72019c9c 10144 case DW_TAG_set_type:
c906108c 10145 case DW_TAG_array_type:
c906108c 10146 case DW_TAG_pointer_type:
c906108c 10147 case DW_TAG_ptr_to_member_type:
c906108c 10148 case DW_TAG_reference_type:
4297a3f0 10149 case DW_TAG_rvalue_reference_type:
c906108c 10150 case DW_TAG_string_type:
c906108c 10151 break;
134d01f1 10152
c906108c 10153 case DW_TAG_base_type:
a02abb62 10154 case DW_TAG_subrange_type:
cb249c71 10155 case DW_TAG_typedef:
134d01f1
DJ
10156 /* Add a typedef symbol for the type definition, if it has a
10157 DW_AT_name. */
f792889a 10158 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10159 break;
c906108c 10160 case DW_TAG_common_block:
e7c27a73 10161 read_common_block (die, cu);
c906108c
SS
10162 break;
10163 case DW_TAG_common_inclusion:
10164 break;
d9fa45fe 10165 case DW_TAG_namespace:
9068261f 10166 cu->processing_has_namespace_info = true;
e7c27a73 10167 read_namespace (die, cu);
d9fa45fe 10168 break;
5d7cb8df 10169 case DW_TAG_module:
9068261f 10170 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10171 read_module (die, cu);
10172 break;
d9fa45fe 10173 case DW_TAG_imported_declaration:
9068261f 10174 cu->processing_has_namespace_info = true;
74921315
KS
10175 if (read_namespace_alias (die, cu))
10176 break;
86a73007
TT
10177 /* The declaration is not a global namespace alias. */
10178 /* Fall through. */
d9fa45fe 10179 case DW_TAG_imported_module:
9068261f 10180 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10181 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10182 || cu->language != language_fortran))
b98664d3 10183 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10184 dwarf_tag_name (die->tag));
10185 read_import_statement (die, cu);
d9fa45fe 10186 break;
95554aad
TT
10187
10188 case DW_TAG_imported_unit:
10189 process_imported_unit_die (die, cu);
10190 break;
10191
71a3c369
TT
10192 case DW_TAG_variable:
10193 read_variable (die, cu);
10194 break;
10195
c906108c 10196 default:
e7c27a73 10197 new_symbol (die, NULL, cu);
c906108c
SS
10198 break;
10199 }
10200}
ca69b9e6
DE
10201\f
10202/* DWARF name computation. */
c906108c 10203
94af9270
KS
10204/* A helper function for dwarf2_compute_name which determines whether DIE
10205 needs to have the name of the scope prepended to the name listed in the
10206 die. */
10207
10208static int
10209die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10210{
1c809c68
TT
10211 struct attribute *attr;
10212
94af9270
KS
10213 switch (die->tag)
10214 {
10215 case DW_TAG_namespace:
10216 case DW_TAG_typedef:
10217 case DW_TAG_class_type:
10218 case DW_TAG_interface_type:
10219 case DW_TAG_structure_type:
10220 case DW_TAG_union_type:
10221 case DW_TAG_enumeration_type:
10222 case DW_TAG_enumerator:
10223 case DW_TAG_subprogram:
08a76f8a 10224 case DW_TAG_inlined_subroutine:
94af9270 10225 case DW_TAG_member:
74921315 10226 case DW_TAG_imported_declaration:
94af9270
KS
10227 return 1;
10228
10229 case DW_TAG_variable:
c2b0a229 10230 case DW_TAG_constant:
94af9270
KS
10231 /* We only need to prefix "globally" visible variables. These include
10232 any variable marked with DW_AT_external or any variable that
10233 lives in a namespace. [Variables in anonymous namespaces
10234 require prefixing, but they are not DW_AT_external.] */
10235
10236 if (dwarf2_attr (die, DW_AT_specification, cu))
10237 {
10238 struct dwarf2_cu *spec_cu = cu;
9a619af0 10239
94af9270
KS
10240 return die_needs_namespace (die_specification (die, &spec_cu),
10241 spec_cu);
10242 }
10243
1c809c68 10244 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10245 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10246 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10247 return 0;
10248 /* A variable in a lexical block of some kind does not need a
10249 namespace, even though in C++ such variables may be external
10250 and have a mangled name. */
10251 if (die->parent->tag == DW_TAG_lexical_block
10252 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10253 || die->parent->tag == DW_TAG_catch_block
10254 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10255 return 0;
10256 return 1;
94af9270
KS
10257
10258 default:
10259 return 0;
10260 }
10261}
10262
73b9be8b
KS
10263/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10264 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10265 defined for the given DIE. */
10266
10267static struct attribute *
10268dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10269{
10270 struct attribute *attr;
10271
10272 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10273 if (attr == NULL)
10274 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10275
10276 return attr;
10277}
10278
10279/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10280 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10281 defined for the given DIE. */
10282
10283static const char *
10284dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10285{
10286 const char *linkage_name;
10287
10288 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10289 if (linkage_name == NULL)
10290 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10291
787de330
TT
10292 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10293 See https://github.com/rust-lang/rust/issues/32925. */
10294 if (cu->language == language_rust && linkage_name != NULL
10295 && strchr (linkage_name, '{') != NULL)
10296 linkage_name = NULL;
10297
73b9be8b
KS
10298 return linkage_name;
10299}
10300
94af9270 10301/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10302 compute the physname for the object, which include a method's:
9c37b5ae 10303 - formal parameters (C++),
a766d390 10304 - receiver type (Go),
a766d390
DE
10305
10306 The term "physname" is a bit confusing.
10307 For C++, for example, it is the demangled name.
10308 For Go, for example, it's the mangled name.
94af9270 10309
af6b7be1
JB
10310 For Ada, return the DIE's linkage name rather than the fully qualified
10311 name. PHYSNAME is ignored..
10312
5989a64e 10313 The result is allocated on the objfile->per_bfd's obstack and
45940949 10314 canonicalized. */
94af9270
KS
10315
10316static const char *
15d034d0
TT
10317dwarf2_compute_name (const char *name,
10318 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10319 int physname)
10320{
5e22e966 10321 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10322
94af9270
KS
10323 if (name == NULL)
10324 name = dwarf2_name (die, cu);
10325
2ee7123e
DE
10326 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10327 but otherwise compute it by typename_concat inside GDB.
10328 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10329 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10330 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10331 will set the demangled name to the result of dwarf2_full_name, and it is
10332 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10333 if (cu->language == language_ada
10334 || (cu->language == language_fortran && physname))
10335 {
10336 /* For Ada unit, we prefer the linkage name over the name, as
10337 the former contains the exported name, which the user expects
10338 to be able to reference. Ideally, we want the user to be able
10339 to reference this entity using either natural or linkage name,
10340 but we haven't started looking at this enhancement yet. */
73b9be8b 10341 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10342
2ee7123e
DE
10343 if (linkage_name != NULL)
10344 return linkage_name;
f55ee35c
JK
10345 }
10346
94af9270
KS
10347 /* These are the only languages we know how to qualify names in. */
10348 if (name != NULL
9c37b5ae 10349 && (cu->language == language_cplus
c44af4eb
TT
10350 || cu->language == language_fortran || cu->language == language_d
10351 || cu->language == language_rust))
94af9270
KS
10352 {
10353 if (die_needs_namespace (die, cu))
10354 {
0d5cff50 10355 const char *prefix;
34a68019 10356 const char *canonical_name = NULL;
94af9270 10357
d7e74731
PA
10358 string_file buf;
10359
94af9270 10360 prefix = determine_prefix (die, cu);
94af9270
KS
10361 if (*prefix != '\0')
10362 {
43816ebc
TT
10363 gdb::unique_xmalloc_ptr<char> prefixed_name
10364 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10365
43816ebc 10366 buf.puts (prefixed_name.get ());
94af9270
KS
10367 }
10368 else
d7e74731 10369 buf.puts (name);
94af9270 10370
98bfdba5
PA
10371 /* Template parameters may be specified in the DIE's DW_AT_name, or
10372 as children with DW_TAG_template_type_param or
10373 DW_TAG_value_type_param. If the latter, add them to the name
10374 here. If the name already has template parameters, then
10375 skip this step; some versions of GCC emit both, and
10376 it is more efficient to use the pre-computed name.
10377
10378 Something to keep in mind about this process: it is very
10379 unlikely, or in some cases downright impossible, to produce
10380 something that will match the mangled name of a function.
10381 If the definition of the function has the same debug info,
10382 we should be able to match up with it anyway. But fallbacks
10383 using the minimal symbol, for instance to find a method
10384 implemented in a stripped copy of libstdc++, will not work.
10385 If we do not have debug info for the definition, we will have to
10386 match them up some other way.
10387
10388 When we do name matching there is a related problem with function
10389 templates; two instantiated function templates are allowed to
10390 differ only by their return types, which we do not add here. */
10391
10392 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10393 {
10394 struct attribute *attr;
10395 struct die_info *child;
10396 int first = 1;
10397
10398 die->building_fullname = 1;
10399
10400 for (child = die->child; child != NULL; child = child->sibling)
10401 {
10402 struct type *type;
12df843f 10403 LONGEST value;
d521ce57 10404 const gdb_byte *bytes;
98bfdba5
PA
10405 struct dwarf2_locexpr_baton *baton;
10406 struct value *v;
10407
10408 if (child->tag != DW_TAG_template_type_param
10409 && child->tag != DW_TAG_template_value_param)
10410 continue;
10411
10412 if (first)
10413 {
d7e74731 10414 buf.puts ("<");
98bfdba5
PA
10415 first = 0;
10416 }
10417 else
d7e74731 10418 buf.puts (", ");
98bfdba5
PA
10419
10420 attr = dwarf2_attr (child, DW_AT_type, cu);
10421 if (attr == NULL)
10422 {
b98664d3 10423 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10424 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10425 continue;
10426 }
10427 type = die_type (child, cu);
10428
10429 if (child->tag == DW_TAG_template_type_param)
10430 {
c1ec8cea
TT
10431 c_print_type (type, "", &buf, -1, 0, cu->language,
10432 &type_print_raw_options);
98bfdba5
PA
10433 continue;
10434 }
10435
10436 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10437 if (attr == NULL)
10438 {
b98664d3 10439 complaint (_("template parameter missing "
3e43a32a 10440 "DW_AT_const_value"));
d7e74731 10441 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10442 continue;
10443 }
10444
10445 dwarf2_const_value_attr (attr, type, name,
10446 &cu->comp_unit_obstack, cu,
10447 &value, &bytes, &baton);
10448
10449 if (TYPE_NOSIGN (type))
10450 /* GDB prints characters as NUMBER 'CHAR'. If that's
10451 changed, this can use value_print instead. */
d7e74731 10452 c_printchar (value, type, &buf);
98bfdba5
PA
10453 else
10454 {
10455 struct value_print_options opts;
10456
10457 if (baton != NULL)
10458 v = dwarf2_evaluate_loc_desc (type, NULL,
10459 baton->data,
10460 baton->size,
9f47c707
SM
10461 baton->per_cu,
10462 baton->per_objfile);
98bfdba5
PA
10463 else if (bytes != NULL)
10464 {
10465 v = allocate_value (type);
10466 memcpy (value_contents_writeable (v), bytes,
10467 TYPE_LENGTH (type));
10468 }
10469 else
10470 v = value_from_longest (type, value);
10471
3e43a32a
MS
10472 /* Specify decimal so that we do not depend on
10473 the radix. */
98bfdba5
PA
10474 get_formatted_print_options (&opts, 'd');
10475 opts.raw = 1;
d7e74731 10476 value_print (v, &buf, &opts);
98bfdba5 10477 release_value (v);
98bfdba5
PA
10478 }
10479 }
10480
10481 die->building_fullname = 0;
10482
10483 if (!first)
10484 {
10485 /* Close the argument list, with a space if necessary
10486 (nested templates). */
d7e74731
PA
10487 if (!buf.empty () && buf.string ().back () == '>')
10488 buf.puts (" >");
98bfdba5 10489 else
d7e74731 10490 buf.puts (">");
98bfdba5
PA
10491 }
10492 }
10493
9c37b5ae 10494 /* For C++ methods, append formal parameter type
94af9270 10495 information, if PHYSNAME. */
6e70227d 10496
94af9270 10497 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10498 && cu->language == language_cplus)
94af9270
KS
10499 {
10500 struct type *type = read_type_die (die, cu);
10501
d7e74731 10502 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10503 &type_print_raw_options);
94af9270 10504
9c37b5ae 10505 if (cu->language == language_cplus)
94af9270 10506 {
60430eff
DJ
10507 /* Assume that an artificial first parameter is
10508 "this", but do not crash if it is not. RealView
10509 marks unnamed (and thus unused) parameters as
10510 artificial; there is no way to differentiate
10511 the two cases. */
1f704f76 10512 if (type->num_fields () > 0
94af9270 10513 && TYPE_FIELD_ARTIFICIAL (type, 0)
940da03e
SM
10514 && type->field (0).type ()->code () == TYPE_CODE_PTR
10515 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
d7e74731 10516 buf.puts (" const");
94af9270
KS
10517 }
10518 }
10519
d7e74731 10520 const std::string &intermediate_name = buf.string ();
94af9270
KS
10521
10522 if (cu->language == language_cplus)
34a68019 10523 canonical_name
322a8516 10524 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10525 objfile);
34a68019
TT
10526
10527 /* If we only computed INTERMEDIATE_NAME, or if
10528 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10529 intern it. */
322a8516 10530 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10531 name = objfile->intern (intermediate_name);
34a68019
TT
10532 else
10533 name = canonical_name;
94af9270
KS
10534 }
10535 }
10536
10537 return name;
10538}
10539
0114d602
DJ
10540/* Return the fully qualified name of DIE, based on its DW_AT_name.
10541 If scope qualifiers are appropriate they will be added. The result
34a68019 10542 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10543 not have a name. NAME may either be from a previous call to
10544 dwarf2_name or NULL.
10545
9c37b5ae 10546 The output string will be canonicalized (if C++). */
0114d602
DJ
10547
10548static const char *
15d034d0 10549dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10550{
94af9270
KS
10551 return dwarf2_compute_name (name, die, cu, 0);
10552}
0114d602 10553
94af9270
KS
10554/* Construct a physname for the given DIE in CU. NAME may either be
10555 from a previous call to dwarf2_name or NULL. The result will be
10556 allocated on the objfile_objstack or NULL if the DIE does not have a
10557 name.
0114d602 10558
9c37b5ae 10559 The output string will be canonicalized (if C++). */
0114d602 10560
94af9270 10561static const char *
15d034d0 10562dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10563{
5e22e966 10564 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10565 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10566 int need_copy = 1;
10567
10568 /* In this case dwarf2_compute_name is just a shortcut not building anything
10569 on its own. */
10570 if (!die_needs_namespace (die, cu))
10571 return dwarf2_compute_name (name, die, cu, 1);
10572
906bb4c5
TT
10573 if (cu->language != language_rust)
10574 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10575
10576 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10577 has computed. */
791afaa2 10578 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10579 if (mangled != NULL)
900e11f9 10580 {
900e11f9 10581
59cc4834
JB
10582 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10583 {
10584 /* Do nothing (do not demangle the symbol name). */
10585 }
10586 else if (cu->language == language_go)
a766d390 10587 {
5e2db402
TT
10588 /* This is a lie, but we already lie to the caller new_symbol.
10589 new_symbol assumes we return the mangled name.
a766d390 10590 This just undoes that lie until things are cleaned up. */
a766d390
DE
10591 }
10592 else
10593 {
0eb876f5
JB
10594 /* Use DMGL_RET_DROP for C++ template functions to suppress
10595 their return type. It is easier for GDB users to search
10596 for such functions as `name(params)' than `long name(params)'.
10597 In such case the minimal symbol names do not match the full
10598 symbol names but for template functions there is never a need
10599 to look up their definition from their declaration so
10600 the only disadvantage remains the minimal symbol variant
10601 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10602 demangled.reset (gdb_demangle (mangled,
10603 (DMGL_PARAMS | DMGL_ANSI
10604 | DMGL_RET_DROP)));
a766d390 10605 }
900e11f9 10606 if (demangled)
791afaa2 10607 canon = demangled.get ();
900e11f9
JK
10608 else
10609 {
10610 canon = mangled;
10611 need_copy = 0;
10612 }
10613 }
10614
10615 if (canon == NULL || check_physname)
10616 {
10617 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10618
10619 if (canon != NULL && strcmp (physname, canon) != 0)
10620 {
10621 /* It may not mean a bug in GDB. The compiler could also
10622 compute DW_AT_linkage_name incorrectly. But in such case
10623 GDB would need to be bug-to-bug compatible. */
10624
b98664d3 10625 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10626 "(from linkage <%s>) - DIE at %s [in module %s]"),
10627 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10628 objfile_name (objfile));
900e11f9
JK
10629
10630 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10631 is available here - over computed PHYSNAME. It is safer
10632 against both buggy GDB and buggy compilers. */
10633
10634 retval = canon;
10635 }
10636 else
10637 {
10638 retval = physname;
10639 need_copy = 0;
10640 }
10641 }
10642 else
10643 retval = canon;
10644
10645 if (need_copy)
be1e3d3e 10646 retval = objfile->intern (retval);
900e11f9 10647
900e11f9 10648 return retval;
0114d602
DJ
10649}
10650
74921315
KS
10651/* Inspect DIE in CU for a namespace alias. If one exists, record
10652 a new symbol for it.
10653
10654 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10655
10656static int
10657read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10658{
10659 struct attribute *attr;
10660
10661 /* If the die does not have a name, this is not a namespace
10662 alias. */
10663 attr = dwarf2_attr (die, DW_AT_name, cu);
10664 if (attr != NULL)
10665 {
10666 int num;
10667 struct die_info *d = die;
10668 struct dwarf2_cu *imported_cu = cu;
10669
10670 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10671 keep inspecting DIEs until we hit the underlying import. */
10672#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10673 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10674 {
10675 attr = dwarf2_attr (d, DW_AT_import, cu);
10676 if (attr == NULL)
10677 break;
10678
10679 d = follow_die_ref (d, attr, &imported_cu);
10680 if (d->tag != DW_TAG_imported_declaration)
10681 break;
10682 }
10683
10684 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10685 {
b98664d3 10686 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10687 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10688 return 0;
10689 }
10690
10691 if (attr != NULL)
10692 {
10693 struct type *type;
0826b30a 10694 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10695
aa66c379 10696 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10697 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10698 {
10699 /* This declaration is a global namespace alias. Add
10700 a symbol for it whose type is the aliased namespace. */
10701 new_symbol (die, type, cu);
10702 return 1;
10703 }
10704 }
10705 }
10706
10707 return 0;
10708}
10709
22cee43f 10710/* Return the using directives repository (global or local?) to use in the
804d2729 10711 current context for CU.
22cee43f
PMR
10712
10713 For Ada, imported declarations can materialize renamings, which *may* be
10714 global. However it is impossible (for now?) in DWARF to distinguish
10715 "external" imported declarations and "static" ones. As all imported
10716 declarations seem to be static in all other languages, make them all CU-wide
10717 global only in Ada. */
10718
10719static struct using_direct **
804d2729 10720using_directives (struct dwarf2_cu *cu)
22cee43f 10721{
c24bdb02
KS
10722 if (cu->language == language_ada
10723 && cu->get_builder ()->outermost_context_p ())
10724 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10725 else
c24bdb02 10726 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10727}
10728
27aa8d6a
SW
10729/* Read the import statement specified by the given die and record it. */
10730
10731static void
10732read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10733{
5e22e966 10734 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10735 struct attribute *import_attr;
32019081 10736 struct die_info *imported_die, *child_die;
de4affc9 10737 struct dwarf2_cu *imported_cu;
27aa8d6a 10738 const char *imported_name;
794684b6 10739 const char *imported_name_prefix;
13387711
SW
10740 const char *canonical_name;
10741 const char *import_alias;
10742 const char *imported_declaration = NULL;
794684b6 10743 const char *import_prefix;
eb1e02fd 10744 std::vector<const char *> excludes;
13387711 10745
27aa8d6a
SW
10746 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10747 if (import_attr == NULL)
10748 {
b98664d3 10749 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10750 dwarf_tag_name (die->tag));
10751 return;
10752 }
10753
de4affc9
CC
10754 imported_cu = cu;
10755 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10756 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10757 if (imported_name == NULL)
10758 {
10759 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10760
10761 The import in the following code:
10762 namespace A
10763 {
10764 typedef int B;
10765 }
10766
10767 int main ()
10768 {
10769 using A::B;
10770 B b;
10771 return b;
10772 }
10773
10774 ...
10775 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10776 <52> DW_AT_decl_file : 1
10777 <53> DW_AT_decl_line : 6
10778 <54> DW_AT_import : <0x75>
10779 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10780 <59> DW_AT_name : B
10781 <5b> DW_AT_decl_file : 1
10782 <5c> DW_AT_decl_line : 2
10783 <5d> DW_AT_type : <0x6e>
10784 ...
10785 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10786 <76> DW_AT_byte_size : 4
10787 <77> DW_AT_encoding : 5 (signed)
10788
10789 imports the wrong die ( 0x75 instead of 0x58 ).
10790 This case will be ignored until the gcc bug is fixed. */
10791 return;
10792 }
10793
82856980
SW
10794 /* Figure out the local name after import. */
10795 import_alias = dwarf2_name (die, cu);
27aa8d6a 10796
794684b6
SW
10797 /* Figure out where the statement is being imported to. */
10798 import_prefix = determine_prefix (die, cu);
10799
10800 /* Figure out what the scope of the imported die is and prepend it
10801 to the name of the imported die. */
de4affc9 10802 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10803
f55ee35c
JK
10804 if (imported_die->tag != DW_TAG_namespace
10805 && imported_die->tag != DW_TAG_module)
794684b6 10806 {
13387711
SW
10807 imported_declaration = imported_name;
10808 canonical_name = imported_name_prefix;
794684b6 10809 }
13387711 10810 else if (strlen (imported_name_prefix) > 0)
12aaed36 10811 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10812 imported_name_prefix,
10813 (cu->language == language_d ? "." : "::"),
10814 imported_name, (char *) NULL);
13387711
SW
10815 else
10816 canonical_name = imported_name;
794684b6 10817
32019081
JK
10818 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10819 for (child_die = die->child; child_die && child_die->tag;
436c571c 10820 child_die = child_die->sibling)
32019081
JK
10821 {
10822 /* DWARF-4: A Fortran use statement with a “rename list” may be
10823 represented by an imported module entry with an import attribute
10824 referring to the module and owned entries corresponding to those
10825 entities that are renamed as part of being imported. */
10826
10827 if (child_die->tag != DW_TAG_imported_declaration)
10828 {
b98664d3 10829 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10830 "- DIE at %s [in module %s]"),
10831 sect_offset_str (child_die->sect_off),
10832 objfile_name (objfile));
32019081
JK
10833 continue;
10834 }
10835
10836 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10837 if (import_attr == NULL)
10838 {
b98664d3 10839 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10840 dwarf_tag_name (child_die->tag));
10841 continue;
10842 }
10843
10844 imported_cu = cu;
10845 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10846 &imported_cu);
10847 imported_name = dwarf2_name (imported_die, imported_cu);
10848 if (imported_name == NULL)
10849 {
b98664d3 10850 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10851 "imported name - DIE at %s [in module %s]"),
10852 sect_offset_str (child_die->sect_off),
10853 objfile_name (objfile));
32019081
JK
10854 continue;
10855 }
10856
eb1e02fd 10857 excludes.push_back (imported_name);
32019081
JK
10858
10859 process_die (child_die, cu);
10860 }
10861
804d2729 10862 add_using_directive (using_directives (cu),
22cee43f
PMR
10863 import_prefix,
10864 canonical_name,
10865 import_alias,
10866 imported_declaration,
10867 excludes,
10868 0,
10869 &objfile->objfile_obstack);
27aa8d6a
SW
10870}
10871
5230b05a
WT
10872/* ICC<14 does not output the required DW_AT_declaration on incomplete
10873 types, but gives them a size of zero. Starting with version 14,
10874 ICC is compatible with GCC. */
10875
9068261f 10876static bool
5230b05a
WT
10877producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10878{
10879 if (!cu->checked_producer)
10880 check_producer (cu);
10881
10882 return cu->producer_is_icc_lt_14;
10883}
10884
eb77c9df
AB
10885/* ICC generates a DW_AT_type for C void functions. This was observed on
10886 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10887 which says that void functions should not have a DW_AT_type. */
10888
10889static bool
10890producer_is_icc (struct dwarf2_cu *cu)
10891{
10892 if (!cu->checked_producer)
10893 check_producer (cu);
10894
10895 return cu->producer_is_icc;
10896}
10897
1b80a9fa
JK
10898/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10899 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10900 this, it was first present in GCC release 4.3.0. */
10901
9068261f 10902static bool
1b80a9fa
JK
10903producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10904{
10905 if (!cu->checked_producer)
10906 check_producer (cu);
10907
10908 return cu->producer_is_gcc_lt_4_3;
10909}
10910
d721ba37
PA
10911static file_and_directory
10912find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10913{
d721ba37
PA
10914 file_and_directory res;
10915
9291a0cd
TT
10916 /* Find the filename. Do not use dwarf2_name here, since the filename
10917 is not a source language identifier. */
d721ba37
PA
10918 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10919 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10920
d721ba37
PA
10921 if (res.comp_dir == NULL
10922 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10923 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10924 {
d721ba37
PA
10925 res.comp_dir_storage = ldirname (res.name);
10926 if (!res.comp_dir_storage.empty ())
10927 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10928 }
d721ba37 10929 if (res.comp_dir != NULL)
9291a0cd
TT
10930 {
10931 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10932 directory, get rid of it. */
d721ba37 10933 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10934
d721ba37
PA
10935 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10936 res.comp_dir = cp + 1;
9291a0cd
TT
10937 }
10938
d721ba37
PA
10939 if (res.name == NULL)
10940 res.name = "<unknown>";
10941
10942 return res;
9291a0cd
TT
10943}
10944
f4dc4d17
DE
10945/* Handle DW_AT_stmt_list for a compilation unit.
10946 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10947 COMP_DIR is the compilation directory. LOWPC is passed to
10948 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10949
10950static void
10951handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10952 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10953{
976ca316 10954 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2ab95328 10955 struct attribute *attr;
527f3840
JK
10956 struct line_header line_header_local;
10957 hashval_t line_header_local_hash;
527f3840
JK
10958 void **slot;
10959 int decode_mapping;
2ab95328 10960
f4dc4d17
DE
10961 gdb_assert (! cu->per_cu->is_debug_types);
10962
2ab95328 10963 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10964 if (attr == NULL)
10965 return;
10966
9c541725 10967 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10968
10969 /* The line header hash table is only created if needed (it exists to
10970 prevent redundant reading of the line table for partial_units).
10971 If we're given a partial_unit, we'll need it. If we're given a
10972 compile_unit, then use the line header hash table if it's already
10973 created, but don't create one just yet. */
10974
976ca316 10975 if (per_objfile->line_header_hash == NULL
527f3840 10976 && die->tag == DW_TAG_partial_unit)
2ab95328 10977 {
976ca316 10978 per_objfile->line_header_hash
d15acc42
TT
10979 .reset (htab_create_alloc (127, line_header_hash_voidp,
10980 line_header_eq_voidp,
10981 free_line_header_voidp,
10982 xcalloc, xfree));
527f3840 10983 }
2ab95328 10984
9c541725 10985 line_header_local.sect_off = line_offset;
527f3840
JK
10986 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10987 line_header_local_hash = line_header_hash (&line_header_local);
976ca316 10988 if (per_objfile->line_header_hash != NULL)
527f3840 10989 {
976ca316 10990 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
10991 &line_header_local,
10992 line_header_local_hash, NO_INSERT);
10993
10994 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10995 is not present in *SLOT (since if there is something in *SLOT then
10996 it will be for a partial_unit). */
10997 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10998 {
527f3840 10999 gdb_assert (*slot != NULL);
9a3c8263 11000 cu->line_header = (struct line_header *) *slot;
527f3840 11001 return;
dee91e82 11002 }
2ab95328 11003 }
527f3840
JK
11004
11005 /* dwarf_decode_line_header does not yet provide sufficient information.
11006 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11007 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11008 if (lh == NULL)
527f3840 11009 return;
4c8aa72d
PA
11010
11011 cu->line_header = lh.release ();
11012 cu->line_header_die_owner = die;
527f3840 11013
976ca316 11014 if (per_objfile->line_header_hash == NULL)
527f3840
JK
11015 slot = NULL;
11016 else
11017 {
976ca316 11018 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
11019 &line_header_local,
11020 line_header_local_hash, INSERT);
11021 gdb_assert (slot != NULL);
11022 }
11023 if (slot != NULL && *slot == NULL)
11024 {
11025 /* This newly decoded line number information unit will be owned
11026 by line_header_hash hash table. */
11027 *slot = cu->line_header;
4c8aa72d 11028 cu->line_header_die_owner = NULL;
527f3840
JK
11029 }
11030 else
11031 {
11032 /* We cannot free any current entry in (*slot) as that struct line_header
11033 may be already used by multiple CUs. Create only temporary decoded
11034 line_header for this CU - it may happen at most once for each line
11035 number information unit. And if we're not using line_header_hash
11036 then this is what we want as well. */
11037 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11038 }
11039 decode_mapping = (die->tag != DW_TAG_partial_unit);
11040 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11041 decode_mapping);
fff8551c 11042
2ab95328
TT
11043}
11044
95554aad 11045/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11046
c906108c 11047static void
e7c27a73 11048read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11049{
976ca316
SM
11050 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11051 struct objfile *objfile = per_objfile->objfile;
08feed99 11052 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 11053 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11054 CORE_ADDR highpc = ((CORE_ADDR) 0);
11055 struct attribute *attr;
c906108c 11056 struct die_info *child_die;
e142c38c 11057 CORE_ADDR baseaddr;
6e70227d 11058
380618d6 11059 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 11060 baseaddr = objfile->text_section_offset ();
c906108c 11061
fae299cd 11062 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11063
11064 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11065 from finish_block. */
2acceee2 11066 if (lowpc == ((CORE_ADDR) -1))
c906108c 11067 lowpc = highpc;
3e29f34a 11068 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11069
d721ba37 11070 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11071
f4b8a18d
KW
11072 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11073 standardised yet. As a workaround for the language detection we fall
11074 back to the DW_AT_producer string. */
11075 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11076 cu->language = language_opencl;
11077
3019eac3
DE
11078 /* Similar hack for Go. */
11079 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11080 set_cu_language (DW_LANG_Go, cu);
11081
c24bdb02 11082 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11083
11084 /* Decode line number information if present. We do this before
11085 processing child DIEs, so that the line header table is available
11086 for DW_AT_decl_file. */
d721ba37 11087 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11088
11089 /* Process all dies in compilation unit. */
11090 if (die->child != NULL)
11091 {
11092 child_die = die->child;
11093 while (child_die && child_die->tag)
11094 {
11095 process_die (child_die, cu);
436c571c 11096 child_die = child_die->sibling;
3019eac3
DE
11097 }
11098 }
11099
11100 /* Decode macro information, if present. Dwarf 2 macro information
11101 refers to information in the line number info statement program
11102 header, so we can only read it if we've read the header
11103 successfully. */
0af92d60
JK
11104 attr = dwarf2_attr (die, DW_AT_macros, cu);
11105 if (attr == NULL)
11106 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11107 if (attr && cu->line_header)
11108 {
11109 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11110 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11111
43f3e411 11112 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11113 }
11114 else
11115 {
11116 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11117 if (attr && cu->line_header)
11118 {
11119 unsigned int macro_offset = DW_UNSND (attr);
11120
43f3e411 11121 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11122 }
11123 }
3019eac3
DE
11124}
11125
c24bdb02
KS
11126void
11127dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11128{
f4dc4d17
DE
11129 struct type_unit_group *tu_group;
11130 int first_time;
3019eac3 11131 struct attribute *attr;
9c541725 11132 unsigned int i;
0186c6a7 11133 struct signatured_type *sig_type;
3019eac3 11134
f4dc4d17 11135 gdb_assert (per_cu->is_debug_types);
0186c6a7 11136 sig_type = (struct signatured_type *) per_cu;
3019eac3 11137
c24bdb02 11138 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11139
f4dc4d17 11140 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11141 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11142 if (sig_type->type_unit_group == NULL)
c24bdb02 11143 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11144 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11145
11146 /* If we've already processed this stmt_list there's no real need to
11147 do it again, we could fake it and just recreate the part we need
11148 (file name,index -> symtab mapping). If data shows this optimization
11149 is useful we can do it then. */
8adb8487
TT
11150 type_unit_group_unshareable *tug_unshare
11151 = per_objfile->get_type_unit_group_unshareable (tu_group);
11152 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
11153
11154 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11155 debug info. */
fff8551c 11156 line_header_up lh;
f4dc4d17 11157 if (attr != NULL)
3019eac3 11158 {
9c541725 11159 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11160 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11161 }
11162 if (lh == NULL)
11163 {
11164 if (first_time)
c24bdb02 11165 start_symtab ("", NULL, 0);
f4dc4d17
DE
11166 else
11167 {
8adb8487 11168 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 11169 gdb_assert (m_builder == nullptr);
8adb8487 11170 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11171 m_builder.reset (new struct buildsym_compunit
11172 (COMPUNIT_OBJFILE (cust), "",
11173 COMPUNIT_DIRNAME (cust),
11174 compunit_language (cust),
11175 0, cust));
770479f2 11176 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11177 }
f4dc4d17 11178 return;
3019eac3
DE
11179 }
11180
c24bdb02
KS
11181 line_header = lh.release ();
11182 line_header_die_owner = die;
3019eac3 11183
f4dc4d17
DE
11184 if (first_time)
11185 {
c24bdb02 11186 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11187
1fd60fc0
DE
11188 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11189 still initializing it, and our caller (a few levels up)
11190 process_full_type_unit still needs to know if this is the first
11191 time. */
11192
8adb8487 11193 tug_unshare->symtabs
4ac93832
TT
11194 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11195 struct symtab *, line_header->file_names_size ());
3019eac3 11196
7ba99d21
AT
11197 auto &file_names = line_header->file_names ();
11198 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11199 {
7ba99d21 11200 file_entry &fe = file_names[i];
c24bdb02
KS
11201 dwarf2_start_subfile (this, fe.name,
11202 fe.include_dir (line_header));
11203 buildsym_compunit *b = get_builder ();
11204 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11205 {
4c8aa72d
PA
11206 /* NOTE: start_subfile will recognize when it's been
11207 passed a file it has already seen. So we can't
11208 assume there's a simple mapping from
11209 cu->line_header->file_names to subfiles, plus
11210 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11211 b->get_current_subfile ()->symtab
11212 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11213 }
11214
c24bdb02 11215 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 11216 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
11217 }
11218 }
11219 else
3019eac3 11220 {
c24bdb02 11221 gdb_assert (m_builder == nullptr);
8adb8487 11222 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11223 m_builder.reset (new struct buildsym_compunit
11224 (COMPUNIT_OBJFILE (cust), "",
11225 COMPUNIT_DIRNAME (cust),
11226 compunit_language (cust),
11227 0, cust));
770479f2 11228 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11229
7ba99d21
AT
11230 auto &file_names = line_header->file_names ();
11231 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11232 {
7ba99d21 11233 file_entry &fe = file_names[i];
8adb8487 11234 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 11235 }
3019eac3
DE
11236 }
11237
f4dc4d17
DE
11238 /* The main symtab is allocated last. Type units don't have DW_AT_name
11239 so they don't have a "real" (so to speak) symtab anyway.
11240 There is later code that will assign the main symtab to all symbols
11241 that don't have one. We need to handle the case of a symbol with a
11242 missing symtab (DW_AT_decl_file) anyway. */
11243}
3019eac3 11244
f4dc4d17
DE
11245/* Process DW_TAG_type_unit.
11246 For TUs we want to skip the first top level sibling if it's not the
11247 actual type being defined by this TU. In this case the first top
11248 level sibling is there to provide context only. */
3019eac3 11249
f4dc4d17
DE
11250static void
11251read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11252{
11253 struct die_info *child_die;
3019eac3 11254
f4dc4d17
DE
11255 prepare_one_comp_unit (cu, die, language_minimal);
11256
11257 /* Initialize (or reinitialize) the machinery for building symtabs.
11258 We do this before processing child DIEs, so that the line header table
11259 is available for DW_AT_decl_file. */
c24bdb02 11260 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11261
11262 if (die->child != NULL)
11263 {
11264 child_die = die->child;
11265 while (child_die && child_die->tag)
11266 {
11267 process_die (child_die, cu);
436c571c 11268 child_die = child_die->sibling;
f4dc4d17
DE
11269 }
11270 }
3019eac3
DE
11271}
11272\f
80626a55
DE
11273/* DWO/DWP files.
11274
11275 http://gcc.gnu.org/wiki/DebugFission
11276 http://gcc.gnu.org/wiki/DebugFissionDWP
11277
11278 To simplify handling of both DWO files ("object" files with the DWARF info)
11279 and DWP files (a file with the DWOs packaged up into one file), we treat
11280 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11281
11282static hashval_t
11283hash_dwo_file (const void *item)
11284{
9a3c8263 11285 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11286 hashval_t hash;
3019eac3 11287
a2ce51a0
DE
11288 hash = htab_hash_string (dwo_file->dwo_name);
11289 if (dwo_file->comp_dir != NULL)
11290 hash += htab_hash_string (dwo_file->comp_dir);
11291 return hash;
3019eac3
DE
11292}
11293
11294static int
11295eq_dwo_file (const void *item_lhs, const void *item_rhs)
11296{
9a3c8263
SM
11297 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11298 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11299
a2ce51a0
DE
11300 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11301 return 0;
11302 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11303 return lhs->comp_dir == rhs->comp_dir;
11304 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11305}
11306
11307/* Allocate a hash table for DWO files. */
11308
51ac9db5 11309static htab_up
298e9637 11310allocate_dwo_file_hash_table ()
3019eac3 11311{
51ac9db5
SM
11312 auto delete_dwo_file = [] (void *item)
11313 {
11314 struct dwo_file *dwo_file = (struct dwo_file *) item;
11315
11316 delete dwo_file;
11317 };
11318
bc68fb19
TT
11319 return htab_up (htab_create_alloc (41,
11320 hash_dwo_file,
11321 eq_dwo_file,
11322 delete_dwo_file,
11323 xcalloc, xfree));
3019eac3
DE
11324}
11325
80626a55
DE
11326/* Lookup DWO file DWO_NAME. */
11327
11328static void **
976ca316 11329lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
ed2dc618
SM
11330 const char *dwo_name,
11331 const char *comp_dir)
80626a55
DE
11332{
11333 struct dwo_file find_entry;
11334 void **slot;
11335
976ca316
SM
11336 if (per_objfile->per_bfd->dwo_files == NULL)
11337 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11338
0ac5b59e
DE
11339 find_entry.dwo_name = dwo_name;
11340 find_entry.comp_dir = comp_dir;
976ca316 11341 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11342 INSERT);
80626a55
DE
11343
11344 return slot;
11345}
11346
3019eac3
DE
11347static hashval_t
11348hash_dwo_unit (const void *item)
11349{
9a3c8263 11350 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11351
11352 /* This drops the top 32 bits of the id, but is ok for a hash. */
11353 return dwo_unit->signature;
11354}
11355
11356static int
11357eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11358{
9a3c8263
SM
11359 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11360 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11361
11362 /* The signature is assumed to be unique within the DWO file.
11363 So while object file CU dwo_id's always have the value zero,
11364 that's OK, assuming each object file DWO file has only one CU,
11365 and that's the rule for now. */
11366 return lhs->signature == rhs->signature;
11367}
11368
11369/* Allocate a hash table for DWO CUs,TUs.
11370 There is one of these tables for each of CUs,TUs for each DWO file. */
11371
b0b6a987 11372static htab_up
298e9637 11373allocate_dwo_unit_table ()
3019eac3
DE
11374{
11375 /* Start out with a pretty small number.
11376 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11377 return htab_up (htab_create_alloc (3,
11378 hash_dwo_unit,
11379 eq_dwo_unit,
11380 NULL, xcalloc, xfree));
3019eac3
DE
11381}
11382
19c3d4c9 11383/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11384
11385static void
19c3d4c9
DE
11386create_dwo_cu_reader (const struct die_reader_specs *reader,
11387 const gdb_byte *info_ptr,
11388 struct die_info *comp_unit_die,
c0ab21c2
TT
11389 struct dwo_file *dwo_file,
11390 struct dwo_unit *dwo_unit)
3019eac3
DE
11391{
11392 struct dwarf2_cu *cu = reader->cu;
9c541725 11393 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11394 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11395
a084a2a6
AT
11396 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11397 if (!signature.has_value ())
3019eac3 11398 {
b98664d3 11399 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11400 " its dwo_id [in module %s]"),
9d8780f0 11401 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11402 return;
11403 }
11404
3019eac3 11405 dwo_unit->dwo_file = dwo_file;
a084a2a6 11406 dwo_unit->signature = *signature;
8a0459fd 11407 dwo_unit->section = section;
9c541725 11408 dwo_unit->sect_off = sect_off;
3019eac3
DE
11409 dwo_unit->length = cu->per_cu->length;
11410
b4f54984 11411 if (dwarf_read_debug)
9d8780f0
SM
11412 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11413 sect_offset_str (sect_off),
9c541725 11414 hex_string (dwo_unit->signature));
3019eac3
DE
11415}
11416
33c5cd75 11417/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11418 Note: This function processes DWO files only, not DWP files. */
3019eac3 11419
33c5cd75 11420static void
976ca316 11421create_cus_hash_table (dwarf2_per_objfile *per_objfile,
18a8505e 11422 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11423 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3 11424{
976ca316
SM
11425 struct objfile *objfile = per_objfile->objfile;
11426 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
d521ce57 11427 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11428
96b79293 11429 section.read (objfile);
33c5cd75 11430 info_ptr = section.buffer;
3019eac3
DE
11431
11432 if (info_ptr == NULL)
33c5cd75 11433 return;
3019eac3 11434
b4f54984 11435 if (dwarf_read_debug)
19c3d4c9
DE
11436 {
11437 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11438 section.get_name (),
11439 section.get_file_name ());
19c3d4c9 11440 }
3019eac3 11441
33c5cd75 11442 end_ptr = info_ptr + section.size;
3019eac3
DE
11443 while (info_ptr < end_ptr)
11444 {
11445 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11446 struct dwo_unit read_unit {};
33c5cd75
DB
11447 struct dwo_unit *dwo_unit;
11448 void **slot;
11449 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11450
11451 memset (&per_cu, 0, sizeof (per_cu));
1859c670 11452 per_cu.per_bfd = per_bfd;
3019eac3 11453 per_cu.is_debug_types = 0;
33c5cd75
DB
11454 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11455 per_cu.section = &section;
11456
976ca316 11457 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
c0ab21c2
TT
11458 if (!reader.dummy_p)
11459 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11460 &dwo_file, &read_unit);
33c5cd75
DB
11461 info_ptr += per_cu.length;
11462
11463 // If the unit could not be parsed, skip it.
c0ab21c2 11464 if (read_unit.dwo_file == NULL)
33c5cd75 11465 continue;
3019eac3 11466
33c5cd75 11467 if (cus_htab == NULL)
298e9637 11468 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11469
1859c670 11470 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11471 struct dwo_unit);
c0ab21c2 11472 *dwo_unit = read_unit;
b0b6a987 11473 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11474 gdb_assert (slot != NULL);
11475 if (*slot != NULL)
19c3d4c9 11476 {
33c5cd75
DB
11477 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11478 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11479
b98664d3 11480 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11481 " the entry at offset %s, signature %s"),
11482 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11483 hex_string (dwo_unit->signature));
19c3d4c9 11484 }
33c5cd75 11485 *slot = (void *)dwo_unit;
3019eac3 11486 }
3019eac3
DE
11487}
11488
80626a55
DE
11489/* DWP file .debug_{cu,tu}_index section format:
11490 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11491
d2415c6c
DE
11492 DWP Version 1:
11493
80626a55
DE
11494 Both index sections have the same format, and serve to map a 64-bit
11495 signature to a set of section numbers. Each section begins with a header,
11496 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11497 indexes, and a pool of 32-bit section numbers. The index sections will be
11498 aligned at 8-byte boundaries in the file.
11499
d2415c6c
DE
11500 The index section header consists of:
11501
11502 V, 32 bit version number
11503 -, 32 bits unused
11504 N, 32 bit number of compilation units or type units in the index
11505 M, 32 bit number of slots in the hash table
80626a55 11506
d2415c6c 11507 Numbers are recorded using the byte order of the application binary.
80626a55 11508
d2415c6c
DE
11509 The hash table begins at offset 16 in the section, and consists of an array
11510 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11511 order of the application binary). Unused slots in the hash table are 0.
11512 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11513
d2415c6c
DE
11514 The parallel table begins immediately after the hash table
11515 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11516 array of 32-bit indexes (using the byte order of the application binary),
11517 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11518 table contains a 32-bit index into the pool of section numbers. For unused
11519 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11520
73869dc2
DE
11521 The pool of section numbers begins immediately following the hash table
11522 (at offset 16 + 12 * M from the beginning of the section). The pool of
11523 section numbers consists of an array of 32-bit words (using the byte order
11524 of the application binary). Each item in the array is indexed starting
11525 from 0. The hash table entry provides the index of the first section
11526 number in the set. Additional section numbers in the set follow, and the
11527 set is terminated by a 0 entry (section number 0 is not used in ELF).
11528
11529 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11530 section must be the first entry in the set, and the .debug_abbrev.dwo must
11531 be the second entry. Other members of the set may follow in any order.
11532
11533 ---
11534
11535 DWP Version 2:
11536
11537 DWP Version 2 combines all the .debug_info, etc. sections into one,
11538 and the entries in the index tables are now offsets into these sections.
11539 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11540 section.
11541
11542 Index Section Contents:
11543 Header
11544 Hash Table of Signatures dwp_hash_table.hash_table
11545 Parallel Table of Indices dwp_hash_table.unit_table
11546 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11547 Table of Section Sizes dwp_hash_table.v2.sizes
11548
11549 The index section header consists of:
11550
11551 V, 32 bit version number
11552 L, 32 bit number of columns in the table of section offsets
11553 N, 32 bit number of compilation units or type units in the index
11554 M, 32 bit number of slots in the hash table
11555
11556 Numbers are recorded using the byte order of the application binary.
11557
11558 The hash table has the same format as version 1.
11559 The parallel table of indices has the same format as version 1,
11560 except that the entries are origin-1 indices into the table of sections
11561 offsets and the table of section sizes.
11562
11563 The table of offsets begins immediately following the parallel table
11564 (at offset 16 + 12 * M from the beginning of the section). The table is
11565 a two-dimensional array of 32-bit words (using the byte order of the
11566 application binary), with L columns and N+1 rows, in row-major order.
11567 Each row in the array is indexed starting from 0. The first row provides
11568 a key to the remaining rows: each column in this row provides an identifier
11569 for a debug section, and the offsets in the same column of subsequent rows
11570 refer to that section. The section identifiers are:
11571
11572 DW_SECT_INFO 1 .debug_info.dwo
11573 DW_SECT_TYPES 2 .debug_types.dwo
11574 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11575 DW_SECT_LINE 4 .debug_line.dwo
11576 DW_SECT_LOC 5 .debug_loc.dwo
11577 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11578 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11579 DW_SECT_MACRO 8 .debug_macro.dwo
11580
11581 The offsets provided by the CU and TU index sections are the base offsets
11582 for the contributions made by each CU or TU to the corresponding section
11583 in the package file. Each CU and TU header contains an abbrev_offset
11584 field, used to find the abbreviations table for that CU or TU within the
11585 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11586 be interpreted as relative to the base offset given in the index section.
11587 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11588 should be interpreted as relative to the base offset for .debug_line.dwo,
11589 and offsets into other debug sections obtained from DWARF attributes should
11590 also be interpreted as relative to the corresponding base offset.
11591
11592 The table of sizes begins immediately following the table of offsets.
11593 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11594 with L columns and N rows, in row-major order. Each row in the array is
11595 indexed starting from 1 (row 0 is shared by the two tables).
11596
11597 ---
11598
11599 Hash table lookup is handled the same in version 1 and 2:
11600
11601 We assume that N and M will not exceed 2^32 - 1.
11602 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11603
d2415c6c
DE
11604 Given a 64-bit compilation unit signature or a type signature S, an entry
11605 in the hash table is located as follows:
80626a55 11606
d2415c6c
DE
11607 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11608 the low-order k bits all set to 1.
80626a55 11609
d2415c6c 11610 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11611
d2415c6c
DE
11612 3) If the hash table entry at index H matches the signature, use that
11613 entry. If the hash table entry at index H is unused (all zeroes),
11614 terminate the search: the signature is not present in the table.
80626a55 11615
d2415c6c 11616 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11617
d2415c6c 11618 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11619 to stop at an unused slot or find the match. */
80626a55
DE
11620
11621/* Create a hash table to map DWO IDs to their CU/TU entry in
11622 .debug_{info,types}.dwo in DWP_FILE.
11623 Returns NULL if there isn't one.
11624 Note: This function processes DWP files only, not DWO files. */
11625
11626static struct dwp_hash_table *
976ca316 11627create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 11628 struct dwp_file *dwp_file, int is_debug_types)
80626a55 11629{
976ca316 11630 struct objfile *objfile = per_objfile->objfile;
400174b1 11631 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11632 const gdb_byte *index_ptr, *index_end;
80626a55 11633 struct dwarf2_section_info *index;
73869dc2 11634 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11635 struct dwp_hash_table *htab;
11636
11637 if (is_debug_types)
11638 index = &dwp_file->sections.tu_index;
11639 else
11640 index = &dwp_file->sections.cu_index;
11641
96b79293 11642 if (index->empty ())
80626a55 11643 return NULL;
96b79293 11644 index->read (objfile);
80626a55
DE
11645
11646 index_ptr = index->buffer;
11647 index_end = index_ptr + index->size;
11648
11649 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11650 index_ptr += 4;
11651 if (version == 2)
11652 nr_columns = read_4_bytes (dbfd, index_ptr);
11653 else
11654 nr_columns = 0;
11655 index_ptr += 4;
80626a55
DE
11656 nr_units = read_4_bytes (dbfd, index_ptr);
11657 index_ptr += 4;
11658 nr_slots = read_4_bytes (dbfd, index_ptr);
11659 index_ptr += 4;
11660
73869dc2 11661 if (version != 1 && version != 2)
80626a55 11662 {
21aa081e 11663 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11664 " [in module %s]"),
21aa081e 11665 pulongest (version), dwp_file->name);
80626a55
DE
11666 }
11667 if (nr_slots != (nr_slots & -nr_slots))
11668 {
21aa081e 11669 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11670 " is not power of 2 [in module %s]"),
21aa081e 11671 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11672 }
11673
976ca316 11674 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11675 htab->version = version;
11676 htab->nr_columns = nr_columns;
80626a55
DE
11677 htab->nr_units = nr_units;
11678 htab->nr_slots = nr_slots;
11679 htab->hash_table = index_ptr;
11680 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11681
11682 /* Exit early if the table is empty. */
11683 if (nr_slots == 0 || nr_units == 0
11684 || (version == 2 && nr_columns == 0))
11685 {
11686 /* All must be zero. */
11687 if (nr_slots != 0 || nr_units != 0
11688 || (version == 2 && nr_columns != 0))
11689 {
b98664d3 11690 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11691 " all zero [in modules %s]"),
11692 dwp_file->name);
11693 }
11694 return htab;
11695 }
11696
11697 if (version == 1)
11698 {
11699 htab->section_pool.v1.indices =
11700 htab->unit_table + sizeof (uint32_t) * nr_slots;
11701 /* It's harder to decide whether the section is too small in v1.
11702 V1 is deprecated anyway so we punt. */
11703 }
11704 else
11705 {
11706 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11707 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11708 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11709 /* Reverse map for error checking. */
11710 int ids_seen[DW_SECT_MAX + 1];
11711 int i;
11712
11713 if (nr_columns < 2)
11714 {
11715 error (_("Dwarf Error: bad DWP hash table, too few columns"
11716 " in section table [in module %s]"),
11717 dwp_file->name);
11718 }
11719 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11720 {
11721 error (_("Dwarf Error: bad DWP hash table, too many columns"
11722 " in section table [in module %s]"),
11723 dwp_file->name);
11724 }
04fd5eed
GB
11725 memset (ids, 255, sizeof_ids);
11726 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11727 for (i = 0; i < nr_columns; ++i)
11728 {
11729 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11730
11731 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11732 {
11733 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11734 " in section table [in module %s]"),
11735 id, dwp_file->name);
11736 }
11737 if (ids_seen[id] != -1)
11738 {
11739 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11740 " id %d in section table [in module %s]"),
11741 id, dwp_file->name);
11742 }
11743 ids_seen[id] = i;
11744 ids[i] = id;
11745 }
11746 /* Must have exactly one info or types section. */
11747 if (((ids_seen[DW_SECT_INFO] != -1)
11748 + (ids_seen[DW_SECT_TYPES] != -1))
11749 != 1)
11750 {
11751 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11752 " DWO info/types section [in module %s]"),
11753 dwp_file->name);
11754 }
11755 /* Must have an abbrev section. */
11756 if (ids_seen[DW_SECT_ABBREV] == -1)
11757 {
11758 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11759 " section [in module %s]"),
11760 dwp_file->name);
11761 }
11762 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11763 htab->section_pool.v2.sizes =
11764 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11765 * nr_units * nr_columns);
11766 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11767 * nr_units * nr_columns))
11768 > index_end)
11769 {
11770 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11771 " [in module %s]"),
11772 dwp_file->name);
11773 }
11774 }
80626a55
DE
11775
11776 return htab;
11777}
11778
11779/* Update SECTIONS with the data from SECTP.
11780
11781 This function is like the other "locate" section routines that are
11782 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11783 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11784
11785 The result is non-zero for success, or zero if an error was found. */
11786
11787static int
73869dc2
DE
11788locate_v1_virtual_dwo_sections (asection *sectp,
11789 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11790{
11791 const struct dwop_section_names *names = &dwop_section_names;
11792
11793 if (section_is_p (sectp->name, &names->abbrev_dwo))
11794 {
11795 /* There can be only one. */
049412e3 11796 if (sections->abbrev.s.section != NULL)
80626a55 11797 return 0;
049412e3 11798 sections->abbrev.s.section = sectp;
fd361982 11799 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11800 }
11801 else if (section_is_p (sectp->name, &names->info_dwo)
11802 || section_is_p (sectp->name, &names->types_dwo))
11803 {
11804 /* There can be only one. */
049412e3 11805 if (sections->info_or_types.s.section != NULL)
80626a55 11806 return 0;
049412e3 11807 sections->info_or_types.s.section = sectp;
fd361982 11808 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11809 }
11810 else if (section_is_p (sectp->name, &names->line_dwo))
11811 {
11812 /* There can be only one. */
049412e3 11813 if (sections->line.s.section != NULL)
80626a55 11814 return 0;
049412e3 11815 sections->line.s.section = sectp;
fd361982 11816 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11817 }
11818 else if (section_is_p (sectp->name, &names->loc_dwo))
11819 {
11820 /* There can be only one. */
049412e3 11821 if (sections->loc.s.section != NULL)
80626a55 11822 return 0;
049412e3 11823 sections->loc.s.section = sectp;
fd361982 11824 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11825 }
11826 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11827 {
11828 /* There can be only one. */
049412e3 11829 if (sections->macinfo.s.section != NULL)
80626a55 11830 return 0;
049412e3 11831 sections->macinfo.s.section = sectp;
fd361982 11832 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11833 }
11834 else if (section_is_p (sectp->name, &names->macro_dwo))
11835 {
11836 /* There can be only one. */
049412e3 11837 if (sections->macro.s.section != NULL)
80626a55 11838 return 0;
049412e3 11839 sections->macro.s.section = sectp;
fd361982 11840 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11841 }
11842 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11843 {
11844 /* There can be only one. */
049412e3 11845 if (sections->str_offsets.s.section != NULL)
80626a55 11846 return 0;
049412e3 11847 sections->str_offsets.s.section = sectp;
fd361982 11848 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11849 }
11850 else
11851 {
11852 /* No other kind of section is valid. */
11853 return 0;
11854 }
11855
11856 return 1;
11857}
11858
73869dc2
DE
11859/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11860 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11861 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11862 This is for DWP version 1 files. */
80626a55
DE
11863
11864static struct dwo_unit *
976ca316 11865create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
ed2dc618 11866 struct dwp_file *dwp_file,
73869dc2
DE
11867 uint32_t unit_index,
11868 const char *comp_dir,
11869 ULONGEST signature, int is_debug_types)
80626a55 11870{
73869dc2
DE
11871 const struct dwp_hash_table *dwp_htab =
11872 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11873 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11874 const char *kind = is_debug_types ? "TU" : "CU";
11875 struct dwo_file *dwo_file;
11876 struct dwo_unit *dwo_unit;
73869dc2 11877 struct virtual_v1_dwo_sections sections;
80626a55 11878 void **dwo_file_slot;
80626a55
DE
11879 int i;
11880
73869dc2
DE
11881 gdb_assert (dwp_file->version == 1);
11882
b4f54984 11883 if (dwarf_read_debug)
80626a55 11884 {
73869dc2 11885 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11886 kind,
73869dc2 11887 pulongest (unit_index), hex_string (signature),
80626a55
DE
11888 dwp_file->name);
11889 }
11890
19ac8c2e 11891 /* Fetch the sections of this DWO unit.
80626a55
DE
11892 Put a limit on the number of sections we look for so that bad data
11893 doesn't cause us to loop forever. */
11894
73869dc2 11895#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11896 (1 /* .debug_info or .debug_types */ \
11897 + 1 /* .debug_abbrev */ \
11898 + 1 /* .debug_line */ \
11899 + 1 /* .debug_loc */ \
11900 + 1 /* .debug_str_offsets */ \
19ac8c2e 11901 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11902 + 1 /* trailing zero */)
11903
11904 memset (&sections, 0, sizeof (sections));
80626a55 11905
73869dc2 11906 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11907 {
11908 asection *sectp;
11909 uint32_t section_nr =
11910 read_4_bytes (dbfd,
73869dc2
DE
11911 dwp_htab->section_pool.v1.indices
11912 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11913
11914 if (section_nr == 0)
11915 break;
11916 if (section_nr >= dwp_file->num_sections)
11917 {
11918 error (_("Dwarf Error: bad DWP hash table, section number too large"
11919 " [in module %s]"),
11920 dwp_file->name);
11921 }
11922
11923 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11924 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11925 {
11926 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11927 " [in module %s]"),
11928 dwp_file->name);
11929 }
11930 }
11931
11932 if (i < 2
96b79293
TT
11933 || sections.info_or_types.empty ()
11934 || sections.abbrev.empty ())
80626a55
DE
11935 {
11936 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11937 " [in module %s]"),
11938 dwp_file->name);
11939 }
73869dc2 11940 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11941 {
11942 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11943 " [in module %s]"),
11944 dwp_file->name);
11945 }
11946
11947 /* It's easier for the rest of the code if we fake a struct dwo_file and
11948 have dwo_unit "live" in that. At least for now.
11949
11950 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11951 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11952 file, we can combine them back into a virtual DWO file to save space
11953 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11954 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11955
791afaa2
TT
11956 std::string virtual_dwo_name =
11957 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11958 sections.abbrev.get_id (),
11959 sections.line.get_id (),
11960 sections.loc.get_id (),
11961 sections.str_offsets.get_id ());
80626a55 11962 /* Can we use an existing virtual DWO file? */
976ca316 11963 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 11964 comp_dir);
80626a55
DE
11965 /* Create one if necessary. */
11966 if (*dwo_file_slot == NULL)
11967 {
b4f54984 11968 if (dwarf_read_debug)
80626a55
DE
11969 {
11970 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11971 virtual_dwo_name.c_str ());
80626a55 11972 }
51ac9db5 11973 dwo_file = new struct dwo_file;
976ca316 11974 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 11975 dwo_file->comp_dir = comp_dir;
80626a55
DE
11976 dwo_file->sections.abbrev = sections.abbrev;
11977 dwo_file->sections.line = sections.line;
11978 dwo_file->sections.loc = sections.loc;
11979 dwo_file->sections.macinfo = sections.macinfo;
11980 dwo_file->sections.macro = sections.macro;
11981 dwo_file->sections.str_offsets = sections.str_offsets;
11982 /* The "str" section is global to the entire DWP file. */
11983 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11984 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11985 there's no need to record it in dwo_file.
11986 Also, we can't simply record type sections in dwo_file because
11987 we record a pointer into the vector in dwo_unit. As we collect more
11988 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11989 for it, invalidating all copies of pointers into the previous
11990 contents. */
80626a55
DE
11991 *dwo_file_slot = dwo_file;
11992 }
11993 else
11994 {
b4f54984 11995 if (dwarf_read_debug)
80626a55
DE
11996 {
11997 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11998 virtual_dwo_name.c_str ());
80626a55 11999 }
9a3c8263 12000 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12001 }
80626a55 12002
976ca316 12003 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
12004 dwo_unit->dwo_file = dwo_file;
12005 dwo_unit->signature = signature;
8d749320 12006 dwo_unit->section =
976ca316 12007 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 12008 *dwo_unit->section = sections.info_or_types;
57d63ce2 12009 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12010
12011 return dwo_unit;
12012}
12013
73869dc2
DE
12014/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12015 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12016 piece within that section used by a TU/CU, return a virtual section
12017 of just that piece. */
12018
12019static struct dwarf2_section_info
976ca316 12020create_dwp_v2_section (dwarf2_per_objfile *per_objfile,
ed2dc618 12021 struct dwarf2_section_info *section,
73869dc2
DE
12022 bfd_size_type offset, bfd_size_type size)
12023{
12024 struct dwarf2_section_info result;
12025 asection *sectp;
12026
12027 gdb_assert (section != NULL);
12028 gdb_assert (!section->is_virtual);
12029
12030 memset (&result, 0, sizeof (result));
12031 result.s.containing_section = section;
dc4ccb6f 12032 result.is_virtual = true;
73869dc2
DE
12033
12034 if (size == 0)
12035 return result;
12036
96b79293 12037 sectp = section->get_bfd_section ();
73869dc2
DE
12038
12039 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12040 bounds of the real section. This is a pretty-rare event, so just
12041 flag an error (easier) instead of a warning and trying to cope. */
12042 if (sectp == NULL
fd361982 12043 || offset + size > bfd_section_size (sectp))
73869dc2 12044 {
73869dc2
DE
12045 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12046 " in section %s [in module %s]"),
fd361982 12047 sectp ? bfd_section_name (sectp) : "<unknown>",
976ca316 12048 objfile_name (per_objfile->objfile));
73869dc2
DE
12049 }
12050
12051 result.virtual_offset = offset;
12052 result.size = size;
12053 return result;
12054}
12055
12056/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12057 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12058 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12059 This is for DWP version 2 files. */
12060
12061static struct dwo_unit *
976ca316 12062create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
ed2dc618 12063 struct dwp_file *dwp_file,
73869dc2
DE
12064 uint32_t unit_index,
12065 const char *comp_dir,
12066 ULONGEST signature, int is_debug_types)
12067{
73869dc2
DE
12068 const struct dwp_hash_table *dwp_htab =
12069 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12070 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12071 const char *kind = is_debug_types ? "TU" : "CU";
12072 struct dwo_file *dwo_file;
12073 struct dwo_unit *dwo_unit;
12074 struct virtual_v2_dwo_sections sections;
12075 void **dwo_file_slot;
73869dc2
DE
12076 int i;
12077
12078 gdb_assert (dwp_file->version == 2);
12079
b4f54984 12080 if (dwarf_read_debug)
73869dc2
DE
12081 {
12082 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12083 kind,
12084 pulongest (unit_index), hex_string (signature),
12085 dwp_file->name);
12086 }
12087
12088 /* Fetch the section offsets of this DWO unit. */
12089
12090 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12091
12092 for (i = 0; i < dwp_htab->nr_columns; ++i)
12093 {
12094 uint32_t offset = read_4_bytes (dbfd,
12095 dwp_htab->section_pool.v2.offsets
12096 + (((unit_index - 1) * dwp_htab->nr_columns
12097 + i)
12098 * sizeof (uint32_t)));
12099 uint32_t size = read_4_bytes (dbfd,
12100 dwp_htab->section_pool.v2.sizes
12101 + (((unit_index - 1) * dwp_htab->nr_columns
12102 + i)
12103 * sizeof (uint32_t)));
12104
12105 switch (dwp_htab->section_pool.v2.section_ids[i])
12106 {
12107 case DW_SECT_INFO:
12108 case DW_SECT_TYPES:
12109 sections.info_or_types_offset = offset;
12110 sections.info_or_types_size = size;
12111 break;
12112 case DW_SECT_ABBREV:
12113 sections.abbrev_offset = offset;
12114 sections.abbrev_size = size;
12115 break;
12116 case DW_SECT_LINE:
12117 sections.line_offset = offset;
12118 sections.line_size = size;
12119 break;
12120 case DW_SECT_LOC:
12121 sections.loc_offset = offset;
12122 sections.loc_size = size;
12123 break;
12124 case DW_SECT_STR_OFFSETS:
12125 sections.str_offsets_offset = offset;
12126 sections.str_offsets_size = size;
12127 break;
12128 case DW_SECT_MACINFO:
12129 sections.macinfo_offset = offset;
12130 sections.macinfo_size = size;
12131 break;
12132 case DW_SECT_MACRO:
12133 sections.macro_offset = offset;
12134 sections.macro_size = size;
12135 break;
12136 }
12137 }
12138
12139 /* It's easier for the rest of the code if we fake a struct dwo_file and
12140 have dwo_unit "live" in that. At least for now.
12141
12142 The DWP file can be made up of a random collection of CUs and TUs.
12143 However, for each CU + set of TUs that came from the same original DWO
12144 file, we can combine them back into a virtual DWO file to save space
12145 (fewer struct dwo_file objects to allocate). Remember that for really
12146 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12147
791afaa2
TT
12148 std::string virtual_dwo_name =
12149 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12150 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12151 (long) (sections.line_size ? sections.line_offset : 0),
12152 (long) (sections.loc_size ? sections.loc_offset : 0),
12153 (long) (sections.str_offsets_size
12154 ? sections.str_offsets_offset : 0));
73869dc2 12155 /* Can we use an existing virtual DWO file? */
976ca316 12156 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12157 comp_dir);
73869dc2
DE
12158 /* Create one if necessary. */
12159 if (*dwo_file_slot == NULL)
12160 {
b4f54984 12161 if (dwarf_read_debug)
73869dc2
DE
12162 {
12163 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12164 virtual_dwo_name.c_str ());
73869dc2 12165 }
51ac9db5 12166 dwo_file = new struct dwo_file;
976ca316 12167 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12168 dwo_file->comp_dir = comp_dir;
12169 dwo_file->sections.abbrev =
976ca316 12170 create_dwp_v2_section (per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12171 sections.abbrev_offset, sections.abbrev_size);
12172 dwo_file->sections.line =
976ca316 12173 create_dwp_v2_section (per_objfile, &dwp_file->sections.line,
73869dc2
DE
12174 sections.line_offset, sections.line_size);
12175 dwo_file->sections.loc =
976ca316 12176 create_dwp_v2_section (per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12177 sections.loc_offset, sections.loc_size);
12178 dwo_file->sections.macinfo =
976ca316 12179 create_dwp_v2_section (per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12180 sections.macinfo_offset, sections.macinfo_size);
12181 dwo_file->sections.macro =
976ca316 12182 create_dwp_v2_section (per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12183 sections.macro_offset, sections.macro_size);
12184 dwo_file->sections.str_offsets =
976ca316 12185 create_dwp_v2_section (per_objfile,
ed2dc618 12186 &dwp_file->sections.str_offsets,
73869dc2
DE
12187 sections.str_offsets_offset,
12188 sections.str_offsets_size);
12189 /* The "str" section is global to the entire DWP file. */
12190 dwo_file->sections.str = dwp_file->sections.str;
12191 /* The info or types section is assigned below to dwo_unit,
12192 there's no need to record it in dwo_file.
12193 Also, we can't simply record type sections in dwo_file because
12194 we record a pointer into the vector in dwo_unit. As we collect more
12195 types we'll grow the vector and eventually have to reallocate space
12196 for it, invalidating all copies of pointers into the previous
12197 contents. */
12198 *dwo_file_slot = dwo_file;
12199 }
12200 else
12201 {
b4f54984 12202 if (dwarf_read_debug)
73869dc2
DE
12203 {
12204 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12205 virtual_dwo_name.c_str ());
73869dc2 12206 }
9a3c8263 12207 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12208 }
73869dc2 12209
976ca316 12210 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12211 dwo_unit->dwo_file = dwo_file;
12212 dwo_unit->signature = signature;
8d749320 12213 dwo_unit->section =
976ca316
SM
12214 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12215 *dwo_unit->section = create_dwp_v2_section (per_objfile,
ed2dc618 12216 is_debug_types
73869dc2
DE
12217 ? &dwp_file->sections.types
12218 : &dwp_file->sections.info,
12219 sections.info_or_types_offset,
12220 sections.info_or_types_size);
12221 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12222
12223 return dwo_unit;
12224}
12225
57d63ce2
DE
12226/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12227 Returns NULL if the signature isn't found. */
80626a55
DE
12228
12229static struct dwo_unit *
976ca316 12230lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
ed2dc618 12231 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12232 ULONGEST signature, int is_debug_types)
80626a55 12233{
57d63ce2
DE
12234 const struct dwp_hash_table *dwp_htab =
12235 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12236 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12237 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12238 uint32_t hash = signature & mask;
12239 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12240 unsigned int i;
12241 void **slot;
870f88f7 12242 struct dwo_unit find_dwo_cu;
80626a55
DE
12243
12244 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12245 find_dwo_cu.signature = signature;
19ac8c2e 12246 slot = htab_find_slot (is_debug_types
48b490f2
TT
12247 ? dwp_file->loaded_tus.get ()
12248 : dwp_file->loaded_cus.get (),
19ac8c2e 12249 &find_dwo_cu, INSERT);
80626a55
DE
12250
12251 if (*slot != NULL)
9a3c8263 12252 return (struct dwo_unit *) *slot;
80626a55
DE
12253
12254 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12255 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12256 {
12257 ULONGEST signature_in_table;
12258
12259 signature_in_table =
57d63ce2 12260 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12261 if (signature_in_table == signature)
12262 {
57d63ce2
DE
12263 uint32_t unit_index =
12264 read_4_bytes (dbfd,
12265 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12266
73869dc2
DE
12267 if (dwp_file->version == 1)
12268 {
976ca316
SM
12269 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12270 unit_index, comp_dir,
12271 signature, is_debug_types);
73869dc2
DE
12272 }
12273 else
12274 {
976ca316
SM
12275 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12276 unit_index, comp_dir,
12277 signature, is_debug_types);
73869dc2 12278 }
9a3c8263 12279 return (struct dwo_unit *) *slot;
80626a55
DE
12280 }
12281 if (signature_in_table == 0)
12282 return NULL;
12283 hash = (hash + hash2) & mask;
12284 }
12285
12286 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12287 " [in module %s]"),
12288 dwp_file->name);
12289}
12290
ab5088bf 12291/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12292 Open the file specified by FILE_NAME and hand it off to BFD for
12293 preliminary analysis. Return a newly initialized bfd *, which
12294 includes a canonicalized copy of FILE_NAME.
80626a55 12295 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12296 SEARCH_CWD is true if the current directory is to be searched.
12297 It will be searched before debug-file-directory.
13aaf454
DE
12298 If successful, the file is added to the bfd include table of the
12299 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12300 If unable to find/open the file, return NULL.
3019eac3
DE
12301 NOTE: This function is derived from symfile_bfd_open. */
12302
192b62ce 12303static gdb_bfd_ref_ptr
976ca316 12304try_open_dwop_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12305 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12306{
24b9144d 12307 int desc;
9c02c129
DE
12308 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12309 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12310 to debug_file_directory. */
e0cc99a6 12311 const char *search_path;
9c02c129
DE
12312 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12313
e0cc99a6 12314 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12315 if (search_cwd)
12316 {
12317 if (*debug_file_directory != '\0')
e0cc99a6
TT
12318 {
12319 search_path_holder.reset (concat (".", dirname_separator_string,
12320 debug_file_directory,
12321 (char *) NULL));
12322 search_path = search_path_holder.get ();
12323 }
6ac97d4c 12324 else
e0cc99a6 12325 search_path = ".";
6ac97d4c 12326 }
9c02c129 12327 else
e0cc99a6 12328 search_path = debug_file_directory;
3019eac3 12329
24b9144d 12330 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12331 if (is_dwp)
12332 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12333
12334 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12335 desc = openp (search_path, flags, file_name,
3019eac3
DE
12336 O_RDONLY | O_BINARY, &absolute_name);
12337 if (desc < 0)
12338 return NULL;
12339
e0cc99a6
TT
12340 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12341 gnutarget, desc));
9c02c129
DE
12342 if (sym_bfd == NULL)
12343 return NULL;
192b62ce 12344 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12345
192b62ce
TT
12346 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12347 return NULL;
3019eac3 12348
13aaf454
DE
12349 /* Success. Record the bfd as having been included by the objfile's bfd.
12350 This is important because things like demangled_names_hash lives in the
12351 objfile's per_bfd space and may have references to things like symbol
12352 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
976ca316 12353 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12354
3019eac3
DE
12355 return sym_bfd;
12356}
12357
ab5088bf 12358/* Try to open DWO file FILE_NAME.
3019eac3
DE
12359 COMP_DIR is the DW_AT_comp_dir attribute.
12360 The result is the bfd handle of the file.
12361 If there is a problem finding or opening the file, return NULL.
12362 Upon success, the canonicalized path of the file is stored in the bfd,
12363 same as symfile_bfd_open. */
12364
192b62ce 12365static gdb_bfd_ref_ptr
976ca316 12366open_dwo_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12367 const char *file_name, const char *comp_dir)
3019eac3 12368{
80626a55 12369 if (IS_ABSOLUTE_PATH (file_name))
976ca316 12370 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12371 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12372
12373 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12374
12375 if (comp_dir != NULL)
12376 {
43816ebc
TT
12377 gdb::unique_xmalloc_ptr<char> path_to_try
12378 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12379
12380 /* NOTE: If comp_dir is a relative path, this will also try the
12381 search path, which seems useful. */
976ca316 12382 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
ed2dc618 12383 0 /*is_dwp*/,
192b62ce 12384 1 /*search_cwd*/));
3019eac3
DE
12385 if (abfd != NULL)
12386 return abfd;
12387 }
12388
12389 /* That didn't work, try debug-file-directory, which, despite its name,
12390 is a list of paths. */
12391
12392 if (*debug_file_directory == '\0')
12393 return NULL;
12394
976ca316 12395 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12396 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12397}
12398
80626a55
DE
12399/* This function is mapped across the sections and remembers the offset and
12400 size of each of the DWO debugging sections we are interested in. */
12401
12402static void
12403dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12404{
9a3c8263 12405 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12406 const struct dwop_section_names *names = &dwop_section_names;
12407
12408 if (section_is_p (sectp->name, &names->abbrev_dwo))
12409 {
049412e3 12410 dwo_sections->abbrev.s.section = sectp;
fd361982 12411 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12412 }
12413 else if (section_is_p (sectp->name, &names->info_dwo))
12414 {
049412e3 12415 dwo_sections->info.s.section = sectp;
fd361982 12416 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12417 }
12418 else if (section_is_p (sectp->name, &names->line_dwo))
12419 {
049412e3 12420 dwo_sections->line.s.section = sectp;
fd361982 12421 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12422 }
12423 else if (section_is_p (sectp->name, &names->loc_dwo))
12424 {
049412e3 12425 dwo_sections->loc.s.section = sectp;
fd361982 12426 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12427 }
41144253 12428 else if (section_is_p (sectp->name, &names->loclists_dwo))
12429 {
12430 dwo_sections->loclists.s.section = sectp;
12431 dwo_sections->loclists.size = bfd_section_size (sectp);
12432 }
80626a55
DE
12433 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12434 {
049412e3 12435 dwo_sections->macinfo.s.section = sectp;
fd361982 12436 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12437 }
12438 else if (section_is_p (sectp->name, &names->macro_dwo))
12439 {
049412e3 12440 dwo_sections->macro.s.section = sectp;
fd361982 12441 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12442 }
12443 else if (section_is_p (sectp->name, &names->str_dwo))
12444 {
049412e3 12445 dwo_sections->str.s.section = sectp;
fd361982 12446 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12447 }
12448 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12449 {
049412e3 12450 dwo_sections->str_offsets.s.section = sectp;
fd361982 12451 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12452 }
12453 else if (section_is_p (sectp->name, &names->types_dwo))
12454 {
12455 struct dwarf2_section_info type_section;
12456
12457 memset (&type_section, 0, sizeof (type_section));
049412e3 12458 type_section.s.section = sectp;
fd361982 12459 type_section.size = bfd_section_size (sectp);
fd5866f6 12460 dwo_sections->types.push_back (type_section);
80626a55
DE
12461 }
12462}
12463
ab5088bf 12464/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12465 by PER_CU. This is for the non-DWP case.
80626a55 12466 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12467
12468static struct dwo_file *
4ab09049
SM
12469open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12470 const char *comp_dir)
3019eac3 12471{
976ca316 12472 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 12473
976ca316 12474 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
80626a55
DE
12475 if (dbfd == NULL)
12476 {
b4f54984 12477 if (dwarf_read_debug)
80626a55
DE
12478 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12479 return NULL;
12480 }
263db9a1 12481
51ac9db5 12482 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12483 dwo_file->dwo_name = dwo_name;
12484 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12485 dwo_file->dbfd = std::move (dbfd);
3019eac3 12486
fb1eb2f9 12487 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12488 &dwo_file->sections);
3019eac3 12489
976ca316
SM
12490 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12491 dwo_file->cus);
3019eac3 12492
976ca316 12493 create_debug_types_hash_table (per_objfile, dwo_file.get (),
ed2dc618 12494 dwo_file->sections.types, dwo_file->tus);
3019eac3 12495
b4f54984 12496 if (dwarf_read_debug)
80626a55
DE
12497 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12498
263db9a1 12499 return dwo_file.release ();
3019eac3
DE
12500}
12501
80626a55 12502/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12503 size of each of the DWP debugging sections common to version 1 and 2 that
12504 we are interested in. */
3019eac3 12505
80626a55 12506static void
73869dc2
DE
12507dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12508 void *dwp_file_ptr)
3019eac3 12509{
9a3c8263 12510 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12511 const struct dwop_section_names *names = &dwop_section_names;
12512 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12513
80626a55 12514 /* Record the ELF section number for later lookup: this is what the
73869dc2 12515 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12516 gdb_assert (elf_section_nr < dwp_file->num_sections);
12517 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12518
80626a55
DE
12519 /* Look for specific sections that we need. */
12520 if (section_is_p (sectp->name, &names->str_dwo))
12521 {
049412e3 12522 dwp_file->sections.str.s.section = sectp;
fd361982 12523 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12524 }
12525 else if (section_is_p (sectp->name, &names->cu_index))
12526 {
049412e3 12527 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12528 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12529 }
12530 else if (section_is_p (sectp->name, &names->tu_index))
12531 {
049412e3 12532 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12533 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12534 }
12535}
3019eac3 12536
73869dc2
DE
12537/* This function is mapped across the sections and remembers the offset and
12538 size of each of the DWP version 2 debugging sections that we are interested
12539 in. This is split into a separate function because we don't know if we
12540 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12541
12542static void
12543dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12544{
9a3c8263 12545 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12546 const struct dwop_section_names *names = &dwop_section_names;
12547 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12548
12549 /* Record the ELF section number for later lookup: this is what the
12550 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12551 gdb_assert (elf_section_nr < dwp_file->num_sections);
12552 dwp_file->elf_sections[elf_section_nr] = sectp;
12553
12554 /* Look for specific sections that we need. */
12555 if (section_is_p (sectp->name, &names->abbrev_dwo))
12556 {
049412e3 12557 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12558 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12559 }
12560 else if (section_is_p (sectp->name, &names->info_dwo))
12561 {
049412e3 12562 dwp_file->sections.info.s.section = sectp;
fd361982 12563 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12564 }
12565 else if (section_is_p (sectp->name, &names->line_dwo))
12566 {
049412e3 12567 dwp_file->sections.line.s.section = sectp;
fd361982 12568 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12569 }
12570 else if (section_is_p (sectp->name, &names->loc_dwo))
12571 {
049412e3 12572 dwp_file->sections.loc.s.section = sectp;
fd361982 12573 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12574 }
12575 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12576 {
049412e3 12577 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12578 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12579 }
12580 else if (section_is_p (sectp->name, &names->macro_dwo))
12581 {
049412e3 12582 dwp_file->sections.macro.s.section = sectp;
fd361982 12583 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12584 }
12585 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12586 {
049412e3 12587 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12588 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12589 }
12590 else if (section_is_p (sectp->name, &names->types_dwo))
12591 {
049412e3 12592 dwp_file->sections.types.s.section = sectp;
fd361982 12593 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12594 }
12595}
12596
80626a55 12597/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12598
80626a55
DE
12599static hashval_t
12600hash_dwp_loaded_cutus (const void *item)
12601{
9a3c8263 12602 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12603
80626a55
DE
12604 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12605 return dwo_unit->signature;
3019eac3
DE
12606}
12607
80626a55 12608/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12609
80626a55
DE
12610static int
12611eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12612{
9a3c8263
SM
12613 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12614 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12615
80626a55
DE
12616 return dua->signature == dub->signature;
12617}
3019eac3 12618
80626a55 12619/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12620
48b490f2 12621static htab_up
298e9637 12622allocate_dwp_loaded_cutus_table ()
80626a55 12623{
48b490f2
TT
12624 return htab_up (htab_create_alloc (3,
12625 hash_dwp_loaded_cutus,
12626 eq_dwp_loaded_cutus,
12627 NULL, xcalloc, xfree));
80626a55 12628}
3019eac3 12629
ab5088bf
DE
12630/* Try to open DWP file FILE_NAME.
12631 The result is the bfd handle of the file.
12632 If there is a problem finding or opening the file, return NULL.
12633 Upon success, the canonicalized path of the file is stored in the bfd,
12634 same as symfile_bfd_open. */
12635
192b62ce 12636static gdb_bfd_ref_ptr
976ca316 12637open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
ab5088bf 12638{
976ca316 12639 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
ed2dc618 12640 1 /*is_dwp*/,
192b62ce 12641 1 /*search_cwd*/));
6ac97d4c
DE
12642 if (abfd != NULL)
12643 return abfd;
12644
12645 /* Work around upstream bug 15652.
12646 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12647 [Whether that's a "bug" is debatable, but it is getting in our way.]
12648 We have no real idea where the dwp file is, because gdb's realpath-ing
12649 of the executable's path may have discarded the needed info.
12650 [IWBN if the dwp file name was recorded in the executable, akin to
12651 .gnu_debuglink, but that doesn't exist yet.]
12652 Strip the directory from FILE_NAME and search again. */
12653 if (*debug_file_directory != '\0')
12654 {
12655 /* Don't implicitly search the current directory here.
12656 If the user wants to search "." to handle this case,
12657 it must be added to debug-file-directory. */
976ca316
SM
12658 return try_open_dwop_file (per_objfile, lbasename (file_name),
12659 1 /*is_dwp*/,
6ac97d4c
DE
12660 0 /*search_cwd*/);
12661 }
12662
12663 return NULL;
ab5088bf
DE
12664}
12665
80626a55
DE
12666/* Initialize the use of the DWP file for the current objfile.
12667 By convention the name of the DWP file is ${objfile}.dwp.
12668 The result is NULL if it can't be found. */
a766d390 12669
400174b1 12670static std::unique_ptr<struct dwp_file>
976ca316 12671open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
80626a55 12672{
976ca316 12673 struct objfile *objfile = per_objfile->objfile;
80626a55 12674
82bf32bc
JK
12675 /* Try to find first .dwp for the binary file before any symbolic links
12676 resolving. */
6c447423
DE
12677
12678 /* If the objfile is a debug file, find the name of the real binary
12679 file and get the name of dwp file from there. */
d721ba37 12680 std::string dwp_name;
6c447423
DE
12681 if (objfile->separate_debug_objfile_backlink != NULL)
12682 {
12683 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12684 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12685
d721ba37 12686 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12687 }
12688 else
d721ba37
PA
12689 dwp_name = objfile->original_name;
12690
12691 dwp_name += ".dwp";
80626a55 12692
976ca316 12693 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12694 if (dbfd == NULL
12695 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12696 {
12697 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12698 dwp_name = objfile_name (objfile);
12699 dwp_name += ".dwp";
976ca316 12700 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
82bf32bc
JK
12701 }
12702
80626a55
DE
12703 if (dbfd == NULL)
12704 {
b4f54984 12705 if (dwarf_read_debug)
d721ba37 12706 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12707 return std::unique_ptr<dwp_file> ();
3019eac3 12708 }
400174b1
TT
12709
12710 const char *name = bfd_get_filename (dbfd.get ());
12711 std::unique_ptr<struct dwp_file> dwp_file
12712 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12713
0a0f4c01 12714 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 12715 dwp_file->elf_sections =
976ca316 12716 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
80626a55
DE
12717 dwp_file->num_sections, asection *);
12718
400174b1
TT
12719 bfd_map_over_sections (dwp_file->dbfd.get (),
12720 dwarf2_locate_common_dwp_sections,
12721 dwp_file.get ());
80626a55 12722
976ca316 12723 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
80626a55 12724
976ca316 12725 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
80626a55 12726
73869dc2 12727 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12728 if (dwp_file->cus && dwp_file->tus
12729 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12730 {
12731 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12732 pretty bizarre. We use pulongest here because that's the established
4d65956b 12733 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12734 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12735 " TU version %s [in DWP file %s]"),
12736 pulongest (dwp_file->cus->version),
d721ba37 12737 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12738 }
08302ed2
DE
12739
12740 if (dwp_file->cus)
12741 dwp_file->version = dwp_file->cus->version;
12742 else if (dwp_file->tus)
12743 dwp_file->version = dwp_file->tus->version;
12744 else
12745 dwp_file->version = 2;
73869dc2
DE
12746
12747 if (dwp_file->version == 2)
400174b1
TT
12748 bfd_map_over_sections (dwp_file->dbfd.get (),
12749 dwarf2_locate_v2_dwp_sections,
12750 dwp_file.get ());
73869dc2 12751
298e9637
SM
12752 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12753 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12754
b4f54984 12755 if (dwarf_read_debug)
80626a55
DE
12756 {
12757 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12758 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12759 " %s CUs, %s TUs\n",
12760 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12761 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12762 }
12763
12764 return dwp_file;
3019eac3 12765}
c906108c 12766
ab5088bf
DE
12767/* Wrapper around open_and_init_dwp_file, only open it once. */
12768
12769static struct dwp_file *
976ca316 12770get_dwp_file (dwarf2_per_objfile *per_objfile)
ab5088bf 12771{
976ca316 12772 if (!per_objfile->per_bfd->dwp_checked)
ab5088bf 12773 {
976ca316
SM
12774 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12775 per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 12776 }
976ca316 12777 return per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
12778}
12779
80626a55
DE
12780/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12781 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12782 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12783 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12784 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12785
12786 This is called, for example, when wanting to read a variable with a
12787 complex location. Therefore we don't want to do file i/o for every call.
12788 Therefore we don't want to look for a DWO file on every call.
12789 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12790 then we check if we've already seen DWO_NAME, and only THEN do we check
12791 for a DWO file.
12792
1c658ad5 12793 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12794 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12795
3019eac3 12796static struct dwo_unit *
4ab09049 12797lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 12798 ULONGEST signature, int is_debug_types)
3019eac3 12799{
976ca316
SM
12800 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12801 struct objfile *objfile = per_objfile->objfile;
80626a55
DE
12802 const char *kind = is_debug_types ? "TU" : "CU";
12803 void **dwo_file_slot;
3019eac3 12804 struct dwo_file *dwo_file;
80626a55 12805 struct dwp_file *dwp_file;
cb1df416 12806
6a506a2d
DE
12807 /* First see if there's a DWP file.
12808 If we have a DWP file but didn't find the DWO inside it, don't
12809 look for the original DWO file. It makes gdb behave differently
12810 depending on whether one is debugging in the build tree. */
cf2c3c16 12811
976ca316 12812 dwp_file = get_dwp_file (per_objfile);
80626a55 12813 if (dwp_file != NULL)
cf2c3c16 12814 {
80626a55
DE
12815 const struct dwp_hash_table *dwp_htab =
12816 is_debug_types ? dwp_file->tus : dwp_file->cus;
12817
12818 if (dwp_htab != NULL)
12819 {
12820 struct dwo_unit *dwo_cutu =
976ca316
SM
12821 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12822 is_debug_types);
80626a55
DE
12823
12824 if (dwo_cutu != NULL)
12825 {
b4f54984 12826 if (dwarf_read_debug)
80626a55
DE
12827 {
12828 fprintf_unfiltered (gdb_stdlog,
12829 "Virtual DWO %s %s found: @%s\n",
12830 kind, hex_string (signature),
12831 host_address_to_string (dwo_cutu));
12832 }
12833 return dwo_cutu;
12834 }
12835 }
12836 }
6a506a2d 12837 else
80626a55 12838 {
6a506a2d 12839 /* No DWP file, look for the DWO file. */
80626a55 12840
976ca316 12841 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
6a506a2d 12842 if (*dwo_file_slot == NULL)
80626a55 12843 {
6a506a2d 12844 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 12845 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 12846 }
6a506a2d 12847 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12848 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12849
6a506a2d 12850 if (dwo_file != NULL)
19c3d4c9 12851 {
6a506a2d
DE
12852 struct dwo_unit *dwo_cutu = NULL;
12853
12854 if (is_debug_types && dwo_file->tus)
12855 {
12856 struct dwo_unit find_dwo_cutu;
12857
12858 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12859 find_dwo_cutu.signature = signature;
9a3c8263 12860 dwo_cutu
b0b6a987
TT
12861 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12862 &find_dwo_cutu);
6a506a2d 12863 }
33c5cd75 12864 else if (!is_debug_types && dwo_file->cus)
80626a55 12865 {
33c5cd75
DB
12866 struct dwo_unit find_dwo_cutu;
12867
12868 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12869 find_dwo_cutu.signature = signature;
b0b6a987 12870 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12871 &find_dwo_cutu);
6a506a2d
DE
12872 }
12873
12874 if (dwo_cutu != NULL)
12875 {
b4f54984 12876 if (dwarf_read_debug)
6a506a2d
DE
12877 {
12878 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12879 kind, dwo_name, hex_string (signature),
12880 host_address_to_string (dwo_cutu));
12881 }
12882 return dwo_cutu;
80626a55
DE
12883 }
12884 }
2e276125 12885 }
9cdd5dbd 12886
80626a55
DE
12887 /* We didn't find it. This could mean a dwo_id mismatch, or
12888 someone deleted the DWO/DWP file, or the search path isn't set up
12889 correctly to find the file. */
12890
b4f54984 12891 if (dwarf_read_debug)
80626a55
DE
12892 {
12893 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12894 kind, dwo_name, hex_string (signature));
12895 }
3019eac3 12896
6656a72d
DE
12897 /* This is a warning and not a complaint because it can be caused by
12898 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12899 {
12900 /* Print the name of the DWP file if we looked there, helps the user
12901 better diagnose the problem. */
791afaa2 12902 std::string dwp_text;
43942612
DE
12903
12904 if (dwp_file != NULL)
791afaa2
TT
12905 dwp_text = string_printf (" [in DWP file %s]",
12906 lbasename (dwp_file->name));
43942612 12907
9d8780f0 12908 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 12909 " [in module %s]"),
4ab09049
SM
12910 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12911 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 12912 }
3019eac3 12913 return NULL;
5fb290d7
DJ
12914}
12915
80626a55
DE
12916/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12917 See lookup_dwo_cutu_unit for details. */
12918
12919static struct dwo_unit *
4ab09049 12920lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
12921 ULONGEST signature)
12922{
4ab09049
SM
12923 gdb_assert (!cu->per_cu->is_debug_types);
12924
12925 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
12926}
12927
12928/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12929 See lookup_dwo_cutu_unit for details. */
12930
12931static struct dwo_unit *
4ab09049 12932lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 12933{
4ab09049
SM
12934 gdb_assert (cu->per_cu->is_debug_types);
12935
12936 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12937
12938 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
12939}
12940
89e63ee4
DE
12941/* Traversal function for queue_and_load_all_dwo_tus. */
12942
12943static int
12944queue_and_load_dwo_tu (void **slot, void *info)
12945{
12946 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 12947 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 12948 ULONGEST signature = dwo_unit->signature;
d460f660 12949 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
12950
12951 if (sig_type != NULL)
12952 {
12953 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12954
12955 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12956 a real dependency of PER_CU on SIG_TYPE. That is detected later
12957 while processing PER_CU. */
120ce1b5 12958 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
d460f660
SM
12959 load_full_type_unit (sig_cu, cu->per_objfile);
12960 cu->per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12961 }
12962
12963 return 1;
12964}
12965
1b555f17 12966/* Queue all TUs contained in the DWO of CU to be read in.
89e63ee4
DE
12967 The DWO may have the only definition of the type, though it may not be
12968 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12969 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12970
12971static void
1b555f17 12972queue_and_load_all_dwo_tus (dwarf2_cu *cu)
89e63ee4
DE
12973{
12974 struct dwo_unit *dwo_unit;
12975 struct dwo_file *dwo_file;
12976
1b555f17
SM
12977 gdb_assert (cu != nullptr);
12978 gdb_assert (!cu->per_cu->is_debug_types);
12979 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
89e63ee4 12980
1b555f17 12981 dwo_unit = cu->dwo_unit;
89e63ee4
DE
12982 gdb_assert (dwo_unit != NULL);
12983
12984 dwo_file = dwo_unit->dwo_file;
12985 if (dwo_file->tus != NULL)
1b555f17 12986 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
89e63ee4
DE
12987}
12988
3019eac3 12989/* Read in various DIEs. */
348e048f 12990
d389af10 12991/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12992 Inherit only the children of the DW_AT_abstract_origin DIE not being
12993 already referenced by DW_AT_abstract_origin from the children of the
12994 current DIE. */
d389af10
JK
12995
12996static void
12997inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12998{
12999 struct die_info *child_die;
791afaa2 13000 sect_offset *offsetp;
d389af10
JK
13001 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13002 struct die_info *origin_die;
13003 /* Iterator of the ORIGIN_DIE children. */
13004 struct die_info *origin_child_die;
d389af10 13005 struct attribute *attr;
cd02d79d
PA
13006 struct dwarf2_cu *origin_cu;
13007 struct pending **origin_previous_list_in_scope;
d389af10
JK
13008
13009 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13010 if (!attr)
13011 return;
13012
cd02d79d
PA
13013 /* Note that following die references may follow to a die in a
13014 different cu. */
13015
13016 origin_cu = cu;
13017 origin_die = follow_die_ref (die, attr, &origin_cu);
13018
13019 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13020 symbols in. */
13021 origin_previous_list_in_scope = origin_cu->list_in_scope;
13022 origin_cu->list_in_scope = cu->list_in_scope;
13023
edb3359d
DJ
13024 if (die->tag != origin_die->tag
13025 && !(die->tag == DW_TAG_inlined_subroutine
13026 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13027 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13028 sect_offset_str (die->sect_off),
13029 sect_offset_str (origin_die->sect_off));
d389af10 13030
791afaa2 13031 std::vector<sect_offset> offsets;
d389af10 13032
3ea89b92
PMR
13033 for (child_die = die->child;
13034 child_die && child_die->tag;
436c571c 13035 child_die = child_die->sibling)
3ea89b92
PMR
13036 {
13037 struct die_info *child_origin_die;
13038 struct dwarf2_cu *child_origin_cu;
13039
13040 /* We are trying to process concrete instance entries:
216f72a1 13041 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13042 it's not relevant to our analysis here. i.e. detecting DIEs that are
13043 present in the abstract instance but not referenced in the concrete
13044 one. */
216f72a1
JK
13045 if (child_die->tag == DW_TAG_call_site
13046 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13047 continue;
13048
c38f313d
DJ
13049 /* For each CHILD_DIE, find the corresponding child of
13050 ORIGIN_DIE. If there is more than one layer of
13051 DW_AT_abstract_origin, follow them all; there shouldn't be,
13052 but GCC versions at least through 4.4 generate this (GCC PR
13053 40573). */
3ea89b92
PMR
13054 child_origin_die = child_die;
13055 child_origin_cu = cu;
c38f313d
DJ
13056 while (1)
13057 {
cd02d79d
PA
13058 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13059 child_origin_cu);
c38f313d
DJ
13060 if (attr == NULL)
13061 break;
cd02d79d
PA
13062 child_origin_die = follow_die_ref (child_origin_die, attr,
13063 &child_origin_cu);
c38f313d
DJ
13064 }
13065
d389af10
JK
13066 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13067 counterpart may exist. */
c38f313d 13068 if (child_origin_die != child_die)
d389af10 13069 {
edb3359d
DJ
13070 if (child_die->tag != child_origin_die->tag
13071 && !(child_die->tag == DW_TAG_inlined_subroutine
13072 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13073 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13074 "different tags"),
9d8780f0
SM
13075 sect_offset_str (child_die->sect_off),
13076 sect_offset_str (child_origin_die->sect_off));
c38f313d 13077 if (child_origin_die->parent != origin_die)
b98664d3 13078 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13079 "different parents"),
9d8780f0
SM
13080 sect_offset_str (child_die->sect_off),
13081 sect_offset_str (child_origin_die->sect_off));
c38f313d 13082 else
791afaa2 13083 offsets.push_back (child_origin_die->sect_off);
d389af10 13084 }
d389af10 13085 }
791afaa2
TT
13086 std::sort (offsets.begin (), offsets.end ());
13087 sect_offset *offsets_end = offsets.data () + offsets.size ();
13088 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13089 if (offsetp[-1] == *offsetp)
b98664d3 13090 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13091 "to DIE %s as their abstract origin"),
13092 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13093
791afaa2 13094 offsetp = offsets.data ();
d389af10
JK
13095 origin_child_die = origin_die->child;
13096 while (origin_child_die && origin_child_die->tag)
13097 {
13098 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13099 while (offsetp < offsets_end
9c541725 13100 && *offsetp < origin_child_die->sect_off)
d389af10 13101 offsetp++;
b64f50a1 13102 if (offsetp >= offsets_end
9c541725 13103 || *offsetp > origin_child_die->sect_off)
d389af10 13104 {
adde2bff
DE
13105 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13106 Check whether we're already processing ORIGIN_CHILD_DIE.
13107 This can happen with mutually referenced abstract_origins.
13108 PR 16581. */
13109 if (!origin_child_die->in_process)
13110 process_die (origin_child_die, origin_cu);
d389af10 13111 }
436c571c 13112 origin_child_die = origin_child_die->sibling;
d389af10 13113 }
cd02d79d 13114 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13115
13116 if (cu != origin_cu)
13117 compute_delayed_physnames (origin_cu);
d389af10
JK
13118}
13119
c906108c 13120static void
e7c27a73 13121read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13122{
5e22e966 13123 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13124 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13125 struct context_stack *newobj;
c906108c
SS
13126 CORE_ADDR lowpc;
13127 CORE_ADDR highpc;
13128 struct die_info *child_die;
edb3359d 13129 struct attribute *attr, *call_line, *call_file;
15d034d0 13130 const char *name;
e142c38c 13131 CORE_ADDR baseaddr;
801e3a5b 13132 struct block *block;
edb3359d 13133 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13134 std::vector<struct symbol *> template_args;
34eaf542 13135 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13136
13137 if (inlined_func)
13138 {
13139 /* If we do not have call site information, we can't show the
13140 caller of this inlined function. That's too confusing, so
13141 only use the scope for local variables. */
13142 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13143 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13144 if (call_line == NULL || call_file == NULL)
13145 {
13146 read_lexical_block_scope (die, cu);
13147 return;
13148 }
13149 }
c906108c 13150
b3b3bada 13151 baseaddr = objfile->text_section_offset ();
e142c38c 13152
94af9270 13153 name = dwarf2_name (die, cu);
c906108c 13154
e8d05480
JB
13155 /* Ignore functions with missing or empty names. These are actually
13156 illegal according to the DWARF standard. */
13157 if (name == NULL)
13158 {
b98664d3 13159 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13160 sect_offset_str (die->sect_off));
e8d05480
JB
13161 return;
13162 }
13163
13164 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13165 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13166 <= PC_BOUNDS_INVALID)
e8d05480 13167 {
ae4d0c03
PM
13168 attr = dwarf2_attr (die, DW_AT_external, cu);
13169 if (!attr || !DW_UNSND (attr))
b98664d3 13170 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13171 "for subprogram DIE at %s"),
13172 sect_offset_str (die->sect_off));
e8d05480
JB
13173 return;
13174 }
c906108c 13175
3e29f34a
MR
13176 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13177 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13178
34eaf542
TT
13179 /* If we have any template arguments, then we must allocate a
13180 different sort of symbol. */
436c571c 13181 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13182 {
13183 if (child_die->tag == DW_TAG_template_type_param
13184 || child_die->tag == DW_TAG_template_value_param)
13185 {
8c14c3a3 13186 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13187 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13188 break;
13189 }
13190 }
13191
c24bdb02 13192 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13193 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13194 (struct symbol *) templ_func);
4c2df51b 13195
81873cc8 13196 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13197 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13198 cu->language);
13199
4cecd739
DJ
13200 /* If there is a location expression for DW_AT_frame_base, record
13201 it. */
e142c38c 13202 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13203 if (attr != nullptr)
fe978cb0 13204 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13205
63e43d3a
PMR
13206 /* If there is a location for the static link, record it. */
13207 newobj->static_link = NULL;
13208 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13209 if (attr != nullptr)
63e43d3a 13210 {
224c3ddb
SM
13211 newobj->static_link
13212 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13213 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13214 cu->addr_type ());
63e43d3a
PMR
13215 }
13216
c24bdb02 13217 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13218
639d11d3 13219 if (die->child != NULL)
c906108c 13220 {
639d11d3 13221 child_die = die->child;
c906108c
SS
13222 while (child_die && child_die->tag)
13223 {
34eaf542
TT
13224 if (child_die->tag == DW_TAG_template_type_param
13225 || child_die->tag == DW_TAG_template_value_param)
13226 {
13227 struct symbol *arg = new_symbol (child_die, NULL, cu);
13228
f1078f66 13229 if (arg != NULL)
2f4732b0 13230 template_args.push_back (arg);
34eaf542
TT
13231 }
13232 else
13233 process_die (child_die, cu);
436c571c 13234 child_die = child_die->sibling;
c906108c
SS
13235 }
13236 }
13237
d389af10
JK
13238 inherit_abstract_dies (die, cu);
13239
4a811a97
UW
13240 /* If we have a DW_AT_specification, we might need to import using
13241 directives from the context of the specification DIE. See the
13242 comment in determine_prefix. */
13243 if (cu->language == language_cplus
13244 && dwarf2_attr (die, DW_AT_specification, cu))
13245 {
13246 struct dwarf2_cu *spec_cu = cu;
13247 struct die_info *spec_die = die_specification (die, &spec_cu);
13248
13249 while (spec_die)
13250 {
13251 child_die = spec_die->child;
13252 while (child_die && child_die->tag)
13253 {
13254 if (child_die->tag == DW_TAG_imported_module)
13255 process_die (child_die, spec_cu);
436c571c 13256 child_die = child_die->sibling;
4a811a97
UW
13257 }
13258
13259 /* In some cases, GCC generates specification DIEs that
13260 themselves contain DW_AT_specification attributes. */
13261 spec_die = die_specification (spec_die, &spec_cu);
13262 }
13263 }
13264
c24bdb02 13265 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13266 /* Make a block for the local symbols within. */
c24bdb02 13267 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13268 cstk.static_link, lowpc, highpc);
801e3a5b 13269
df8a16a1 13270 /* For C++, set the block's scope. */
45280282
IB
13271 if ((cu->language == language_cplus
13272 || cu->language == language_fortran
c44af4eb
TT
13273 || cu->language == language_d
13274 || cu->language == language_rust)
4d4ec4e5 13275 && cu->processing_has_namespace_info)
195a3f6c
TT
13276 block_set_scope (block, determine_prefix (die, cu),
13277 &objfile->objfile_obstack);
df8a16a1 13278
801e3a5b
JB
13279 /* If we have address ranges, record them. */
13280 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13281
a60f3166 13282 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13283
34eaf542 13284 /* Attach template arguments to function. */
2f4732b0 13285 if (!template_args.empty ())
34eaf542
TT
13286 {
13287 gdb_assert (templ_func != NULL);
13288
2f4732b0 13289 templ_func->n_template_arguments = template_args.size ();
34eaf542 13290 templ_func->template_arguments
8d749320
SM
13291 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13292 templ_func->n_template_arguments);
34eaf542 13293 memcpy (templ_func->template_arguments,
2f4732b0 13294 template_args.data (),
34eaf542 13295 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13296
13297 /* Make sure that the symtab is set on the new symbols. Even
13298 though they don't appear in this symtab directly, other parts
13299 of gdb assume that symbols do, and this is reasonably
13300 true. */
8634679f 13301 for (symbol *sym : template_args)
3e1d3d8c 13302 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13303 }
13304
208d8187
JB
13305 /* In C++, we can have functions nested inside functions (e.g., when
13306 a function declares a class that has methods). This means that
13307 when we finish processing a function scope, we may need to go
13308 back to building a containing block's symbol lists. */
c24bdb02
KS
13309 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13310 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13311
921e78cf
JB
13312 /* If we've finished processing a top-level function, subsequent
13313 symbols go in the file symbol list. */
c24bdb02
KS
13314 if (cu->get_builder ()->outermost_context_p ())
13315 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13316}
13317
13318/* Process all the DIES contained within a lexical block scope. Start
13319 a new scope, process the dies, and then close the scope. */
13320
13321static void
e7c27a73 13322read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13323{
5e22e966 13324 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13325 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13326 CORE_ADDR lowpc, highpc;
13327 struct die_info *child_die;
e142c38c
DJ
13328 CORE_ADDR baseaddr;
13329
b3b3bada 13330 baseaddr = objfile->text_section_offset ();
c906108c
SS
13331
13332 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13333 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13334 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13335 be nasty. Might be easier to properly extend generic blocks to
af34e669 13336 describe ranges. */
e385593e
JK
13337 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13338 {
13339 case PC_BOUNDS_NOT_PRESENT:
13340 /* DW_TAG_lexical_block has no attributes, process its children as if
13341 there was no wrapping by that DW_TAG_lexical_block.
13342 GCC does no longer produces such DWARF since GCC r224161. */
13343 for (child_die = die->child;
13344 child_die != NULL && child_die->tag;
436c571c 13345 child_die = child_die->sibling)
4f7bc5ed
TT
13346 {
13347 /* We might already be processing this DIE. This can happen
13348 in an unusual circumstance -- where a subroutine A
13349 appears lexically in another subroutine B, but A actually
13350 inlines B. The recursion is broken here, rather than in
13351 inherit_abstract_dies, because it seems better to simply
13352 drop concrete children here. */
13353 if (!child_die->in_process)
13354 process_die (child_die, cu);
13355 }
e385593e
JK
13356 return;
13357 case PC_BOUNDS_INVALID:
13358 return;
13359 }
3e29f34a
MR
13360 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13361 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13362
c24bdb02 13363 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13364 if (die->child != NULL)
c906108c 13365 {
639d11d3 13366 child_die = die->child;
c906108c
SS
13367 while (child_die && child_die->tag)
13368 {
e7c27a73 13369 process_die (child_die, cu);
436c571c 13370 child_die = child_die->sibling;
c906108c
SS
13371 }
13372 }
3ea89b92 13373 inherit_abstract_dies (die, cu);
c24bdb02 13374 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13375
c24bdb02
KS
13376 if (*cu->get_builder ()->get_local_symbols () != NULL
13377 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13378 {
801e3a5b 13379 struct block *block
c24bdb02 13380 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13381 cstk.start_addr, highpc);
801e3a5b
JB
13382
13383 /* Note that recording ranges after traversing children, as we
13384 do here, means that recording a parent's ranges entails
13385 walking across all its children's ranges as they appear in
13386 the address map, which is quadratic behavior.
13387
13388 It would be nicer to record the parent's ranges before
13389 traversing its children, simply overriding whatever you find
13390 there. But since we don't even decide whether to create a
13391 block until after we've traversed its children, that's hard
13392 to do. */
13393 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13394 }
c24bdb02
KS
13395 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13396 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13397}
13398
216f72a1 13399/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13400
13401static void
13402read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13403{
5e22e966 13404 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13405 struct objfile *objfile = per_objfile->objfile;
08feed99 13406 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13407 CORE_ADDR pc, baseaddr;
13408 struct attribute *attr;
13409 struct call_site *call_site, call_site_local;
13410 void **slot;
13411 int nparams;
13412 struct die_info *child_die;
13413
b3b3bada 13414 baseaddr = objfile->text_section_offset ();
96408a79 13415
216f72a1
JK
13416 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13417 if (attr == NULL)
13418 {
13419 /* This was a pre-DWARF-5 GNU extension alias
13420 for DW_AT_call_return_pc. */
13421 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13422 }
96408a79
SA
13423 if (!attr)
13424 {
b98664d3 13425 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13426 "DIE %s [in module %s]"),
13427 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13428 return;
13429 }
cd6c91b4 13430 pc = attr->value_as_address () + baseaddr;
3e29f34a 13431 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13432
13433 if (cu->call_site_htab == NULL)
13434 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13435 NULL, &objfile->objfile_obstack,
13436 hashtab_obstack_allocate, NULL);
13437 call_site_local.pc = pc;
13438 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13439 if (*slot != NULL)
13440 {
b98664d3 13441 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13442 "DIE %s [in module %s]"),
13443 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13444 objfile_name (objfile));
96408a79
SA
13445 return;
13446 }
13447
13448 /* Count parameters at the caller. */
13449
13450 nparams = 0;
13451 for (child_die = die->child; child_die && child_die->tag;
436c571c 13452 child_die = child_die->sibling)
96408a79 13453 {
216f72a1
JK
13454 if (child_die->tag != DW_TAG_call_site_parameter
13455 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13456 {
b98664d3 13457 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13458 "DW_TAG_call_site child DIE %s [in module %s]"),
13459 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13460 objfile_name (objfile));
96408a79
SA
13461 continue;
13462 }
13463
13464 nparams++;
13465 }
13466
224c3ddb
SM
13467 call_site
13468 = ((struct call_site *)
13469 obstack_alloc (&objfile->objfile_obstack,
13470 sizeof (*call_site)
13471 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13472 *slot = call_site;
13473 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13474 call_site->pc = pc;
13475
216f72a1
JK
13476 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13477 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13478 {
13479 struct die_info *func_die;
13480
13481 /* Skip also over DW_TAG_inlined_subroutine. */
13482 for (func_die = die->parent;
13483 func_die && func_die->tag != DW_TAG_subprogram
13484 && func_die->tag != DW_TAG_subroutine_type;
13485 func_die = func_die->parent);
13486
216f72a1
JK
13487 /* DW_AT_call_all_calls is a superset
13488 of DW_AT_call_all_tail_calls. */
96408a79 13489 if (func_die
216f72a1 13490 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13491 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13492 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13493 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13494 {
13495 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13496 not complete. But keep CALL_SITE for look ups via call_site_htab,
13497 both the initial caller containing the real return address PC and
13498 the final callee containing the current PC of a chain of tail
13499 calls do not need to have the tail call list complete. But any
13500 function candidate for a virtual tail call frame searched via
13501 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13502 determined unambiguously. */
13503 }
13504 else
13505 {
13506 struct type *func_type = NULL;
13507
13508 if (func_die)
13509 func_type = get_die_type (func_die, cu);
13510 if (func_type != NULL)
13511 {
78134374 13512 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13513
13514 /* Enlist this call site to the function. */
13515 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13516 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13517 }
13518 else
b98664d3 13519 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13520 "DIE %s [in module %s]"),
13521 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13522 }
13523 }
13524
216f72a1
JK
13525 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13526 if (attr == NULL)
13527 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13528 if (attr == NULL)
13529 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13530 if (attr == NULL)
216f72a1
JK
13531 {
13532 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13533 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13534 }
96408a79 13535 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13536 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13537 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13538 else if (attr->form_is_block ())
96408a79
SA
13539 {
13540 struct dwarf2_locexpr_baton *dlbaton;
13541
8d749320 13542 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13543 dlbaton->data = DW_BLOCK (attr)->data;
13544 dlbaton->size = DW_BLOCK (attr)->size;
a50264ba 13545 dlbaton->per_objfile = per_objfile;
96408a79
SA
13546 dlbaton->per_cu = cu->per_cu;
13547
13548 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13549 }
cd6c91b4 13550 else if (attr->form_is_ref ())
96408a79 13551 {
96408a79
SA
13552 struct dwarf2_cu *target_cu = cu;
13553 struct die_info *target_die;
13554
ac9ec31b 13555 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13556 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13557 if (die_is_declaration (target_die, target_cu))
13558 {
7d45c7c3 13559 const char *target_physname;
9112db09
JK
13560
13561 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13562 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13563 if (target_physname == NULL)
9112db09 13564 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13565 if (target_physname == NULL)
b98664d3 13566 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13567 "physname, for referencing DIE %s [in module %s]"),
13568 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13569 else
7d455152 13570 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13571 }
13572 else
13573 {
13574 CORE_ADDR lowpc;
13575
13576 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13577 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13578 <= PC_BOUNDS_INVALID)
b98664d3 13579 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13580 "low pc, for referencing DIE %s [in module %s]"),
13581 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13582 else
3e29f34a
MR
13583 {
13584 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13585 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13586 }
96408a79
SA
13587 }
13588 }
13589 else
b98664d3 13590 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13591 "block nor reference, for DIE %s [in module %s]"),
13592 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13593
13594 call_site->per_cu = cu->per_cu;
9f47c707 13595 call_site->per_objfile = per_objfile;
96408a79
SA
13596
13597 for (child_die = die->child;
13598 child_die && child_die->tag;
436c571c 13599 child_die = child_die->sibling)
96408a79 13600 {
96408a79 13601 struct call_site_parameter *parameter;
1788b2d3 13602 struct attribute *loc, *origin;
96408a79 13603
216f72a1
JK
13604 if (child_die->tag != DW_TAG_call_site_parameter
13605 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13606 {
13607 /* Already printed the complaint above. */
13608 continue;
13609 }
13610
13611 gdb_assert (call_site->parameter_count < nparams);
13612 parameter = &call_site->parameter[call_site->parameter_count];
13613
1788b2d3
JK
13614 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13615 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13616 register is contained in DW_AT_call_value. */
96408a79 13617
24c5c679 13618 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13619 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13620 if (origin == NULL)
13621 {
13622 /* This was a pre-DWARF-5 GNU extension alias
13623 for DW_AT_call_parameter. */
13624 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13625 }
cd6c91b4 13626 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13627 {
1788b2d3 13628 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13629
0826b30a 13630 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13631 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13632 {
13633 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13634 binding can be done only inside one CU. Such referenced DIE
13635 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13636 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13637 "DW_TAG_call_site child DIE %s [in module %s]"),
13638 sect_offset_str (child_die->sect_off),
9c541725 13639 objfile_name (objfile));
d76b7dbc
JK
13640 continue;
13641 }
9c541725
PA
13642 parameter->u.param_cu_off
13643 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13644 }
4fc6c0d5 13645 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13646 {
b98664d3 13647 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13648 "DW_TAG_call_site child DIE %s [in module %s]"),
13649 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13650 continue;
13651 }
24c5c679 13652 else
96408a79 13653 {
24c5c679
JK
13654 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13655 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13656 if (parameter->u.dwarf_reg != -1)
13657 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13658 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13659 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13660 &parameter->u.fb_offset))
13661 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13662 else
13663 {
b98664d3 13664 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13665 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13666 "DW_TAG_call_site child DIE %s "
24c5c679 13667 "[in module %s]"),
9d8780f0 13668 sect_offset_str (child_die->sect_off),
9c541725 13669 objfile_name (objfile));
24c5c679
JK
13670 continue;
13671 }
96408a79
SA
13672 }
13673
216f72a1
JK
13674 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13675 if (attr == NULL)
13676 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13677 if (attr == NULL || !attr->form_is_block ())
96408a79 13678 {
b98664d3 13679 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13680 "DW_TAG_call_site child DIE %s [in module %s]"),
13681 sect_offset_str (child_die->sect_off),
9c541725 13682 objfile_name (objfile));
96408a79
SA
13683 continue;
13684 }
13685 parameter->value = DW_BLOCK (attr)->data;
13686 parameter->value_size = DW_BLOCK (attr)->size;
13687
13688 /* Parameters are not pre-cleared by memset above. */
13689 parameter->data_value = NULL;
13690 parameter->data_value_size = 0;
13691 call_site->parameter_count++;
13692
216f72a1
JK
13693 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13694 if (attr == NULL)
13695 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13696 if (attr != nullptr)
96408a79 13697 {
4fc6c0d5 13698 if (!attr->form_is_block ())
b98664d3 13699 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13700 "DW_TAG_call_site child DIE %s [in module %s]"),
13701 sect_offset_str (child_die->sect_off),
9c541725 13702 objfile_name (objfile));
96408a79
SA
13703 else
13704 {
13705 parameter->data_value = DW_BLOCK (attr)->data;
13706 parameter->data_value_size = DW_BLOCK (attr)->size;
13707 }
13708 }
13709 }
13710}
13711
71a3c369
TT
13712/* Helper function for read_variable. If DIE represents a virtual
13713 table, then return the type of the concrete object that is
13714 associated with the virtual table. Otherwise, return NULL. */
13715
13716static struct type *
13717rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13718{
13719 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13720 if (attr == NULL)
13721 return NULL;
13722
13723 /* Find the type DIE. */
13724 struct die_info *type_die = NULL;
13725 struct dwarf2_cu *type_cu = cu;
13726
cd6c91b4 13727 if (attr->form_is_ref ())
71a3c369
TT
13728 type_die = follow_die_ref (die, attr, &type_cu);
13729 if (type_die == NULL)
13730 return NULL;
13731
13732 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13733 return NULL;
13734 return die_containing_type (type_die, type_cu);
13735}
13736
13737/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13738
13739static void
13740read_variable (struct die_info *die, struct dwarf2_cu *cu)
13741{
13742 struct rust_vtable_symbol *storage = NULL;
13743
13744 if (cu->language == language_rust)
13745 {
13746 struct type *containing_type = rust_containing_type (die, cu);
13747
13748 if (containing_type != NULL)
13749 {
5e22e966 13750 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 13751
8c14c3a3 13752 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 13753 storage->concrete_type = containing_type;
cf724bc9 13754 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13755 }
13756 }
13757
e4a62c65
TV
13758 struct symbol *res = new_symbol (die, NULL, cu, storage);
13759 struct attribute *abstract_origin
13760 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13761 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13762 if (res == NULL && loc && abstract_origin)
13763 {
13764 /* We have a variable without a name, but with a location and an abstract
13765 origin. This may be a concrete instance of an abstract variable
13766 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13767 later. */
13768 struct dwarf2_cu *origin_cu = cu;
13769 struct die_info *origin_die
13770 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
13771 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13772 per_objfile->per_bfd->abstract_to_concrete
13773 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13774 }
71a3c369
TT
13775}
13776
43988095
JK
13777/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13778 reading .debug_rnglists.
13779 Callback's type should be:
13780 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13781 Return true if the attributes are present and valid, otherwise,
13782 return false. */
13783
13784template <typename Callback>
13785static bool
13786dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13787 Callback &&callback)
13788{
976ca316
SM
13789 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13790 struct objfile *objfile = per_objfile->objfile;
43988095 13791 bfd *obfd = objfile->obfd;
43988095 13792 /* Base address selection entry. */
2b24b6e4 13793 gdb::optional<CORE_ADDR> base;
43988095 13794 const gdb_byte *buffer;
43988095
JK
13795 CORE_ADDR baseaddr;
13796 bool overflow = false;
13797
43988095
JK
13798 base = cu->base_address;
13799
976ca316
SM
13800 per_objfile->per_bfd->rnglists.read (objfile);
13801 if (offset >= per_objfile->per_bfd->rnglists.size)
43988095 13802 {
b98664d3 13803 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13804 offset);
13805 return false;
13806 }
976ca316 13807 buffer = per_objfile->per_bfd->rnglists.buffer + offset;
43988095 13808
b3b3bada 13809 baseaddr = objfile->text_section_offset ();
43988095
JK
13810
13811 while (1)
13812 {
7814882a
JK
13813 /* Initialize it due to a false compiler warning. */
13814 CORE_ADDR range_beginning = 0, range_end = 0;
976ca316
SM
13815 const gdb_byte *buf_end = (per_objfile->per_bfd->rnglists.buffer
13816 + per_objfile->per_bfd->rnglists.size);
43988095
JK
13817 unsigned int bytes_read;
13818
13819 if (buffer == buf_end)
13820 {
13821 overflow = true;
13822 break;
13823 }
13824 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13825 switch (rlet)
13826 {
13827 case DW_RLE_end_of_list:
13828 break;
13829 case DW_RLE_base_address:
13830 if (buffer + cu->header.addr_size > buf_end)
13831 {
13832 overflow = true;
13833 break;
13834 }
c8a7a66f 13835 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13836 buffer += bytes_read;
13837 break;
13838 case DW_RLE_start_length:
13839 if (buffer + cu->header.addr_size > buf_end)
13840 {
13841 overflow = true;
13842 break;
13843 }
c8a7a66f
TT
13844 range_beginning = cu->header.read_address (obfd, buffer,
13845 &bytes_read);
43988095
JK
13846 buffer += bytes_read;
13847 range_end = (range_beginning
13848 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13849 buffer += bytes_read;
13850 if (buffer > buf_end)
13851 {
13852 overflow = true;
13853 break;
13854 }
13855 break;
13856 case DW_RLE_offset_pair:
13857 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13858 buffer += bytes_read;
13859 if (buffer > buf_end)
13860 {
13861 overflow = true;
13862 break;
13863 }
13864 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13865 buffer += bytes_read;
13866 if (buffer > buf_end)
13867 {
13868 overflow = true;
13869 break;
13870 }
13871 break;
13872 case DW_RLE_start_end:
13873 if (buffer + 2 * cu->header.addr_size > buf_end)
13874 {
13875 overflow = true;
13876 break;
13877 }
c8a7a66f
TT
13878 range_beginning = cu->header.read_address (obfd, buffer,
13879 &bytes_read);
43988095 13880 buffer += bytes_read;
c8a7a66f 13881 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13882 buffer += bytes_read;
13883 break;
13884 default:
b98664d3 13885 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13886 return false;
13887 }
13888 if (rlet == DW_RLE_end_of_list || overflow)
13889 break;
13890 if (rlet == DW_RLE_base_address)
13891 continue;
13892
2b24b6e4 13893 if (!base.has_value ())
43988095
JK
13894 {
13895 /* We have no valid base address for the ranges
13896 data. */
b98664d3 13897 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13898 return false;
13899 }
13900
13901 if (range_beginning > range_end)
13902 {
13903 /* Inverted range entries are invalid. */
b98664d3 13904 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13905 return false;
13906 }
13907
13908 /* Empty range entries have no effect. */
13909 if (range_beginning == range_end)
13910 continue;
13911
2b24b6e4
TT
13912 range_beginning += *base;
13913 range_end += *base;
43988095
JK
13914
13915 /* A not-uncommon case of bad debug info.
13916 Don't pollute the addrmap with bad data. */
13917 if (range_beginning + baseaddr == 0
976ca316 13918 && !per_objfile->per_bfd->has_section_at_zero)
43988095 13919 {
b98664d3 13920 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13921 " [in module %s]"), objfile_name (objfile));
13922 continue;
13923 }
13924
13925 callback (range_beginning, range_end);
13926 }
13927
13928 if (overflow)
13929 {
b98664d3 13930 complaint (_("Offset %d is not terminated "
43988095
JK
13931 "for DW_AT_ranges attribute"),
13932 offset);
13933 return false;
13934 }
13935
13936 return true;
13937}
13938
13939/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13940 Callback's type should be:
13941 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13942 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13943
43988095 13944template <typename Callback>
43039443 13945static int
5f46c5a5 13946dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13947 Callback &&callback)
43039443 13948{
5e22e966
SM
13949 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13950 struct objfile *objfile = per_objfile->objfile;
43039443
JK
13951 struct comp_unit_head *cu_header = &cu->header;
13952 bfd *obfd = objfile->obfd;
13953 unsigned int addr_size = cu_header->addr_size;
13954 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13955 /* Base address selection entry. */
2b24b6e4 13956 gdb::optional<CORE_ADDR> base;
43039443 13957 unsigned int dummy;
d521ce57 13958 const gdb_byte *buffer;
ff013f42 13959 CORE_ADDR baseaddr;
43039443 13960
43988095
JK
13961 if (cu_header->version >= 5)
13962 return dwarf2_rnglists_process (offset, cu, callback);
13963
d00adf39 13964 base = cu->base_address;
43039443 13965
5e22e966
SM
13966 per_objfile->per_bfd->ranges.read (objfile);
13967 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 13968 {
b98664d3 13969 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13970 offset);
13971 return 0;
13972 }
5e22e966 13973 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 13974
b3b3bada 13975 baseaddr = objfile->text_section_offset ();
ff013f42 13976
43039443
JK
13977 while (1)
13978 {
13979 CORE_ADDR range_beginning, range_end;
13980
c8a7a66f 13981 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13982 buffer += addr_size;
c8a7a66f 13983 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13984 buffer += addr_size;
13985 offset += 2 * addr_size;
13986
13987 /* An end of list marker is a pair of zero addresses. */
13988 if (range_beginning == 0 && range_end == 0)
13989 /* Found the end of list entry. */
13990 break;
13991
13992 /* Each base address selection entry is a pair of 2 values.
13993 The first is the largest possible address, the second is
13994 the base address. Check for a base address here. */
13995 if ((range_beginning & mask) == mask)
13996 {
28d2bfb9
AB
13997 /* If we found the largest possible address, then we already
13998 have the base address in range_end. */
13999 base = range_end;
43039443
JK
14000 continue;
14001 }
14002
2b24b6e4 14003 if (!base.has_value ())
43039443
JK
14004 {
14005 /* We have no valid base address for the ranges
14006 data. */
b98664d3 14007 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14008 return 0;
14009 }
14010
9277c30c
UW
14011 if (range_beginning > range_end)
14012 {
14013 /* Inverted range entries are invalid. */
b98664d3 14014 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14015 return 0;
14016 }
14017
14018 /* Empty range entries have no effect. */
14019 if (range_beginning == range_end)
14020 continue;
14021
2b24b6e4
TT
14022 range_beginning += *base;
14023 range_end += *base;
43039443 14024
01093045
DE
14025 /* A not-uncommon case of bad debug info.
14026 Don't pollute the addrmap with bad data. */
14027 if (range_beginning + baseaddr == 0
5e22e966 14028 && !per_objfile->per_bfd->has_section_at_zero)
01093045 14029 {
b98664d3 14030 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14031 " [in module %s]"), objfile_name (objfile));
01093045
DE
14032 continue;
14033 }
14034
5f46c5a5
JK
14035 callback (range_beginning, range_end);
14036 }
14037
14038 return 1;
14039}
14040
14041/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14042 Return 1 if the attributes are present and valid, otherwise, return 0.
14043 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14044
14045static int
14046dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14047 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 14048 dwarf2_psymtab *ranges_pst)
5f46c5a5 14049{
5e22e966 14050 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14051 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 14052 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
14053 int low_set = 0;
14054 CORE_ADDR low = 0;
14055 CORE_ADDR high = 0;
14056 int retval;
14057
14058 retval = dwarf2_ranges_process (offset, cu,
14059 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14060 {
9277c30c 14061 if (ranges_pst != NULL)
3e29f34a
MR
14062 {
14063 CORE_ADDR lowpc;
14064 CORE_ADDR highpc;
14065
79748972
TT
14066 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14067 range_beginning + baseaddr)
14068 - baseaddr);
14069 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14070 range_end + baseaddr)
14071 - baseaddr);
d320c2b5
TT
14072 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14073 lowpc, highpc - 1, ranges_pst);
3e29f34a 14074 }
ff013f42 14075
43039443
JK
14076 /* FIXME: This is recording everything as a low-high
14077 segment of consecutive addresses. We should have a
14078 data structure for discontiguous block ranges
14079 instead. */
14080 if (! low_set)
14081 {
14082 low = range_beginning;
14083 high = range_end;
14084 low_set = 1;
14085 }
14086 else
14087 {
14088 if (range_beginning < low)
14089 low = range_beginning;
14090 if (range_end > high)
14091 high = range_end;
14092 }
5f46c5a5
JK
14093 });
14094 if (!retval)
14095 return 0;
43039443
JK
14096
14097 if (! low_set)
14098 /* If the first entry is an end-of-list marker, the range
14099 describes an empty scope, i.e. no instructions. */
14100 return 0;
14101
14102 if (low_return)
14103 *low_return = low;
14104 if (high_return)
14105 *high_return = high;
14106 return 1;
14107}
14108
3a2b436a
JK
14109/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14110 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14111 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14112
3a2b436a 14113static enum pc_bounds_kind
af34e669 14114dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14115 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14116 dwarf2_psymtab *pst)
c906108c 14117{
976ca316 14118 dwarf2_per_objfile *per_objfile = cu->per_objfile;
c906108c 14119 struct attribute *attr;
91da1414 14120 struct attribute *attr_high;
af34e669
DJ
14121 CORE_ADDR low = 0;
14122 CORE_ADDR high = 0;
e385593e 14123 enum pc_bounds_kind ret;
c906108c 14124
91da1414
MW
14125 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14126 if (attr_high)
af34e669 14127 {
e142c38c 14128 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14129 if (attr != nullptr)
91da1414 14130 {
cd6c91b4
TT
14131 low = attr->value_as_address ();
14132 high = attr_high->value_as_address ();
14133 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14134 high += low;
91da1414 14135 }
af34e669
DJ
14136 else
14137 /* Found high w/o low attribute. */
e385593e 14138 return PC_BOUNDS_INVALID;
af34e669
DJ
14139
14140 /* Found consecutive range of addresses. */
3a2b436a 14141 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14142 }
c906108c 14143 else
af34e669 14144 {
e142c38c 14145 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14146 if (attr != NULL)
14147 {
18a8505e 14148 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14149 We take advantage of the fact that DW_AT_ranges does not appear
14150 in DW_TAG_compile_unit of DWO files. */
14151 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14152 unsigned int ranges_offset = (DW_UNSND (attr)
14153 + (need_ranges_base
14154 ? cu->ranges_base
14155 : 0));
2e3cf129 14156
af34e669 14157 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14158 .debug_ranges section. */
2e3cf129 14159 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14160 return PC_BOUNDS_INVALID;
43039443 14161 /* Found discontinuous range of addresses. */
3a2b436a 14162 ret = PC_BOUNDS_RANGES;
af34e669 14163 }
e385593e
JK
14164 else
14165 return PC_BOUNDS_NOT_PRESENT;
af34e669 14166 }
c906108c 14167
48fbe735 14168 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14169 if (high <= low)
e385593e 14170 return PC_BOUNDS_INVALID;
c906108c
SS
14171
14172 /* When using the GNU linker, .gnu.linkonce. sections are used to
14173 eliminate duplicate copies of functions and vtables and such.
14174 The linker will arbitrarily choose one and discard the others.
14175 The AT_*_pc values for such functions refer to local labels in
14176 these sections. If the section from that file was discarded, the
14177 labels are not in the output, so the relocs get a value of 0.
14178 If this is a discarded function, mark the pc bounds as invalid,
14179 so that GDB will ignore it. */
976ca316 14180 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
e385593e 14181 return PC_BOUNDS_INVALID;
c906108c
SS
14182
14183 *lowpc = low;
96408a79
SA
14184 if (highpc)
14185 *highpc = high;
af34e669 14186 return ret;
c906108c
SS
14187}
14188
b084d499
JB
14189/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14190 its low and high PC addresses. Do nothing if these addresses could not
14191 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14192 and HIGHPC to the high address if greater than HIGHPC. */
14193
14194static void
14195dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14196 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14197 struct dwarf2_cu *cu)
14198{
14199 CORE_ADDR low, high;
14200 struct die_info *child = die->child;
14201
e385593e 14202 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14203 {
325fac50
PA
14204 *lowpc = std::min (*lowpc, low);
14205 *highpc = std::max (*highpc, high);
b084d499
JB
14206 }
14207
14208 /* If the language does not allow nested subprograms (either inside
14209 subprograms or lexical blocks), we're done. */
14210 if (cu->language != language_ada)
14211 return;
6e70227d 14212
b084d499
JB
14213 /* Check all the children of the given DIE. If it contains nested
14214 subprograms, then check their pc bounds. Likewise, we need to
14215 check lexical blocks as well, as they may also contain subprogram
14216 definitions. */
14217 while (child && child->tag)
14218 {
14219 if (child->tag == DW_TAG_subprogram
14220 || child->tag == DW_TAG_lexical_block)
14221 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14222 child = child->sibling;
b084d499
JB
14223 }
14224}
14225
fae299cd
DC
14226/* Get the low and high pc's represented by the scope DIE, and store
14227 them in *LOWPC and *HIGHPC. If the correct values can't be
14228 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14229
14230static void
14231get_scope_pc_bounds (struct die_info *die,
14232 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14233 struct dwarf2_cu *cu)
14234{
14235 CORE_ADDR best_low = (CORE_ADDR) -1;
14236 CORE_ADDR best_high = (CORE_ADDR) 0;
14237 CORE_ADDR current_low, current_high;
14238
3a2b436a 14239 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14240 >= PC_BOUNDS_RANGES)
fae299cd
DC
14241 {
14242 best_low = current_low;
14243 best_high = current_high;
14244 }
14245 else
14246 {
14247 struct die_info *child = die->child;
14248
14249 while (child && child->tag)
14250 {
14251 switch (child->tag) {
14252 case DW_TAG_subprogram:
b084d499 14253 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14254 break;
14255 case DW_TAG_namespace:
f55ee35c 14256 case DW_TAG_module:
fae299cd
DC
14257 /* FIXME: carlton/2004-01-16: Should we do this for
14258 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14259 that current GCC's always emit the DIEs corresponding
14260 to definitions of methods of classes as children of a
14261 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14262 the DIEs giving the declarations, which could be
14263 anywhere). But I don't see any reason why the
14264 standards says that they have to be there. */
14265 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14266
14267 if (current_low != ((CORE_ADDR) -1))
14268 {
325fac50
PA
14269 best_low = std::min (best_low, current_low);
14270 best_high = std::max (best_high, current_high);
fae299cd
DC
14271 }
14272 break;
14273 default:
0963b4bd 14274 /* Ignore. */
fae299cd
DC
14275 break;
14276 }
14277
436c571c 14278 child = child->sibling;
fae299cd
DC
14279 }
14280 }
14281
14282 *lowpc = best_low;
14283 *highpc = best_high;
14284}
14285
801e3a5b
JB
14286/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14287 in DIE. */
380bca97 14288
801e3a5b
JB
14289static void
14290dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14291 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14292{
5e22e966 14293 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14294 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14295 struct attribute *attr;
91da1414 14296 struct attribute *attr_high;
801e3a5b 14297
91da1414
MW
14298 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14299 if (attr_high)
801e3a5b 14300 {
801e3a5b 14301 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14302 if (attr != nullptr)
801e3a5b 14303 {
cd6c91b4
TT
14304 CORE_ADDR low = attr->value_as_address ();
14305 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 14306
cd6c91b4 14307 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14308 high += low;
9a619af0 14309
3e29f34a
MR
14310 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14311 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14312 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14313 }
14314 }
14315
14316 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14317 if (attr != nullptr)
801e3a5b 14318 {
18a8505e 14319 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14320 We take advantage of the fact that DW_AT_ranges does not appear
14321 in DW_TAG_compile_unit of DWO files. */
14322 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14323
14324 /* The value of the DW_AT_ranges attribute is the offset of the
14325 address range list in the .debug_ranges section. */
ab435259
DE
14326 unsigned long offset = (DW_UNSND (attr)
14327 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14328
2d5f09ec 14329 std::vector<blockrange> blockvec;
5f46c5a5
JK
14330 dwarf2_ranges_process (offset, cu,
14331 [&] (CORE_ADDR start, CORE_ADDR end)
14332 {
58fdfd2c
JK
14333 start += baseaddr;
14334 end += baseaddr;
5f46c5a5
JK
14335 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14336 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14337 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14338 blockvec.emplace_back (start, end);
5f46c5a5 14339 });
2d5f09ec
KB
14340
14341 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14342 }
14343}
14344
685b1105
JK
14345/* Check whether the producer field indicates either of GCC < 4.6, or the
14346 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14347
685b1105
JK
14348static void
14349check_producer (struct dwarf2_cu *cu)
60d5a603 14350{
38360086 14351 int major, minor;
60d5a603
JK
14352
14353 if (cu->producer == NULL)
14354 {
14355 /* For unknown compilers expect their behavior is DWARF version
14356 compliant.
14357
14358 GCC started to support .debug_types sections by -gdwarf-4 since
14359 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14360 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14361 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14362 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14363 }
b1ffba5a 14364 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14365 {
38360086
MW
14366 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14367 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14368 }
5230b05a 14369 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14370 {
14371 cu->producer_is_icc = true;
14372 cu->producer_is_icc_lt_14 = major < 14;
14373 }
c258c396
JD
14374 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14375 cu->producer_is_codewarrior = true;
685b1105
JK
14376 else
14377 {
14378 /* For other non-GCC compilers, expect their behavior is DWARF version
14379 compliant. */
60d5a603
JK
14380 }
14381
9068261f 14382 cu->checked_producer = true;
685b1105 14383}
ba919b58 14384
685b1105
JK
14385/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14386 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14387 during 4.6.0 experimental. */
14388
9068261f 14389static bool
685b1105
JK
14390producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14391{
14392 if (!cu->checked_producer)
14393 check_producer (cu);
14394
14395 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14396}
14397
c258c396
JD
14398
14399/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14400 with incorrect is_stmt attributes. */
14401
14402static bool
14403producer_is_codewarrior (struct dwarf2_cu *cu)
14404{
14405 if (!cu->checked_producer)
14406 check_producer (cu);
14407
14408 return cu->producer_is_codewarrior;
14409}
14410
405feb71 14411/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14412 DW_AT_accessibility. */
14413
14414static enum dwarf_access_attribute
14415dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14416{
14417 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14418 {
14419 /* The default DWARF 2 accessibility for members is public, the default
14420 accessibility for inheritance is private. */
14421
14422 if (die->tag != DW_TAG_inheritance)
14423 return DW_ACCESS_public;
14424 else
14425 return DW_ACCESS_private;
14426 }
14427 else
14428 {
14429 /* DWARF 3+ defines the default accessibility a different way. The same
14430 rules apply now for DW_TAG_inheritance as for the members and it only
14431 depends on the container kind. */
14432
14433 if (die->parent->tag == DW_TAG_class_type)
14434 return DW_ACCESS_private;
14435 else
14436 return DW_ACCESS_public;
14437 }
14438}
14439
74ac6d43
TT
14440/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14441 offset. If the attribute was not found return 0, otherwise return
14442 1. If it was found but could not properly be handled, set *OFFSET
14443 to 0. */
14444
14445static int
14446handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14447 LONGEST *offset)
14448{
14449 struct attribute *attr;
14450
14451 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14452 if (attr != NULL)
14453 {
14454 *offset = 0;
14455
14456 /* Note that we do not check for a section offset first here.
14457 This is because DW_AT_data_member_location is new in DWARF 4,
14458 so if we see it, we can assume that a constant form is really
14459 a constant and not a section offset. */
cd6c91b4 14460 if (attr->form_is_constant ())
0826b30a 14461 *offset = attr->constant_value (0);
cd6c91b4 14462 else if (attr->form_is_section_offset ())
74ac6d43 14463 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14464 else if (attr->form_is_block ())
74ac6d43
TT
14465 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14466 else
14467 dwarf2_complex_location_expr_complaint ();
14468
14469 return 1;
14470 }
14471
14472 return 0;
14473}
14474
7d79de9a
TT
14475/* Look for DW_AT_data_member_location and store the results in FIELD. */
14476
14477static void
14478handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14479 struct field *field)
14480{
14481 struct attribute *attr;
14482
14483 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14484 if (attr != NULL)
14485 {
14486 if (attr->form_is_constant ())
14487 {
14488 LONGEST offset = attr->constant_value (0);
14489 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14490 }
14491 else if (attr->form_is_section_offset ())
14492 dwarf2_complex_location_expr_complaint ();
14493 else if (attr->form_is_block ())
14494 {
14495 bool handled;
14496 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14497 if (handled)
14498 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14499 else
14500 {
5e22e966
SM
14501 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14502 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14503 struct dwarf2_locexpr_baton *dlbaton
14504 = XOBNEW (&objfile->objfile_obstack,
14505 struct dwarf2_locexpr_baton);
14506 dlbaton->data = DW_BLOCK (attr)->data;
14507 dlbaton->size = DW_BLOCK (attr)->size;
14508 /* When using this baton, we want to compute the address
14509 of the field, not the value. This is why
14510 is_reference is set to false here. */
14511 dlbaton->is_reference = false;
5e22e966 14512 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14513 dlbaton->per_cu = cu->per_cu;
14514
14515 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14516 }
14517 }
14518 else
14519 dwarf2_complex_location_expr_complaint ();
14520 }
14521}
14522
c906108c
SS
14523/* Add an aggregate field to the field list. */
14524
14525static void
107d2387 14526dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14527 struct dwarf2_cu *cu)
6e70227d 14528{
5e22e966 14529 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14530 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14531 struct nextfield *new_field;
14532 struct attribute *attr;
14533 struct field *fp;
15d034d0 14534 const char *fieldname = "";
c906108c 14535
7d0ccb61
DJ
14536 if (die->tag == DW_TAG_inheritance)
14537 {
be2daae6
TT
14538 fip->baseclasses.emplace_back ();
14539 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14540 }
14541 else
14542 {
be2daae6
TT
14543 fip->fields.emplace_back ();
14544 new_field = &fip->fields.back ();
7d0ccb61 14545 }
be2daae6 14546
9c6a1327
TT
14547 new_field->offset = die->sect_off;
14548
e142c38c 14549 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14550 if (attr != nullptr)
c906108c 14551 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14552 else
14553 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14554 if (new_field->accessibility != DW_ACCESS_public)
14555 fip->non_public_fields = 1;
60d5a603 14556
e142c38c 14557 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14558 if (attr != nullptr)
c906108c 14559 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14560 else
14561 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14562
14563 fp = &new_field->field;
a9a9bd0f 14564
e142c38c 14565 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14566 {
a9a9bd0f 14567 /* Data member other than a C++ static data member. */
6e70227d 14568
c906108c 14569 /* Get type of field. */
5d14b6e5 14570 fp->set_type (die_type (die, cu));
c906108c 14571
d6a843b5 14572 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14573
c906108c 14574 /* Get bit size of field (zero if none). */
e142c38c 14575 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14576 if (attr != nullptr)
c906108c
SS
14577 {
14578 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14579 }
14580 else
14581 {
14582 FIELD_BITSIZE (*fp) = 0;
14583 }
14584
14585 /* Get bit offset of field. */
7d79de9a 14586 handle_data_member_location (die, cu, fp);
e142c38c 14587 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14588 if (attr != nullptr)
c906108c 14589 {
d5a22e77 14590 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14591 {
14592 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14593 additional bit offset from the MSB of the containing
14594 anonymous object to the MSB of the field. We don't
14595 have to do anything special since we don't need to
14596 know the size of the anonymous object. */
f41f5e61 14597 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14598 }
14599 else
14600 {
14601 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14602 MSB of the anonymous object, subtract off the number of
14603 bits from the MSB of the field to the MSB of the
14604 object, and then subtract off the number of bits of
14605 the field itself. The result is the bit offset of
14606 the LSB of the field. */
c906108c
SS
14607 int anonymous_size;
14608 int bit_offset = DW_UNSND (attr);
14609
e142c38c 14610 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14611 if (attr != nullptr)
c906108c
SS
14612 {
14613 /* The size of the anonymous object containing
14614 the bit field is explicit, so use the
14615 indicated size (in bytes). */
14616 anonymous_size = DW_UNSND (attr);
14617 }
14618 else
14619 {
14620 /* The size of the anonymous object containing
14621 the bit field must be inferred from the type
14622 attribute of the data member containing the
14623 bit field. */
5d14b6e5 14624 anonymous_size = TYPE_LENGTH (fp->type ());
c906108c 14625 }
f41f5e61
PA
14626 SET_FIELD_BITPOS (*fp,
14627 (FIELD_BITPOS (*fp)
14628 + anonymous_size * bits_per_byte
14629 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14630 }
14631 }
da5b30da
AA
14632 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14633 if (attr != NULL)
14634 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14635 + attr->constant_value (0)));
c906108c
SS
14636
14637 /* Get name of field. */
39cbfefa
DJ
14638 fieldname = dwarf2_name (die, cu);
14639 if (fieldname == NULL)
14640 fieldname = "";
d8151005
DJ
14641
14642 /* The name is already allocated along with this objfile, so we don't
14643 need to duplicate it for the type. */
14644 fp->name = fieldname;
c906108c
SS
14645
14646 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14647 pointer or virtual base class pointer) to private. */
e142c38c 14648 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14649 {
d48cc9dd 14650 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14651 new_field->accessibility = DW_ACCESS_private;
14652 fip->non_public_fields = 1;
14653 }
14654 }
a9a9bd0f 14655 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14656 {
a9a9bd0f
DC
14657 /* C++ static member. */
14658
14659 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14660 is a declaration, but all versions of G++ as of this writing
14661 (so through at least 3.2.1) incorrectly generate
14662 DW_TAG_variable tags. */
6e70227d 14663
ff355380 14664 const char *physname;
c906108c 14665
a9a9bd0f 14666 /* Get name of field. */
39cbfefa
DJ
14667 fieldname = dwarf2_name (die, cu);
14668 if (fieldname == NULL)
c906108c
SS
14669 return;
14670
254e6b9e 14671 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14672 if (attr
14673 /* Only create a symbol if this is an external value.
14674 new_symbol checks this and puts the value in the global symbol
14675 table, which we want. If it is not external, new_symbol
14676 will try to put the value in cu->list_in_scope which is wrong. */
14677 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14678 {
14679 /* A static const member, not much different than an enum as far as
14680 we're concerned, except that we can support more types. */
14681 new_symbol (die, NULL, cu);
14682 }
14683
2df3850c 14684 /* Get physical name. */
ff355380 14685 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14686
d8151005
DJ
14687 /* The name is already allocated along with this objfile, so we don't
14688 need to duplicate it for the type. */
14689 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
5d14b6e5 14690 fp->set_type (die_type (die, cu));
d8151005 14691 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14692 }
14693 else if (die->tag == DW_TAG_inheritance)
14694 {
74ac6d43 14695 /* C++ base class field. */
7d79de9a 14696 handle_data_member_location (die, cu, fp);
c906108c 14697 FIELD_BITSIZE (*fp) = 0;
5d14b6e5
SM
14698 fp->set_type (die_type (die, cu));
14699 FIELD_NAME (*fp) = fp->type ()->name ();
c906108c 14700 }
2ddeaf8a
TT
14701 else
14702 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14703}
14704
883fd55a
KS
14705/* Can the type given by DIE define another type? */
14706
14707static bool
14708type_can_define_types (const struct die_info *die)
14709{
14710 switch (die->tag)
14711 {
14712 case DW_TAG_typedef:
14713 case DW_TAG_class_type:
14714 case DW_TAG_structure_type:
14715 case DW_TAG_union_type:
14716 case DW_TAG_enumeration_type:
14717 return true;
14718
14719 default:
14720 return false;
14721 }
14722}
14723
14724/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14725
14726static void
883fd55a
KS
14727dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14728 struct dwarf2_cu *cu)
6e70227d 14729{
be2daae6
TT
14730 struct decl_field fp;
14731 memset (&fp, 0, sizeof (fp));
98751a41 14732
883fd55a 14733 gdb_assert (type_can_define_types (die));
98751a41 14734
883fd55a 14735 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14736 fp.name = dwarf2_name (die, cu);
14737 fp.type = read_type_die (die, cu);
98751a41 14738
c191a687
KS
14739 /* Save accessibility. */
14740 enum dwarf_access_attribute accessibility;
14741 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14742 if (attr != NULL)
14743 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14744 else
14745 accessibility = dwarf2_default_access_attribute (die, cu);
14746 switch (accessibility)
14747 {
14748 case DW_ACCESS_public:
14749 /* The assumed value if neither private nor protected. */
14750 break;
14751 case DW_ACCESS_private:
be2daae6 14752 fp.is_private = 1;
c191a687
KS
14753 break;
14754 case DW_ACCESS_protected:
be2daae6 14755 fp.is_protected = 1;
c191a687
KS
14756 break;
14757 default:
b98664d3 14758 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14759 }
14760
883fd55a 14761 if (die->tag == DW_TAG_typedef)
be2daae6 14762 fip->typedef_field_list.push_back (fp);
883fd55a 14763 else
be2daae6 14764 fip->nested_types_list.push_back (fp);
98751a41
JK
14765}
14766
9c6a1327
TT
14767/* A convenience typedef that's used when finding the discriminant
14768 field for a variant part. */
1b95cdb7
SM
14769typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14770 offset_map_type;
9c6a1327
TT
14771
14772/* Compute the discriminant range for a given variant. OBSTACK is
14773 where the results will be stored. VARIANT is the variant to
14774 process. IS_UNSIGNED indicates whether the discriminant is signed
14775 or unsigned. */
14776
14777static const gdb::array_view<discriminant_range>
14778convert_variant_range (struct obstack *obstack, const variant_field &variant,
14779 bool is_unsigned)
14780{
14781 std::vector<discriminant_range> ranges;
14782
14783 if (variant.default_branch)
14784 return {};
14785
14786 if (variant.discr_list_data == nullptr)
14787 {
14788 discriminant_range r
14789 = {variant.discriminant_value, variant.discriminant_value};
14790 ranges.push_back (r);
14791 }
14792 else
14793 {
14794 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14795 variant.discr_list_data->size);
14796 while (!data.empty ())
14797 {
14798 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14799 {
14800 complaint (_("invalid discriminant marker: %d"), data[0]);
14801 break;
14802 }
14803 bool is_range = data[0] == DW_DSC_range;
14804 data = data.slice (1);
14805
14806 ULONGEST low, high;
14807 unsigned int bytes_read;
14808
14809 if (data.empty ())
14810 {
14811 complaint (_("DW_AT_discr_list missing low value"));
14812 break;
14813 }
14814 if (is_unsigned)
14815 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14816 else
14817 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14818 &bytes_read);
14819 data = data.slice (bytes_read);
14820
14821 if (is_range)
14822 {
14823 if (data.empty ())
14824 {
14825 complaint (_("DW_AT_discr_list missing high value"));
14826 break;
14827 }
14828 if (is_unsigned)
14829 high = read_unsigned_leb128 (nullptr, data.data (),
14830 &bytes_read);
14831 else
14832 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14833 &bytes_read);
14834 data = data.slice (bytes_read);
14835 }
14836 else
14837 high = low;
14838
14839 ranges.push_back ({ low, high });
14840 }
14841 }
14842
14843 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14844 ranges.size ());
14845 std::copy (ranges.begin (), ranges.end (), result);
14846 return gdb::array_view<discriminant_range> (result, ranges.size ());
14847}
14848
14849static const gdb::array_view<variant_part> create_variant_parts
14850 (struct obstack *obstack,
14851 const offset_map_type &offset_map,
14852 struct field_info *fi,
14853 const std::vector<variant_part_builder> &variant_parts);
14854
14855/* Fill in a "struct variant" for a given variant field. RESULT is
14856 the variant to fill in. OBSTACK is where any needed allocations
14857 will be done. OFFSET_MAP holds the mapping from section offsets to
14858 fields for the type. FI describes the fields of the type we're
14859 processing. FIELD is the variant field we're converting. */
14860
14861static void
14862create_one_variant (variant &result, struct obstack *obstack,
14863 const offset_map_type &offset_map,
14864 struct field_info *fi, const variant_field &field)
14865{
14866 result.discriminants = convert_variant_range (obstack, field, false);
14867 result.first_field = field.first_field + fi->baseclasses.size ();
14868 result.last_field = field.last_field + fi->baseclasses.size ();
14869 result.parts = create_variant_parts (obstack, offset_map, fi,
14870 field.variant_parts);
14871}
14872
14873/* Fill in a "struct variant_part" for a given variant part. RESULT
14874 is the variant part to fill in. OBSTACK is where any needed
14875 allocations will be done. OFFSET_MAP holds the mapping from
14876 section offsets to fields for the type. FI describes the fields of
14877 the type we're processing. BUILDER is the variant part to be
14878 converted. */
14879
14880static void
14881create_one_variant_part (variant_part &result,
14882 struct obstack *obstack,
14883 const offset_map_type &offset_map,
14884 struct field_info *fi,
14885 const variant_part_builder &builder)
14886{
14887 auto iter = offset_map.find (builder.discriminant_offset);
14888 if (iter == offset_map.end ())
14889 {
14890 result.discriminant_index = -1;
14891 /* Doesn't matter. */
14892 result.is_unsigned = false;
14893 }
14894 else
14895 {
14896 result.discriminant_index = iter->second;
14897 result.is_unsigned
b6cdac4b 14898 = TYPE_UNSIGNED (fi->fields[result.discriminant_index].field.type ());
9c6a1327
TT
14899 }
14900
14901 size_t n = builder.variants.size ();
14902 variant *output = new (obstack) variant[n];
14903 for (size_t i = 0; i < n; ++i)
14904 create_one_variant (output[i], obstack, offset_map, fi,
14905 builder.variants[i]);
14906
14907 result.variants = gdb::array_view<variant> (output, n);
14908}
14909
14910/* Create a vector of variant parts that can be attached to a type.
14911 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14912 holds the mapping from section offsets to fields for the type. FI
14913 describes the fields of the type we're processing. VARIANT_PARTS
14914 is the vector to convert. */
14915
14916static const gdb::array_view<variant_part>
14917create_variant_parts (struct obstack *obstack,
14918 const offset_map_type &offset_map,
14919 struct field_info *fi,
14920 const std::vector<variant_part_builder> &variant_parts)
14921{
14922 if (variant_parts.empty ())
14923 return {};
14924
14925 size_t n = variant_parts.size ();
14926 variant_part *result = new (obstack) variant_part[n];
14927 for (size_t i = 0; i < n; ++i)
14928 create_one_variant_part (result[i], obstack, offset_map, fi,
14929 variant_parts[i]);
14930
14931 return gdb::array_view<variant_part> (result, n);
14932}
14933
14934/* Compute the variant part vector for FIP, attaching it to TYPE when
14935 done. */
14936
14937static void
14938add_variant_property (struct field_info *fip, struct type *type,
14939 struct dwarf2_cu *cu)
14940{
14941 /* Map section offsets of fields to their field index. Note the
14942 field index here does not take the number of baseclasses into
14943 account. */
14944 offset_map_type offset_map;
14945 for (int i = 0; i < fip->fields.size (); ++i)
14946 offset_map[fip->fields[i].offset] = i;
14947
5e22e966 14948 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
14949 gdb::array_view<variant_part> parts
14950 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14951 fip->variant_parts);
14952
14953 struct dynamic_prop prop;
14954 prop.kind = PROP_VARIANT_PARTS;
14955 prop.data.variant_parts
14956 = ((gdb::array_view<variant_part> *)
14957 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14958
5c54719c 14959 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
14960}
14961
c906108c
SS
14962/* Create the vector of fields, and attach it to the type. */
14963
14964static void
fba45db2 14965dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14966 struct dwarf2_cu *cu)
c906108c 14967{
317f7127 14968 int nfields = fip->nfields ();
c906108c
SS
14969
14970 /* Record the field count, allocate space for the array of fields,
14971 and create blank accessibility bitfields if necessary. */
5e33d5f4 14972 type->set_num_fields (nfields);
3cabb6b0
SM
14973 type->set_fields
14974 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 14975
b4ba55a1 14976 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14977 {
14978 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14979
14980 TYPE_FIELD_PRIVATE_BITS (type) =
14981 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14982 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14983
14984 TYPE_FIELD_PROTECTED_BITS (type) =
14985 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14986 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14987
774b6a14
TT
14988 TYPE_FIELD_IGNORE_BITS (type) =
14989 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14990 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14991 }
14992
14993 /* If the type has baseclasses, allocate and clear a bit vector for
14994 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14995 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14996 {
be2daae6 14997 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14998 unsigned char *pointer;
c906108c
SS
14999
15000 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15001 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15002 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15003 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15004 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15005 }
15006
9c6a1327
TT
15007 if (!fip->variant_parts.empty ())
15008 add_variant_property (fip, type, cu);
2ddeaf8a 15009
be2daae6
TT
15010 /* Copy the saved-up fields into the field vector. */
15011 for (int i = 0; i < nfields; ++i)
c906108c 15012 {
be2daae6
TT
15013 struct nextfield &field
15014 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15015 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15016
ceacbf6e 15017 type->field (i) = field.field;
be2daae6 15018 switch (field.accessibility)
c906108c 15019 {
c5aa993b 15020 case DW_ACCESS_private:
b4ba55a1 15021 if (cu->language != language_ada)
be2daae6 15022 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15023 break;
c906108c 15024
c5aa993b 15025 case DW_ACCESS_protected:
b4ba55a1 15026 if (cu->language != language_ada)
be2daae6 15027 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15028 break;
c906108c 15029
c5aa993b
JM
15030 case DW_ACCESS_public:
15031 break;
c906108c 15032
c5aa993b
JM
15033 default:
15034 /* Unknown accessibility. Complain and treat it as public. */
15035 {
b98664d3 15036 complaint (_("unsupported accessibility %d"),
be2daae6 15037 field.accessibility);
c5aa993b
JM
15038 }
15039 break;
c906108c 15040 }
be2daae6 15041 if (i < fip->baseclasses.size ())
c906108c 15042 {
be2daae6 15043 switch (field.virtuality)
c906108c 15044 {
c5aa993b
JM
15045 case DW_VIRTUALITY_virtual:
15046 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15047 if (cu->language == language_ada)
a73c6dcd 15048 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15049 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15050 break;
c906108c
SS
15051 }
15052 }
c906108c
SS
15053 }
15054}
15055
7d27a96d
TT
15056/* Return true if this member function is a constructor, false
15057 otherwise. */
15058
15059static int
15060dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15061{
15062 const char *fieldname;
fe978cb0 15063 const char *type_name;
7d27a96d
TT
15064 int len;
15065
15066 if (die->parent == NULL)
15067 return 0;
15068
15069 if (die->parent->tag != DW_TAG_structure_type
15070 && die->parent->tag != DW_TAG_union_type
15071 && die->parent->tag != DW_TAG_class_type)
15072 return 0;
15073
15074 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15075 type_name = dwarf2_name (die->parent, cu);
15076 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15077 return 0;
15078
15079 len = strlen (fieldname);
fe978cb0
PA
15080 return (strncmp (fieldname, type_name, len) == 0
15081 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15082}
15083
e35000a7
TBA
15084/* Check if the given VALUE is a recognized enum
15085 dwarf_defaulted_attribute constant according to DWARF5 spec,
15086 Table 7.24. */
15087
15088static bool
15089is_valid_DW_AT_defaulted (ULONGEST value)
15090{
15091 switch (value)
15092 {
15093 case DW_DEFAULTED_no:
15094 case DW_DEFAULTED_in_class:
15095 case DW_DEFAULTED_out_of_class:
15096 return true;
15097 }
15098
3142e908 15099 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
15100 return false;
15101}
15102
c906108c
SS
15103/* Add a member function to the proper fieldlist. */
15104
15105static void
107d2387 15106dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15107 struct type *type, struct dwarf2_cu *cu)
c906108c 15108{
5e22e966 15109 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15110 struct attribute *attr;
c906108c 15111 int i;
be2daae6 15112 struct fnfieldlist *flp = nullptr;
c906108c 15113 struct fn_field *fnp;
15d034d0 15114 const char *fieldname;
f792889a 15115 struct type *this_type;
60d5a603 15116 enum dwarf_access_attribute accessibility;
c906108c 15117
b4ba55a1 15118 if (cu->language == language_ada)
a73c6dcd 15119 error (_("unexpected member function in Ada type"));
b4ba55a1 15120
2df3850c 15121 /* Get name of member function. */
39cbfefa
DJ
15122 fieldname = dwarf2_name (die, cu);
15123 if (fieldname == NULL)
2df3850c 15124 return;
c906108c 15125
c906108c 15126 /* Look up member function name in fieldlist. */
be2daae6 15127 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15128 {
27bfe10e 15129 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15130 {
15131 flp = &fip->fnfieldlists[i];
15132 break;
15133 }
c906108c
SS
15134 }
15135
be2daae6
TT
15136 /* Create a new fnfieldlist if necessary. */
15137 if (flp == nullptr)
c906108c 15138 {
be2daae6
TT
15139 fip->fnfieldlists.emplace_back ();
15140 flp = &fip->fnfieldlists.back ();
c906108c 15141 flp->name = fieldname;
be2daae6 15142 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15143 }
15144
be2daae6
TT
15145 /* Create a new member function field and add it to the vector of
15146 fnfieldlists. */
15147 flp->fnfields.emplace_back ();
15148 fnp = &flp->fnfields.back ();
3da10d80
KS
15149
15150 /* Delay processing of the physname until later. */
9c37b5ae 15151 if (cu->language == language_cplus)
be2daae6
TT
15152 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15153 die, cu);
3da10d80
KS
15154 else
15155 {
1d06ead6 15156 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15157 fnp->physname = physname ? physname : "";
15158 }
15159
c906108c 15160 fnp->type = alloc_type (objfile);
f792889a 15161 this_type = read_type_die (die, cu);
78134374 15162 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15163 {
1f704f76 15164 int nparams = this_type->num_fields ();
c906108c 15165
f792889a 15166 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15167 of the method itself (TYPE_CODE_METHOD). */
15168 smash_to_method_type (fnp->type, type,
f792889a 15169 TYPE_TARGET_TYPE (this_type),
80fc5e77 15170 this_type->fields (),
1f704f76 15171 this_type->num_fields (),
f792889a 15172 TYPE_VARARGS (this_type));
c906108c
SS
15173
15174 /* Handle static member functions.
c5aa993b 15175 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15176 member functions. G++ helps GDB by marking the first
15177 parameter for non-static member functions (which is the this
15178 pointer) as artificial. We obtain this information from
15179 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15180 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15181 fnp->voffset = VOFFSET_STATIC;
15182 }
15183 else
b98664d3 15184 complaint (_("member function type missing for '%s'"),
3da10d80 15185 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15186
15187 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15188 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15189 fnp->fcontext = die_containing_type (die, cu);
c906108c 15190
3e43a32a
MS
15191 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15192 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15193
15194 /* Get accessibility. */
e142c38c 15195 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15196 if (attr != nullptr)
aead7601 15197 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15198 else
15199 accessibility = dwarf2_default_access_attribute (die, cu);
15200 switch (accessibility)
c906108c 15201 {
60d5a603
JK
15202 case DW_ACCESS_private:
15203 fnp->is_private = 1;
15204 break;
15205 case DW_ACCESS_protected:
15206 fnp->is_protected = 1;
15207 break;
c906108c
SS
15208 }
15209
b02dede2 15210 /* Check for artificial methods. */
e142c38c 15211 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15212 if (attr && DW_UNSND (attr) != 0)
15213 fnp->is_artificial = 1;
15214
e35000a7
TBA
15215 /* Check for defaulted methods. */
15216 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15217 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15218 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15219
15220 /* Check for deleted methods. */
15221 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15222 if (attr != nullptr && DW_UNSND (attr) != 0)
15223 fnp->is_deleted = 1;
15224
7d27a96d
TT
15225 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15226
0d564a31 15227 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15228 function. For older versions of GCC, this is an offset in the
15229 appropriate virtual table, as specified by DW_AT_containing_type.
15230 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15231 to the object address. */
15232
e142c38c 15233 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15234 if (attr != nullptr)
8e19ed76 15235 {
4fc6c0d5 15236 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 15237 {
aec5aa8b
TT
15238 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15239 {
15240 /* Old-style GCC. */
15241 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15242 }
15243 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15244 || (DW_BLOCK (attr)->size > 1
15245 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15246 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15247 {
aec5aa8b
TT
15248 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15249 if ((fnp->voffset % cu->header.addr_size) != 0)
15250 dwarf2_complex_location_expr_complaint ();
15251 else
15252 fnp->voffset /= cu->header.addr_size;
15253 fnp->voffset += 2;
15254 }
15255 else
15256 dwarf2_complex_location_expr_complaint ();
15257
15258 if (!fnp->fcontext)
7e993ebf
KS
15259 {
15260 /* If there is no `this' field and no DW_AT_containing_type,
15261 we cannot actually find a base class context for the
15262 vtable! */
1f704f76 15263 if (this_type->num_fields () == 0
7e993ebf
KS
15264 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15265 {
b98664d3 15266 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15267 "function \"%s\" (offset %s)"),
15268 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15269 }
15270 else
15271 {
15272 fnp->fcontext
940da03e 15273 = TYPE_TARGET_TYPE (this_type->field (0).type ());
7e993ebf
KS
15274 }
15275 }
aec5aa8b 15276 }
cd6c91b4 15277 else if (attr->form_is_section_offset ())
8e19ed76 15278 {
4d3c2250 15279 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15280 }
15281 else
15282 {
4d3c2250
KB
15283 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15284 fieldname);
8e19ed76 15285 }
0d564a31 15286 }
d48cc9dd
DJ
15287 else
15288 {
15289 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15290 if (attr && DW_UNSND (attr))
15291 {
15292 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15293 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15294 "but the vtable offset is not specified"),
9d8780f0 15295 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15296 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15297 TYPE_CPLUS_DYNAMIC (type) = 1;
15298 }
15299 }
c906108c
SS
15300}
15301
15302/* Create the vector of member function fields, and attach it to the type. */
15303
15304static void
fba45db2 15305dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15306 struct dwarf2_cu *cu)
c906108c 15307{
b4ba55a1 15308 if (cu->language == language_ada)
a73c6dcd 15309 error (_("unexpected member functions in Ada type"));
b4ba55a1 15310
c906108c
SS
15311 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15312 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15313 TYPE_ALLOC (type,
15314 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15315
be2daae6 15316 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15317 {
be2daae6 15318 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15319 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15320
be2daae6
TT
15321 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15322 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15323 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15324 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15325
15326 for (int k = 0; k < nf.fnfields.size (); ++k)
15327 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15328 }
15329
be2daae6 15330 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15331}
15332
1168df01
JB
15333/* Returns non-zero if NAME is the name of a vtable member in CU's
15334 language, zero otherwise. */
15335static int
15336is_vtable_name (const char *name, struct dwarf2_cu *cu)
15337{
15338 static const char vptr[] = "_vptr";
15339
9c37b5ae
TT
15340 /* Look for the C++ form of the vtable. */
15341 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15342 return 1;
15343
15344 return 0;
15345}
15346
c0dd20ea 15347/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15348 functions, with the ABI-specified layout. If TYPE describes
15349 such a structure, smash it into a member function type.
61049d3b
DJ
15350
15351 GCC shouldn't do this; it should just output pointer to member DIEs.
15352 This is GCC PR debug/28767. */
c0dd20ea 15353
0b92b5bb
TT
15354static void
15355quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15356{
09e2d7c7 15357 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15358
15359 /* Check for a structure with no name and two children. */
1f704f76 15360 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15361 return;
c0dd20ea
DJ
15362
15363 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15364 if (TYPE_FIELD_NAME (type, 0) == NULL
15365 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15366 || TYPE_FIELD_NAME (type, 1) == NULL
15367 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15368 return;
c0dd20ea
DJ
15369
15370 /* Find the type of the method. */
940da03e 15371 pfn_type = type->field (0).type ();
c0dd20ea 15372 if (pfn_type == NULL
78134374
SM
15373 || pfn_type->code () != TYPE_CODE_PTR
15374 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15375 return;
c0dd20ea
DJ
15376
15377 /* Look for the "this" argument. */
15378 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15379 if (pfn_type->num_fields () == 0
940da03e
SM
15380 /* || pfn_type->field (0).type () == NULL */
15381 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
0b92b5bb 15382 return;
c0dd20ea 15383
940da03e 15384 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
0b92b5bb 15385 new_type = alloc_type (objfile);
09e2d7c7 15386 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15387 pfn_type->fields (), pfn_type->num_fields (),
c0dd20ea 15388 TYPE_VARARGS (pfn_type));
0b92b5bb 15389 smash_to_methodptr_type (type, new_type);
c0dd20ea 15390}
1168df01 15391
2b4424c3
TT
15392/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15393 appropriate error checking and issuing complaints if there is a
15394 problem. */
15395
15396static ULONGEST
15397get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15398{
15399 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15400
15401 if (attr == nullptr)
15402 return 0;
15403
cd6c91b4 15404 if (!attr->form_is_constant ())
2b4424c3 15405 {
b98664d3 15406 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15407 " - DIE at %s [in module %s]"),
15408 sect_offset_str (die->sect_off),
5e22e966 15409 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15410 return 0;
15411 }
15412
15413 ULONGEST align;
15414 if (attr->form == DW_FORM_sdata)
15415 {
15416 LONGEST val = DW_SND (attr);
15417 if (val < 0)
15418 {
b98664d3 15419 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15420 " - DIE at %s [in module %s]"),
15421 sect_offset_str (die->sect_off),
5e22e966 15422 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15423 return 0;
15424 }
15425 align = val;
15426 }
15427 else
15428 align = DW_UNSND (attr);
15429
15430 if (align == 0)
15431 {
b98664d3 15432 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15433 " - DIE at %s [in module %s]"),
15434 sect_offset_str (die->sect_off),
5e22e966 15435 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15436 return 0;
15437 }
15438 if ((align & (align - 1)) != 0)
15439 {
b98664d3 15440 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15441 " - DIE at %s [in module %s]"),
15442 sect_offset_str (die->sect_off),
5e22e966 15443 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15444 return 0;
15445 }
15446
15447 return align;
15448}
15449
15450/* If the DIE has a DW_AT_alignment attribute, use its value to set
15451 the alignment for TYPE. */
15452
15453static void
15454maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15455 struct type *type)
15456{
15457 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15458 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15459 " - DIE at %s [in module %s]"),
15460 sect_offset_str (die->sect_off),
5e22e966 15461 objfile_name (cu->per_objfile->objfile));
2b4424c3 15462}
685b1105 15463
e35000a7
TBA
15464/* Check if the given VALUE is a valid enum dwarf_calling_convention
15465 constant for a type, according to DWARF5 spec, Table 5.5. */
15466
15467static bool
15468is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15469{
15470 switch (value)
15471 {
15472 case DW_CC_normal:
15473 case DW_CC_pass_by_reference:
15474 case DW_CC_pass_by_value:
15475 return true;
15476
15477 default:
15478 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15479 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15480 return false;
15481 }
15482}
15483
d0922fcf
TBA
15484/* Check if the given VALUE is a valid enum dwarf_calling_convention
15485 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15486 also according to GNU-specific values (see include/dwarf2.h). */
15487
15488static bool
15489is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15490{
15491 switch (value)
15492 {
15493 case DW_CC_normal:
15494 case DW_CC_program:
15495 case DW_CC_nocall:
15496 return true;
15497
15498 case DW_CC_GNU_renesas_sh:
15499 case DW_CC_GNU_borland_fastcall_i386:
15500 case DW_CC_GDB_IBM_OpenCL:
15501 return true;
15502
15503 default:
15504 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15505 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15506 return false;
15507 }
15508}
15509
c906108c 15510/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15511 (definition) to create a type for the structure or union. Fill in
15512 the type's name and general properties; the members will not be
83655187
DE
15513 processed until process_structure_scope. A symbol table entry for
15514 the type will also not be done until process_structure_scope (assuming
15515 the type has a name).
c906108c 15516
c767944b
DJ
15517 NOTE: we need to call these functions regardless of whether or not the
15518 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15519 structure or union. This gets the type entered into our set of
83655187 15520 user defined types. */
c906108c 15521
f792889a 15522static struct type *
134d01f1 15523read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15524{
5e22e966 15525 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
15526 struct type *type;
15527 struct attribute *attr;
15d034d0 15528 const char *name;
c906108c 15529
348e048f
DE
15530 /* If the definition of this type lives in .debug_types, read that type.
15531 Don't follow DW_AT_specification though, that will take us back up
15532 the chain and we want to go down. */
052c8bb8 15533 attr = die->attr (DW_AT_signature);
435d3d88 15534 if (attr != nullptr)
348e048f 15535 {
ac9ec31b 15536 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15537
ac9ec31b 15538 /* The type's CU may not be the same as CU.
02142a6c 15539 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15540 return set_die_type (die, type, cu);
15541 }
15542
c0dd20ea 15543 type = alloc_type (objfile);
c906108c 15544 INIT_CPLUS_SPECIFIC (type);
93311388 15545
39cbfefa
DJ
15546 name = dwarf2_name (die, cu);
15547 if (name != NULL)
c906108c 15548 {
987504bb 15549 if (cu->language == language_cplus
c44af4eb
TT
15550 || cu->language == language_d
15551 || cu->language == language_rust)
63d06c5c 15552 {
15d034d0 15553 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15554
15555 /* dwarf2_full_name might have already finished building the DIE's
15556 type. If so, there is no need to continue. */
15557 if (get_die_type (die, cu) != NULL)
15558 return get_die_type (die, cu);
15559
d0e39ea2 15560 type->set_name (full_name);
63d06c5c
DC
15561 }
15562 else
15563 {
d8151005
DJ
15564 /* The name is already allocated along with this objfile, so
15565 we don't need to duplicate it for the type. */
d0e39ea2 15566 type->set_name (name);
63d06c5c 15567 }
c906108c
SS
15568 }
15569
15570 if (die->tag == DW_TAG_structure_type)
15571 {
67607e24 15572 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15573 }
15574 else if (die->tag == DW_TAG_union_type)
15575 {
67607e24 15576 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15577 }
15578 else
15579 {
67607e24 15580 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15581 }
15582
0cc2414c
TT
15583 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15584 TYPE_DECLARED_CLASS (type) = 1;
15585
e35000a7
TBA
15586 /* Store the calling convention in the type if it's available in
15587 the die. Otherwise the calling convention remains set to
15588 the default value DW_CC_normal. */
15589 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15590 if (attr != nullptr
15591 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15592 {
15593 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15594 TYPE_CPLUS_CALLING_CONVENTION (type)
15595 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15596 }
15597
e142c38c 15598 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15599 if (attr != nullptr)
c906108c 15600 {
cd6c91b4 15601 if (attr->form_is_constant ())
155bfbd3
JB
15602 TYPE_LENGTH (type) = DW_UNSND (attr);
15603 else
15604 {
f8e89861 15605 struct dynamic_prop prop;
293e7e51 15606 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 15607 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
155bfbd3
JB
15608 TYPE_LENGTH (type) = 0;
15609 }
c906108c
SS
15610 }
15611 else
15612 {
15613 TYPE_LENGTH (type) = 0;
15614 }
15615
2b4424c3
TT
15616 maybe_set_alignment (cu, die, type);
15617
5230b05a 15618 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15619 {
5230b05a
WT
15620 /* ICC<14 does not output the required DW_AT_declaration on
15621 incomplete types, but gives them a size of zero. */
422b1cb0 15622 TYPE_STUB (type) = 1;
685b1105
JK
15623 }
15624 else
15625 TYPE_STUB_SUPPORTED (type) = 1;
15626
dc718098 15627 if (die_is_declaration (die, cu))
876cecd0 15628 TYPE_STUB (type) = 1;
a6c727b2
DJ
15629 else if (attr == NULL && die->child == NULL
15630 && producer_is_realview (cu->producer))
15631 /* RealView does not output the required DW_AT_declaration
15632 on incomplete types. */
15633 TYPE_STUB (type) = 1;
dc718098 15634
c906108c
SS
15635 /* We need to add the type field to the die immediately so we don't
15636 infinitely recurse when dealing with pointers to the structure
0963b4bd 15637 type within the structure itself. */
1c379e20 15638 set_die_type (die, type, cu);
c906108c 15639
7e314c57
JK
15640 /* set_die_type should be already done. */
15641 set_descriptive_type (type, die, cu);
15642
c767944b
DJ
15643 return type;
15644}
15645
9c6a1327
TT
15646static void handle_struct_member_die
15647 (struct die_info *child_die,
15648 struct type *type,
15649 struct field_info *fi,
15650 std::vector<struct symbol *> *template_args,
15651 struct dwarf2_cu *cu);
15652
15653/* A helper for handle_struct_member_die that handles
15654 DW_TAG_variant_part. */
15655
15656static void
15657handle_variant_part (struct die_info *die, struct type *type,
15658 struct field_info *fi,
15659 std::vector<struct symbol *> *template_args,
15660 struct dwarf2_cu *cu)
15661{
15662 variant_part_builder *new_part;
15663 if (fi->current_variant_part == nullptr)
15664 {
15665 fi->variant_parts.emplace_back ();
15666 new_part = &fi->variant_parts.back ();
15667 }
15668 else if (!fi->current_variant_part->processing_variant)
15669 {
15670 complaint (_("nested DW_TAG_variant_part seen "
15671 "- DIE at %s [in module %s]"),
15672 sect_offset_str (die->sect_off),
5e22e966 15673 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15674 return;
15675 }
15676 else
15677 {
15678 variant_field &current = fi->current_variant_part->variants.back ();
15679 current.variant_parts.emplace_back ();
15680 new_part = &current.variant_parts.back ();
15681 }
15682
15683 /* When we recurse, we want callees to add to this new variant
15684 part. */
15685 scoped_restore save_current_variant_part
15686 = make_scoped_restore (&fi->current_variant_part, new_part);
15687
15688 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15689 if (discr == NULL)
15690 {
15691 /* It's a univariant form, an extension we support. */
15692 }
15693 else if (discr->form_is_ref ())
15694 {
15695 struct dwarf2_cu *target_cu = cu;
15696 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15697
15698 new_part->discriminant_offset = target_die->sect_off;
15699 }
15700 else
15701 {
15702 complaint (_("DW_AT_discr does not have DIE reference form"
15703 " - DIE at %s [in module %s]"),
15704 sect_offset_str (die->sect_off),
5e22e966 15705 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15706 }
15707
15708 for (die_info *child_die = die->child;
15709 child_die != NULL;
15710 child_die = child_die->sibling)
15711 handle_struct_member_die (child_die, type, fi, template_args, cu);
15712}
15713
15714/* A helper for handle_struct_member_die that handles
15715 DW_TAG_variant. */
15716
15717static void
15718handle_variant (struct die_info *die, struct type *type,
15719 struct field_info *fi,
15720 std::vector<struct symbol *> *template_args,
15721 struct dwarf2_cu *cu)
15722{
15723 if (fi->current_variant_part == nullptr)
15724 {
15725 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15726 "- DIE at %s [in module %s]"),
15727 sect_offset_str (die->sect_off),
5e22e966 15728 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15729 return;
15730 }
15731 if (fi->current_variant_part->processing_variant)
15732 {
15733 complaint (_("nested DW_TAG_variant seen "
15734 "- DIE at %s [in module %s]"),
15735 sect_offset_str (die->sect_off),
5e22e966 15736 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15737 return;
15738 }
15739
15740 scoped_restore save_processing_variant
15741 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15742 true);
15743
15744 fi->current_variant_part->variants.emplace_back ();
15745 variant_field &variant = fi->current_variant_part->variants.back ();
15746 variant.first_field = fi->fields.size ();
15747
15748 /* In a variant we want to get the discriminant and also add a
15749 field for our sole member child. */
15750 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15751 if (discr == nullptr)
15752 {
15753 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15754 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15755 variant.default_branch = true;
15756 else
15757 variant.discr_list_data = DW_BLOCK (discr);
15758 }
15759 else
15760 variant.discriminant_value = DW_UNSND (discr);
15761
15762 for (die_info *variant_child = die->child;
15763 variant_child != NULL;
15764 variant_child = variant_child->sibling)
15765 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15766
15767 variant.last_field = fi->fields.size ();
15768}
15769
2ddeaf8a
TT
15770/* A helper for process_structure_scope that handles a single member
15771 DIE. */
15772
15773static void
15774handle_struct_member_die (struct die_info *child_die, struct type *type,
15775 struct field_info *fi,
15776 std::vector<struct symbol *> *template_args,
15777 struct dwarf2_cu *cu)
15778{
15779 if (child_die->tag == DW_TAG_member
9c6a1327 15780 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
15781 {
15782 /* NOTE: carlton/2002-11-05: A C++ static data member
15783 should be a DW_TAG_member that is a declaration, but
15784 all versions of G++ as of this writing (so through at
15785 least 3.2.1) incorrectly generate DW_TAG_variable
15786 tags for them instead. */
15787 dwarf2_add_field (fi, child_die, cu);
15788 }
15789 else if (child_die->tag == DW_TAG_subprogram)
15790 {
15791 /* Rust doesn't have member functions in the C++ sense.
15792 However, it does emit ordinary functions as children
15793 of a struct DIE. */
15794 if (cu->language == language_rust)
15795 read_func_scope (child_die, cu);
15796 else
15797 {
15798 /* C++ member function. */
15799 dwarf2_add_member_fn (fi, child_die, type, cu);
15800 }
15801 }
15802 else if (child_die->tag == DW_TAG_inheritance)
15803 {
15804 /* C++ base class field. */
15805 dwarf2_add_field (fi, child_die, cu);
15806 }
15807 else if (type_can_define_types (child_die))
15808 dwarf2_add_type_defn (fi, child_die, cu);
15809 else if (child_die->tag == DW_TAG_template_type_param
15810 || child_die->tag == DW_TAG_template_value_param)
15811 {
15812 struct symbol *arg = new_symbol (child_die, NULL, cu);
15813
15814 if (arg != NULL)
15815 template_args->push_back (arg);
15816 }
9c6a1327
TT
15817 else if (child_die->tag == DW_TAG_variant_part)
15818 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 15819 else if (child_die->tag == DW_TAG_variant)
9c6a1327 15820 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
15821}
15822
c767944b
DJ
15823/* Finish creating a structure or union type, including filling in
15824 its members and creating a symbol for it. */
15825
15826static void
15827process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15828{
5e22e966 15829 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 15830 struct die_info *child_die;
c767944b
DJ
15831 struct type *type;
15832
15833 type = get_die_type (die, cu);
15834 if (type == NULL)
15835 type = read_structure_type (die, cu);
15836
3e1d3d8c 15837 bool has_template_parameters = false;
e142c38c 15838 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15839 {
15840 struct field_info fi;
2f4732b0 15841 std::vector<struct symbol *> template_args;
c906108c 15842
639d11d3 15843 child_die = die->child;
c906108c
SS
15844
15845 while (child_die && child_die->tag)
15846 {
2ddeaf8a 15847 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 15848 child_die = child_die->sibling;
c906108c
SS
15849 }
15850
34eaf542 15851 /* Attach template arguments to type. */
2f4732b0 15852 if (!template_args.empty ())
34eaf542 15853 {
3e1d3d8c 15854 has_template_parameters = true;
34eaf542 15855 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15856 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15857 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15858 = XOBNEWVEC (&objfile->objfile_obstack,
15859 struct symbol *,
15860 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15861 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15862 template_args.data (),
34eaf542
TT
15863 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15864 * sizeof (struct symbol *)));
34eaf542
TT
15865 }
15866
c906108c 15867 /* Attach fields and member functions to the type. */
317f7127 15868 if (fi.nfields () > 0)
e7c27a73 15869 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15870 if (!fi.fnfieldlists.empty ())
c906108c 15871 {
e7c27a73 15872 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15873
c5aa993b 15874 /* Get the type which refers to the base class (possibly this
c906108c 15875 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15876 class from the DW_AT_containing_type attribute. This use of
15877 DW_AT_containing_type is a GNU extension. */
c906108c 15878
e142c38c 15879 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15880 {
e7c27a73 15881 struct type *t = die_containing_type (die, cu);
c906108c 15882
ae6ae975 15883 set_type_vptr_basetype (type, t);
c906108c
SS
15884 if (type == t)
15885 {
c906108c
SS
15886 int i;
15887
15888 /* Our own class provides vtbl ptr. */
1f704f76 15889 for (i = t->num_fields () - 1;
c906108c
SS
15890 i >= TYPE_N_BASECLASSES (t);
15891 --i)
15892 {
0d5cff50 15893 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15894
1168df01 15895 if (is_vtable_name (fieldname, cu))
c906108c 15896 {
ae6ae975 15897 set_type_vptr_fieldno (type, i);
c906108c
SS
15898 break;
15899 }
15900 }
15901
15902 /* Complain if virtual function table field not found. */
15903 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15904 complaint (_("virtual function table pointer "
3e43a32a 15905 "not found when defining class '%s'"),
7d93a1e0 15906 type->name () ? type->name () : "");
c906108c
SS
15907 }
15908 else
15909 {
ae6ae975 15910 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15911 }
15912 }
f6235d4c 15913 else if (cu->producer
61012eef 15914 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15915 {
15916 /* The IBM XLC compiler does not provide direct indication
15917 of the containing type, but the vtable pointer is
15918 always named __vfp. */
15919
15920 int i;
15921
1f704f76 15922 for (i = type->num_fields () - 1;
f6235d4c
EZ
15923 i >= TYPE_N_BASECLASSES (type);
15924 --i)
15925 {
15926 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15927 {
ae6ae975
DE
15928 set_type_vptr_fieldno (type, i);
15929 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15930 break;
15931 }
15932 }
15933 }
c906108c 15934 }
98751a41
JK
15935
15936 /* Copy fi.typedef_field_list linked list elements content into the
15937 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15938 if (!fi.typedef_field_list.empty ())
98751a41 15939 {
be2daae6 15940 int count = fi.typedef_field_list.size ();
98751a41 15941
a0d7a4ff 15942 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15943 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15944 = ((struct decl_field *)
be2daae6
TT
15945 TYPE_ALLOC (type,
15946 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15947 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15948
be2daae6
TT
15949 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15950 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15951 }
c767944b 15952
883fd55a
KS
15953 /* Copy fi.nested_types_list linked list elements content into the
15954 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15955 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15956 {
be2daae6 15957 int count = fi.nested_types_list.size ();
883fd55a
KS
15958
15959 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15960 TYPE_NESTED_TYPES_ARRAY (type)
15961 = ((struct decl_field *)
be2daae6
TT
15962 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15963 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15964
be2daae6
TT
15965 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15966 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15967 }
c906108c 15968 }
63d06c5c 15969
bb5ed363 15970 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15971 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15972 cu->rust_unions.push_back (type);
0b92b5bb 15973
90aeadfc
DC
15974 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15975 snapshots) has been known to create a die giving a declaration
15976 for a class that has, as a child, a die giving a definition for a
15977 nested class. So we have to process our children even if the
15978 current die is a declaration. Normally, of course, a declaration
15979 won't have any children at all. */
134d01f1 15980
ca040673
DE
15981 child_die = die->child;
15982
90aeadfc
DC
15983 while (child_die != NULL && child_die->tag)
15984 {
15985 if (child_die->tag == DW_TAG_member
15986 || child_die->tag == DW_TAG_variable
34eaf542
TT
15987 || child_die->tag == DW_TAG_inheritance
15988 || child_die->tag == DW_TAG_template_value_param
15989 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15990 {
90aeadfc 15991 /* Do nothing. */
134d01f1 15992 }
90aeadfc
DC
15993 else
15994 process_die (child_die, cu);
134d01f1 15995
436c571c 15996 child_die = child_die->sibling;
134d01f1
DJ
15997 }
15998
fa4028e9
JB
15999 /* Do not consider external references. According to the DWARF standard,
16000 these DIEs are identified by the fact that they have no byte_size
16001 attribute, and a declaration attribute. */
16002 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
16003 || !die_is_declaration (die, cu)
16004 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
16005 {
16006 struct symbol *sym = new_symbol (die, type, cu);
16007
16008 if (has_template_parameters)
16009 {
a776957c
TT
16010 struct symtab *symtab;
16011 if (sym != nullptr)
16012 symtab = symbol_symtab (sym);
16013 else if (cu->line_header != nullptr)
16014 {
16015 /* Any related symtab will do. */
16016 symtab
7ba99d21 16017 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16018 }
16019 else
16020 {
16021 symtab = nullptr;
16022 complaint (_("could not find suitable "
16023 "symtab for template parameter"
16024 " - DIE at %s [in module %s]"),
16025 sect_offset_str (die->sect_off),
16026 objfile_name (objfile));
16027 }
16028
16029 if (symtab != nullptr)
16030 {
16031 /* Make sure that the symtab is set on the new symbols.
16032 Even though they don't appear in this symtab directly,
16033 other parts of gdb assume that symbols do, and this is
16034 reasonably true. */
16035 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16036 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16037 }
3e1d3d8c
TT
16038 }
16039 }
134d01f1
DJ
16040}
16041
ed6acedd
TT
16042/* Assuming DIE is an enumeration type, and TYPE is its associated
16043 type, update TYPE using some information only available in DIE's
16044 children. In particular, the fields are computed. */
55426c9d
JB
16045
16046static void
16047update_enumeration_type_from_children (struct die_info *die,
16048 struct type *type,
16049 struct dwarf2_cu *cu)
16050{
60f7655a 16051 struct die_info *child_die;
55426c9d
JB
16052 int unsigned_enum = 1;
16053 int flag_enum = 1;
55426c9d 16054
8268c778 16055 auto_obstack obstack;
ed6acedd 16056 std::vector<struct field> fields;
55426c9d 16057
60f7655a
DE
16058 for (child_die = die->child;
16059 child_die != NULL && child_die->tag;
436c571c 16060 child_die = child_die->sibling)
55426c9d
JB
16061 {
16062 struct attribute *attr;
16063 LONGEST value;
16064 const gdb_byte *bytes;
16065 struct dwarf2_locexpr_baton *baton;
16066 const char *name;
60f7655a 16067
55426c9d
JB
16068 if (child_die->tag != DW_TAG_enumerator)
16069 continue;
16070
16071 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16072 if (attr == NULL)
16073 continue;
16074
16075 name = dwarf2_name (child_die, cu);
16076 if (name == NULL)
16077 name = "<anonymous enumerator>";
16078
16079 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16080 &value, &bytes, &baton);
16081 if (value < 0)
16082 {
16083 unsigned_enum = 0;
16084 flag_enum = 0;
16085 }
55426c9d 16086 else
edd45eb0
SM
16087 {
16088 if (count_one_bits_ll (value) >= 2)
16089 flag_enum = 0;
edd45eb0 16090 }
55426c9d 16091
ed6acedd
TT
16092 fields.emplace_back ();
16093 struct field &field = fields.back ();
16094 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16095 SET_FIELD_ENUMVAL (field, value);
16096 }
16097
16098 if (!fields.empty ())
16099 {
5e33d5f4 16100 type->set_num_fields (fields.size ());
3cabb6b0
SM
16101 type->set_fields
16102 ((struct field *)
16103 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16104 memcpy (type->fields (), fields.data (),
ed6acedd 16105 sizeof (struct field) * fields.size ());
55426c9d
JB
16106 }
16107
16108 if (unsigned_enum)
16109 TYPE_UNSIGNED (type) = 1;
16110 if (flag_enum)
16111 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16112}
16113
134d01f1
DJ
16114/* Given a DW_AT_enumeration_type die, set its type. We do not
16115 complete the type's fields yet, or create any symbols. */
c906108c 16116
f792889a 16117static struct type *
134d01f1 16118read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16119{
5e22e966 16120 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16121 struct type *type;
c906108c 16122 struct attribute *attr;
0114d602 16123 const char *name;
134d01f1 16124
348e048f
DE
16125 /* If the definition of this type lives in .debug_types, read that type.
16126 Don't follow DW_AT_specification though, that will take us back up
16127 the chain and we want to go down. */
052c8bb8 16128 attr = die->attr (DW_AT_signature);
435d3d88 16129 if (attr != nullptr)
348e048f 16130 {
ac9ec31b 16131 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16132
ac9ec31b 16133 /* The type's CU may not be the same as CU.
02142a6c 16134 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16135 return set_die_type (die, type, cu);
16136 }
16137
c906108c
SS
16138 type = alloc_type (objfile);
16139
67607e24 16140 type->set_code (TYPE_CODE_ENUM);
94af9270 16141 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16142 if (name != NULL)
d0e39ea2 16143 type->set_name (name);
c906108c 16144
0626fc76
TT
16145 attr = dwarf2_attr (die, DW_AT_type, cu);
16146 if (attr != NULL)
16147 {
16148 struct type *underlying_type = die_type (die, cu);
16149
16150 TYPE_TARGET_TYPE (type) = underlying_type;
16151 }
16152
e142c38c 16153 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16154 if (attr != nullptr)
c906108c
SS
16155 {
16156 TYPE_LENGTH (type) = DW_UNSND (attr);
16157 }
16158 else
16159 {
16160 TYPE_LENGTH (type) = 0;
16161 }
16162
2b4424c3
TT
16163 maybe_set_alignment (cu, die, type);
16164
137033e9
JB
16165 /* The enumeration DIE can be incomplete. In Ada, any type can be
16166 declared as private in the package spec, and then defined only
16167 inside the package body. Such types are known as Taft Amendment
16168 Types. When another package uses such a type, an incomplete DIE
16169 may be generated by the compiler. */
02eb380e 16170 if (die_is_declaration (die, cu))
876cecd0 16171 TYPE_STUB (type) = 1;
02eb380e 16172
0626fc76
TT
16173 /* If this type has an underlying type that is not a stub, then we
16174 may use its attributes. We always use the "unsigned" attribute
16175 in this situation, because ordinarily we guess whether the type
16176 is unsigned -- but the guess can be wrong and the underlying type
16177 can tell us the reality. However, we defer to a local size
16178 attribute if one exists, because this lets the compiler override
16179 the underlying type if needed. */
16180 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16181 {
9e7c9a03
HD
16182 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16183 underlying_type = check_typedef (underlying_type);
16184 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
0626fc76 16185 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16186 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
2b4424c3 16187 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16188 && TYPE_RAW_ALIGN (underlying_type) != 0)
16189 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16190 }
16191
3d567982
TT
16192 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16193
ed6acedd
TT
16194 set_die_type (die, type, cu);
16195
16196 /* Finish the creation of this type by using the enum's children.
16197 Note that, as usual, this must come after set_die_type to avoid
16198 infinite recursion when trying to compute the names of the
16199 enumerators. */
16200 update_enumeration_type_from_children (die, type, cu);
16201
16202 return type;
134d01f1
DJ
16203}
16204
16205/* Given a pointer to a die which begins an enumeration, process all
16206 the dies that define the members of the enumeration, and create the
16207 symbol for the enumeration type.
16208
16209 NOTE: We reverse the order of the element list. */
16210
16211static void
16212process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16213{
f792889a 16214 struct type *this_type;
134d01f1 16215
f792889a
DJ
16216 this_type = get_die_type (die, cu);
16217 if (this_type == NULL)
16218 this_type = read_enumeration_type (die, cu);
9dc481d3 16219
639d11d3 16220 if (die->child != NULL)
c906108c 16221 {
9dc481d3 16222 struct die_info *child_die;
15d034d0 16223 const char *name;
9dc481d3 16224
639d11d3 16225 child_die = die->child;
c906108c
SS
16226 while (child_die && child_die->tag)
16227 {
16228 if (child_die->tag != DW_TAG_enumerator)
16229 {
e7c27a73 16230 process_die (child_die, cu);
c906108c
SS
16231 }
16232 else
16233 {
39cbfefa
DJ
16234 name = dwarf2_name (child_die, cu);
16235 if (name)
ed6acedd 16236 new_symbol (child_die, this_type, cu);
c906108c
SS
16237 }
16238
436c571c 16239 child_die = child_die->sibling;
c906108c 16240 }
c906108c 16241 }
134d01f1 16242
6c83ed52
TT
16243 /* If we are reading an enum from a .debug_types unit, and the enum
16244 is a declaration, and the enum is not the signatured type in the
16245 unit, then we do not want to add a symbol for it. Adding a
16246 symbol would in some cases obscure the true definition of the
16247 enum, giving users an incomplete type when the definition is
16248 actually available. Note that we do not want to do this for all
16249 enums which are just declarations, because C++0x allows forward
16250 enum declarations. */
3019eac3 16251 if (cu->per_cu->is_debug_types
6c83ed52
TT
16252 && die_is_declaration (die, cu))
16253 {
52dc124a 16254 struct signatured_type *sig_type;
6c83ed52 16255
c0f78cd4 16256 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16257 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16258 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16259 return;
16260 }
16261
f792889a 16262 new_symbol (die, this_type, cu);
c906108c
SS
16263}
16264
16265/* Extract all information from a DW_TAG_array_type DIE and put it in
16266 the DIE's type field. For now, this only handles one dimensional
16267 arrays. */
16268
f792889a 16269static struct type *
e7c27a73 16270read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16271{
5e22e966 16272 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16273 struct die_info *child_die;
7e314c57 16274 struct type *type;
c906108c 16275 struct type *element_type, *range_type, *index_type;
c906108c 16276 struct attribute *attr;
15d034d0 16277 const char *name;
a405673c 16278 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16279 unsigned int bit_stride = 0;
c906108c 16280
e7c27a73 16281 element_type = die_type (die, cu);
c906108c 16282
7e314c57
JK
16283 /* The die_type call above may have already set the type for this DIE. */
16284 type = get_die_type (die, cu);
16285 if (type)
16286 return type;
16287
dc53a7ad
JB
16288 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16289 if (attr != NULL)
a405673c
JB
16290 {
16291 int stride_ok;
293e7e51 16292 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
16293
16294 byte_stride_prop
16295 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16296 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16297 prop_type);
a405673c
JB
16298 if (!stride_ok)
16299 {
b98664d3 16300 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16301 " - DIE at %s [in module %s]"),
16302 sect_offset_str (die->sect_off),
5e22e966 16303 objfile_name (cu->per_objfile->objfile));
a405673c
JB
16304 /* Ignore this attribute. We will likely not be able to print
16305 arrays of this type correctly, but there is little we can do
16306 to help if we cannot read the attribute's value. */
16307 byte_stride_prop = NULL;
16308 }
16309 }
dc53a7ad
JB
16310
16311 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16312 if (attr != NULL)
16313 bit_stride = DW_UNSND (attr);
16314
c906108c
SS
16315 /* Irix 6.2 native cc creates array types without children for
16316 arrays with unspecified length. */
639d11d3 16317 if (die->child == NULL)
c906108c 16318 {
46bf5051 16319 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16320 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16321 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16322 byte_stride_prop, bit_stride);
f792889a 16323 return set_die_type (die, type, cu);
c906108c
SS
16324 }
16325
791afaa2 16326 std::vector<struct type *> range_types;
639d11d3 16327 child_die = die->child;
c906108c
SS
16328 while (child_die && child_die->tag)
16329 {
16330 if (child_die->tag == DW_TAG_subrange_type)
16331 {
f792889a 16332 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16333
f792889a 16334 if (child_type != NULL)
a02abb62 16335 {
0963b4bd
MS
16336 /* The range type was succesfully read. Save it for the
16337 array type creation. */
791afaa2 16338 range_types.push_back (child_type);
a02abb62 16339 }
c906108c 16340 }
436c571c 16341 child_die = child_die->sibling;
c906108c
SS
16342 }
16343
16344 /* Dwarf2 dimensions are output from left to right, create the
16345 necessary array types in backwards order. */
7ca2d3a3 16346
c906108c 16347 type = element_type;
7ca2d3a3
DL
16348
16349 if (read_array_order (die, cu) == DW_ORD_col_major)
16350 {
16351 int i = 0;
9a619af0 16352
791afaa2 16353 while (i < range_types.size ())
dc53a7ad 16354 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16355 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16356 }
16357 else
16358 {
791afaa2 16359 size_t ndim = range_types.size ();
7ca2d3a3 16360 while (ndim-- > 0)
dc53a7ad 16361 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16362 byte_stride_prop, bit_stride);
7ca2d3a3 16363 }
c906108c 16364
f5f8a009
EZ
16365 /* Understand Dwarf2 support for vector types (like they occur on
16366 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16367 array type. This is not part of the Dwarf2/3 standard yet, but a
16368 custom vendor extension. The main difference between a regular
16369 array and the vector variant is that vectors are passed by value
16370 to functions. */
e142c38c 16371 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16372 if (attr != nullptr)
ea37ba09 16373 make_vector_type (type);
f5f8a009 16374
dbc98a8b
KW
16375 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16376 implementation may choose to implement triple vectors using this
16377 attribute. */
16378 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16379 if (attr != nullptr)
dbc98a8b
KW
16380 {
16381 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16382 TYPE_LENGTH (type) = DW_UNSND (attr);
16383 else
b98664d3 16384 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16385 "than the total size of elements"));
dbc98a8b
KW
16386 }
16387
39cbfefa
DJ
16388 name = dwarf2_name (die, cu);
16389 if (name)
d0e39ea2 16390 type->set_name (name);
6e70227d 16391
2b4424c3
TT
16392 maybe_set_alignment (cu, die, type);
16393
0963b4bd 16394 /* Install the type in the die. */
7e314c57
JK
16395 set_die_type (die, type, cu);
16396
16397 /* set_die_type should be already done. */
b4ba55a1
JB
16398 set_descriptive_type (type, die, cu);
16399
7e314c57 16400 return type;
c906108c
SS
16401}
16402
7ca2d3a3 16403static enum dwarf_array_dim_ordering
6e70227d 16404read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16405{
16406 struct attribute *attr;
16407
16408 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16409
435d3d88 16410 if (attr != nullptr)
aead7601 16411 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16412
0963b4bd
MS
16413 /* GNU F77 is a special case, as at 08/2004 array type info is the
16414 opposite order to the dwarf2 specification, but data is still
16415 laid out as per normal fortran.
7ca2d3a3 16416
0963b4bd
MS
16417 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16418 version checking. */
7ca2d3a3 16419
905e0470
PM
16420 if (cu->language == language_fortran
16421 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16422 {
16423 return DW_ORD_row_major;
16424 }
16425
6e70227d 16426 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16427 {
16428 case array_column_major:
16429 return DW_ORD_col_major;
16430 case array_row_major:
16431 default:
16432 return DW_ORD_row_major;
16433 };
16434}
16435
72019c9c 16436/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16437 the DIE's type field. */
72019c9c 16438
f792889a 16439static struct type *
72019c9c
GM
16440read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16441{
7e314c57
JK
16442 struct type *domain_type, *set_type;
16443 struct attribute *attr;
f792889a 16444
7e314c57
JK
16445 domain_type = die_type (die, cu);
16446
16447 /* The die_type call above may have already set the type for this DIE. */
16448 set_type = get_die_type (die, cu);
16449 if (set_type)
16450 return set_type;
16451
16452 set_type = create_set_type (NULL, domain_type);
16453
16454 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16455 if (attr != nullptr)
d09039dd 16456 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16457
2b4424c3
TT
16458 maybe_set_alignment (cu, die, set_type);
16459
f792889a 16460 return set_die_type (die, set_type, cu);
72019c9c 16461}
7ca2d3a3 16462
0971de02
TT
16463/* A helper for read_common_block that creates a locexpr baton.
16464 SYM is the symbol which we are marking as computed.
16465 COMMON_DIE is the DIE for the common block.
16466 COMMON_LOC is the location expression attribute for the common
16467 block itself.
16468 MEMBER_LOC is the location expression attribute for the particular
16469 member of the common block that we are processing.
16470 CU is the CU from which the above come. */
16471
16472static void
16473mark_common_block_symbol_computed (struct symbol *sym,
16474 struct die_info *common_die,
16475 struct attribute *common_loc,
16476 struct attribute *member_loc,
16477 struct dwarf2_cu *cu)
16478{
5e22e966 16479 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 16480 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
16481 struct dwarf2_locexpr_baton *baton;
16482 gdb_byte *ptr;
16483 unsigned int cu_off;
08feed99 16484 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
16485 LONGEST offset = 0;
16486
16487 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16488 gdb_assert (common_loc->form_is_block ());
16489 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16490 || member_loc->form_is_constant ());
0971de02 16491
8d749320 16492 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 16493 baton->per_objfile = per_objfile;
0971de02
TT
16494 baton->per_cu = cu->per_cu;
16495 gdb_assert (baton->per_cu);
16496
16497 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16498
cd6c91b4 16499 if (member_loc->form_is_constant ())
0971de02 16500 {
0826b30a 16501 offset = member_loc->constant_value (0);
0971de02
TT
16502 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16503 }
16504 else
16505 baton->size += DW_BLOCK (member_loc)->size;
16506
224c3ddb 16507 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16508 baton->data = ptr;
16509
16510 *ptr++ = DW_OP_call4;
9c541725 16511 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16512 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16513 ptr += 4;
16514
cd6c91b4 16515 if (member_loc->form_is_constant ())
0971de02
TT
16516 {
16517 *ptr++ = DW_OP_addr;
16518 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16519 ptr += cu->header.addr_size;
16520 }
16521 else
16522 {
16523 /* We have to copy the data here, because DW_OP_call4 will only
16524 use a DW_AT_location attribute. */
16525 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16526 ptr += DW_BLOCK (member_loc)->size;
16527 }
16528
16529 *ptr++ = DW_OP_plus;
16530 gdb_assert (ptr - baton->data == baton->size);
16531
0971de02 16532 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16533 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16534}
16535
4357ac6c
TT
16536/* Create appropriate locally-scoped variables for all the
16537 DW_TAG_common_block entries. Also create a struct common_block
16538 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16539 is used to separate the common blocks name namespace from regular
4357ac6c 16540 variable names. */
c906108c
SS
16541
16542static void
e7c27a73 16543read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16544{
0971de02
TT
16545 struct attribute *attr;
16546
16547 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16548 if (attr != nullptr)
0971de02
TT
16549 {
16550 /* Support the .debug_loc offsets. */
4fc6c0d5 16551 if (attr->form_is_block ())
0971de02
TT
16552 {
16553 /* Ok. */
16554 }
cd6c91b4 16555 else if (attr->form_is_section_offset ())
0971de02
TT
16556 {
16557 dwarf2_complex_location_expr_complaint ();
16558 attr = NULL;
16559 }
16560 else
16561 {
16562 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16563 "common block member");
16564 attr = NULL;
16565 }
16566 }
16567
639d11d3 16568 if (die->child != NULL)
c906108c 16569 {
5e22e966 16570 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
16571 struct die_info *child_die;
16572 size_t n_entries = 0, size;
16573 struct common_block *common_block;
16574 struct symbol *sym;
74ac6d43 16575
4357ac6c
TT
16576 for (child_die = die->child;
16577 child_die && child_die->tag;
436c571c 16578 child_die = child_die->sibling)
4357ac6c
TT
16579 ++n_entries;
16580
16581 size = (sizeof (struct common_block)
16582 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16583 common_block
16584 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16585 size);
4357ac6c
TT
16586 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16587 common_block->n_entries = 0;
16588
16589 for (child_die = die->child;
16590 child_die && child_die->tag;
436c571c 16591 child_die = child_die->sibling)
4357ac6c
TT
16592 {
16593 /* Create the symbol in the DW_TAG_common_block block in the current
16594 symbol scope. */
e7c27a73 16595 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16596 if (sym != NULL)
16597 {
16598 struct attribute *member_loc;
16599
16600 common_block->contents[common_block->n_entries++] = sym;
16601
16602 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16603 cu);
16604 if (member_loc)
16605 {
16606 /* GDB has handled this for a long time, but it is
16607 not specified by DWARF. It seems to have been
16608 emitted by gfortran at least as recently as:
16609 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16610 complaint (_("Variable in common block has "
0971de02 16611 "DW_AT_data_member_location "
9d8780f0
SM
16612 "- DIE at %s [in module %s]"),
16613 sect_offset_str (child_die->sect_off),
518817b3 16614 objfile_name (objfile));
0971de02 16615
cd6c91b4 16616 if (member_loc->form_is_section_offset ())
0971de02 16617 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16618 else if (member_loc->form_is_constant ()
4fc6c0d5 16619 || member_loc->form_is_block ())
0971de02 16620 {
435d3d88 16621 if (attr != nullptr)
0971de02
TT
16622 mark_common_block_symbol_computed (sym, die, attr,
16623 member_loc, cu);
16624 }
16625 else
16626 dwarf2_complex_location_expr_complaint ();
16627 }
16628 }
c906108c 16629 }
4357ac6c
TT
16630
16631 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16632 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16633 }
16634}
16635
0114d602 16636/* Create a type for a C++ namespace. */
d9fa45fe 16637
0114d602
DJ
16638static struct type *
16639read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16640{
5e22e966 16641 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16642 const char *previous_prefix, *name;
9219021c 16643 int is_anonymous;
0114d602
DJ
16644 struct type *type;
16645
16646 /* For extensions, reuse the type of the original namespace. */
16647 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16648 {
16649 struct die_info *ext_die;
16650 struct dwarf2_cu *ext_cu = cu;
9a619af0 16651
0114d602
DJ
16652 ext_die = dwarf2_extension (die, &ext_cu);
16653 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16654
16655 /* EXT_CU may not be the same as CU.
02142a6c 16656 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16657 return set_die_type (die, type, cu);
16658 }
9219021c 16659
e142c38c 16660 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16661
16662 /* Now build the name of the current namespace. */
16663
0114d602
DJ
16664 previous_prefix = determine_prefix (die, cu);
16665 if (previous_prefix[0] != '\0')
16666 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16667 previous_prefix, name, 0, cu);
0114d602
DJ
16668
16669 /* Create the type. */
19f392bc 16670 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16671
60531b24 16672 return set_die_type (die, type, cu);
0114d602
DJ
16673}
16674
22cee43f 16675/* Read a namespace scope. */
0114d602
DJ
16676
16677static void
16678read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16679{
5e22e966 16680 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16681 int is_anonymous;
9219021c 16682
5c4e30ca
DC
16683 /* Add a symbol associated to this if we haven't seen the namespace
16684 before. Also, add a using directive if it's an anonymous
16685 namespace. */
9219021c 16686
f2f0e013 16687 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16688 {
16689 struct type *type;
16690
0114d602 16691 type = read_type_die (die, cu);
e7c27a73 16692 new_symbol (die, type, cu);
5c4e30ca 16693
e8e80198 16694 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16695 if (is_anonymous)
0114d602
DJ
16696 {
16697 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16698
eb1e02fd 16699 std::vector<const char *> excludes;
804d2729 16700 add_using_directive (using_directives (cu),
7d93a1e0 16701 previous_prefix, type->name (), NULL,
eb1e02fd 16702 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16703 }
5c4e30ca 16704 }
9219021c 16705
639d11d3 16706 if (die->child != NULL)
d9fa45fe 16707 {
639d11d3 16708 struct die_info *child_die = die->child;
6e70227d 16709
d9fa45fe
DC
16710 while (child_die && child_die->tag)
16711 {
e7c27a73 16712 process_die (child_die, cu);
436c571c 16713 child_die = child_die->sibling;
d9fa45fe
DC
16714 }
16715 }
38d518c9
EZ
16716}
16717
f55ee35c
JK
16718/* Read a Fortran module as type. This DIE can be only a declaration used for
16719 imported module. Still we need that type as local Fortran "use ... only"
16720 declaration imports depend on the created type in determine_prefix. */
16721
16722static struct type *
16723read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16724{
5e22e966 16725 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 16726 const char *module_name;
f55ee35c
JK
16727 struct type *type;
16728
16729 module_name = dwarf2_name (die, cu);
19f392bc 16730 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16731
f55ee35c
JK
16732 return set_die_type (die, type, cu);
16733}
16734
5d7cb8df
JK
16735/* Read a Fortran module. */
16736
16737static void
16738read_module (struct die_info *die, struct dwarf2_cu *cu)
16739{
16740 struct die_info *child_die = die->child;
530e8392
KB
16741 struct type *type;
16742
16743 type = read_type_die (die, cu);
16744 new_symbol (die, type, cu);
5d7cb8df 16745
5d7cb8df
JK
16746 while (child_die && child_die->tag)
16747 {
16748 process_die (child_die, cu);
436c571c 16749 child_die = child_die->sibling;
5d7cb8df
JK
16750 }
16751}
16752
38d518c9
EZ
16753/* Return the name of the namespace represented by DIE. Set
16754 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16755 namespace. */
16756
16757static const char *
e142c38c 16758namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16759{
16760 struct die_info *current_die;
16761 const char *name = NULL;
16762
16763 /* Loop through the extensions until we find a name. */
16764
16765 for (current_die = die;
16766 current_die != NULL;
f2f0e013 16767 current_die = dwarf2_extension (die, &cu))
38d518c9 16768 {
96553a0c
DE
16769 /* We don't use dwarf2_name here so that we can detect the absence
16770 of a name -> anonymous namespace. */
7d45c7c3 16771 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16772
38d518c9
EZ
16773 if (name != NULL)
16774 break;
16775 }
16776
16777 /* Is it an anonymous namespace? */
16778
16779 *is_anonymous = (name == NULL);
16780 if (*is_anonymous)
2b1dbab0 16781 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16782
16783 return name;
d9fa45fe
DC
16784}
16785
c906108c
SS
16786/* Extract all information from a DW_TAG_pointer_type DIE and add to
16787 the user defined type vector. */
16788
f792889a 16789static struct type *
e7c27a73 16790read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16791{
5e22e966 16792 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 16793 struct comp_unit_head *cu_header = &cu->header;
c906108c 16794 struct type *type;
8b2dbe47
KB
16795 struct attribute *attr_byte_size;
16796 struct attribute *attr_address_class;
16797 int byte_size, addr_class;
7e314c57
JK
16798 struct type *target_type;
16799
16800 target_type = die_type (die, cu);
c906108c 16801
7e314c57
JK
16802 /* The die_type call above may have already set the type for this DIE. */
16803 type = get_die_type (die, cu);
16804 if (type)
16805 return type;
16806
16807 type = lookup_pointer_type (target_type);
8b2dbe47 16808
e142c38c 16809 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16810 if (attr_byte_size)
16811 byte_size = DW_UNSND (attr_byte_size);
c906108c 16812 else
8b2dbe47
KB
16813 byte_size = cu_header->addr_size;
16814
e142c38c 16815 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16816 if (attr_address_class)
16817 addr_class = DW_UNSND (attr_address_class);
16818 else
16819 addr_class = DW_ADDR_none;
16820
2b4424c3
TT
16821 ULONGEST alignment = get_alignment (cu, die);
16822
16823 /* If the pointer size, alignment, or address class is different
16824 than the default, create a type variant marked as such and set
16825 the length accordingly. */
16826 if (TYPE_LENGTH (type) != byte_size
16827 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16828 && alignment != TYPE_RAW_ALIGN (type))
16829 || addr_class != DW_ADDR_none)
c906108c 16830 {
5e2b427d 16831 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16832 {
16833 int type_flags;
16834
849957d9 16835 type_flags = gdbarch_address_class_type_flags
5e2b427d 16836 (gdbarch, byte_size, addr_class);
876cecd0
TT
16837 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16838 == 0);
8b2dbe47
KB
16839 type = make_type_with_address_space (type, type_flags);
16840 }
16841 else if (TYPE_LENGTH (type) != byte_size)
16842 {
b98664d3 16843 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16844 }
2b4424c3
TT
16845 else if (TYPE_RAW_ALIGN (type) != alignment)
16846 {
b98664d3 16847 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16848 " - DIE at %s [in module %s]"),
16849 sect_offset_str (die->sect_off),
5e22e966 16850 objfile_name (cu->per_objfile->objfile));
2b4424c3 16851 }
6e70227d 16852 else
9a619af0
MS
16853 {
16854 /* Should we also complain about unhandled address classes? */
16855 }
c906108c 16856 }
8b2dbe47
KB
16857
16858 TYPE_LENGTH (type) = byte_size;
2b4424c3 16859 set_type_align (type, alignment);
f792889a 16860 return set_die_type (die, type, cu);
c906108c
SS
16861}
16862
16863/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16864 the user defined type vector. */
16865
f792889a 16866static struct type *
e7c27a73 16867read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16868{
16869 struct type *type;
16870 struct type *to_type;
16871 struct type *domain;
16872
e7c27a73
DJ
16873 to_type = die_type (die, cu);
16874 domain = die_containing_type (die, cu);
0d5de010 16875
7e314c57
JK
16876 /* The calls above may have already set the type for this DIE. */
16877 type = get_die_type (die, cu);
16878 if (type)
16879 return type;
16880
78134374 16881 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 16882 type = lookup_methodptr_type (to_type);
78134374 16883 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 16884 {
5e22e966 16885 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
16886
16887 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 16888 to_type->fields (), to_type->num_fields (),
7078baeb
TT
16889 TYPE_VARARGS (to_type));
16890 type = lookup_methodptr_type (new_type);
16891 }
0d5de010
DJ
16892 else
16893 type = lookup_memberptr_type (to_type, domain);
c906108c 16894
f792889a 16895 return set_die_type (die, type, cu);
c906108c
SS
16896}
16897
4297a3f0 16898/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16899 the user defined type vector. */
16900
f792889a 16901static struct type *
4297a3f0
AV
16902read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16903 enum type_code refcode)
c906108c 16904{
e7c27a73 16905 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16906 struct type *type, *target_type;
c906108c
SS
16907 struct attribute *attr;
16908
4297a3f0
AV
16909 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16910
7e314c57
JK
16911 target_type = die_type (die, cu);
16912
16913 /* The die_type call above may have already set the type for this DIE. */
16914 type = get_die_type (die, cu);
16915 if (type)
16916 return type;
16917
4297a3f0 16918 type = lookup_reference_type (target_type, refcode);
e142c38c 16919 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16920 if (attr != nullptr)
c906108c
SS
16921 {
16922 TYPE_LENGTH (type) = DW_UNSND (attr);
16923 }
16924 else
16925 {
107d2387 16926 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16927 }
2b4424c3 16928 maybe_set_alignment (cu, die, type);
f792889a 16929 return set_die_type (die, type, cu);
c906108c
SS
16930}
16931
cf363f18
MW
16932/* Add the given cv-qualifiers to the element type of the array. GCC
16933 outputs DWARF type qualifiers that apply to an array, not the
16934 element type. But GDB relies on the array element type to carry
16935 the cv-qualifiers. This mimics section 6.7.3 of the C99
16936 specification. */
16937
16938static struct type *
16939add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16940 struct type *base_type, int cnst, int voltl)
16941{
16942 struct type *el_type, *inner_array;
16943
16944 base_type = copy_type (base_type);
16945 inner_array = base_type;
16946
78134374 16947 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16948 {
16949 TYPE_TARGET_TYPE (inner_array) =
16950 copy_type (TYPE_TARGET_TYPE (inner_array));
16951 inner_array = TYPE_TARGET_TYPE (inner_array);
16952 }
16953
16954 el_type = TYPE_TARGET_TYPE (inner_array);
16955 cnst |= TYPE_CONST (el_type);
16956 voltl |= TYPE_VOLATILE (el_type);
16957 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16958
16959 return set_die_type (die, base_type, cu);
16960}
16961
f792889a 16962static struct type *
e7c27a73 16963read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16964{
f792889a 16965 struct type *base_type, *cv_type;
c906108c 16966
e7c27a73 16967 base_type = die_type (die, cu);
7e314c57
JK
16968
16969 /* The die_type call above may have already set the type for this DIE. */
16970 cv_type = get_die_type (die, cu);
16971 if (cv_type)
16972 return cv_type;
16973
2f608a3a
KW
16974 /* In case the const qualifier is applied to an array type, the element type
16975 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 16976 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 16977 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16978
f792889a
DJ
16979 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16980 return set_die_type (die, cv_type, cu);
c906108c
SS
16981}
16982
f792889a 16983static struct type *
e7c27a73 16984read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16985{
f792889a 16986 struct type *base_type, *cv_type;
c906108c 16987
e7c27a73 16988 base_type = die_type (die, cu);
7e314c57
JK
16989
16990 /* The die_type call above may have already set the type for this DIE. */
16991 cv_type = get_die_type (die, cu);
16992 if (cv_type)
16993 return cv_type;
16994
cf363f18
MW
16995 /* In case the volatile qualifier is applied to an array type, the
16996 element type is so qualified, not the array type (section 6.7.3
16997 of C99). */
78134374 16998 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16999 return add_array_cv_type (die, cu, base_type, 0, 1);
17000
f792889a
DJ
17001 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17002 return set_die_type (die, cv_type, cu);
c906108c
SS
17003}
17004
06d66ee9
TT
17005/* Handle DW_TAG_restrict_type. */
17006
17007static struct type *
17008read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17009{
17010 struct type *base_type, *cv_type;
17011
17012 base_type = die_type (die, cu);
17013
17014 /* The die_type call above may have already set the type for this DIE. */
17015 cv_type = get_die_type (die, cu);
17016 if (cv_type)
17017 return cv_type;
17018
17019 cv_type = make_restrict_type (base_type);
17020 return set_die_type (die, cv_type, cu);
17021}
17022
a2c2acaf
MW
17023/* Handle DW_TAG_atomic_type. */
17024
17025static struct type *
17026read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17027{
17028 struct type *base_type, *cv_type;
17029
17030 base_type = die_type (die, cu);
17031
17032 /* The die_type call above may have already set the type for this DIE. */
17033 cv_type = get_die_type (die, cu);
17034 if (cv_type)
17035 return cv_type;
17036
17037 cv_type = make_atomic_type (base_type);
17038 return set_die_type (die, cv_type, cu);
17039}
17040
c906108c
SS
17041/* Extract all information from a DW_TAG_string_type DIE and add to
17042 the user defined type vector. It isn't really a user defined type,
17043 but it behaves like one, with other DIE's using an AT_user_def_type
17044 attribute to reference it. */
17045
f792889a 17046static struct type *
e7c27a73 17047read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17048{
5e22e966 17049 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 17050 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
17051 struct type *type, *range_type, *index_type, *char_type;
17052 struct attribute *attr;
216a7e6b
AB
17053 struct dynamic_prop prop;
17054 bool length_is_constant = true;
17055 LONGEST length;
17056
17057 /* There are a couple of places where bit sizes might be made use of
17058 when parsing a DW_TAG_string_type, however, no producer that we know
17059 of make use of these. Handling bit sizes that are a multiple of the
17060 byte size is easy enough, but what about other bit sizes? Lets deal
17061 with that problem when we have to. Warn about these attributes being
17062 unsupported, then parse the type and ignore them like we always
17063 have. */
17064 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17065 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17066 {
17067 static bool warning_printed = false;
17068 if (!warning_printed)
17069 {
17070 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17071 "currently supported on DW_TAG_string_type."));
17072 warning_printed = true;
17073 }
17074 }
c906108c 17075
e142c38c 17076 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 17077 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
17078 {
17079 /* The string length describes the location at which the length of
17080 the string can be found. The size of the length field can be
17081 specified with one of the attributes below. */
17082 struct type *prop_type;
17083 struct attribute *len
17084 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17085 if (len == nullptr)
17086 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17087 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17088 {
17089 /* Pass 0 as the default as we know this attribute is constant
17090 and the default value will not be returned. */
0826b30a 17091 LONGEST sz = len->constant_value (0);
293e7e51 17092 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17093 }
17094 else
17095 {
17096 /* If the size is not specified then we assume it is the size of
17097 an address on this target. */
293e7e51 17098 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17099 }
17100
17101 /* Convert the attribute into a dynamic property. */
17102 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17103 length = 1;
17104 else
17105 length_is_constant = false;
17106 }
17107 else if (attr != nullptr)
17108 {
17109 /* This DW_AT_string_length just contains the length with no
17110 indirection. There's no need to create a dynamic property in this
17111 case. Pass 0 for the default value as we know it will not be
17112 returned in this case. */
0826b30a 17113 length = attr->constant_value (0);
216a7e6b
AB
17114 }
17115 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17116 {
216a7e6b 17117 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17118 length = attr->constant_value (1);
c906108c
SS
17119 }
17120 else
17121 {
216a7e6b
AB
17122 /* Use 1 as a fallback length if we have nothing else. */
17123 length = 1;
c906108c 17124 }
6ccb9162 17125
46bf5051 17126 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17127 if (length_is_constant)
17128 range_type = create_static_range_type (NULL, index_type, 1, length);
17129 else
17130 {
17131 struct dynamic_prop low_bound;
17132
17133 low_bound.kind = PROP_CONST;
17134 low_bound.data.const_val = 1;
17135 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17136 }
3b7538c0
UW
17137 char_type = language_string_char_type (cu->language_defn, gdbarch);
17138 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17139
f792889a 17140 return set_die_type (die, type, cu);
c906108c
SS
17141}
17142
4d804846
JB
17143/* Assuming that DIE corresponds to a function, returns nonzero
17144 if the function is prototyped. */
17145
17146static int
17147prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17148{
17149 struct attribute *attr;
17150
17151 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17152 if (attr && (DW_UNSND (attr) != 0))
17153 return 1;
17154
17155 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17156 is only meaningful for C, but the concept also extends to other
4d804846
JB
17157 languages that allow unprototyped functions (Eg: Objective C).
17158 For all other languages, assume that functions are always
17159 prototyped. */
17160 if (cu->language != language_c
17161 && cu->language != language_objc
17162 && cu->language != language_opencl)
17163 return 1;
17164
17165 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17166 prototyped and unprototyped functions; default to prototyped,
17167 since that is more common in modern code (and RealView warns
17168 about unprototyped functions). */
17169 if (producer_is_realview (cu->producer))
17170 return 1;
17171
17172 return 0;
17173}
17174
c906108c
SS
17175/* Handle DIES due to C code like:
17176
17177 struct foo
c5aa993b
JM
17178 {
17179 int (*funcp)(int a, long l);
17180 int b;
17181 };
c906108c 17182
0963b4bd 17183 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17184
f792889a 17185static struct type *
e7c27a73 17186read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17187{
5e22e966 17188 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
17189 struct type *type; /* Type that this function returns. */
17190 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17191 struct attribute *attr;
17192
e7c27a73 17193 type = die_type (die, cu);
7e314c57
JK
17194
17195 /* The die_type call above may have already set the type for this DIE. */
17196 ftype = get_die_type (die, cu);
17197 if (ftype)
17198 return ftype;
17199
0c8b41f1 17200 ftype = lookup_function_type (type);
c906108c 17201
4d804846 17202 if (prototyped_function_p (die, cu))
a6c727b2 17203 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17204
c055b101
CV
17205 /* Store the calling convention in the type if it's available in
17206 the subroutine die. Otherwise set the calling convention to
17207 the default value DW_CC_normal. */
17208 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17209 if (attr != nullptr
17210 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17211 TYPE_CALLING_CONVENTION (ftype)
17212 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17213 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17214 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17215 else
17216 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17217
743649fd
MW
17218 /* Record whether the function returns normally to its caller or not
17219 if the DWARF producer set that information. */
17220 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17221 if (attr && (DW_UNSND (attr) != 0))
17222 TYPE_NO_RETURN (ftype) = 1;
17223
76c10ea2
GM
17224 /* We need to add the subroutine type to the die immediately so
17225 we don't infinitely recurse when dealing with parameters
0963b4bd 17226 declared as the same subroutine type. */
76c10ea2 17227 set_die_type (die, ftype, cu);
6e70227d 17228
639d11d3 17229 if (die->child != NULL)
c906108c 17230 {
bb5ed363 17231 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17232 struct die_info *child_die;
8072405b 17233 int nparams, iparams;
c906108c
SS
17234
17235 /* Count the number of parameters.
17236 FIXME: GDB currently ignores vararg functions, but knows about
17237 vararg member functions. */
8072405b 17238 nparams = 0;
639d11d3 17239 child_die = die->child;
c906108c
SS
17240 while (child_die && child_die->tag)
17241 {
17242 if (child_die->tag == DW_TAG_formal_parameter)
17243 nparams++;
17244 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17245 TYPE_VARARGS (ftype) = 1;
436c571c 17246 child_die = child_die->sibling;
c906108c
SS
17247 }
17248
17249 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17250 ftype->set_num_fields (nparams);
3cabb6b0
SM
17251 ftype->set_fields
17252 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17253
8072405b
JK
17254 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17255 even if we error out during the parameters reading below. */
17256 for (iparams = 0; iparams < nparams; iparams++)
5d14b6e5 17257 ftype->field (iparams).set_type (void_type);
8072405b
JK
17258
17259 iparams = 0;
639d11d3 17260 child_die = die->child;
c906108c
SS
17261 while (child_die && child_die->tag)
17262 {
17263 if (child_die->tag == DW_TAG_formal_parameter)
17264 {
3ce3b1ba
PA
17265 struct type *arg_type;
17266
17267 /* DWARF version 2 has no clean way to discern C++
17268 static and non-static member functions. G++ helps
17269 GDB by marking the first parameter for non-static
17270 member functions (which is the this pointer) as
17271 artificial. We pass this information to
17272 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17273
17274 DWARF version 3 added DW_AT_object_pointer, which GCC
17275 4.5 does not yet generate. */
e142c38c 17276 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17277 if (attr != nullptr)
c906108c
SS
17278 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17279 else
9c37b5ae 17280 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17281 arg_type = die_type (child_die, cu);
17282
17283 /* RealView does not mark THIS as const, which the testsuite
17284 expects. GCC marks THIS as const in method definitions,
17285 but not in the class specifications (GCC PR 43053). */
17286 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17287 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17288 {
17289 int is_this = 0;
17290 struct dwarf2_cu *arg_cu = cu;
17291 const char *name = dwarf2_name (child_die, cu);
17292
17293 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17294 if (attr != nullptr)
3ce3b1ba
PA
17295 {
17296 /* If the compiler emits this, use it. */
17297 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17298 is_this = 1;
17299 }
17300 else if (name && strcmp (name, "this") == 0)
17301 /* Function definitions will have the argument names. */
17302 is_this = 1;
17303 else if (name == NULL && iparams == 0)
17304 /* Declarations may not have the names, so like
17305 elsewhere in GDB, assume an artificial first
17306 argument is "this". */
17307 is_this = 1;
17308
17309 if (is_this)
17310 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17311 arg_type, 0);
17312 }
17313
5d14b6e5 17314 ftype->field (iparams).set_type (arg_type);
c906108c
SS
17315 iparams++;
17316 }
436c571c 17317 child_die = child_die->sibling;
c906108c
SS
17318 }
17319 }
17320
76c10ea2 17321 return ftype;
c906108c
SS
17322}
17323
f792889a 17324static struct type *
e7c27a73 17325read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17326{
5e22e966 17327 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17328 const char *name = NULL;
3c8e0968 17329 struct type *this_type, *target_type;
c906108c 17330
94af9270 17331 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17332 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17333 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17334 set_die_type (die, this_type, cu);
3c8e0968
DE
17335 target_type = die_type (die, cu);
17336 if (target_type != this_type)
17337 TYPE_TARGET_TYPE (this_type) = target_type;
17338 else
17339 {
17340 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17341 spec and cause infinite loops in GDB. */
b98664d3 17342 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17343 "- DIE at %s [in module %s]"),
17344 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17345 TYPE_TARGET_TYPE (this_type) = NULL;
17346 }
e4003a34
TV
17347 if (name == NULL)
17348 {
17349 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17350 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17351 Handle these by just returning the target type, rather than
17352 constructing an anonymous typedef type and trying to handle this
17353 elsewhere. */
17354 set_die_type (die, target_type, cu);
17355 return target_type;
17356 }
f792889a 17357 return this_type;
c906108c
SS
17358}
17359
9b790ce7
UW
17360/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17361 (which may be different from NAME) to the architecture back-end to allow
17362 it to guess the correct format if necessary. */
17363
17364static struct type *
17365dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17366 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 17367{
08feed99 17368 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
17369 const struct floatformat **format;
17370 struct type *type;
17371
17372 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17373 if (format)
103a685e 17374 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17375 else
77b7c781 17376 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17377
17378 return type;
17379}
17380
eb77c9df
AB
17381/* Allocate an integer type of size BITS and name NAME. */
17382
17383static struct type *
17384dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17385 int bits, int unsigned_p, const char *name)
17386{
17387 struct type *type;
17388
17389 /* Versions of Intel's C Compiler generate an integer type called "void"
17390 instead of using DW_TAG_unspecified_type. This has been seen on
17391 at least versions 14, 17, and 18. */
35ee2dc2
AB
17392 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17393 && strcmp (name, "void") == 0)
eb77c9df
AB
17394 type = objfile_type (objfile)->builtin_void;
17395 else
17396 type = init_integer_type (objfile, bits, unsigned_p, name);
17397
17398 return type;
17399}
17400
8bdc1658
AB
17401/* Initialise and return a floating point type of size BITS suitable for
17402 use as a component of a complex number. The NAME_HINT is passed through
17403 when initialising the floating point type and is the name of the complex
17404 type.
17405
17406 As DWARF doesn't currently provide an explicit name for the components
17407 of a complex number, but it can be helpful to have these components
17408 named, we try to select a suitable name based on the size of the
17409 component. */
17410static struct type *
17411dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17412 struct objfile *objfile,
103a685e
TT
17413 int bits, const char *name_hint,
17414 enum bfd_endian byte_order)
8bdc1658 17415{
08feed99 17416 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
17417 struct type *tt = nullptr;
17418
35add35e
AB
17419 /* Try to find a suitable floating point builtin type of size BITS.
17420 We're going to use the name of this type as the name for the complex
17421 target type that we are about to create. */
1db455a7 17422 switch (cu->language)
8bdc1658 17423 {
1db455a7
AB
17424 case language_fortran:
17425 switch (bits)
17426 {
17427 case 32:
17428 tt = builtin_f_type (gdbarch)->builtin_real;
17429 break;
17430 case 64:
17431 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17432 break;
17433 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17434 case 128:
17435 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17436 break;
17437 }
8bdc1658 17438 break;
1db455a7
AB
17439 default:
17440 switch (bits)
17441 {
17442 case 32:
17443 tt = builtin_type (gdbarch)->builtin_float;
17444 break;
17445 case 64:
17446 tt = builtin_type (gdbarch)->builtin_double;
17447 break;
17448 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17449 case 128:
17450 tt = builtin_type (gdbarch)->builtin_long_double;
17451 break;
17452 }
8bdc1658
AB
17453 break;
17454 }
17455
35add35e
AB
17456 /* If the type we found doesn't match the size we were looking for, then
17457 pretend we didn't find a type at all, the complex target type we
17458 create will then be nameless. */
a12e5744 17459 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17460 tt = nullptr;
17461
7d93a1e0 17462 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 17463 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17464}
17465
c906108c
SS
17466/* Find a representation of a given base type and install
17467 it in the TYPE field of the die. */
17468
f792889a 17469static struct type *
e7c27a73 17470read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17471{
5e22e966 17472 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
17473 struct type *type;
17474 struct attribute *attr;
19f392bc 17475 int encoding = 0, bits = 0;
15d034d0 17476 const char *name;
34877895 17477 gdbarch *arch;
c906108c 17478
e142c38c 17479 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17480 if (attr != nullptr)
34877895 17481 encoding = DW_UNSND (attr);
e142c38c 17482 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17483 if (attr != nullptr)
34877895 17484 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17485 name = dwarf2_name (die, cu);
6ccb9162 17486 if (!name)
34877895 17487 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 17488
08feed99 17489 arch = objfile->arch ();
103a685e
TT
17490 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17491
34877895
PJ
17492 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17493 if (attr)
103a685e
TT
17494 {
17495 int endianity = DW_UNSND (attr);
17496
17497 switch (endianity)
17498 {
17499 case DW_END_big:
17500 byte_order = BFD_ENDIAN_BIG;
17501 break;
17502 case DW_END_little:
17503 byte_order = BFD_ENDIAN_LITTLE;
17504 break;
17505 default:
17506 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17507 break;
17508 }
17509 }
6ccb9162
UW
17510
17511 switch (encoding)
c906108c 17512 {
6ccb9162
UW
17513 case DW_ATE_address:
17514 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17515 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17516 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17517 break;
17518 case DW_ATE_boolean:
19f392bc 17519 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17520 break;
17521 case DW_ATE_complex_float:
103a685e
TT
17522 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17523 byte_order);
78134374 17524 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
17525 {
17526 if (name == nullptr)
17527 {
17528 struct obstack *obstack
5e22e966 17529 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 17530 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
17531 nullptr);
17532 }
17533 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17534 }
17535 else
17536 type = init_complex_type (name, type);
6ccb9162
UW
17537 break;
17538 case DW_ATE_decimal_float:
19f392bc 17539 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17540 break;
17541 case DW_ATE_float:
103a685e 17542 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17543 break;
17544 case DW_ATE_signed:
eb77c9df 17545 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17546 break;
17547 case DW_ATE_unsigned:
3b2b8fea
TT
17548 if (cu->language == language_fortran
17549 && name
61012eef 17550 && startswith (name, "character("))
19f392bc
UW
17551 type = init_character_type (objfile, bits, 1, name);
17552 else
eb77c9df 17553 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17554 break;
17555 case DW_ATE_signed_char:
6e70227d 17556 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17557 || cu->language == language_pascal
17558 || cu->language == language_fortran)
19f392bc
UW
17559 type = init_character_type (objfile, bits, 0, name);
17560 else
eb77c9df 17561 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17562 break;
17563 case DW_ATE_unsigned_char:
868a0084 17564 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17565 || cu->language == language_pascal
c44af4eb
TT
17566 || cu->language == language_fortran
17567 || cu->language == language_rust)
19f392bc
UW
17568 type = init_character_type (objfile, bits, 1, name);
17569 else
eb77c9df 17570 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17571 break;
75079b2b 17572 case DW_ATE_UTF:
53e710ac 17573 {
53e710ac
PA
17574 if (bits == 16)
17575 type = builtin_type (arch)->builtin_char16;
17576 else if (bits == 32)
17577 type = builtin_type (arch)->builtin_char32;
17578 else
17579 {
b98664d3 17580 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17581 bits);
eb77c9df 17582 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17583 }
17584 return set_die_type (die, type, cu);
17585 }
75079b2b
TT
17586 break;
17587
6ccb9162 17588 default:
b98664d3 17589 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17590 dwarf_type_encoding_name (encoding));
77b7c781 17591 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17592 break;
c906108c 17593 }
6ccb9162 17594
0114d602 17595 if (name && strcmp (name, "char") == 0)
876cecd0 17596 TYPE_NOSIGN (type) = 1;
0114d602 17597
2b4424c3
TT
17598 maybe_set_alignment (cu, die, type);
17599
103a685e 17600 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17601
f792889a 17602 return set_die_type (die, type, cu);
c906108c
SS
17603}
17604
80180f79
SA
17605/* Parse dwarf attribute if it's a block, reference or constant and put the
17606 resulting value of the attribute into struct bound_prop.
17607 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17608
17609static int
17610attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17611 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17612 struct type *default_type)
80180f79
SA
17613{
17614 struct dwarf2_property_baton *baton;
5e22e966 17615 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
17616 struct objfile *objfile = per_objfile->objfile;
17617 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 17618
9a49df9d
AB
17619 gdb_assert (default_type != NULL);
17620
80180f79
SA
17621 if (attr == NULL || prop == NULL)
17622 return 0;
17623
4fc6c0d5 17624 if (attr->form_is_block ())
80180f79 17625 {
8d749320 17626 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17627 baton->property_type = default_type;
80180f79 17628 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17629 baton->locexpr.per_objfile = per_objfile;
80180f79
SA
17630 baton->locexpr.size = DW_BLOCK (attr)->size;
17631 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17632 switch (attr->name)
17633 {
17634 case DW_AT_string_length:
17635 baton->locexpr.is_reference = true;
17636 break;
17637 default:
17638 baton->locexpr.is_reference = false;
17639 break;
17640 }
80180f79
SA
17641 prop->data.baton = baton;
17642 prop->kind = PROP_LOCEXPR;
17643 gdb_assert (prop->data.baton != NULL);
17644 }
cd6c91b4 17645 else if (attr->form_is_ref ())
80180f79
SA
17646 {
17647 struct dwarf2_cu *target_cu = cu;
17648 struct die_info *target_die;
17649 struct attribute *target_attr;
17650
17651 target_die = follow_die_ref (die, attr, &target_cu);
17652 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17653 if (target_attr == NULL)
17654 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17655 target_cu);
80180f79
SA
17656 if (target_attr == NULL)
17657 return 0;
17658
df25ebbd 17659 switch (target_attr->name)
80180f79 17660 {
df25ebbd 17661 case DW_AT_location:
cd6c91b4 17662 if (target_attr->form_is_section_offset ())
df25ebbd 17663 {
8d749320 17664 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17665 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17666 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17667 prop->data.baton = baton;
17668 prop->kind = PROP_LOCLIST;
17669 gdb_assert (prop->data.baton != NULL);
17670 }
4fc6c0d5 17671 else if (target_attr->form_is_block ())
df25ebbd 17672 {
8d749320 17673 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17674 baton->property_type = die_type (target_die, target_cu);
df25ebbd 17675 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17676 baton->locexpr.per_objfile = per_objfile;
df25ebbd
JB
17677 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17678 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17679 baton->locexpr.is_reference = true;
df25ebbd
JB
17680 prop->data.baton = baton;
17681 prop->kind = PROP_LOCEXPR;
17682 gdb_assert (prop->data.baton != NULL);
17683 }
17684 else
17685 {
17686 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17687 "dynamic property");
17688 return 0;
17689 }
17690 break;
17691 case DW_AT_data_member_location:
17692 {
17693 LONGEST offset;
17694
17695 if (!handle_data_member_location (target_die, target_cu,
17696 &offset))
17697 return 0;
17698
8d749320 17699 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17700 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17701 target_cu);
df25ebbd
JB
17702 baton->offset_info.offset = offset;
17703 baton->offset_info.type = die_type (target_die, target_cu);
17704 prop->data.baton = baton;
17705 prop->kind = PROP_ADDR_OFFSET;
17706 break;
17707 }
80180f79
SA
17708 }
17709 }
cd6c91b4 17710 else if (attr->form_is_constant ())
80180f79 17711 {
0826b30a 17712 prop->data.const_val = attr->constant_value (0);
80180f79
SA
17713 prop->kind = PROP_CONST;
17714 }
17715 else
17716 {
17717 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17718 dwarf2_name (die, cu));
17719 return 0;
17720 }
17721
17722 return 1;
17723}
17724
09ba997f 17725/* See read.h. */
9a49df9d 17726
09ba997f 17727struct type *
293e7e51 17728dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17729{
9a49df9d
AB
17730 struct type *int_type;
17731
17732 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17733#define TRY_TYPE(F) \
17734 int_type = (unsigned_p \
17735 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17736 : objfile_type (objfile)->builtin_ ## F); \
17737 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17738 return int_type
17739
17740 TRY_TYPE (char);
17741 TRY_TYPE (short);
17742 TRY_TYPE (int);
17743 TRY_TYPE (long);
17744 TRY_TYPE (long_long);
17745
17746#undef TRY_TYPE
17747
17748 gdb_assert_not_reached ("unable to find suitable integer type");
17749}
17750
09ba997f 17751/* See read.h. */
11a8b164 17752
09ba997f 17753struct type *
293e7e51 17754dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
11a8b164 17755{
293e7e51
SM
17756 int addr_size = this->per_cu->addr_size ();
17757 return this->per_objfile->int_type (addr_size, unsigned_p);
11a8b164
AB
17758}
17759
b86352cf
AB
17760/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17761 present (which is valid) then compute the default type based on the
17762 compilation units address size. */
17763
17764static struct type *
17765read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17766{
17767 struct type *index_type = die_type (die, cu);
17768
17769 /* Dwarf-2 specifications explicitly allows to create subrange types
17770 without specifying a base type.
17771 In that case, the base type must be set to the type of
17772 the lower bound, upper bound or count, in that order, if any of these
17773 three attributes references an object that has a type.
17774 If no base type is found, the Dwarf-2 specifications say that
17775 a signed integer type of size equal to the size of an address should
17776 be used.
17777 For the following C code: `extern char gdb_int [];'
17778 GCC produces an empty range DIE.
17779 FIXME: muller/2010-05-28: Possible references to object for low bound,
17780 high bound or count are not yet handled by this code. */
78134374 17781 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 17782 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
17783
17784 return index_type;
17785}
17786
a02abb62
JB
17787/* Read the given DW_AT_subrange DIE. */
17788
f792889a 17789static struct type *
a02abb62
JB
17790read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17791{
4c9ad8c2 17792 struct type *base_type, *orig_base_type;
a02abb62
JB
17793 struct type *range_type;
17794 struct attribute *attr;
729efb13 17795 struct dynamic_prop low, high;
4fae6e18 17796 int low_default_is_valid;
c451ebe5 17797 int high_bound_is_count = 0;
15d034d0 17798 const char *name;
d359392f 17799 ULONGEST negative_mask;
e77813c8 17800
b86352cf
AB
17801 orig_base_type = read_subrange_index_type (die, cu);
17802
4c9ad8c2
TT
17803 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17804 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17805 creating the range type, but we use the result of check_typedef
17806 when examining properties of the type. */
17807 base_type = check_typedef (orig_base_type);
a02abb62 17808
7e314c57
JK
17809 /* The die_type call above may have already set the type for this DIE. */
17810 range_type = get_die_type (die, cu);
17811 if (range_type)
17812 return range_type;
17813
729efb13
SA
17814 low.kind = PROP_CONST;
17815 high.kind = PROP_CONST;
17816 high.data.const_val = 0;
17817
4fae6e18
JK
17818 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17819 omitting DW_AT_lower_bound. */
17820 switch (cu->language)
6e70227d 17821 {
4fae6e18
JK
17822 case language_c:
17823 case language_cplus:
729efb13 17824 low.data.const_val = 0;
4fae6e18
JK
17825 low_default_is_valid = 1;
17826 break;
17827 case language_fortran:
729efb13 17828 low.data.const_val = 1;
4fae6e18
JK
17829 low_default_is_valid = 1;
17830 break;
17831 case language_d:
4fae6e18 17832 case language_objc:
c44af4eb 17833 case language_rust:
729efb13 17834 low.data.const_val = 0;
4fae6e18
JK
17835 low_default_is_valid = (cu->header.version >= 4);
17836 break;
17837 case language_ada:
17838 case language_m2:
17839 case language_pascal:
729efb13 17840 low.data.const_val = 1;
4fae6e18
JK
17841 low_default_is_valid = (cu->header.version >= 4);
17842 break;
17843 default:
729efb13 17844 low.data.const_val = 0;
4fae6e18
JK
17845 low_default_is_valid = 0;
17846 break;
a02abb62
JB
17847 }
17848
e142c38c 17849 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17850 if (attr != nullptr)
9a49df9d 17851 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17852 else if (!low_default_is_valid)
b98664d3 17853 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17854 "- DIE at %s [in module %s]"),
17855 sect_offset_str (die->sect_off),
5e22e966 17856 objfile_name (cu->per_objfile->objfile));
a02abb62 17857
506f5c41
TV
17858 struct attribute *attr_ub, *attr_count;
17859 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17860 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17861 {
506f5c41 17862 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17863 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17864 {
c451ebe5
SA
17865 /* If bounds are constant do the final calculation here. */
17866 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17867 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17868 else
17869 high_bound_is_count = 1;
c2ff108b 17870 }
506f5c41
TV
17871 else
17872 {
17873 if (attr_ub != NULL)
17874 complaint (_("Unresolved DW_AT_upper_bound "
17875 "- DIE at %s [in module %s]"),
17876 sect_offset_str (die->sect_off),
5e22e966 17877 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
17878 if (attr_count != NULL)
17879 complaint (_("Unresolved DW_AT_count "
17880 "- DIE at %s [in module %s]"),
17881 sect_offset_str (die->sect_off),
5e22e966 17882 objfile_name (cu->per_objfile->objfile));
506f5c41 17883 }
e77813c8 17884 }
a02abb62 17885
4e962e74
TT
17886 LONGEST bias = 0;
17887 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17888 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 17889 bias = bias_attr->constant_value (0);
4e962e74 17890
dbb9c2b1
JB
17891 /* Normally, the DWARF producers are expected to use a signed
17892 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17893 But this is unfortunately not always the case, as witnessed
17894 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17895 is used instead. To work around that ambiguity, we treat
17896 the bounds as signed, and thus sign-extend their values, when
17897 the base type is signed. */
6e70227d 17898 negative_mask =
d359392f 17899 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17900 if (low.kind == PROP_CONST
17901 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17902 low.data.const_val |= negative_mask;
17903 if (high.kind == PROP_CONST
17904 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17905 high.data.const_val |= negative_mask;
43bbcdc2 17906
5bbd8269
AB
17907 /* Check for bit and byte strides. */
17908 struct dynamic_prop byte_stride_prop;
17909 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17910 if (attr_byte_stride != nullptr)
17911 {
293e7e51 17912 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
17913 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17914 prop_type);
17915 }
17916
17917 struct dynamic_prop bit_stride_prop;
17918 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17919 if (attr_bit_stride != nullptr)
17920 {
17921 /* It only makes sense to have either a bit or byte stride. */
17922 if (attr_byte_stride != nullptr)
17923 {
17924 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17925 "- DIE at %s [in module %s]"),
17926 sect_offset_str (die->sect_off),
5e22e966 17927 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
17928 attr_bit_stride = nullptr;
17929 }
17930 else
17931 {
293e7e51 17932 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
17933 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17934 prop_type);
17935 }
17936 }
17937
17938 if (attr_byte_stride != nullptr
17939 || attr_bit_stride != nullptr)
17940 {
17941 bool byte_stride_p = (attr_byte_stride != nullptr);
17942 struct dynamic_prop *stride
17943 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17944
17945 range_type
17946 = create_range_type_with_stride (NULL, orig_base_type, &low,
17947 &high, bias, stride, byte_stride_p);
17948 }
17949 else
17950 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17951
c451ebe5
SA
17952 if (high_bound_is_count)
17953 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17954
c2ff108b
JK
17955 /* Ada expects an empty array on no boundary attributes. */
17956 if (attr == NULL && cu->language != language_ada)
729efb13 17957 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17958
39cbfefa
DJ
17959 name = dwarf2_name (die, cu);
17960 if (name)
d0e39ea2 17961 range_type->set_name (name);
6e70227d 17962
e142c38c 17963 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17964 if (attr != nullptr)
a02abb62
JB
17965 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17966
2b4424c3
TT
17967 maybe_set_alignment (cu, die, range_type);
17968
7e314c57
JK
17969 set_die_type (die, range_type, cu);
17970
17971 /* set_die_type should be already done. */
b4ba55a1
JB
17972 set_descriptive_type (range_type, die, cu);
17973
7e314c57 17974 return range_type;
a02abb62 17975}
6e70227d 17976
f792889a 17977static struct type *
81a17f79
JB
17978read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17979{
17980 struct type *type;
81a17f79 17981
5e22e966 17982 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 17983 type->set_name (dwarf2_name (die, cu));
81a17f79 17984
74a2f8ff 17985 /* In Ada, an unspecified type is typically used when the description
85102364 17986 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17987 such a type, we treat it as a stub, and try to resolve it later on,
17988 when needed. */
17989 if (cu->language == language_ada)
17990 TYPE_STUB (type) = 1;
17991
f792889a 17992 return set_die_type (die, type, cu);
81a17f79 17993}
a02abb62 17994
639d11d3
DC
17995/* Read a single die and all its descendents. Set the die's sibling
17996 field to NULL; set other fields in the die correctly, and set all
17997 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17998 location of the info_ptr after reading all of those dies. PARENT
17999 is the parent of the die in question. */
18000
18001static struct die_info *
dee91e82 18002read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18003 const gdb_byte *info_ptr,
18004 const gdb_byte **new_info_ptr,
dee91e82 18005 struct die_info *parent)
639d11d3
DC
18006{
18007 struct die_info *die;
d521ce57 18008 const gdb_byte *cur_ptr;
639d11d3 18009
3e225074 18010 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
18011 if (die == NULL)
18012 {
18013 *new_info_ptr = cur_ptr;
18014 return NULL;
18015 }
93311388 18016 store_in_ref_table (die, reader->cu);
639d11d3 18017
3e225074 18018 if (die->has_children)
bf6af496 18019 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18020 else
18021 {
18022 die->child = NULL;
18023 *new_info_ptr = cur_ptr;
18024 }
18025
18026 die->sibling = NULL;
18027 die->parent = parent;
18028 return die;
18029}
18030
18031/* Read a die, all of its descendents, and all of its siblings; set
18032 all of the fields of all of the dies correctly. Arguments are as
18033 in read_die_and_children. */
18034
18035static struct die_info *
bf6af496 18036read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18037 const gdb_byte *info_ptr,
18038 const gdb_byte **new_info_ptr,
bf6af496 18039 struct die_info *parent)
639d11d3
DC
18040{
18041 struct die_info *first_die, *last_sibling;
d521ce57 18042 const gdb_byte *cur_ptr;
639d11d3 18043
c906108c 18044 cur_ptr = info_ptr;
639d11d3
DC
18045 first_die = last_sibling = NULL;
18046
18047 while (1)
c906108c 18048 {
639d11d3 18049 struct die_info *die
dee91e82 18050 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18051
1d325ec1 18052 if (die == NULL)
c906108c 18053 {
639d11d3
DC
18054 *new_info_ptr = cur_ptr;
18055 return first_die;
c906108c 18056 }
1d325ec1
DJ
18057
18058 if (!first_die)
18059 first_die = die;
c906108c 18060 else
1d325ec1
DJ
18061 last_sibling->sibling = die;
18062
18063 last_sibling = die;
c906108c 18064 }
c906108c
SS
18065}
18066
bf6af496
DE
18067/* Read a die, all of its descendents, and all of its siblings; set
18068 all of the fields of all of the dies correctly. Arguments are as
18069 in read_die_and_children.
18070 This the main entry point for reading a DIE and all its children. */
18071
18072static struct die_info *
18073read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18074 const gdb_byte *info_ptr,
18075 const gdb_byte **new_info_ptr,
bf6af496
DE
18076 struct die_info *parent)
18077{
18078 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18079 new_info_ptr, parent);
18080
b4f54984 18081 if (dwarf_die_debug)
bf6af496
DE
18082 {
18083 fprintf_unfiltered (gdb_stdlog,
18084 "Read die from %s@0x%x of %s:\n",
96b79293 18085 reader->die_section->get_name (),
bf6af496
DE
18086 (unsigned) (info_ptr - reader->die_section->buffer),
18087 bfd_get_filename (reader->abfd));
b4f54984 18088 dump_die (die, dwarf_die_debug);
bf6af496
DE
18089 }
18090
18091 return die;
18092}
18093
3019eac3
DE
18094/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18095 attributes.
18096 The caller is responsible for filling in the extra attributes
18097 and updating (*DIEP)->num_attrs.
18098 Set DIEP to point to a newly allocated die with its information,
3e225074 18099 except for its child, sibling, and parent fields. */
93311388 18100
d521ce57 18101static const gdb_byte *
3019eac3 18102read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18103 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 18104 int num_extra_attrs)
93311388 18105{
b64f50a1 18106 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18107 struct abbrev_info *abbrev;
18108 struct die_info *die;
18109 struct dwarf2_cu *cu = reader->cu;
18110 bfd *abfd = reader->abfd;
18111
9c541725 18112 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18113 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18114 info_ptr += bytes_read;
18115 if (!abbrev_number)
18116 {
18117 *diep = NULL;
93311388
DE
18118 return info_ptr;
18119 }
18120
685af9cd 18121 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18122 if (!abbrev)
348e048f
DE
18123 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18124 abbrev_number,
18125 bfd_get_filename (abfd));
18126
3019eac3 18127 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18128 die->sect_off = sect_off;
93311388
DE
18129 die->tag = abbrev->tag;
18130 die->abbrev = abbrev_number;
3e225074 18131 die->has_children = abbrev->has_children;
93311388 18132
3019eac3
DE
18133 /* Make the result usable.
18134 The caller needs to update num_attrs after adding the extra
18135 attributes. */
93311388
DE
18136 die->num_attrs = abbrev->num_attrs;
18137
18a8505e 18138 std::vector<int> indexes_that_need_reprocess;
93311388 18139 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
18140 {
18141 bool need_reprocess;
18142 info_ptr =
18143 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18144 info_ptr, &need_reprocess);
18145 if (need_reprocess)
18146 indexes_that_need_reprocess.push_back (i);
18147 }
18148
052c8bb8 18149 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18a8505e
AT
18150 if (attr != nullptr)
18151 cu->str_offsets_base = DW_UNSND (attr);
93311388 18152
41144253 18153 attr = die->attr (DW_AT_loclists_base);
18154 if (attr != nullptr)
18155 cu->loclist_base = DW_UNSND (attr);
18156
a39fdb41 18157 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
18158 if (maybe_addr_base.has_value ())
18159 cu->addr_base = *maybe_addr_base;
18160 for (int index : indexes_that_need_reprocess)
18161 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 18162 *diep = die;
93311388
DE
18163 return info_ptr;
18164}
18165
3019eac3
DE
18166/* Read a die and all its attributes.
18167 Set DIEP to point to a newly allocated die with its information,
3e225074 18168 except for its child, sibling, and parent fields. */
3019eac3 18169
d521ce57 18170static const gdb_byte *
3019eac3 18171read_full_die (const struct die_reader_specs *reader,
3e225074 18172 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 18173{
d521ce57 18174 const gdb_byte *result;
bf6af496 18175
3e225074 18176 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 18177
b4f54984 18178 if (dwarf_die_debug)
bf6af496
DE
18179 {
18180 fprintf_unfiltered (gdb_stdlog,
18181 "Read die from %s@0x%x of %s:\n",
96b79293 18182 reader->die_section->get_name (),
bf6af496
DE
18183 (unsigned) (info_ptr - reader->die_section->buffer),
18184 bfd_get_filename (reader->abfd));
b4f54984 18185 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18186 }
18187
18188 return result;
3019eac3 18189}
433df2d4 18190\f
c906108c 18191
72bf9492
DJ
18192/* Returns nonzero if TAG represents a type that we might generate a partial
18193 symbol for. */
18194
18195static int
18196is_type_tag_for_partial (int tag)
18197{
18198 switch (tag)
18199 {
18200#if 0
18201 /* Some types that would be reasonable to generate partial symbols for,
18202 that we don't at present. */
18203 case DW_TAG_array_type:
18204 case DW_TAG_file_type:
18205 case DW_TAG_ptr_to_member_type:
18206 case DW_TAG_set_type:
18207 case DW_TAG_string_type:
18208 case DW_TAG_subroutine_type:
18209#endif
18210 case DW_TAG_base_type:
18211 case DW_TAG_class_type:
680b30c7 18212 case DW_TAG_interface_type:
72bf9492
DJ
18213 case DW_TAG_enumeration_type:
18214 case DW_TAG_structure_type:
18215 case DW_TAG_subrange_type:
18216 case DW_TAG_typedef:
18217 case DW_TAG_union_type:
18218 return 1;
18219 default:
18220 return 0;
18221 }
18222}
18223
18224/* Load all DIEs that are interesting for partial symbols into memory. */
18225
18226static struct partial_die_info *
dee91e82 18227load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18228 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18229{
dee91e82 18230 struct dwarf2_cu *cu = reader->cu;
5e22e966 18231 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 18232 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18233 unsigned int bytes_read;
5afb4e99 18234 unsigned int load_all = 0;
72bf9492
DJ
18235 int nesting_level = 1;
18236
18237 parent_die = NULL;
18238 last_die = NULL;
18239
7adf1e79
DE
18240 gdb_assert (cu->per_cu != NULL);
18241 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18242 load_all = 1;
18243
72bf9492
DJ
18244 cu->partial_dies
18245 = htab_create_alloc_ex (cu->header.length / 12,
18246 partial_die_hash,
18247 partial_die_eq,
18248 NULL,
18249 &cu->comp_unit_obstack,
18250 hashtab_obstack_allocate,
18251 dummy_obstack_deallocate);
18252
72bf9492
DJ
18253 while (1)
18254 {
685af9cd 18255 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18256
18257 /* A NULL abbrev means the end of a series of children. */
18258 if (abbrev == NULL)
18259 {
18260 if (--nesting_level == 0)
cd9983dd
YQ
18261 return first_die;
18262
72bf9492
DJ
18263 info_ptr += bytes_read;
18264 last_die = parent_die;
18265 parent_die = parent_die->die_parent;
18266 continue;
18267 }
18268
98bfdba5
PA
18269 /* Check for template arguments. We never save these; if
18270 they're seen, we just mark the parent, and go on our way. */
18271 if (parent_die != NULL
18272 && cu->language == language_cplus
18273 && (abbrev->tag == DW_TAG_template_type_param
18274 || abbrev->tag == DW_TAG_template_value_param))
18275 {
18276 parent_die->has_template_arguments = 1;
18277
18278 if (!load_all)
18279 {
18280 /* We don't need a partial DIE for the template argument. */
dee91e82 18281 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18282 continue;
18283 }
18284 }
18285
0d99eb77 18286 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18287 Skip their other children. */
18288 if (!load_all
18289 && cu->language == language_cplus
18290 && parent_die != NULL
f9b5d5ea
TV
18291 && parent_die->tag == DW_TAG_subprogram
18292 && abbrev->tag != DW_TAG_inlined_subroutine)
98bfdba5 18293 {
dee91e82 18294 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18295 continue;
18296 }
18297
5afb4e99
DJ
18298 /* Check whether this DIE is interesting enough to save. Normally
18299 we would not be interested in members here, but there may be
18300 later variables referencing them via DW_AT_specification (for
18301 static members). */
18302 if (!load_all
18303 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18304 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18305 && abbrev->tag != DW_TAG_enumerator
18306 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18307 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18308 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18309 && abbrev->tag != DW_TAG_variable
5afb4e99 18310 && abbrev->tag != DW_TAG_namespace
f55ee35c 18311 && abbrev->tag != DW_TAG_module
95554aad 18312 && abbrev->tag != DW_TAG_member
74921315
KS
18313 && abbrev->tag != DW_TAG_imported_unit
18314 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18315 {
18316 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18317 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18318 continue;
18319 }
18320
6f06d47b
YQ
18321 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18322 abbrev);
cd9983dd 18323
48fbe735 18324 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18325
18326 /* This two-pass algorithm for processing partial symbols has a
18327 high cost in cache pressure. Thus, handle some simple cases
18328 here which cover the majority of C partial symbols. DIEs
18329 which neither have specification tags in them, nor could have
18330 specification tags elsewhere pointing at them, can simply be
18331 processed and discarded.
18332
18333 This segment is also optional; scan_partial_symbols and
18334 add_partial_symbol will handle these DIEs if we chain
18335 them in normally. When compilers which do not emit large
18336 quantities of duplicate debug information are more common,
18337 this code can probably be removed. */
18338
18339 /* Any complete simple types at the top level (pretty much all
18340 of them, for a language without namespaces), can be processed
18341 directly. */
18342 if (parent_die == NULL
cd9983dd
YQ
18343 && pdi.has_specification == 0
18344 && pdi.is_declaration == 0
18345 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18346 || pdi.tag == DW_TAG_base_type
18347 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18348 {
7d00ffec 18349 if (building_psymtab && pdi.raw_name != NULL)
f0fbb768
TT
18350 add_partial_symbol (&pdi, cu);
18351
cd9983dd 18352 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18353 continue;
18354 }
18355
d8228535
JK
18356 /* The exception for DW_TAG_typedef with has_children above is
18357 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18358 type_name_or_error will error on such types later.
d8228535
JK
18359
18360 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18361 it could not find the child DIEs referenced later, this is checked
18362 above. In correct DWARF DW_TAG_typedef should have no children. */
18363
cd9983dd 18364 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18365 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18366 "- DIE at %s [in module %s]"),
cd9983dd 18367 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18368
72bf9492
DJ
18369 /* If we're at the second level, and we're an enumerator, and
18370 our parent has no specification (meaning possibly lives in a
18371 namespace elsewhere), then we can add the partial symbol now
18372 instead of queueing it. */
cd9983dd 18373 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18374 && parent_die != NULL
18375 && parent_die->die_parent == NULL
18376 && parent_die->tag == DW_TAG_enumeration_type
18377 && parent_die->has_specification == 0)
18378 {
7d00ffec 18379 if (pdi.raw_name == NULL)
b98664d3 18380 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18381 else if (building_psymtab)
f0fbb768 18382 add_partial_symbol (&pdi, cu);
72bf9492 18383
cd9983dd 18384 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18385 continue;
18386 }
18387
cd9983dd 18388 struct partial_die_info *part_die
6f06d47b 18389 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18390
72bf9492
DJ
18391 /* We'll save this DIE so link it in. */
18392 part_die->die_parent = parent_die;
18393 part_die->die_sibling = NULL;
18394 part_die->die_child = NULL;
18395
18396 if (last_die && last_die == parent_die)
18397 last_die->die_child = part_die;
18398 else if (last_die)
18399 last_die->die_sibling = part_die;
18400
18401 last_die = part_die;
18402
18403 if (first_die == NULL)
18404 first_die = part_die;
18405
18406 /* Maybe add the DIE to the hash table. Not all DIEs that we
18407 find interesting need to be in the hash table, because we
18408 also have the parent/sibling/child chains; only those that we
18409 might refer to by offset later during partial symbol reading.
18410
18411 For now this means things that might have be the target of a
18412 DW_AT_specification, DW_AT_abstract_origin, or
18413 DW_AT_extension. DW_AT_extension will refer only to
18414 namespaces; DW_AT_abstract_origin refers to functions (and
18415 many things under the function DIE, but we do not recurse
18416 into function DIEs during partial symbol reading) and
18417 possibly variables as well; DW_AT_specification refers to
18418 declarations. Declarations ought to have the DW_AT_declaration
18419 flag. It happens that GCC forgets to put it in sometimes, but
18420 only for functions, not for types.
18421
18422 Adding more things than necessary to the hash table is harmless
18423 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18424 wasted time in find_partial_die, when we reread the compilation
18425 unit with load_all_dies set. */
72bf9492 18426
5afb4e99 18427 if (load_all
72929c62 18428 || abbrev->tag == DW_TAG_constant
5afb4e99 18429 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18430 || abbrev->tag == DW_TAG_variable
18431 || abbrev->tag == DW_TAG_namespace
18432 || part_die->is_declaration)
18433 {
18434 void **slot;
18435
18436 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18437 to_underlying (part_die->sect_off),
18438 INSERT);
72bf9492
DJ
18439 *slot = part_die;
18440 }
18441
72bf9492 18442 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18443 we have no reason to follow the children of structures; for other
98bfdba5
PA
18444 languages we have to, so that we can get at method physnames
18445 to infer fully qualified class names, for DW_AT_specification,
18446 and for C++ template arguments. For C++, we also look one level
18447 inside functions to find template arguments (if the name of the
18448 function does not already contain the template arguments).
bc30ff58 18449
0a4b0913
AB
18450 For Ada and Fortran, we need to scan the children of subprograms
18451 and lexical blocks as well because these languages allow the
18452 definition of nested entities that could be interesting for the
18453 debugger, such as nested subprograms for instance. */
72bf9492 18454 if (last_die->has_children
5afb4e99
DJ
18455 && (load_all
18456 || last_die->tag == DW_TAG_namespace
f55ee35c 18457 || last_die->tag == DW_TAG_module
72bf9492 18458 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18459 || (cu->language == language_cplus
18460 && last_die->tag == DW_TAG_subprogram
7d00ffec
TT
18461 && (last_die->raw_name == NULL
18462 || strchr (last_die->raw_name, '<') == NULL))
72bf9492
DJ
18463 || (cu->language != language_c
18464 && (last_die->tag == DW_TAG_class_type
680b30c7 18465 || last_die->tag == DW_TAG_interface_type
72bf9492 18466 || last_die->tag == DW_TAG_structure_type
bc30ff58 18467 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18468 || ((cu->language == language_ada
18469 || cu->language == language_fortran)
bc30ff58
JB
18470 && (last_die->tag == DW_TAG_subprogram
18471 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18472 {
18473 nesting_level++;
18474 parent_die = last_die;
18475 continue;
18476 }
18477
18478 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18479 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18480
18481 /* Back to the top, do it again. */
18482 }
18483}
18484
6f06d47b
YQ
18485partial_die_info::partial_die_info (sect_offset sect_off_,
18486 struct abbrev_info *abbrev)
18487 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18488{
18489}
18490
7d00ffec
TT
18491/* See class definition. */
18492
18493const char *
18494partial_die_info::name (dwarf2_cu *cu)
18495{
18496 if (!canonical_name && raw_name != nullptr)
18497 {
18498 struct objfile *objfile = cu->per_objfile->objfile;
18499 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
18500 canonical_name = 1;
18501 }
18502
18503 return raw_name;
18504}
18505
35cc7ed7
YQ
18506/* Read a minimal amount of information into the minimal die structure.
18507 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18508
48fbe735
YQ
18509const gdb_byte *
18510partial_die_info::read (const struct die_reader_specs *reader,
18511 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18512{
dee91e82 18513 struct dwarf2_cu *cu = reader->cu;
976ca316 18514 dwarf2_per_objfile *per_objfile = cu->per_objfile;
fa238c03 18515 unsigned int i;
c5aa993b 18516 int has_low_pc_attr = 0;
c906108c 18517 int has_high_pc_attr = 0;
91da1414 18518 int high_pc_relative = 0;
c906108c 18519
fd0a254f 18520 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18521 {
e7da7f8f 18522 attribute attr;
18a8505e 18523 bool need_reprocess;
e7da7f8f 18524 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18a8505e
AT
18525 info_ptr, &need_reprocess);
18526 /* String and address offsets that need to do the reprocessing have
18527 already been read at this point, so there is no need to wait until
18528 the loop terminates to do the reprocessing. */
18529 if (need_reprocess)
e7da7f8f 18530 read_attribute_reprocess (reader, &attr);
c906108c 18531 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18532 partial symbol table. */
c906108c
SS
18533 switch (attr.name)
18534 {
18535 case DW_AT_name:
48fbe735 18536 switch (tag)
71c25dea
TT
18537 {
18538 case DW_TAG_compile_unit:
95554aad 18539 case DW_TAG_partial_unit:
348e048f 18540 case DW_TAG_type_unit:
71c25dea
TT
18541 /* Compilation units have a DW_AT_name that is a filename, not
18542 a source language identifier. */
18543 case DW_TAG_enumeration_type:
18544 case DW_TAG_enumerator:
18545 /* These tags always have simple identifiers already; no need
18546 to canonicalize them. */
7d00ffec
TT
18547 canonical_name = 1;
18548 raw_name = DW_STRING (&attr);
71c25dea
TT
18549 break;
18550 default:
7d00ffec
TT
18551 canonical_name = 0;
18552 raw_name = DW_STRING (&attr);
71c25dea
TT
18553 break;
18554 }
c906108c 18555 break;
31ef98ae 18556 case DW_AT_linkage_name:
c906108c 18557 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18558 /* Note that both forms of linkage name might appear. We
18559 assume they will be the same, and we only store the last
18560 one we see. */
e61108c9 18561 linkage_name = attr.value_as_string ();
787de330
TT
18562 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18563 See https://github.com/rust-lang/rust/issues/32925. */
18564 if (cu->language == language_rust && linkage_name != NULL
18565 && strchr (linkage_name, '{') != NULL)
18566 linkage_name = NULL;
c906108c
SS
18567 break;
18568 case DW_AT_low_pc:
18569 has_low_pc_attr = 1;
cd6c91b4 18570 lowpc = attr.value_as_address ();
c906108c
SS
18571 break;
18572 case DW_AT_high_pc:
18573 has_high_pc_attr = 1;
cd6c91b4
TT
18574 highpc = attr.value_as_address ();
18575 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 18576 high_pc_relative = 1;
c906108c
SS
18577 break;
18578 case DW_AT_location:
0963b4bd 18579 /* Support the .debug_loc offsets. */
4fc6c0d5 18580 if (attr.form_is_block ())
8e19ed76 18581 {
48fbe735 18582 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18583 }
cd6c91b4 18584 else if (attr.form_is_section_offset ())
8e19ed76 18585 {
4d3c2250 18586 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18587 }
18588 else
18589 {
4d3c2250
KB
18590 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18591 "partial symbol information");
8e19ed76 18592 }
c906108c 18593 break;
c906108c 18594 case DW_AT_external:
48fbe735 18595 is_external = DW_UNSND (&attr);
c906108c
SS
18596 break;
18597 case DW_AT_declaration:
48fbe735 18598 is_declaration = DW_UNSND (&attr);
c906108c
SS
18599 break;
18600 case DW_AT_type:
48fbe735 18601 has_type = 1;
c906108c
SS
18602 break;
18603 case DW_AT_abstract_origin:
18604 case DW_AT_specification:
72bf9492 18605 case DW_AT_extension:
48fbe735 18606 has_specification = 1;
0826b30a 18607 spec_offset = attr.get_ref_die_offset ();
48fbe735 18608 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18609 || cu->per_cu->is_dwz);
c906108c
SS
18610 break;
18611 case DW_AT_sibling:
18612 /* Ignore absolute siblings, they might point outside of
18613 the current compile unit. */
18614 if (attr.form == DW_FORM_ref_addr)
b98664d3 18615 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18616 else
b9502d3f 18617 {
48fbe735 18618 const gdb_byte *buffer = reader->buffer;
0826b30a 18619 sect_offset off = attr.get_ref_die_offset ();
9c541725 18620 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18621
18622 if (sibling_ptr < info_ptr)
b98664d3 18623 complaint (_("DW_AT_sibling points backwards"));
22869d73 18624 else if (sibling_ptr > reader->buffer_end)
a0194fa8 18625 reader->die_section->overflow_complaint ();
b9502d3f 18626 else
48fbe735 18627 sibling = sibling_ptr;
b9502d3f 18628 }
c906108c 18629 break;
fa4028e9 18630 case DW_AT_byte_size:
48fbe735 18631 has_byte_size = 1;
fa4028e9 18632 break;
ff908ebf 18633 case DW_AT_const_value:
48fbe735 18634 has_const_value = 1;
ff908ebf 18635 break;
68511cec
CES
18636 case DW_AT_calling_convention:
18637 /* DWARF doesn't provide a way to identify a program's source-level
18638 entry point. DW_AT_calling_convention attributes are only meant
18639 to describe functions' calling conventions.
18640
18641 However, because it's a necessary piece of information in
0c1b455e
TT
18642 Fortran, and before DWARF 4 DW_CC_program was the only
18643 piece of debugging information whose definition refers to
18644 a 'main program' at all, several compilers marked Fortran
18645 main programs with DW_CC_program --- even when those
18646 functions use the standard calling conventions.
18647
18648 Although DWARF now specifies a way to provide this
18649 information, we support this practice for backward
18650 compatibility. */
68511cec 18651 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18652 && cu->language == language_fortran)
48fbe735 18653 main_subprogram = 1;
68511cec 18654 break;
481860b3
GB
18655 case DW_AT_inline:
18656 if (DW_UNSND (&attr) == DW_INL_inlined
18657 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18658 may_be_inlined = 1;
481860b3 18659 break;
95554aad
TT
18660
18661 case DW_AT_import:
48fbe735 18662 if (tag == DW_TAG_imported_unit)
36586728 18663 {
0826b30a 18664 d.sect_off = attr.get_ref_die_offset ();
48fbe735 18665 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18666 || cu->per_cu->is_dwz);
18667 }
95554aad
TT
18668 break;
18669
0c1b455e 18670 case DW_AT_main_subprogram:
48fbe735 18671 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18672 break;
18673
05caa1d2
TT
18674 case DW_AT_ranges:
18675 {
18676 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18677 but that requires a full DIE, so instead we just
18678 reimplement it. */
18679 int need_ranges_base = tag != DW_TAG_compile_unit;
18680 unsigned int ranges_offset = (DW_UNSND (&attr)
18681 + (need_ranges_base
18682 ? cu->ranges_base
18683 : 0));
18684
18685 /* Value of the DW_AT_ranges attribute is the offset in the
18686 .debug_ranges section. */
18687 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18688 nullptr))
18689 has_pc_info = 1;
18690 }
18691 break;
18692
c906108c
SS
18693 default:
18694 break;
18695 }
18696 }
18697
10d06d82
TT
18698 /* For Ada, if both the name and the linkage name appear, we prefer
18699 the latter. This lets "catch exception" work better, regardless
18700 of the order in which the name and linkage name were emitted.
18701 Really, though, this is just a workaround for the fact that gdb
18702 doesn't store both the name and the linkage name. */
18703 if (cu->language == language_ada && linkage_name != nullptr)
7d00ffec 18704 raw_name = linkage_name;
10d06d82 18705
91da1414 18706 if (high_pc_relative)
48fbe735 18707 highpc += lowpc;
91da1414 18708
9373cf26
JK
18709 if (has_low_pc_attr && has_high_pc_attr)
18710 {
18711 /* When using the GNU linker, .gnu.linkonce. sections are used to
18712 eliminate duplicate copies of functions and vtables and such.
18713 The linker will arbitrarily choose one and discard the others.
18714 The AT_*_pc values for such functions refer to local labels in
18715 these sections. If the section from that file was discarded, the
18716 labels are not in the output, so the relocs get a value of 0.
18717 If this is a discarded function, mark the pc bounds as invalid,
18718 so that GDB will ignore it. */
976ca316 18719 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
9373cf26 18720 {
976ca316 18721 struct objfile *objfile = per_objfile->objfile;
08feed99 18722 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18723
b98664d3 18724 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18725 "for DIE at %s [in module %s]"),
48fbe735
YQ
18726 paddress (gdbarch, lowpc),
18727 sect_offset_str (sect_off),
9d8780f0 18728 objfile_name (objfile));
9373cf26
JK
18729 }
18730 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18731 else if (lowpc >= highpc)
9373cf26 18732 {
976ca316 18733 struct objfile *objfile = per_objfile->objfile;
08feed99 18734 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18735
b98664d3 18736 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18737 "for DIE at %s [in module %s]"),
48fbe735
YQ
18738 paddress (gdbarch, lowpc),
18739 paddress (gdbarch, highpc),
18740 sect_offset_str (sect_off),
9c541725 18741 objfile_name (objfile));
9373cf26
JK
18742 }
18743 else
48fbe735 18744 has_pc_info = 1;
9373cf26 18745 }
85cbf3d3 18746
c906108c
SS
18747 return info_ptr;
18748}
18749
72bf9492
DJ
18750/* Find a cached partial DIE at OFFSET in CU. */
18751
d590ff25
YQ
18752struct partial_die_info *
18753dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18754{
18755 struct partial_die_info *lookup_die = NULL;
6f06d47b 18756 struct partial_die_info part_die (sect_off);
72bf9492 18757
9a3c8263 18758 lookup_die = ((struct partial_die_info *)
d590ff25 18759 htab_find_with_hash (partial_dies, &part_die,
9c541725 18760 to_underlying (sect_off)));
72bf9492 18761
72bf9492
DJ
18762 return lookup_die;
18763}
18764
348e048f
DE
18765/* Find a partial DIE at OFFSET, which may or may not be in CU,
18766 except in the case of .debug_types DIEs which do not reference
18767 outside their CU (they do however referencing other types via
55f1336d 18768 DW_FORM_ref_sig8). */
72bf9492 18769
122cf0f2 18770static const struct cu_partial_die_info
9c541725 18771find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18772{
976ca316
SM
18773 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18774 struct objfile *objfile = per_objfile->objfile;
5afb4e99 18775 struct partial_die_info *pd = NULL;
72bf9492 18776
36586728 18777 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18778 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18779 {
d590ff25 18780 pd = cu->find_partial_die (sect_off);
5afb4e99 18781 if (pd != NULL)
fb816e8b 18782 return { cu, pd };
0d99eb77
DE
18783 /* We missed recording what we needed.
18784 Load all dies and try again. */
5afb4e99 18785 }
0d99eb77
DE
18786 else
18787 {
18788 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18789 if (cu->per_cu->is_debug_types)
0d99eb77 18790 {
9d8780f0
SM
18791 error (_("Dwarf Error: Type Unit at offset %s contains"
18792 " external reference to offset %s [in module %s].\n"),
18793 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18794 bfd_get_filename (objfile->obfd));
18795 }
7188ed02
SM
18796 dwarf2_per_cu_data *per_cu
18797 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 18798 per_objfile);
72bf9492 18799
976ca316 18800 cu = per_objfile->get_cu (per_cu);
7188ed02 18801 if (cu == NULL || cu->partial_dies == NULL)
976ca316 18802 load_partial_comp_unit (per_cu, per_objfile, nullptr);
ae038cb0 18803
976ca316 18804 cu = per_objfile->get_cu (per_cu);
7188ed02
SM
18805
18806 cu->last_used = 0;
18807 pd = cu->find_partial_die (sect_off);
0d99eb77 18808 }
5afb4e99 18809
dee91e82
DE
18810 /* If we didn't find it, and not all dies have been loaded,
18811 load them all and try again. */
18812
7188ed02 18813 if (pd == NULL && cu->per_cu->load_all_dies == 0)
5afb4e99 18814 {
7188ed02 18815 cu->per_cu->load_all_dies = 1;
fd820528
DE
18816
18817 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18818 THIS_CU->cu may already be in use. So we can't just free it and
18819 replace its DIEs with the ones we read in. Instead, we leave those
18820 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18821 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18822 set. */
976ca316 18823 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
5afb4e99 18824
7188ed02 18825 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
18826 }
18827
18828 if (pd == NULL)
18829 internal_error (__FILE__, __LINE__,
9d8780f0 18830 _("could not find partial DIE %s "
3e43a32a 18831 "in cache [from module %s]\n"),
9d8780f0 18832 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
7188ed02 18833 return { cu, pd };
72bf9492
DJ
18834}
18835
abc72ce4
DE
18836/* See if we can figure out if the class lives in a namespace. We do
18837 this by looking for a member function; its demangled name will
18838 contain namespace info, if there is any. */
18839
18840static void
18841guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18842 struct dwarf2_cu *cu)
18843{
18844 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18845 what template types look like, because the demangler
18846 frequently doesn't give the same name as the debug info. We
18847 could fix this by only using the demangled name to get the
18848 prefix (but see comment in read_structure_type). */
18849
18850 struct partial_die_info *real_pdi;
18851 struct partial_die_info *child_pdi;
18852
18853 /* If this DIE (this DIE's specification, if any) has a parent, then
18854 we should not do this. We'll prepend the parent's fully qualified
18855 name when we create the partial symbol. */
18856
18857 real_pdi = struct_pdi;
18858 while (real_pdi->has_specification)
fb816e8b 18859 {
122cf0f2
AB
18860 auto res = find_partial_die (real_pdi->spec_offset,
18861 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18862 real_pdi = res.pdi;
18863 cu = res.cu;
18864 }
abc72ce4
DE
18865
18866 if (real_pdi->die_parent != NULL)
18867 return;
18868
18869 for (child_pdi = struct_pdi->die_child;
18870 child_pdi != NULL;
18871 child_pdi = child_pdi->die_sibling)
18872 {
18873 if (child_pdi->tag == DW_TAG_subprogram
18874 && child_pdi->linkage_name != NULL)
18875 {
43816ebc 18876 gdb::unique_xmalloc_ptr<char> actual_class_name
eff93b4d
AB
18877 (cu->language_defn->class_name_from_physname
18878 (child_pdi->linkage_name));
abc72ce4
DE
18879 if (actual_class_name != NULL)
18880 {
5e22e966 18881 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
18882 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
18883 struct_pdi->canonical_name = 1;
abc72ce4
DE
18884 }
18885 break;
18886 }
18887 }
18888}
18889
25c11aca
TV
18890/* Return true if a DIE with TAG may have the DW_AT_const_value
18891 attribute. */
18892
18893static bool
18894can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18895{
18896 switch (tag)
18897 {
18898 case DW_TAG_constant:
18899 case DW_TAG_enumerator:
18900 case DW_TAG_formal_parameter:
18901 case DW_TAG_template_value_param:
18902 case DW_TAG_variable:
18903 return true;
18904 }
18905
18906 return false;
18907}
18908
52356b79
YQ
18909void
18910partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18911{
abc72ce4
DE
18912 /* Once we've fixed up a die, there's no point in doing so again.
18913 This also avoids a memory leak if we were to call
18914 guess_partial_die_structure_name multiple times. */
52356b79 18915 if (fixup_called)
abc72ce4
DE
18916 return;
18917
72bf9492
DJ
18918 /* If we found a reference attribute and the DIE has no name, try
18919 to find a name in the referred to DIE. */
18920
7d00ffec 18921 if (raw_name == NULL && has_specification)
72bf9492
DJ
18922 {
18923 struct partial_die_info *spec_die;
72bf9492 18924
122cf0f2 18925 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18926 spec_die = res.pdi;
18927 cu = res.cu;
72bf9492 18928
52356b79 18929 spec_die->fixup (cu);
72bf9492 18930
7d00ffec 18931 if (spec_die->raw_name)
72bf9492 18932 {
7d00ffec
TT
18933 raw_name = spec_die->raw_name;
18934 canonical_name = spec_die->canonical_name;
72bf9492
DJ
18935
18936 /* Copy DW_AT_external attribute if it is set. */
18937 if (spec_die->is_external)
52356b79 18938 is_external = spec_die->is_external;
72bf9492
DJ
18939 }
18940 }
18941
25c11aca
TV
18942 if (!has_const_value && has_specification
18943 && can_have_DW_AT_const_value_p (tag))
18944 {
18945 struct partial_die_info *spec_die;
18946
18947 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18948 spec_die = res.pdi;
18949 cu = res.cu;
18950
18951 spec_die->fixup (cu);
18952
18953 if (spec_die->has_const_value)
18954 {
18955 /* Copy DW_AT_const_value attribute if it is set. */
18956 has_const_value = spec_die->has_const_value;
18957 }
18958 }
18959
72bf9492 18960 /* Set default names for some unnamed DIEs. */
72bf9492 18961
7d00ffec
TT
18962 if (raw_name == NULL && tag == DW_TAG_namespace)
18963 {
18964 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
18965 canonical_name = 1;
18966 }
72bf9492 18967
abc72ce4
DE
18968 /* If there is no parent die to provide a namespace, and there are
18969 children, see if we can determine the namespace from their linkage
122d1940 18970 name. */
abc72ce4 18971 if (cu->language == language_cplus
5e22e966 18972 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
18973 && die_parent == NULL
18974 && has_children
18975 && (tag == DW_TAG_class_type
18976 || tag == DW_TAG_structure_type
18977 || tag == DW_TAG_union_type))
18978 guess_partial_die_structure_name (this, cu);
abc72ce4 18979
53832f31
TT
18980 /* GCC might emit a nameless struct or union that has a linkage
18981 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
7d00ffec 18982 if (raw_name == NULL
52356b79
YQ
18983 && (tag == DW_TAG_class_type
18984 || tag == DW_TAG_interface_type
18985 || tag == DW_TAG_structure_type
18986 || tag == DW_TAG_union_type)
18987 && linkage_name != NULL)
53832f31 18988 {
43816ebc
TT
18989 gdb::unique_xmalloc_ptr<char> demangled
18990 (gdb_demangle (linkage_name, DMGL_TYPES));
18991 if (demangled != nullptr)
53832f31 18992 {
96408a79
SA
18993 const char *base;
18994
18995 /* Strip any leading namespaces/classes, keep only the base name.
18996 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18997 base = strrchr (demangled.get (), ':');
18998 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18999 base++;
19000 else
43816ebc 19001 base = demangled.get ();
96408a79 19002
5e22e966 19003 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
19004 raw_name = objfile->intern (base);
19005 canonical_name = 1;
53832f31
TT
19006 }
19007 }
19008
52356b79 19009 fixup_called = 1;
72bf9492
DJ
19010}
19011
41144253 19012/* Read the .debug_loclists header contents from the given SECTION in the
19013 HEADER. */
19014static void
19015read_loclist_header (struct loclist_header *header,
19016 struct dwarf2_section_info *section)
19017{
19018 unsigned int bytes_read;
19019 bfd *abfd = section->get_bfd_owner ();
19020 const gdb_byte *info_ptr = section->buffer;
19021 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19022 info_ptr += bytes_read;
19023 header->version = read_2_bytes (abfd, info_ptr);
19024 info_ptr += 2;
19025 header->addr_size = read_1_byte (abfd, info_ptr);
19026 info_ptr += 1;
19027 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19028 info_ptr += 1;
19029 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19030}
19031
19032/* Return the DW_AT_loclists_base value for the CU. */
19033static ULONGEST
19034lookup_loclist_base (struct dwarf2_cu *cu)
19035{
19036 /* For the .dwo unit, the loclist_base points to the first offset following
19037 the header. The header consists of the following entities-
19038 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19039 bit format)
19040 2. version (2 bytes)
19041 3. address size (1 byte)
19042 4. segment selector size (1 byte)
19043 5. offset entry count (4 bytes)
19044 These sizes are derived as per the DWARFv5 standard. */
19045 if (cu->dwo_unit != nullptr)
19046 {
19047 if (cu->header.initial_length_size == 4)
19048 return LOCLIST_HEADER_SIZE32;
19049 return LOCLIST_HEADER_SIZE64;
19050 }
19051 return cu->loclist_base;
19052}
19053
19054/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19055 array of offsets in the .debug_loclists section. */
19056static CORE_ADDR
19057read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19058{
976ca316
SM
19059 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19060 struct objfile *objfile = per_objfile->objfile;
41144253 19061 bfd *abfd = objfile->obfd;
19062 ULONGEST loclist_base = lookup_loclist_base (cu);
19063 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19064
19065 section->read (objfile);
19066 if (section->buffer == NULL)
19067 complaint (_("DW_FORM_loclistx used without .debug_loclists "
19068 "section [in module %s]"), objfile_name (objfile));
19069 struct loclist_header header;
19070 read_loclist_header (&header, section);
19071 if (loclist_index >= header.offset_entry_count)
19072 complaint (_("DW_FORM_loclistx pointing outside of "
19073 ".debug_loclists offset array [in module %s]"),
19074 objfile_name (objfile));
19075 if (loclist_base + loclist_index * cu->header.offset_size
19076 >= section->size)
19077 complaint (_("DW_FORM_loclistx pointing outside of "
19078 ".debug_loclists section [in module %s]"),
19079 objfile_name (objfile));
19080 const gdb_byte *info_ptr
19081 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
19082
19083 if (cu->header.offset_size == 4)
19084 return bfd_get_32 (abfd, info_ptr) + loclist_base;
19085 else
19086 return bfd_get_64 (abfd, info_ptr) + loclist_base;
19087}
19088
18a8505e
AT
19089/* Process the attributes that had to be skipped in the first round. These
19090 attributes are the ones that need str_offsets_base or addr_base attributes.
19091 They could not have been processed in the first round, because at the time
19092 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
19093static void
19094read_attribute_reprocess (const struct die_reader_specs *reader,
19095 struct attribute *attr)
18a8505e
AT
19096{
19097 struct dwarf2_cu *cu = reader->cu;
19098 switch (attr->form)
19099 {
19100 case DW_FORM_addrx:
19101 case DW_FORM_GNU_addr_index:
19102 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19103 break;
41144253 19104 case DW_FORM_loclistx:
19105 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
19106 break;
18a8505e
AT
19107 case DW_FORM_strx:
19108 case DW_FORM_strx1:
19109 case DW_FORM_strx2:
19110 case DW_FORM_strx3:
19111 case DW_FORM_strx4:
19112 case DW_FORM_GNU_str_index:
19113 {
19114 unsigned int str_index = DW_UNSND (attr);
19115 if (reader->dwo_file != NULL)
19116 {
19117 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19118 DW_STRING_IS_CANONICAL (attr) = 0;
19119 }
19120 else
19121 {
19122 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19123 DW_STRING_IS_CANONICAL (attr) = 0;
19124 }
19125 break;
19126 }
19127 default:
19128 gdb_assert_not_reached (_("Unexpected DWARF form."));
19129 }
19130}
19131
a8329558 19132/* Read an attribute value described by an attribute form. */
c906108c 19133
d521ce57 19134static const gdb_byte *
dee91e82
DE
19135read_attribute_value (const struct die_reader_specs *reader,
19136 struct attribute *attr, unsigned form,
18a8505e
AT
19137 LONGEST implicit_const, const gdb_byte *info_ptr,
19138 bool *need_reprocess)
c906108c 19139{
dee91e82 19140 struct dwarf2_cu *cu = reader->cu;
976ca316
SM
19141 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19142 struct objfile *objfile = per_objfile->objfile;
dee91e82 19143 bfd *abfd = reader->abfd;
e7c27a73 19144 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19145 unsigned int bytes_read;
19146 struct dwarf_block *blk;
18a8505e 19147 *need_reprocess = false;
c906108c 19148
aead7601 19149 attr->form = (enum dwarf_form) form;
a8329558 19150 switch (form)
c906108c 19151 {
c906108c 19152 case DW_FORM_ref_addr:
ae411497 19153 if (cu->header.version == 2)
c8a7a66f
TT
19154 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19155 &bytes_read);
ae411497 19156 else
8266302d
TT
19157 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19158 &bytes_read);
ae411497
TT
19159 info_ptr += bytes_read;
19160 break;
36586728 19161 case DW_FORM_GNU_ref_alt:
8266302d 19162 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
19163 info_ptr += bytes_read;
19164 break;
ae411497 19165 case DW_FORM_addr:
08feed99
TT
19166 {
19167 struct gdbarch *gdbarch = objfile->arch ();
19168 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19169 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19170 info_ptr += bytes_read;
19171 }
c906108c
SS
19172 break;
19173 case DW_FORM_block2:
7b5a2f43 19174 blk = dwarf_alloc_block (cu);
c906108c
SS
19175 blk->size = read_2_bytes (abfd, info_ptr);
19176 info_ptr += 2;
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_block4:
7b5a2f43 19182 blk = dwarf_alloc_block (cu);
c906108c
SS
19183 blk->size = read_4_bytes (abfd, info_ptr);
19184 info_ptr += 4;
19185 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19186 info_ptr += blk->size;
19187 DW_BLOCK (attr) = blk;
19188 break;
19189 case DW_FORM_data2:
19190 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19191 info_ptr += 2;
19192 break;
19193 case DW_FORM_data4:
19194 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19195 info_ptr += 4;
19196 break;
19197 case DW_FORM_data8:
19198 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19199 info_ptr += 8;
19200 break;
0224619f
JK
19201 case DW_FORM_data16:
19202 blk = dwarf_alloc_block (cu);
19203 blk->size = 16;
19204 blk->data = read_n_bytes (abfd, info_ptr, 16);
19205 info_ptr += 16;
19206 DW_BLOCK (attr) = blk;
19207 break;
2dc7f7b3 19208 case DW_FORM_sec_offset:
8266302d 19209 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
19210 info_ptr += bytes_read;
19211 break;
41144253 19212 case DW_FORM_loclistx:
19213 {
19214 *need_reprocess = true;
19215 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19216 info_ptr += bytes_read;
19217 }
19218 break;
c906108c 19219 case DW_FORM_string:
9b1c24c8 19220 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19221 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19222 info_ptr += bytes_read;
19223 break;
4bdf3d34 19224 case DW_FORM_strp:
36586728
TT
19225 if (!cu->per_cu->is_dwz)
19226 {
976ca316 19227 DW_STRING (attr) = read_indirect_string (per_objfile,
ed2dc618 19228 abfd, info_ptr, cu_header,
36586728
TT
19229 &bytes_read);
19230 DW_STRING_IS_CANONICAL (attr) = 0;
19231 info_ptr += bytes_read;
19232 break;
19233 }
19234 /* FALLTHROUGH */
43988095
JK
19235 case DW_FORM_line_strp:
19236 if (!cu->per_cu->is_dwz)
19237 {
976ca316
SM
19238 DW_STRING (attr) = per_objfile->read_line_string (info_ptr, cu_header,
19239 &bytes_read);
43988095
JK
19240 DW_STRING_IS_CANONICAL (attr) = 0;
19241 info_ptr += bytes_read;
19242 break;
19243 }
19244 /* FALLTHROUGH */
36586728
TT
19245 case DW_FORM_GNU_strp_alt:
19246 {
976ca316 19247 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8266302d
TT
19248 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19249 &bytes_read);
36586728 19250
0314b390 19251 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
36586728
TT
19252 DW_STRING_IS_CANONICAL (attr) = 0;
19253 info_ptr += bytes_read;
19254 }
4bdf3d34 19255 break;
2dc7f7b3 19256 case DW_FORM_exprloc:
c906108c 19257 case DW_FORM_block:
7b5a2f43 19258 blk = dwarf_alloc_block (cu);
c906108c
SS
19259 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19260 info_ptr += bytes_read;
19261 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19262 info_ptr += blk->size;
19263 DW_BLOCK (attr) = blk;
19264 break;
19265 case DW_FORM_block1:
7b5a2f43 19266 blk = dwarf_alloc_block (cu);
c906108c
SS
19267 blk->size = read_1_byte (abfd, info_ptr);
19268 info_ptr += 1;
19269 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19270 info_ptr += blk->size;
19271 DW_BLOCK (attr) = blk;
19272 break;
19273 case DW_FORM_data1:
19274 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19275 info_ptr += 1;
19276 break;
19277 case DW_FORM_flag:
19278 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19279 info_ptr += 1;
19280 break;
2dc7f7b3
TT
19281 case DW_FORM_flag_present:
19282 DW_UNSND (attr) = 1;
19283 break;
c906108c
SS
19284 case DW_FORM_sdata:
19285 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19286 info_ptr += bytes_read;
19287 break;
19288 case DW_FORM_udata:
18a8505e 19289 case DW_FORM_rnglistx:
c906108c
SS
19290 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19291 info_ptr += bytes_read;
19292 break;
19293 case DW_FORM_ref1:
9c541725 19294 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19295 + read_1_byte (abfd, info_ptr));
c906108c
SS
19296 info_ptr += 1;
19297 break;
19298 case DW_FORM_ref2:
9c541725 19299 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19300 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19301 info_ptr += 2;
19302 break;
19303 case DW_FORM_ref4:
9c541725 19304 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19305 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19306 info_ptr += 4;
19307 break;
613e1657 19308 case DW_FORM_ref8:
9c541725 19309 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19310 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19311 info_ptr += 8;
19312 break;
55f1336d 19313 case DW_FORM_ref_sig8:
ac9ec31b 19314 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19315 info_ptr += 8;
19316 break;
c906108c 19317 case DW_FORM_ref_udata:
9c541725 19318 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19319 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19320 info_ptr += bytes_read;
19321 break;
c906108c 19322 case DW_FORM_indirect:
a8329558
KW
19323 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19324 info_ptr += bytes_read;
43988095
JK
19325 if (form == DW_FORM_implicit_const)
19326 {
19327 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19328 info_ptr += bytes_read;
19329 }
19330 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 19331 info_ptr, need_reprocess);
43988095
JK
19332 break;
19333 case DW_FORM_implicit_const:
19334 DW_SND (attr) = implicit_const;
a8329558 19335 break;
336d760d 19336 case DW_FORM_addrx:
3019eac3 19337 case DW_FORM_GNU_addr_index:
18a8505e
AT
19338 *need_reprocess = true;
19339 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
19340 info_ptr += bytes_read;
19341 break;
cf532bd1 19342 case DW_FORM_strx:
15f18d14
AT
19343 case DW_FORM_strx1:
19344 case DW_FORM_strx2:
19345 case DW_FORM_strx3:
19346 case DW_FORM_strx4:
3019eac3 19347 case DW_FORM_GNU_str_index:
3019eac3 19348 {
15f18d14
AT
19349 ULONGEST str_index;
19350 if (form == DW_FORM_strx1)
19351 {
19352 str_index = read_1_byte (abfd, info_ptr);
19353 info_ptr += 1;
19354 }
19355 else if (form == DW_FORM_strx2)
19356 {
19357 str_index = read_2_bytes (abfd, info_ptr);
19358 info_ptr += 2;
19359 }
19360 else if (form == DW_FORM_strx3)
19361 {
19362 str_index = read_3_bytes (abfd, info_ptr);
19363 info_ptr += 3;
19364 }
19365 else if (form == DW_FORM_strx4)
19366 {
19367 str_index = read_4_bytes (abfd, info_ptr);
19368 info_ptr += 4;
19369 }
19370 else
19371 {
19372 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19373 info_ptr += bytes_read;
19374 }
18a8505e
AT
19375 *need_reprocess = true;
19376 DW_UNSND (attr) = str_index;
19377 }
3019eac3 19378 break;
c906108c 19379 default:
8a3fe4f8 19380 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19381 dwarf_form_name (form),
19382 bfd_get_filename (abfd));
c906108c 19383 }
28e94949 19384
36586728 19385 /* Super hack. */
cd6c91b4 19386 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
19387 attr->form = DW_FORM_GNU_ref_alt;
19388
28e94949
JB
19389 /* We have seen instances where the compiler tried to emit a byte
19390 size attribute of -1 which ended up being encoded as an unsigned
19391 0xffffffff. Although 0xffffffff is technically a valid size value,
19392 an object of this size seems pretty unlikely so we can relatively
19393 safely treat these cases as if the size attribute was invalid and
19394 treat them as zero by default. */
19395 if (attr->name == DW_AT_byte_size
19396 && form == DW_FORM_data4
19397 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19398 {
19399 complaint
b98664d3 19400 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19401 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19402 DW_UNSND (attr) = 0;
19403 }
28e94949 19404
c906108c
SS
19405 return info_ptr;
19406}
19407
a8329558
KW
19408/* Read an attribute described by an abbreviated attribute. */
19409
d521ce57 19410static const gdb_byte *
dee91e82
DE
19411read_attribute (const struct die_reader_specs *reader,
19412 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 19413 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
19414{
19415 attr->name = abbrev->name;
43988095 19416 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
19417 abbrev->implicit_const, info_ptr,
19418 need_reprocess);
a8329558
KW
19419}
19420
43988095
JK
19421/* Return pointer to string at .debug_str offset STR_OFFSET. */
19422
19423static const char *
976ca316 19424read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
4f44ae6c 19425 LONGEST str_offset)
43988095 19426{
976ca316
SM
19427 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
19428 str_offset, "DW_FORM_strp");
c906108c
SS
19429}
19430
43988095
JK
19431/* Return pointer to string at .debug_str offset as read from BUF.
19432 BUF is assumed to be in a compilation unit described by CU_HEADER.
19433 Return *BYTES_READ_PTR count of bytes read from BUF. */
19434
d521ce57 19435static const char *
976ca316 19436read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
ed2dc618 19437 const gdb_byte *buf,
cf2c3c16
TT
19438 const struct comp_unit_head *cu_header,
19439 unsigned int *bytes_read_ptr)
19440{
8266302d 19441 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 19442
976ca316 19443 return read_indirect_string_at_offset (per_objfile, str_offset);
cf2c3c16
TT
19444}
19445
86c0bb4c 19446/* See read.h. */
43988095 19447
86c0bb4c
TT
19448const char *
19449dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
19450 const struct comp_unit_head *cu_header,
19451 unsigned int *bytes_read_ptr)
43988095 19452{
86c0bb4c 19453 bfd *abfd = objfile->obfd;
8266302d 19454 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 19455
5989a64e 19456 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
19457}
19458
3019eac3 19459/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 19460 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
19461 ADDR_SIZE is the size of addresses from the CU header. */
19462
19463static CORE_ADDR
976ca316
SM
19464read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
19465 gdb::optional<ULONGEST> addr_base, int addr_size)
3019eac3 19466{
976ca316 19467 struct objfile *objfile = per_objfile->objfile;
3019eac3
DE
19468 bfd *abfd = objfile->obfd;
19469 const gdb_byte *info_ptr;
18a8505e 19470 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 19471
976ca316
SM
19472 per_objfile->per_bfd->addr.read (objfile);
19473 if (per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 19474 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19475 objfile_name (objfile));
18a8505e 19476 if (addr_base_or_zero + addr_index * addr_size
976ca316 19477 >= per_objfile->per_bfd->addr.size)
3019eac3
DE
19478 error (_("DW_FORM_addr_index pointing outside of "
19479 ".debug_addr section [in module %s]"),
4262abfb 19480 objfile_name (objfile));
976ca316
SM
19481 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
19482 + addr_index * addr_size);
3019eac3
DE
19483 if (addr_size == 4)
19484 return bfd_get_32 (abfd, info_ptr);
19485 else
19486 return bfd_get_64 (abfd, info_ptr);
19487}
19488
19489/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19490
19491static CORE_ADDR
19492read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19493{
5e22e966 19494 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 19495 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19496}
19497
19498/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19499
19500static CORE_ADDR
d521ce57 19501read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19502 unsigned int *bytes_read)
19503{
5e22e966 19504 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
19505 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19506
19507 return read_addr_index (cu, addr_index);
19508}
19509
450a1bfc 19510/* See read.h. */
3019eac3
DE
19511
19512CORE_ADDR
82ca3f51 19513dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
976ca316 19514 dwarf2_per_objfile *per_objfile,
82ca3f51 19515 unsigned int addr_index)
3019eac3 19516{
976ca316 19517 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
18a8505e 19518 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
19519 int addr_size;
19520
3019eac3
DE
19521 /* We need addr_base and addr_size.
19522 If we don't have PER_CU->cu, we have to get it.
19523 Nasty, but the alternative is storing the needed info in PER_CU,
19524 which at this point doesn't seem justified: it's not clear how frequently
19525 it would get used and it would increase the size of every PER_CU.
19526 Entry points like dwarf2_per_cu_addr_size do a similar thing
19527 so we're not in uncharted territory here.
19528 Alas we need to be a bit more complicated as addr_base is contained
19529 in the DIE.
19530
19531 We don't need to read the entire CU(/TU).
19532 We just need the header and top level die.
a1b64ce1 19533
3019eac3 19534 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19535 For now we skip this optimization. */
3019eac3
DE
19536
19537 if (cu != NULL)
19538 {
19539 addr_base = cu->addr_base;
19540 addr_size = cu->header.addr_size;
19541 }
19542 else
19543 {
976ca316 19544 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
19545 addr_base = reader.cu->addr_base;
19546 addr_size = reader.cu->header.addr_size;
3019eac3
DE
19547 }
19548
976ca316 19549 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
3019eac3
DE
19550}
19551
18a8505e
AT
19552/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19553 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19554 DWO file. */
3019eac3 19555
d521ce57 19556static const char *
18a8505e
AT
19557read_str_index (struct dwarf2_cu *cu,
19558 struct dwarf2_section_info *str_section,
19559 struct dwarf2_section_info *str_offsets_section,
19560 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 19561{
976ca316
SM
19562 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19563 struct objfile *objfile = per_objfile->objfile;
c5164cbc 19564 const char *objf_name = objfile_name (objfile);
3019eac3 19565 bfd *abfd = objfile->obfd;
d521ce57 19566 const gdb_byte *info_ptr;
3019eac3 19567 ULONGEST str_offset;
cf532bd1 19568 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 19569
96b79293
TT
19570 str_section->read (objfile);
19571 str_offsets_section->read (objfile);
73869dc2 19572 if (str_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 if (str_offsets_section->buffer == NULL)
18a8505e 19578 error (_("%s used without %s section"
9d8780f0 19579 " in CU at offset %s [in module %s]"),
96b79293 19580 form_name, str_section->get_name (),
18a8505e 19581 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19582 info_ptr = (str_offsets_section->buffer
18a8505e 19583 + str_offsets_base
3019eac3
DE
19584 + str_index * cu->header.offset_size);
19585 if (cu->header.offset_size == 4)
19586 str_offset = bfd_get_32 (abfd, info_ptr);
19587 else
19588 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19589 if (str_offset >= str_section->size)
57d63ce2 19590 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19591 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19592 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19593 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19594}
19595
18a8505e
AT
19596/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19597
19598static const char *
19599read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19600{
19601 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19602 ? reader->cu->header.addr_size : 0;
19603 return read_str_index (reader->cu,
19604 &reader->dwo_file->sections.str,
19605 &reader->dwo_file->sections.str_offsets,
19606 str_offsets_base, str_index);
19607}
19608
19609/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19610
19611static const char *
19612read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19613{
5e22e966 19614 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
19615 const char *objf_name = objfile_name (objfile);
19616 static const char form_name[] = "DW_FORM_GNU_str_index";
19617 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19618
19619 if (!cu->str_offsets_base.has_value ())
19620 error (_("%s used in Fission stub without %s"
19621 " in CU at offset 0x%lx [in module %s]"),
19622 form_name, str_offsets_attr_name,
19623 (long) cu->header.offset_size, objf_name);
19624
19625 return read_str_index (cu,
5e22e966
SM
19626 &cu->per_objfile->per_bfd->str,
19627 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
19628 *cu->str_offsets_base, str_index);
19629}
19630
3019eac3
DE
19631/* Return the length of an LEB128 number in BUF. */
19632
19633static int
19634leb128_size (const gdb_byte *buf)
19635{
19636 const gdb_byte *begin = buf;
19637 gdb_byte byte;
19638
19639 while (1)
19640 {
19641 byte = *buf++;
19642 if ((byte & 128) == 0)
19643 return buf - begin;
19644 }
19645}
19646
c906108c 19647static void
e142c38c 19648set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19649{
19650 switch (lang)
19651 {
19652 case DW_LANG_C89:
76bee0cc 19653 case DW_LANG_C99:
0cfd832f 19654 case DW_LANG_C11:
c906108c 19655 case DW_LANG_C:
d1be3247 19656 case DW_LANG_UPC:
e142c38c 19657 cu->language = language_c;
c906108c 19658 break;
9c37b5ae 19659 case DW_LANG_Java:
c906108c 19660 case DW_LANG_C_plus_plus:
0cfd832f
MW
19661 case DW_LANG_C_plus_plus_11:
19662 case DW_LANG_C_plus_plus_14:
e142c38c 19663 cu->language = language_cplus;
c906108c 19664 break;
6aecb9c2
JB
19665 case DW_LANG_D:
19666 cu->language = language_d;
19667 break;
c906108c
SS
19668 case DW_LANG_Fortran77:
19669 case DW_LANG_Fortran90:
b21b22e0 19670 case DW_LANG_Fortran95:
f7de9aab
MW
19671 case DW_LANG_Fortran03:
19672 case DW_LANG_Fortran08:
e142c38c 19673 cu->language = language_fortran;
c906108c 19674 break;
a766d390
DE
19675 case DW_LANG_Go:
19676 cu->language = language_go;
19677 break;
c906108c 19678 case DW_LANG_Mips_Assembler:
e142c38c 19679 cu->language = language_asm;
c906108c
SS
19680 break;
19681 case DW_LANG_Ada83:
8aaf0b47 19682 case DW_LANG_Ada95:
bc5f45f8
JB
19683 cu->language = language_ada;
19684 break;
72019c9c
GM
19685 case DW_LANG_Modula2:
19686 cu->language = language_m2;
19687 break;
fe8e67fd
PM
19688 case DW_LANG_Pascal83:
19689 cu->language = language_pascal;
19690 break;
22566fbd
DJ
19691 case DW_LANG_ObjC:
19692 cu->language = language_objc;
19693 break;
c44af4eb
TT
19694 case DW_LANG_Rust:
19695 case DW_LANG_Rust_old:
19696 cu->language = language_rust;
19697 break;
c906108c
SS
19698 case DW_LANG_Cobol74:
19699 case DW_LANG_Cobol85:
c906108c 19700 default:
e142c38c 19701 cu->language = language_minimal;
c906108c
SS
19702 break;
19703 }
e142c38c 19704 cu->language_defn = language_def (cu->language);
c906108c
SS
19705}
19706
19707/* Return the named attribute or NULL if not there. */
19708
19709static struct attribute *
e142c38c 19710dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19711{
a48e046c 19712 for (;;)
c906108c 19713 {
a48e046c
TT
19714 unsigned int i;
19715 struct attribute *spec = NULL;
19716
19717 for (i = 0; i < die->num_attrs; ++i)
19718 {
19719 if (die->attrs[i].name == name)
19720 return &die->attrs[i];
19721 if (die->attrs[i].name == DW_AT_specification
19722 || die->attrs[i].name == DW_AT_abstract_origin)
19723 spec = &die->attrs[i];
19724 }
19725
19726 if (!spec)
19727 break;
c906108c 19728
f2f0e013 19729 die = follow_die_ref (die, spec, &cu);
f2f0e013 19730 }
c5aa993b 19731
c906108c
SS
19732 return NULL;
19733}
19734
7d45c7c3
KB
19735/* Return the string associated with a string-typed attribute, or NULL if it
19736 is either not found or is of an incorrect type. */
19737
19738static const char *
19739dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19740{
19741 struct attribute *attr;
19742 const char *str = NULL;
19743
19744 attr = dwarf2_attr (die, name, cu);
19745
19746 if (attr != NULL)
19747 {
e61108c9
TT
19748 str = attr->value_as_string ();
19749 if (str == nullptr)
b98664d3 19750 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19751 "DIE at %s in module %s"),
19752 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 19753 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
19754 }
19755
19756 return str;
19757}
19758
a084a2a6 19759/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19760 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19761static const char *
19762dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19763{
19764 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19765 if (dwo_name == nullptr)
19766 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19767 return dwo_name;
19768}
19769
05cf31d1
JB
19770/* Return non-zero iff the attribute NAME is defined for the given DIE,
19771 and holds a non-zero value. This function should only be used for
2dc7f7b3 19772 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19773
19774static int
19775dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19776{
19777 struct attribute *attr = dwarf2_attr (die, name, cu);
19778
19779 return (attr && DW_UNSND (attr));
19780}
19781
3ca72b44 19782static int
e142c38c 19783die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19784{
05cf31d1
JB
19785 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19786 which value is non-zero. However, we have to be careful with
19787 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19788 (via dwarf2_flag_true_p) follows this attribute. So we may
19789 end up accidently finding a declaration attribute that belongs
19790 to a different DIE referenced by the specification attribute,
19791 even though the given DIE does not have a declaration attribute. */
19792 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19793 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19794}
19795
63d06c5c 19796/* Return the die giving the specification for DIE, if there is
f2f0e013 19797 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19798 containing the return value on output. If there is no
19799 specification, but there is an abstract origin, that is
19800 returned. */
63d06c5c
DC
19801
19802static struct die_info *
f2f0e013 19803die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19804{
f2f0e013
DJ
19805 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19806 *spec_cu);
63d06c5c 19807
edb3359d
DJ
19808 if (spec_attr == NULL)
19809 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19810
63d06c5c
DC
19811 if (spec_attr == NULL)
19812 return NULL;
19813 else
f2f0e013 19814 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19815}
c906108c 19816
527f3840
JK
19817/* Stub for free_line_header to match void * callback types. */
19818
19819static void
19820free_line_header_voidp (void *arg)
19821{
9a3c8263 19822 struct line_header *lh = (struct line_header *) arg;
527f3840 19823
fff8551c 19824 delete lh;
527f3840
JK
19825}
19826
83769d0b 19827/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19828
19829static struct dwarf2_section_info *
19830get_debug_line_section (struct dwarf2_cu *cu)
19831{
19832 struct dwarf2_section_info *section;
976ca316 19833 dwarf2_per_objfile *per_objfile = cu->per_objfile;
36586728
TT
19834
19835 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19836 DWO file. */
19837 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19838 section = &cu->dwo_unit->dwo_file->sections.line;
19839 else if (cu->per_cu->is_dwz)
19840 {
976ca316 19841 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
36586728
TT
19842
19843 section = &dwz->line;
19844 }
19845 else
976ca316 19846 section = &per_objfile->per_bfd->line;
36586728
TT
19847
19848 return section;
19849}
19850
debd256d 19851/* Read the statement program header starting at OFFSET in
3019eac3 19852 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19853 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19854 Returns NULL if there is a problem reading the header, e.g., if it
19855 has a version we don't understand.
debd256d
JB
19856
19857 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19858 the returned object point into the dwarf line section buffer,
19859 and must not be freed. */
ae2de4f8 19860
fff8551c 19861static line_header_up
9c541725 19862dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19863{
3019eac3 19864 struct dwarf2_section_info *section;
976ca316 19865 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 19866
36586728 19867 section = get_debug_line_section (cu);
976ca316 19868 section->read (per_objfile->objfile);
3019eac3 19869 if (section->buffer == NULL)
debd256d 19870 {
3019eac3 19871 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19872 complaint (_("missing .debug_line.dwo section"));
3019eac3 19873 else
b98664d3 19874 complaint (_("missing .debug_line section"));
debd256d
JB
19875 return 0;
19876 }
19877
0df7ad3a 19878 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
976ca316 19879 per_objfile, section, &cu->header);
debd256d 19880}
c906108c 19881
c6da4cef 19882/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19883 Return the file name of the psymtab for the given file_entry.
c6da4cef 19884 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19885 If space for the result is malloc'd, *NAME_HOLDER will be set.
19886 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19887
d521ce57 19888static const char *
7ba99d21 19889psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19890 const dwarf2_psymtab *pst,
c89b44cd
TT
19891 const char *comp_dir,
19892 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19893{
d521ce57
TT
19894 const char *include_name = fe.name;
19895 const char *include_name_to_compare = include_name;
72b9f47f 19896 const char *pst_filename;
c6da4cef
DE
19897 int file_is_pst;
19898
8c43009f 19899 const char *dir_name = fe.include_dir (lh);
c6da4cef 19900
c89b44cd 19901 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19902 if (!IS_ABSOLUTE_PATH (include_name)
19903 && (dir_name != NULL || comp_dir != NULL))
19904 {
19905 /* Avoid creating a duplicate psymtab for PST.
19906 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19907 Before we do the comparison, however, we need to account
19908 for DIR_NAME and COMP_DIR.
19909 First prepend dir_name (if non-NULL). If we still don't
19910 have an absolute path prepend comp_dir (if non-NULL).
19911 However, the directory we record in the include-file's
19912 psymtab does not contain COMP_DIR (to match the
19913 corresponding symtab(s)).
19914
19915 Example:
19916
19917 bash$ cd /tmp
19918 bash$ gcc -g ./hello.c
19919 include_name = "hello.c"
19920 dir_name = "."
19921 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19922 DW_AT_name = "./hello.c"
19923
19924 */
c6da4cef
DE
19925
19926 if (dir_name != NULL)
19927 {
c89b44cd
TT
19928 name_holder->reset (concat (dir_name, SLASH_STRING,
19929 include_name, (char *) NULL));
19930 include_name = name_holder->get ();
c6da4cef 19931 include_name_to_compare = include_name;
c6da4cef
DE
19932 }
19933 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19934 {
c89b44cd
TT
19935 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19936 include_name, (char *) NULL));
19937 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19938 }
19939 }
19940
19941 pst_filename = pst->filename;
c89b44cd 19942 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19943 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19944 {
c89b44cd
TT
19945 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19946 pst_filename, (char *) NULL));
19947 pst_filename = copied_name.get ();
c6da4cef
DE
19948 }
19949
1e3fad37 19950 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19951
c6da4cef
DE
19952 if (file_is_pst)
19953 return NULL;
19954 return include_name;
19955}
19956
d9b3de22
DE
19957/* State machine to track the state of the line number program. */
19958
6f77053d 19959class lnp_state_machine
d9b3de22 19960{
6f77053d
PA
19961public:
19962 /* Initialize a machine state for the start of a line number
19963 program. */
804d2729
TT
19964 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19965 bool record_lines_p);
6f77053d 19966
8c43009f
PA
19967 file_entry *current_file ()
19968 {
19969 /* lh->file_names is 0-based, but the file name numbers in the
19970 statement program are 1-based. */
6f77053d
PA
19971 return m_line_header->file_name_at (m_file);
19972 }
19973
19974 /* Record the line in the state machine. END_SEQUENCE is true if
19975 we're processing the end of a sequence. */
19976 void record_line (bool end_sequence);
19977
7ab6656f
OJ
19978 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19979 nop-out rest of the lines in this sequence. */
6f77053d
PA
19980 void check_line_address (struct dwarf2_cu *cu,
19981 const gdb_byte *line_ptr,
7ab6656f 19982 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19983
19984 void handle_set_discriminator (unsigned int discriminator)
19985 {
19986 m_discriminator = discriminator;
19987 m_line_has_non_zero_discriminator |= discriminator != 0;
19988 }
19989
19990 /* Handle DW_LNE_set_address. */
19991 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19992 {
19993 m_op_index = 0;
19994 address += baseaddr;
19995 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19996 }
19997
19998 /* Handle DW_LNS_advance_pc. */
19999 void handle_advance_pc (CORE_ADDR adjust);
20000
20001 /* Handle a special opcode. */
20002 void handle_special_opcode (unsigned char op_code);
20003
20004 /* Handle DW_LNS_advance_line. */
20005 void handle_advance_line (int line_delta)
20006 {
20007 advance_line (line_delta);
20008 }
20009
20010 /* Handle DW_LNS_set_file. */
20011 void handle_set_file (file_name_index file);
20012
20013 /* Handle DW_LNS_negate_stmt. */
20014 void handle_negate_stmt ()
20015 {
20016 m_is_stmt = !m_is_stmt;
20017 }
20018
20019 /* Handle DW_LNS_const_add_pc. */
20020 void handle_const_add_pc ();
20021
20022 /* Handle DW_LNS_fixed_advance_pc. */
20023 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20024 {
20025 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20026 m_op_index = 0;
20027 }
20028
20029 /* Handle DW_LNS_copy. */
20030 void handle_copy ()
20031 {
20032 record_line (false);
20033 m_discriminator = 0;
20034 }
20035
20036 /* Handle DW_LNE_end_sequence. */
20037 void handle_end_sequence ()
20038 {
804d2729 20039 m_currently_recording_lines = true;
6f77053d
PA
20040 }
20041
20042private:
20043 /* Advance the line by LINE_DELTA. */
20044 void advance_line (int line_delta)
20045 {
20046 m_line += line_delta;
20047
20048 if (line_delta != 0)
20049 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20050 }
20051
804d2729
TT
20052 struct dwarf2_cu *m_cu;
20053
6f77053d
PA
20054 gdbarch *m_gdbarch;
20055
20056 /* True if we're recording lines.
20057 Otherwise we're building partial symtabs and are just interested in
20058 finding include files mentioned by the line number program. */
20059 bool m_record_lines_p;
20060
8c43009f 20061 /* The line number header. */
6f77053d 20062 line_header *m_line_header;
8c43009f 20063
6f77053d
PA
20064 /* These are part of the standard DWARF line number state machine,
20065 and initialized according to the DWARF spec. */
d9b3de22 20066
6f77053d 20067 unsigned char m_op_index = 0;
7ba99d21
AT
20068 /* The line table index of the current file. */
20069 file_name_index m_file = 1;
6f77053d
PA
20070 unsigned int m_line = 1;
20071
20072 /* These are initialized in the constructor. */
20073
20074 CORE_ADDR m_address;
20075 bool m_is_stmt;
20076 unsigned int m_discriminator;
d9b3de22
DE
20077
20078 /* Additional bits of state we need to track. */
20079
20080 /* The last file that we called dwarf2_start_subfile for.
20081 This is only used for TLLs. */
6f77053d 20082 unsigned int m_last_file = 0;
d9b3de22 20083 /* The last file a line number was recorded for. */
6f77053d 20084 struct subfile *m_last_subfile = NULL;
d9b3de22 20085
1313c56e
AB
20086 /* The address of the last line entry. */
20087 CORE_ADDR m_last_address;
20088
20089 /* Set to true when a previous line at the same address (using
20090 m_last_address) had m_is_stmt true. This is reset to false when a
20091 line entry at a new address (m_address different to m_last_address) is
20092 processed. */
20093 bool m_stmt_at_address = false;
20094
804d2729
TT
20095 /* When true, record the lines we decode. */
20096 bool m_currently_recording_lines = false;
d9b3de22
DE
20097
20098 /* The last line number that was recorded, used to coalesce
20099 consecutive entries for the same line. This can happen, for
20100 example, when discriminators are present. PR 17276. */
6f77053d
PA
20101 unsigned int m_last_line = 0;
20102 bool m_line_has_non_zero_discriminator = false;
8c43009f 20103};
d9b3de22 20104
6f77053d
PA
20105void
20106lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20107{
20108 CORE_ADDR addr_adj = (((m_op_index + adjust)
20109 / m_line_header->maximum_ops_per_instruction)
20110 * m_line_header->minimum_instruction_length);
20111 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20112 m_op_index = ((m_op_index + adjust)
20113 % m_line_header->maximum_ops_per_instruction);
20114}
d9b3de22 20115
6f77053d
PA
20116void
20117lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20118{
6f77053d 20119 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
20120 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20121 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20122 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
20123 / m_line_header->maximum_ops_per_instruction)
20124 * m_line_header->minimum_instruction_length);
20125 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 20126 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 20127 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20128
258bf0ee 20129 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
20130 advance_line (line_delta);
20131 record_line (false);
20132 m_discriminator = 0;
20133}
d9b3de22 20134
6f77053d
PA
20135void
20136lnp_state_machine::handle_set_file (file_name_index file)
20137{
20138 m_file = file;
20139
20140 const file_entry *fe = current_file ();
20141 if (fe == NULL)
20142 dwarf2_debug_line_missing_file_complaint ();
20143 else if (m_record_lines_p)
20144 {
20145 const char *dir = fe->include_dir (m_line_header);
20146
c24bdb02 20147 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20148 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20149 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20150 }
20151}
20152
20153void
20154lnp_state_machine::handle_const_add_pc ()
20155{
20156 CORE_ADDR adjust
20157 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20158
20159 CORE_ADDR addr_adj
20160 = (((m_op_index + adjust)
20161 / m_line_header->maximum_ops_per_instruction)
20162 * m_line_header->minimum_instruction_length);
20163
20164 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20165 m_op_index = ((m_op_index + adjust)
20166 % m_line_header->maximum_ops_per_instruction);
20167}
d9b3de22 20168
a05a36a5
DE
20169/* Return non-zero if we should add LINE to the line number table.
20170 LINE is the line to add, LAST_LINE is the last line that was added,
20171 LAST_SUBFILE is the subfile for LAST_LINE.
20172 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20173 had a non-zero discriminator.
20174
20175 We have to be careful in the presence of discriminators.
20176 E.g., for this line:
20177
20178 for (i = 0; i < 100000; i++);
20179
20180 clang can emit four line number entries for that one line,
20181 each with a different discriminator.
20182 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20183
20184 However, we want gdb to coalesce all four entries into one.
20185 Otherwise the user could stepi into the middle of the line and
20186 gdb would get confused about whether the pc really was in the
20187 middle of the line.
20188
20189 Things are further complicated by the fact that two consecutive
20190 line number entries for the same line is a heuristic used by gcc
20191 to denote the end of the prologue. So we can't just discard duplicate
20192 entries, we have to be selective about it. The heuristic we use is
20193 that we only collapse consecutive entries for the same line if at least
20194 one of those entries has a non-zero discriminator. PR 17276.
20195
20196 Note: Addresses in the line number state machine can never go backwards
20197 within one sequence, thus this coalescing is ok. */
20198
20199static int
804d2729
TT
20200dwarf_record_line_p (struct dwarf2_cu *cu,
20201 unsigned int line, unsigned int last_line,
a05a36a5
DE
20202 int line_has_non_zero_discriminator,
20203 struct subfile *last_subfile)
20204{
c24bdb02 20205 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20206 return 1;
20207 if (line != last_line)
20208 return 1;
20209 /* Same line for the same file that we've seen already.
20210 As a last check, for pr 17276, only record the line if the line
20211 has never had a non-zero discriminator. */
20212 if (!line_has_non_zero_discriminator)
20213 return 1;
20214 return 0;
20215}
20216
804d2729
TT
20217/* Use the CU's builder to record line number LINE beginning at
20218 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20219
20220static void
d9b3de22 20221dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 20222 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 20223 struct dwarf2_cu *cu)
252a6764
DE
20224{
20225 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20226
27e0867f
DE
20227 if (dwarf_line_debug)
20228 {
20229 fprintf_unfiltered (gdb_stdlog,
20230 "Recording line %u, file %s, address %s\n",
20231 line, lbasename (subfile->name),
20232 paddress (gdbarch, address));
20233 }
20234
804d2729 20235 if (cu != nullptr)
8c95582d 20236 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
20237}
20238
20239/* Subroutine of dwarf_decode_lines_1 to simplify it.
20240 Mark the end of a set of line number records.
d9b3de22 20241 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20242 If SUBFILE is NULL the request is ignored. */
20243
20244static void
20245dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20246 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20247{
27e0867f
DE
20248 if (subfile == NULL)
20249 return;
20250
20251 if (dwarf_line_debug)
20252 {
20253 fprintf_unfiltered (gdb_stdlog,
20254 "Finishing current line, file %s, address %s\n",
20255 lbasename (subfile->name),
20256 paddress (gdbarch, address));
20257 }
20258
8c95582d 20259 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
20260}
20261
6f77053d
PA
20262void
20263lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20264{
d9b3de22
DE
20265 if (dwarf_line_debug)
20266 {
20267 fprintf_unfiltered (gdb_stdlog,
20268 "Processing actual line %u: file %u,"
94a72be7 20269 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 20270 m_line, m_file,
6f77053d 20271 paddress (m_gdbarch, m_address),
94a72be7
AB
20272 m_is_stmt, m_discriminator,
20273 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
20274 }
20275
6f77053d 20276 file_entry *fe = current_file ();
8c43009f
PA
20277
20278 if (fe == NULL)
d9b3de22
DE
20279 dwarf2_debug_line_missing_file_complaint ();
20280 /* For now we ignore lines not starting on an instruction boundary.
20281 But not when processing end_sequence for compatibility with the
20282 previous version of the code. */
6f77053d 20283 else if (m_op_index == 0 || end_sequence)
d9b3de22 20284 {
8c43009f 20285 fe->included_p = 1;
8c95582d 20286 if (m_record_lines_p)
d9b3de22 20287 {
1313c56e
AB
20288 /* When we switch files we insert an end maker in the first file,
20289 switch to the second file and add a new line entry. The
20290 problem is that the end marker inserted in the first file will
20291 discard any previous line entries at the same address. If the
20292 line entries in the first file are marked as is-stmt, while
20293 the new line in the second file is non-stmt, then this means
20294 the end marker will discard is-stmt lines so we can have a
20295 non-stmt line. This means that there are less addresses at
20296 which the user can insert a breakpoint.
20297
20298 To improve this we track the last address in m_last_address,
20299 and whether we have seen an is-stmt at this address. Then
20300 when switching files, if we have seen a stmt at the current
20301 address, and we are switching to create a non-stmt line, then
20302 discard the new line. */
20303 bool file_changed
20304 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
20305 bool ignore_this_line
20306 = (file_changed && !end_sequence && m_last_address == m_address
20307 && !m_is_stmt && m_stmt_at_address);
20308
20309 if ((file_changed && !ignore_this_line) || end_sequence)
d9b3de22 20310 {
804d2729
TT
20311 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20312 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20313 }
20314
1313c56e 20315 if (!end_sequence && !ignore_this_line)
d9b3de22 20316 {
8c95582d
AB
20317 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20318
804d2729 20319 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20320 m_line_has_non_zero_discriminator,
20321 m_last_subfile))
d9b3de22 20322 {
c24bdb02 20323 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20324 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20325 builder->get_current_subfile (),
8c95582d 20326 m_line, m_address, is_stmt,
804d2729 20327 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20328 }
c24bdb02 20329 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20330 m_last_line = m_line;
d9b3de22
DE
20331 }
20332 }
20333 }
1313c56e
AB
20334
20335 /* Track whether we have seen any m_is_stmt true at m_address in case we
20336 have multiple line table entries all at m_address. */
20337 if (m_last_address != m_address)
20338 {
20339 m_stmt_at_address = false;
20340 m_last_address = m_address;
20341 }
20342 m_stmt_at_address |= m_is_stmt;
d9b3de22
DE
20343}
20344
804d2729
TT
20345lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20346 line_header *lh, bool record_lines_p)
d9b3de22 20347{
804d2729 20348 m_cu = cu;
6f77053d
PA
20349 m_gdbarch = arch;
20350 m_record_lines_p = record_lines_p;
20351 m_line_header = lh;
d9b3de22 20352
804d2729 20353 m_currently_recording_lines = true;
d9b3de22 20354
d9b3de22
DE
20355 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20356 was a line entry for it so that the backend has a chance to adjust it
20357 and also record it in case it needs it. This is currently used by MIPS
20358 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20359 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20360 m_is_stmt = lh->default_is_stmt;
20361 m_discriminator = 0;
1313c56e
AB
20362
20363 m_last_address = m_address;
20364 m_stmt_at_address = false;
252a6764
DE
20365}
20366
6f77053d
PA
20367void
20368lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20369 const gdb_byte *line_ptr,
7ab6656f 20370 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20371{
7ab6656f
OJ
20372 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20373 the pc range of the CU. However, we restrict the test to only ADDRESS
20374 values of zero to preserve GDB's previous behaviour which is to handle
20375 the specific case of a function being GC'd by the linker. */
924c2928 20376
7ab6656f 20377 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20378 {
20379 /* This line table is for a function which has been
20380 GCd by the linker. Ignore it. PR gdb/12528 */
20381
5e22e966 20382 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
20383 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20384
b98664d3 20385 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20386 line_offset, objfile_name (objfile));
804d2729
TT
20387 m_currently_recording_lines = false;
20388 /* Note: m_currently_recording_lines is left as false until we see
20389 DW_LNE_end_sequence. */
924c2928
DE
20390 }
20391}
20392
f3f5162e 20393/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20394 Process the line number information in LH.
20395 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20396 program in order to set included_p for every referenced header. */
debd256d 20397
c906108c 20398static void
43f3e411
DE
20399dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20400 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20401{
d521ce57
TT
20402 const gdb_byte *line_ptr, *extended_end;
20403 const gdb_byte *line_end;
a8c50c1f 20404 unsigned int bytes_read, extended_len;
699ca60a 20405 unsigned char op_code, extended_op;
e142c38c 20406 CORE_ADDR baseaddr;
5e22e966 20407 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20408 bfd *abfd = objfile->obfd;
08feed99 20409 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
20410 /* True if we're recording line info (as opposed to building partial
20411 symtabs and just interested in finding include files mentioned by
20412 the line number program). */
20413 bool record_lines_p = !decode_for_pst_p;
e142c38c 20414
b3b3bada 20415 baseaddr = objfile->text_section_offset ();
c906108c 20416
debd256d
JB
20417 line_ptr = lh->statement_program_start;
20418 line_end = lh->statement_program_end;
c906108c
SS
20419
20420 /* Read the statement sequences until there's nothing left. */
20421 while (line_ptr < line_end)
20422 {
6f77053d
PA
20423 /* The DWARF line number program state machine. Reset the state
20424 machine at the start of each sequence. */
804d2729 20425 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20426 bool end_sequence = false;
d9b3de22 20427
8c43009f 20428 if (record_lines_p)
c906108c 20429 {
8c43009f
PA
20430 /* Start a subfile for the current file of the state
20431 machine. */
20432 const file_entry *fe = state_machine.current_file ();
20433
20434 if (fe != NULL)
804d2729 20435 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20436 }
20437
a738430d 20438 /* Decode the table. */
d9b3de22 20439 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20440 {
20441 op_code = read_1_byte (abfd, line_ptr);
20442 line_ptr += 1;
9aa1fe7e 20443
debd256d 20444 if (op_code >= lh->opcode_base)
6e70227d 20445 {
8e07a239 20446 /* Special opcode. */
6f77053d 20447 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20448 }
20449 else switch (op_code)
c906108c
SS
20450 {
20451 case DW_LNS_extended_op:
3e43a32a
MS
20452 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20453 &bytes_read);
473b7be6 20454 line_ptr += bytes_read;
a8c50c1f 20455 extended_end = line_ptr + extended_len;
c906108c
SS
20456 extended_op = read_1_byte (abfd, line_ptr);
20457 line_ptr += 1;
20458 switch (extended_op)
20459 {
20460 case DW_LNE_end_sequence:
6f77053d
PA
20461 state_machine.handle_end_sequence ();
20462 end_sequence = true;
c906108c
SS
20463 break;
20464 case DW_LNE_set_address:
d9b3de22
DE
20465 {
20466 CORE_ADDR address
c8a7a66f 20467 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 20468 line_ptr += bytes_read;
6f77053d
PA
20469
20470 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20471 lowpc - baseaddr, address);
6f77053d 20472 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20473 }
c906108c
SS
20474 break;
20475 case DW_LNE_define_file:
debd256d 20476 {
d521ce57 20477 const char *cur_file;
ecfb656c
PA
20478 unsigned int mod_time, length;
20479 dir_index dindex;
6e70227d 20480
3e43a32a
MS
20481 cur_file = read_direct_string (abfd, line_ptr,
20482 &bytes_read);
debd256d 20483 line_ptr += bytes_read;
ecfb656c 20484 dindex = (dir_index)
debd256d
JB
20485 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20486 line_ptr += bytes_read;
20487 mod_time =
20488 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20489 line_ptr += bytes_read;
20490 length =
20491 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20492 line_ptr += bytes_read;
ecfb656c 20493 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20494 }
c906108c 20495 break;
d0c6ba3d 20496 case DW_LNE_set_discriminator:
6f77053d
PA
20497 {
20498 /* The discriminator is not interesting to the
20499 debugger; just ignore it. We still need to
20500 check its value though:
20501 if there are consecutive entries for the same
20502 (non-prologue) line we want to coalesce them.
20503 PR 17276. */
20504 unsigned int discr
20505 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20506 line_ptr += bytes_read;
20507
20508 state_machine.handle_set_discriminator (discr);
20509 }
d0c6ba3d 20510 break;
c906108c 20511 default:
b98664d3 20512 complaint (_("mangled .debug_line section"));
debd256d 20513 return;
c906108c 20514 }
a8c50c1f
DJ
20515 /* Make sure that we parsed the extended op correctly. If e.g.
20516 we expected a different address size than the producer used,
20517 we may have read the wrong number of bytes. */
20518 if (line_ptr != extended_end)
20519 {
b98664d3 20520 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20521 return;
20522 }
c906108c
SS
20523 break;
20524 case DW_LNS_copy:
6f77053d 20525 state_machine.handle_copy ();
c906108c
SS
20526 break;
20527 case DW_LNS_advance_pc:
2dc7f7b3
TT
20528 {
20529 CORE_ADDR adjust
20530 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20531 line_ptr += bytes_read;
6f77053d
PA
20532
20533 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20534 }
c906108c
SS
20535 break;
20536 case DW_LNS_advance_line:
a05a36a5
DE
20537 {
20538 int line_delta
20539 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20540 line_ptr += bytes_read;
6f77053d
PA
20541
20542 state_machine.handle_advance_line (line_delta);
a05a36a5 20543 }
c906108c
SS
20544 break;
20545 case DW_LNS_set_file:
d9b3de22 20546 {
6f77053d 20547 file_name_index file
ecfb656c
PA
20548 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20549 &bytes_read);
d9b3de22 20550 line_ptr += bytes_read;
8c43009f 20551
6f77053d 20552 state_machine.handle_set_file (file);
d9b3de22 20553 }
c906108c
SS
20554 break;
20555 case DW_LNS_set_column:
0ad93d4f 20556 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20557 line_ptr += bytes_read;
20558 break;
20559 case DW_LNS_negate_stmt:
6f77053d 20560 state_machine.handle_negate_stmt ();
c906108c
SS
20561 break;
20562 case DW_LNS_set_basic_block:
c906108c 20563 break;
c2c6d25f
JM
20564 /* Add to the address register of the state machine the
20565 address increment value corresponding to special opcode
a738430d
MK
20566 255. I.e., this value is scaled by the minimum
20567 instruction length since special opcode 255 would have
b021a221 20568 scaled the increment. */
c906108c 20569 case DW_LNS_const_add_pc:
6f77053d 20570 state_machine.handle_const_add_pc ();
c906108c
SS
20571 break;
20572 case DW_LNS_fixed_advance_pc:
3e29f34a 20573 {
6f77053d 20574 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20575 line_ptr += 2;
6f77053d
PA
20576
20577 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20578 }
c906108c 20579 break;
9aa1fe7e 20580 default:
a738430d
MK
20581 {
20582 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20583 int i;
a738430d 20584
debd256d 20585 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20586 {
20587 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20588 line_ptr += bytes_read;
20589 }
20590 }
c906108c
SS
20591 }
20592 }
d9b3de22
DE
20593
20594 if (!end_sequence)
20595 dwarf2_debug_line_missing_end_sequence_complaint ();
20596
20597 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20598 in which case we still finish recording the last line). */
6f77053d 20599 state_machine.record_line (true);
c906108c 20600 }
f3f5162e
DE
20601}
20602
20603/* Decode the Line Number Program (LNP) for the given line_header
20604 structure and CU. The actual information extracted and the type
20605 of structures created from the LNP depends on the value of PST.
20606
20607 1. If PST is NULL, then this procedure uses the data from the program
20608 to create all necessary symbol tables, and their linetables.
20609
20610 2. If PST is not NULL, this procedure reads the program to determine
20611 the list of files included by the unit represented by PST, and
20612 builds all the associated partial symbol tables.
20613
20614 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20615 It is used for relative paths in the line table.
20616 NOTE: When processing partial symtabs (pst != NULL),
20617 comp_dir == pst->dirname.
20618
20619 NOTE: It is important that psymtabs have the same file name (via strcmp)
20620 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20621 symtab we don't use it in the name of the psymtabs we create.
20622 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20623 A good testcase for this is mb-inline.exp.
20624
527f3840
JK
20625 LOWPC is the lowest address in CU (or 0 if not known).
20626
20627 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20628 for its PC<->lines mapping information. Otherwise only the filename
20629 table is read in. */
f3f5162e
DE
20630
20631static void
20632dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20633 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20634 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20635{
5e22e966 20636 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20637 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20638
527f3840
JK
20639 if (decode_mapping)
20640 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20641
20642 if (decode_for_pst_p)
20643 {
aaa75496
JB
20644 /* Now that we're done scanning the Line Header Program, we can
20645 create the psymtab of each included file. */
7ba99d21
AT
20646 for (auto &file_entry : lh->file_names ())
20647 if (file_entry.included_p == 1)
aaa75496 20648 {
c89b44cd 20649 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20650 const char *include_name =
7ba99d21
AT
20651 psymtab_include_file_name (lh, file_entry, pst,
20652 comp_dir, &name_holder);
c6da4cef 20653 if (include_name != NULL)
aaa75496
JB
20654 dwarf2_create_include_psymtab (include_name, pst, objfile);
20655 }
20656 }
cb1df416
DJ
20657 else
20658 {
20659 /* Make sure a symtab is created for every file, even files
20660 which contain only variables (i.e. no code with associated
20661 line numbers). */
c24bdb02
KS
20662 buildsym_compunit *builder = cu->get_builder ();
20663 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20664
7ba99d21 20665 for (auto &fe : lh->file_names ())
cb1df416 20666 {
804d2729 20667 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20668 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20669 {
c24bdb02 20670 builder->get_current_subfile ()->symtab
804d2729 20671 = allocate_symtab (cust,
c24bdb02 20672 builder->get_current_subfile ()->name);
43f3e411 20673 }
c24bdb02 20674 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20675 }
20676 }
c906108c
SS
20677}
20678
20679/* Start a subfile for DWARF. FILENAME is the name of the file and
20680 DIRNAME the name of the source directory which contains FILENAME
4d663531 20681 or NULL if not known.
c906108c
SS
20682 This routine tries to keep line numbers from identical absolute and
20683 relative file names in a common subfile.
20684
20685 Using the `list' example from the GDB testsuite, which resides in
20686 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20687 of /srcdir/list0.c yields the following debugging information for list0.c:
20688
c5aa993b 20689 DW_AT_name: /srcdir/list0.c
4d663531 20690 DW_AT_comp_dir: /compdir
357e46e7 20691 files.files[0].name: list0.h
c5aa993b 20692 files.files[0].dir: /srcdir
357e46e7 20693 files.files[1].name: list0.c
c5aa993b 20694 files.files[1].dir: /srcdir
c906108c
SS
20695
20696 The line number information for list0.c has to end up in a single
4f1520fb
FR
20697 subfile, so that `break /srcdir/list0.c:1' works as expected.
20698 start_subfile will ensure that this happens provided that we pass the
20699 concatenation of files.files[1].dir and files.files[1].name as the
20700 subfile's name. */
c906108c
SS
20701
20702static void
804d2729
TT
20703dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20704 const char *dirname)
c906108c 20705{
43816ebc 20706 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20707
4d663531 20708 /* In order not to lose the line information directory,
4f1520fb
FR
20709 we concatenate it to the filename when it makes sense.
20710 Note that the Dwarf3 standard says (speaking of filenames in line
20711 information): ``The directory index is ignored for file names
20712 that represent full path names''. Thus ignoring dirname in the
20713 `else' branch below isn't an issue. */
c906108c 20714
d5166ae1 20715 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20716 {
43816ebc
TT
20717 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20718 filename = copy.get ();
d521ce57 20719 }
c906108c 20720
c24bdb02 20721 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20722}
20723
804d2729
TT
20724/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20725 buildsym_compunit constructor. */
f4dc4d17 20726
c24bdb02
KS
20727struct compunit_symtab *
20728dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20729 CORE_ADDR low_pc)
f4dc4d17 20730{
c24bdb02 20731 gdb_assert (m_builder == nullptr);
43f3e411 20732
c24bdb02 20733 m_builder.reset (new struct buildsym_compunit
f6e649dd 20734 (this->per_objfile->objfile,
c24bdb02 20735 name, comp_dir, language, low_pc));
93b8bea4 20736
c24bdb02 20737 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20738
c24bdb02
KS
20739 get_builder ()->record_debugformat ("DWARF 2");
20740 get_builder ()->record_producer (producer);
f4dc4d17 20741
c24bdb02 20742 processing_has_namespace_info = false;
43f3e411 20743
c24bdb02 20744 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20745}
20746
4c2df51b
DJ
20747static void
20748var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20749 struct dwarf2_cu *cu)
4c2df51b 20750{
5e22e966 20751 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
20752 struct comp_unit_head *cu_header = &cu->header;
20753
4c2df51b
DJ
20754 /* NOTE drow/2003-01-30: There used to be a comment and some special
20755 code here to turn a symbol with DW_AT_external and a
20756 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20757 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20758 with some versions of binutils) where shared libraries could have
20759 relocations against symbols in their debug information - the
20760 minimal symbol would have the right address, but the debug info
20761 would not. It's no longer necessary, because we will explicitly
20762 apply relocations when we read in the debug information now. */
20763
20764 /* A DW_AT_location attribute with no contents indicates that a
20765 variable has been optimized away. */
4fc6c0d5 20766 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20767 {
f1e6e072 20768 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20769 return;
20770 }
20771
20772 /* Handle one degenerate form of location expression specially, to
20773 preserve GDB's previous behavior when section offsets are
336d760d
AT
20774 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20775 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20776
4fc6c0d5 20777 if (attr->form_is_block ()
3019eac3
DE
20778 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20779 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20780 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20781 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20782 && (DW_BLOCK (attr)->size
20783 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20784 {
891d2f0b 20785 unsigned int dummy;
4c2df51b 20786
3019eac3 20787 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20788 SET_SYMBOL_VALUE_ADDRESS
20789 (sym, cu->header.read_address (objfile->obfd,
20790 DW_BLOCK (attr)->data + 1,
20791 &dummy));
3019eac3 20792 else
38583298
TT
20793 SET_SYMBOL_VALUE_ADDRESS
20794 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20795 &dummy));
f1e6e072 20796 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20797 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20798 SET_SYMBOL_VALUE_ADDRESS
20799 (sym,
20800 SYMBOL_VALUE_ADDRESS (sym)
20801 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20802 return;
20803 }
20804
20805 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20806 expression evaluator, and use LOC_COMPUTED only when necessary
20807 (i.e. when the value of a register or memory location is
20808 referenced, or a thread-local block, etc.). Then again, it might
20809 not be worthwhile. I'm assuming that it isn't unless performance
20810 or memory numbers show me otherwise. */
20811
f1e6e072 20812 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20813
f1e6e072 20814 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20815 cu->has_loclist = true;
4c2df51b
DJ
20816}
20817
c906108c
SS
20818/* Given a pointer to a DWARF information entry, figure out if we need
20819 to make a symbol table entry for it, and if so, create a new entry
20820 and return a pointer to it.
20821 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20822 used the passed type.
20823 If SPACE is not NULL, use it to hold the new symbol. If it is
20824 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20825
20826static struct symbol *
5e2db402
TT
20827new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20828 struct symbol *space)
c906108c 20829{
976ca316
SM
20830 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20831 struct objfile *objfile = per_objfile->objfile;
08feed99 20832 struct gdbarch *gdbarch = objfile->arch ();
c906108c 20833 struct symbol *sym = NULL;
15d034d0 20834 const char *name;
c906108c
SS
20835 struct attribute *attr = NULL;
20836 struct attribute *attr2 = NULL;
e142c38c 20837 CORE_ADDR baseaddr;
e37fd15a
SW
20838 struct pending **list_to_add = NULL;
20839
edb3359d 20840 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20841
b3b3bada 20842 baseaddr = objfile->text_section_offset ();
c906108c 20843
94af9270 20844 name = dwarf2_name (die, cu);
c906108c
SS
20845 if (name)
20846 {
34eaf542 20847 int suppress_add = 0;
94af9270 20848
34eaf542
TT
20849 if (space)
20850 sym = space;
20851 else
8c14c3a3 20852 sym = new (&objfile->objfile_obstack) symbol;
c906108c 20853 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20854
20855 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20856 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
20857 /* Fortran does not have mangling standard and the mangling does differ
20858 between gfortran, iFort etc. */
bcfe6157
TT
20859 const char *physname
20860 = (cu->language == language_fortran
20861 ? dwarf2_full_name (name, die, cu)
20862 : dwarf2_physname (name, die, cu));
20863 const char *linkagename = dw2_linkage_name (die, cu);
20864
20865 if (linkagename == nullptr || cu->language == language_ada)
20866 sym->set_linkage_name (physname);
20867 else
20868 {
20869 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20870 sym->set_linkage_name (linkagename);
20871 }
f55ee35c 20872
c906108c 20873 /* Default assumptions.
c5aa993b 20874 Use the passed type or decode it from the die. */
176620f1 20875 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20876 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20877 if (type != NULL)
20878 SYMBOL_TYPE (sym) = type;
20879 else
e7c27a73 20880 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20881 attr = dwarf2_attr (die,
20882 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20883 cu);
435d3d88 20884 if (attr != nullptr)
c906108c
SS
20885 {
20886 SYMBOL_LINE (sym) = DW_UNSND (attr);
20887 }
cb1df416 20888
edb3359d
DJ
20889 attr = dwarf2_attr (die,
20890 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20891 cu);
435d3d88 20892 if (attr != nullptr)
cb1df416 20893 {
ecfb656c 20894 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20895 struct file_entry *fe;
9a619af0 20896
ecfb656c
PA
20897 if (cu->line_header != NULL)
20898 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20899 else
20900 fe = NULL;
20901
20902 if (fe == NULL)
b98664d3 20903 complaint (_("file index out of range"));
8c43009f
PA
20904 else
20905 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20906 }
20907
c906108c
SS
20908 switch (die->tag)
20909 {
20910 case DW_TAG_label:
e142c38c 20911 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20912 if (attr != nullptr)
3e29f34a
MR
20913 {
20914 CORE_ADDR addr;
20915
cd6c91b4 20916 addr = attr->value_as_address ();
3e29f34a 20917 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20918 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20919 }
0f5238ed
TT
20920 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20921 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20922 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20923 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20924 break;
20925 case DW_TAG_subprogram:
20926 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20927 finish_block. */
f1e6e072 20928 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20929 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20930 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20931 || cu->language == language_ada
20932 || cu->language == language_fortran)
c906108c 20933 {
2cfa0c8d 20934 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20935 Ada and Fortran subprograms, whether marked external or
20936 not, are always stored as a global symbol, because we want
20937 to be able to access them globally. For instance, we want
20938 to be able to break on a nested subprogram without having
20939 to specify the context. */
c24bdb02 20940 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20941 }
20942 else
20943 {
e37fd15a 20944 list_to_add = cu->list_in_scope;
c906108c
SS
20945 }
20946 break;
edb3359d
DJ
20947 case DW_TAG_inlined_subroutine:
20948 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20949 finish_block. */
f1e6e072 20950 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20951 SYMBOL_INLINED (sym) = 1;
481860b3 20952 list_to_add = cu->list_in_scope;
edb3359d 20953 break;
34eaf542
TT
20954 case DW_TAG_template_value_param:
20955 suppress_add = 1;
20956 /* Fall through. */
72929c62 20957 case DW_TAG_constant:
c906108c 20958 case DW_TAG_variable:
254e6b9e 20959 case DW_TAG_member:
0963b4bd
MS
20960 /* Compilation with minimal debug info may result in
20961 variables with missing type entries. Change the
20962 misleading `void' type to something sensible. */
78134374 20963 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 20964 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20965
e142c38c 20966 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20967 /* In the case of DW_TAG_member, we should only be called for
20968 static const members. */
20969 if (die->tag == DW_TAG_member)
20970 {
3863f96c
DE
20971 /* dwarf2_add_field uses die_is_declaration,
20972 so we do the same. */
254e6b9e
DE
20973 gdb_assert (die_is_declaration (die, cu));
20974 gdb_assert (attr);
20975 }
435d3d88 20976 if (attr != nullptr)
c906108c 20977 {
e7c27a73 20978 dwarf2_const_value (attr, sym, cu);
e142c38c 20979 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20980 if (!suppress_add)
34eaf542
TT
20981 {
20982 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20983 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20984 else
e37fd15a 20985 list_to_add = cu->list_in_scope;
34eaf542 20986 }
c906108c
SS
20987 break;
20988 }
e142c38c 20989 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20990 if (attr != nullptr)
c906108c 20991 {
e7c27a73 20992 var_decode_location (attr, sym, cu);
e142c38c 20993 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20994
20995 /* Fortran explicitly imports any global symbols to the local
20996 scope by DW_TAG_common_block. */
20997 if (cu->language == language_fortran && die->parent
20998 && die->parent->tag == DW_TAG_common_block)
20999 attr2 = NULL;
21000
caac4577
JG
21001 if (SYMBOL_CLASS (sym) == LOC_STATIC
21002 && SYMBOL_VALUE_ADDRESS (sym) == 0
976ca316 21003 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
21004 {
21005 /* When a static variable is eliminated by the linker,
21006 the corresponding debug information is not stripped
21007 out, but the variable address is set to null;
21008 do not add such variables into symbol table. */
21009 }
21010 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21011 {
4b610737
TT
21012 if (SYMBOL_CLASS (sym) == LOC_STATIC
21013 && (objfile->flags & OBJF_MAINLINE) == 0
976ca316 21014 && per_objfile->per_bfd->can_copy)
4b610737
TT
21015 {
21016 /* A global static variable might be subject to
21017 copy relocation. We first check for a local
21018 minsym, though, because maybe the symbol was
21019 marked hidden, in which case this would not
21020 apply. */
21021 bound_minimal_symbol found
21022 = (lookup_minimal_symbol_linkage
987012b8 21023 (sym->linkage_name (), objfile));
4b610737
TT
21024 if (found.minsym != nullptr)
21025 sym->maybe_copied = 1;
21026 }
f55ee35c 21027
1c809c68
TT
21028 /* A variable with DW_AT_external is never static,
21029 but it may be block-scoped. */
804d2729 21030 list_to_add
c24bdb02
KS
21031 = ((cu->list_in_scope
21032 == cu->get_builder ()->get_file_symbols ())
21033 ? cu->get_builder ()->get_global_symbols ()
804d2729 21034 : cu->list_in_scope);
1c809c68 21035 }
c906108c 21036 else
e37fd15a 21037 list_to_add = cu->list_in_scope;
c906108c
SS
21038 }
21039 else
21040 {
21041 /* We do not know the address of this symbol.
c5aa993b
JM
21042 If it is an external symbol and we have type information
21043 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21044 The address of the variable will then be determined from
21045 the minimal symbol table whenever the variable is
21046 referenced. */
e142c38c 21047 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21048
21049 /* Fortran explicitly imports any global symbols to the local
21050 scope by DW_TAG_common_block. */
21051 if (cu->language == language_fortran && die->parent
21052 && die->parent->tag == DW_TAG_common_block)
21053 {
21054 /* SYMBOL_CLASS doesn't matter here because
21055 read_common_block is going to reset it. */
21056 if (!suppress_add)
21057 list_to_add = cu->list_in_scope;
21058 }
21059 else if (attr2 && (DW_UNSND (attr2) != 0)
21060 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21061 {
0fe7935b
DJ
21062 /* A variable with DW_AT_external is never static, but it
21063 may be block-scoped. */
804d2729 21064 list_to_add
c24bdb02
KS
21065 = ((cu->list_in_scope
21066 == cu->get_builder ()->get_file_symbols ())
21067 ? cu->get_builder ()->get_global_symbols ()
804d2729 21068 : cu->list_in_scope);
0fe7935b 21069
f1e6e072 21070 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21071 }
442ddf59
JK
21072 else if (!die_is_declaration (die, cu))
21073 {
21074 /* Use the default LOC_OPTIMIZED_OUT class. */
21075 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21076 if (!suppress_add)
21077 list_to_add = cu->list_in_scope;
442ddf59 21078 }
c906108c
SS
21079 }
21080 break;
21081 case DW_TAG_formal_parameter:
a60f3166
TT
21082 {
21083 /* If we are inside a function, mark this as an argument. If
21084 not, we might be looking at an argument to an inlined function
21085 when we do not have enough information to show inlined frames;
21086 pretend it's a local variable in that case so that the user can
21087 still see it. */
804d2729 21088 struct context_stack *curr
c24bdb02 21089 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
21090 if (curr != nullptr && curr->name != nullptr)
21091 SYMBOL_IS_ARGUMENT (sym) = 1;
21092 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21093 if (attr != nullptr)
a60f3166
TT
21094 {
21095 var_decode_location (attr, sym, cu);
21096 }
21097 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21098 if (attr != nullptr)
a60f3166
TT
21099 {
21100 dwarf2_const_value (attr, sym, cu);
21101 }
f346a30d 21102
a60f3166
TT
21103 list_to_add = cu->list_in_scope;
21104 }
c906108c
SS
21105 break;
21106 case DW_TAG_unspecified_parameters:
21107 /* From varargs functions; gdb doesn't seem to have any
21108 interest in this information, so just ignore it for now.
21109 (FIXME?) */
21110 break;
34eaf542
TT
21111 case DW_TAG_template_type_param:
21112 suppress_add = 1;
21113 /* Fall through. */
c906108c 21114 case DW_TAG_class_type:
680b30c7 21115 case DW_TAG_interface_type:
c906108c
SS
21116 case DW_TAG_structure_type:
21117 case DW_TAG_union_type:
72019c9c 21118 case DW_TAG_set_type:
c906108c 21119 case DW_TAG_enumeration_type:
f1e6e072 21120 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21121 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21122
63d06c5c 21123 {
9c37b5ae 21124 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21125 really ever be static objects: otherwise, if you try
21126 to, say, break of a class's method and you're in a file
21127 which doesn't mention that class, it won't work unless
21128 the check for all static symbols in lookup_symbol_aux
21129 saves you. See the OtherFileClass tests in
21130 gdb.c++/namespace.exp. */
21131
e37fd15a 21132 if (!suppress_add)
34eaf542 21133 {
c24bdb02 21134 buildsym_compunit *builder = cu->get_builder ();
804d2729 21135 list_to_add
c24bdb02 21136 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 21137 && cu->language == language_cplus
c24bdb02 21138 ? builder->get_global_symbols ()
804d2729 21139 : cu->list_in_scope);
63d06c5c 21140
64382290 21141 /* The semantics of C++ state that "struct foo {
9c37b5ae 21142 ... }" also defines a typedef for "foo". */
64382290 21143 if (cu->language == language_cplus
45280282 21144 || cu->language == language_ada
c44af4eb
TT
21145 || cu->language == language_d
21146 || cu->language == language_rust)
64382290
TT
21147 {
21148 /* The symbol's name is already allocated along
21149 with this objfile, so we don't need to
21150 duplicate it for the type. */
7d93a1e0 21151 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 21152 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 21153 }
63d06c5c
DC
21154 }
21155 }
c906108c
SS
21156 break;
21157 case DW_TAG_typedef:
f1e6e072 21158 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21159 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21160 list_to_add = cu->list_in_scope;
63d06c5c 21161 break;
c906108c 21162 case DW_TAG_base_type:
a02abb62 21163 case DW_TAG_subrange_type:
f1e6e072 21164 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21165 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21166 list_to_add = cu->list_in_scope;
c906108c
SS
21167 break;
21168 case DW_TAG_enumerator:
e142c38c 21169 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21170 if (attr != nullptr)
c906108c 21171 {
e7c27a73 21172 dwarf2_const_value (attr, sym, cu);
c906108c 21173 }
63d06c5c
DC
21174 {
21175 /* NOTE: carlton/2003-11-10: See comment above in the
21176 DW_TAG_class_type, etc. block. */
21177
804d2729 21178 list_to_add
c24bdb02 21179 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21180 && cu->language == language_cplus
c24bdb02 21181 ? cu->get_builder ()->get_global_symbols ()
804d2729 21182 : cu->list_in_scope);
63d06c5c 21183 }
c906108c 21184 break;
74921315 21185 case DW_TAG_imported_declaration:
5c4e30ca 21186 case DW_TAG_namespace:
f1e6e072 21187 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21188 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21189 break;
530e8392
KB
21190 case DW_TAG_module:
21191 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21192 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21193 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21194 break;
4357ac6c 21195 case DW_TAG_common_block:
f1e6e072 21196 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21197 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21198 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21199 break;
c906108c
SS
21200 default:
21201 /* Not a tag we recognize. Hopefully we aren't processing
21202 trash data, but since we must specifically ignore things
21203 we don't recognize, there is nothing else we should do at
0963b4bd 21204 this point. */
b98664d3 21205 complaint (_("unsupported tag: '%s'"),
4d3c2250 21206 dwarf_tag_name (die->tag));
c906108c
SS
21207 break;
21208 }
df8a16a1 21209
e37fd15a
SW
21210 if (suppress_add)
21211 {
21212 sym->hash_next = objfile->template_symbols;
21213 objfile->template_symbols = sym;
21214 list_to_add = NULL;
21215 }
21216
21217 if (list_to_add != NULL)
d3cb6808 21218 add_symbol_to_list (sym, list_to_add);
e37fd15a 21219
df8a16a1
DJ
21220 /* For the benefit of old versions of GCC, check for anonymous
21221 namespaces based on the demangled name. */
4d4ec4e5 21222 if (!cu->processing_has_namespace_info
94af9270 21223 && cu->language == language_cplus)
c24bdb02 21224 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21225 }
21226 return (sym);
21227}
21228
98bfdba5
PA
21229/* Given an attr with a DW_FORM_dataN value in host byte order,
21230 zero-extend it as appropriate for the symbol's type. The DWARF
21231 standard (v4) is not entirely clear about the meaning of using
21232 DW_FORM_dataN for a constant with a signed type, where the type is
21233 wider than the data. The conclusion of a discussion on the DWARF
21234 list was that this is unspecified. We choose to always zero-extend
21235 because that is the interpretation long in use by GCC. */
c906108c 21236
98bfdba5 21237static gdb_byte *
ff39bb5e 21238dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21239 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21240{
5e22e966 21241 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
21242 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21243 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21244 LONGEST l = DW_UNSND (attr);
21245
21246 if (bits < sizeof (*value) * 8)
21247 {
21248 l &= ((LONGEST) 1 << bits) - 1;
21249 *value = l;
21250 }
21251 else if (bits == sizeof (*value) * 8)
21252 *value = l;
21253 else
21254 {
224c3ddb 21255 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21256 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21257 return bytes;
21258 }
21259
21260 return NULL;
21261}
21262
21263/* Read a constant value from an attribute. Either set *VALUE, or if
21264 the value does not fit in *VALUE, set *BYTES - either already
21265 allocated on the objfile obstack, or newly allocated on OBSTACK,
21266 or, set *BATON, if we translated the constant to a location
21267 expression. */
21268
21269static void
ff39bb5e 21270dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21271 const char *name, struct obstack *obstack,
21272 struct dwarf2_cu *cu,
d521ce57 21273 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21274 struct dwarf2_locexpr_baton **baton)
21275{
5e22e966 21276 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 21277 struct objfile *objfile = per_objfile->objfile;
98bfdba5 21278 struct comp_unit_head *cu_header = &cu->header;
c906108c 21279 struct dwarf_block *blk;
98bfdba5
PA
21280 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21281 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21282
21283 *value = 0;
21284 *bytes = NULL;
21285 *baton = NULL;
c906108c
SS
21286
21287 switch (attr->form)
21288 {
21289 case DW_FORM_addr:
336d760d 21290 case DW_FORM_addrx:
3019eac3 21291 case DW_FORM_GNU_addr_index:
ac56253d 21292 {
ac56253d
TT
21293 gdb_byte *data;
21294
98bfdba5
PA
21295 if (TYPE_LENGTH (type) != cu_header->addr_size)
21296 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21297 cu_header->addr_size,
98bfdba5 21298 TYPE_LENGTH (type));
ac56253d
TT
21299 /* Symbols of this form are reasonably rare, so we just
21300 piggyback on the existing location code rather than writing
21301 a new implementation of symbol_computed_ops. */
8d749320 21302 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 21303 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
21304 (*baton)->per_cu = cu->per_cu;
21305 gdb_assert ((*baton)->per_cu);
ac56253d 21306
98bfdba5 21307 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21308 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21309 (*baton)->data = data;
ac56253d
TT
21310
21311 data[0] = DW_OP_addr;
21312 store_unsigned_integer (&data[1], cu_header->addr_size,
21313 byte_order, DW_ADDR (attr));
21314 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21315 }
c906108c 21316 break;
4ac36638 21317 case DW_FORM_string:
93b5768b 21318 case DW_FORM_strp:
cf532bd1 21319 case DW_FORM_strx:
3019eac3 21320 case DW_FORM_GNU_str_index:
36586728 21321 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21322 /* DW_STRING is already allocated on the objfile obstack, point
21323 directly to it. */
d521ce57 21324 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21325 break;
c906108c
SS
21326 case DW_FORM_block1:
21327 case DW_FORM_block2:
21328 case DW_FORM_block4:
21329 case DW_FORM_block:
2dc7f7b3 21330 case DW_FORM_exprloc:
0224619f 21331 case DW_FORM_data16:
c906108c 21332 blk = DW_BLOCK (attr);
98bfdba5
PA
21333 if (TYPE_LENGTH (type) != blk->size)
21334 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21335 TYPE_LENGTH (type));
21336 *bytes = blk->data;
c906108c 21337 break;
2df3850c
JM
21338
21339 /* The DW_AT_const_value attributes are supposed to carry the
21340 symbol's value "represented as it would be on the target
21341 architecture." By the time we get here, it's already been
21342 converted to host endianness, so we just need to sign- or
21343 zero-extend it as appropriate. */
21344 case DW_FORM_data1:
3aef2284 21345 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21346 break;
c906108c 21347 case DW_FORM_data2:
3aef2284 21348 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21349 break;
c906108c 21350 case DW_FORM_data4:
3aef2284 21351 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21352 break;
c906108c 21353 case DW_FORM_data8:
3aef2284 21354 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21355 break;
21356
c906108c 21357 case DW_FORM_sdata:
663c44ac 21358 case DW_FORM_implicit_const:
98bfdba5 21359 *value = DW_SND (attr);
2df3850c
JM
21360 break;
21361
c906108c 21362 case DW_FORM_udata:
98bfdba5 21363 *value = DW_UNSND (attr);
c906108c 21364 break;
2df3850c 21365
c906108c 21366 default:
b98664d3 21367 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21368 dwarf_form_name (attr->form));
98bfdba5 21369 *value = 0;
c906108c
SS
21370 break;
21371 }
21372}
21373
2df3850c 21374
98bfdba5
PA
21375/* Copy constant value from an attribute to a symbol. */
21376
2df3850c 21377static void
ff39bb5e 21378dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21379 struct dwarf2_cu *cu)
2df3850c 21380{
5e22e966 21381 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 21382 LONGEST value;
d521ce57 21383 const gdb_byte *bytes;
98bfdba5 21384 struct dwarf2_locexpr_baton *baton;
2df3850c 21385
98bfdba5 21386 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21387 sym->print_name (),
98bfdba5
PA
21388 &objfile->objfile_obstack, cu,
21389 &value, &bytes, &baton);
2df3850c 21390
98bfdba5
PA
21391 if (baton != NULL)
21392 {
98bfdba5 21393 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21394 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21395 }
21396 else if (bytes != NULL)
21397 {
21398 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21399 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21400 }
21401 else
21402 {
21403 SYMBOL_VALUE (sym) = value;
f1e6e072 21404 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21405 }
2df3850c
JM
21406}
21407
c906108c
SS
21408/* Return the type of the die in question using its DW_AT_type attribute. */
21409
21410static struct type *
e7c27a73 21411die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21412{
c906108c 21413 struct attribute *type_attr;
c906108c 21414
e142c38c 21415 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21416 if (!type_attr)
21417 {
5e22e966 21418 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21419 /* A missing DW_AT_type represents a void type. */
518817b3 21420 return objfile_type (objfile)->builtin_void;
c906108c 21421 }
348e048f 21422
673bfd45 21423 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21424}
21425
b4ba55a1
JB
21426/* True iff CU's producer generates GNAT Ada auxiliary information
21427 that allows to find parallel types through that information instead
21428 of having to do expensive parallel lookups by type name. */
21429
21430static int
21431need_gnat_info (struct dwarf2_cu *cu)
21432{
de4cb04a
JB
21433 /* Assume that the Ada compiler was GNAT, which always produces
21434 the auxiliary information. */
21435 return (cu->language == language_ada);
b4ba55a1
JB
21436}
21437
b4ba55a1
JB
21438/* Return the auxiliary type of the die in question using its
21439 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21440 attribute is not present. */
21441
21442static struct type *
21443die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21444{
b4ba55a1 21445 struct attribute *type_attr;
b4ba55a1
JB
21446
21447 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21448 if (!type_attr)
21449 return NULL;
21450
673bfd45 21451 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21452}
21453
21454/* If DIE has a descriptive_type attribute, then set the TYPE's
21455 descriptive type accordingly. */
21456
21457static void
21458set_descriptive_type (struct type *type, struct die_info *die,
21459 struct dwarf2_cu *cu)
21460{
21461 struct type *descriptive_type = die_descriptive_type (die, cu);
21462
21463 if (descriptive_type)
21464 {
21465 ALLOCATE_GNAT_AUX_TYPE (type);
21466 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21467 }
21468}
21469
c906108c
SS
21470/* Return the containing type of the die in question using its
21471 DW_AT_containing_type attribute. */
21472
21473static struct type *
e7c27a73 21474die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21475{
c906108c 21476 struct attribute *type_attr;
5e22e966 21477 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21478
e142c38c 21479 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21480 if (!type_attr)
21481 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21482 "[in module %s]"), objfile_name (objfile));
33ac96f0 21483
673bfd45 21484 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21485}
21486
ac9ec31b
DE
21487/* Return an error marker type to use for the ill formed type in DIE/CU. */
21488
21489static struct type *
21490build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21491{
976ca316
SM
21492 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21493 struct objfile *objfile = per_objfile->objfile;
528e1572 21494 char *saved;
ac9ec31b 21495
528e1572
SM
21496 std::string message
21497 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21498 objfile_name (objfile),
21499 sect_offset_str (cu->header.sect_off),
21500 sect_offset_str (die->sect_off));
efba19b0 21501 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21502
19f392bc 21503 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21504}
21505
673bfd45 21506/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21507 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21508 DW_AT_containing_type.
673bfd45
DE
21509 If there is no type substitute an error marker. */
21510
c906108c 21511static struct type *
ff39bb5e 21512lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21513 struct dwarf2_cu *cu)
c906108c 21514{
976ca316
SM
21515 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21516 struct objfile *objfile = per_objfile->objfile;
f792889a
DJ
21517 struct type *this_type;
21518
ac9ec31b
DE
21519 gdb_assert (attr->name == DW_AT_type
21520 || attr->name == DW_AT_GNAT_descriptive_type
21521 || attr->name == DW_AT_containing_type);
21522
673bfd45
DE
21523 /* First see if we have it cached. */
21524
36586728
TT
21525 if (attr->form == DW_FORM_GNU_ref_alt)
21526 {
21527 struct dwarf2_per_cu_data *per_cu;
0826b30a 21528 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 21529
976ca316
SM
21530 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
21531 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
36586728 21532 }
cd6c91b4 21533 else if (attr->form_is_ref ())
673bfd45 21534 {
0826b30a 21535 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 21536
976ca316 21537 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
673bfd45 21538 }
55f1336d 21539 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21540 {
ac9ec31b 21541 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21542
ac9ec31b 21543 return get_signatured_type (die, signature, cu);
673bfd45
DE
21544 }
21545 else
21546 {
b98664d3 21547 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21548 " at %s [in module %s]"),
21549 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21550 objfile_name (objfile));
ac9ec31b 21551 return build_error_marker_type (cu, die);
673bfd45
DE
21552 }
21553
21554 /* If not cached we need to read it in. */
21555
21556 if (this_type == NULL)
21557 {
ac9ec31b 21558 struct die_info *type_die = NULL;
673bfd45
DE
21559 struct dwarf2_cu *type_cu = cu;
21560
cd6c91b4 21561 if (attr->form_is_ref ())
ac9ec31b
DE
21562 type_die = follow_die_ref (die, attr, &type_cu);
21563 if (type_die == NULL)
21564 return build_error_marker_type (cu, die);
21565 /* If we find the type now, it's probably because the type came
3019eac3
DE
21566 from an inter-CU reference and the type's CU got expanded before
21567 ours. */
ac9ec31b 21568 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21569 }
21570
21571 /* If we still don't have a type use an error marker. */
21572
21573 if (this_type == NULL)
ac9ec31b 21574 return build_error_marker_type (cu, die);
673bfd45 21575
f792889a 21576 return this_type;
c906108c
SS
21577}
21578
673bfd45
DE
21579/* Return the type in DIE, CU.
21580 Returns NULL for invalid types.
21581
02142a6c 21582 This first does a lookup in die_type_hash,
673bfd45
DE
21583 and only reads the die in if necessary.
21584
21585 NOTE: This can be called when reading in partial or full symbols. */
21586
f792889a 21587static struct type *
e7c27a73 21588read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21589{
f792889a
DJ
21590 struct type *this_type;
21591
21592 this_type = get_die_type (die, cu);
21593 if (this_type)
21594 return this_type;
21595
673bfd45
DE
21596 return read_type_die_1 (die, cu);
21597}
21598
21599/* Read the type in DIE, CU.
21600 Returns NULL for invalid types. */
21601
21602static struct type *
21603read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21604{
21605 struct type *this_type = NULL;
21606
c906108c
SS
21607 switch (die->tag)
21608 {
21609 case DW_TAG_class_type:
680b30c7 21610 case DW_TAG_interface_type:
c906108c
SS
21611 case DW_TAG_structure_type:
21612 case DW_TAG_union_type:
f792889a 21613 this_type = read_structure_type (die, cu);
c906108c
SS
21614 break;
21615 case DW_TAG_enumeration_type:
f792889a 21616 this_type = read_enumeration_type (die, cu);
c906108c
SS
21617 break;
21618 case DW_TAG_subprogram:
21619 case DW_TAG_subroutine_type:
edb3359d 21620 case DW_TAG_inlined_subroutine:
f792889a 21621 this_type = read_subroutine_type (die, cu);
c906108c
SS
21622 break;
21623 case DW_TAG_array_type:
f792889a 21624 this_type = read_array_type (die, cu);
c906108c 21625 break;
72019c9c 21626 case DW_TAG_set_type:
f792889a 21627 this_type = read_set_type (die, cu);
72019c9c 21628 break;
c906108c 21629 case DW_TAG_pointer_type:
f792889a 21630 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21631 break;
21632 case DW_TAG_ptr_to_member_type:
f792889a 21633 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21634 break;
21635 case DW_TAG_reference_type:
4297a3f0
AV
21636 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21637 break;
21638 case DW_TAG_rvalue_reference_type:
21639 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21640 break;
21641 case DW_TAG_const_type:
f792889a 21642 this_type = read_tag_const_type (die, cu);
c906108c
SS
21643 break;
21644 case DW_TAG_volatile_type:
f792889a 21645 this_type = read_tag_volatile_type (die, cu);
c906108c 21646 break;
06d66ee9
TT
21647 case DW_TAG_restrict_type:
21648 this_type = read_tag_restrict_type (die, cu);
21649 break;
c906108c 21650 case DW_TAG_string_type:
f792889a 21651 this_type = read_tag_string_type (die, cu);
c906108c
SS
21652 break;
21653 case DW_TAG_typedef:
f792889a 21654 this_type = read_typedef (die, cu);
c906108c 21655 break;
a02abb62 21656 case DW_TAG_subrange_type:
f792889a 21657 this_type = read_subrange_type (die, cu);
a02abb62 21658 break;
c906108c 21659 case DW_TAG_base_type:
f792889a 21660 this_type = read_base_type (die, cu);
c906108c 21661 break;
81a17f79 21662 case DW_TAG_unspecified_type:
f792889a 21663 this_type = read_unspecified_type (die, cu);
81a17f79 21664 break;
0114d602
DJ
21665 case DW_TAG_namespace:
21666 this_type = read_namespace_type (die, cu);
21667 break;
f55ee35c
JK
21668 case DW_TAG_module:
21669 this_type = read_module_type (die, cu);
21670 break;
a2c2acaf
MW
21671 case DW_TAG_atomic_type:
21672 this_type = read_tag_atomic_type (die, cu);
21673 break;
c906108c 21674 default:
b98664d3 21675 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21676 dwarf_tag_name (die->tag));
c906108c
SS
21677 break;
21678 }
63d06c5c 21679
f792889a 21680 return this_type;
63d06c5c
DC
21681}
21682
abc72ce4
DE
21683/* See if we can figure out if the class lives in a namespace. We do
21684 this by looking for a member function; its demangled name will
21685 contain namespace info, if there is any.
21686 Return the computed name or NULL.
21687 Space for the result is allocated on the objfile's obstack.
21688 This is the full-die version of guess_partial_die_structure_name.
21689 In this case we know DIE has no useful parent. */
21690
43816ebc 21691static const char *
abc72ce4
DE
21692guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21693{
21694 struct die_info *spec_die;
21695 struct dwarf2_cu *spec_cu;
21696 struct die_info *child;
5e22e966 21697 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
21698
21699 spec_cu = cu;
21700 spec_die = die_specification (die, &spec_cu);
21701 if (spec_die != NULL)
21702 {
21703 die = spec_die;
21704 cu = spec_cu;
21705 }
21706
21707 for (child = die->child;
21708 child != NULL;
21709 child = child->sibling)
21710 {
21711 if (child->tag == DW_TAG_subprogram)
21712 {
73b9be8b 21713 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21714
7d45c7c3 21715 if (linkage_name != NULL)
abc72ce4 21716 {
43816ebc 21717 gdb::unique_xmalloc_ptr<char> actual_name
eff93b4d 21718 (cu->language_defn->class_name_from_physname (linkage_name));
43816ebc 21719 const char *name = NULL;
abc72ce4
DE
21720
21721 if (actual_name != NULL)
21722 {
15d034d0 21723 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21724
21725 if (die_name != NULL
43816ebc 21726 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21727 {
21728 /* Strip off the class name from the full name.
21729 We want the prefix. */
21730 int die_name_len = strlen (die_name);
43816ebc
TT
21731 int actual_name_len = strlen (actual_name.get ());
21732 const char *ptr = actual_name.get ();
abc72ce4
DE
21733
21734 /* Test for '::' as a sanity check. */
21735 if (actual_name_len > die_name_len + 2
43816ebc 21736 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21737 name = obstack_strndup (
e3b94546 21738 &objfile->per_bfd->storage_obstack,
43816ebc 21739 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21740 }
21741 }
abc72ce4
DE
21742 return name;
21743 }
21744 }
21745 }
21746
21747 return NULL;
21748}
21749
96408a79
SA
21750/* GCC might emit a nameless typedef that has a linkage name. Determine the
21751 prefix part in such case. See
21752 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21753
a121b7c1 21754static const char *
96408a79
SA
21755anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21756{
21757 struct attribute *attr;
e6a959d6 21758 const char *base;
96408a79
SA
21759
21760 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21761 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21762 return NULL;
21763
7d45c7c3 21764 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21765 return NULL;
21766
73b9be8b 21767 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21768 if (attr == NULL || DW_STRING (attr) == NULL)
21769 return NULL;
21770
21771 /* dwarf2_name had to be already called. */
21772 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21773
21774 /* Strip the base name, keep any leading namespaces/classes. */
21775 base = strrchr (DW_STRING (attr), ':');
21776 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21777 return "";
21778
5e22e966 21779 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9
TT
21780 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21781 DW_STRING (attr),
21782 &base[-1] - DW_STRING (attr));
96408a79
SA
21783}
21784
fdde2d81 21785/* Return the name of the namespace/class that DIE is defined within,
0114d602 21786 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21787
0114d602
DJ
21788 For example, if we're within the method foo() in the following
21789 code:
21790
21791 namespace N {
21792 class C {
21793 void foo () {
21794 }
21795 };
21796 }
21797
21798 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21799
0d5cff50 21800static const char *
e142c38c 21801determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21802{
976ca316 21803 dwarf2_per_objfile *per_objfile = cu->per_objfile;
0114d602
DJ
21804 struct die_info *parent, *spec_die;
21805 struct dwarf2_cu *spec_cu;
21806 struct type *parent_type;
a121b7c1 21807 const char *retval;
63d06c5c 21808
9c37b5ae 21809 if (cu->language != language_cplus
c44af4eb
TT
21810 && cu->language != language_fortran && cu->language != language_d
21811 && cu->language != language_rust)
0114d602
DJ
21812 return "";
21813
96408a79
SA
21814 retval = anonymous_struct_prefix (die, cu);
21815 if (retval)
21816 return retval;
21817
0114d602
DJ
21818 /* We have to be careful in the presence of DW_AT_specification.
21819 For example, with GCC 3.4, given the code
21820
21821 namespace N {
21822 void foo() {
21823 // Definition of N::foo.
21824 }
21825 }
21826
21827 then we'll have a tree of DIEs like this:
21828
21829 1: DW_TAG_compile_unit
21830 2: DW_TAG_namespace // N
21831 3: DW_TAG_subprogram // declaration of N::foo
21832 4: DW_TAG_subprogram // definition of N::foo
21833 DW_AT_specification // refers to die #3
21834
21835 Thus, when processing die #4, we have to pretend that we're in
21836 the context of its DW_AT_specification, namely the contex of die
21837 #3. */
21838 spec_cu = cu;
21839 spec_die = die_specification (die, &spec_cu);
21840 if (spec_die == NULL)
21841 parent = die->parent;
21842 else
63d06c5c 21843 {
0114d602
DJ
21844 parent = spec_die->parent;
21845 cu = spec_cu;
63d06c5c 21846 }
0114d602
DJ
21847
21848 if (parent == NULL)
21849 return "";
98bfdba5
PA
21850 else if (parent->building_fullname)
21851 {
21852 const char *name;
21853 const char *parent_name;
21854
21855 /* It has been seen on RealView 2.2 built binaries,
21856 DW_TAG_template_type_param types actually _defined_ as
21857 children of the parent class:
21858
21859 enum E {};
21860 template class <class Enum> Class{};
21861 Class<enum E> class_e;
21862
21863 1: DW_TAG_class_type (Class)
21864 2: DW_TAG_enumeration_type (E)
21865 3: DW_TAG_enumerator (enum1:0)
21866 3: DW_TAG_enumerator (enum2:1)
21867 ...
21868 2: DW_TAG_template_type_param
21869 DW_AT_type DW_FORM_ref_udata (E)
21870
21871 Besides being broken debug info, it can put GDB into an
21872 infinite loop. Consider:
21873
21874 When we're building the full name for Class<E>, we'll start
21875 at Class, and go look over its template type parameters,
21876 finding E. We'll then try to build the full name of E, and
21877 reach here. We're now trying to build the full name of E,
21878 and look over the parent DIE for containing scope. In the
21879 broken case, if we followed the parent DIE of E, we'd again
21880 find Class, and once again go look at its template type
21881 arguments, etc., etc. Simply don't consider such parent die
21882 as source-level parent of this die (it can't be, the language
21883 doesn't allow it), and break the loop here. */
21884 name = dwarf2_name (die, cu);
21885 parent_name = dwarf2_name (parent, cu);
b98664d3 21886 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21887 name ? name : "<unknown>",
21888 parent_name ? parent_name : "<unknown>");
21889 return "";
21890 }
63d06c5c 21891 else
0114d602
DJ
21892 switch (parent->tag)
21893 {
63d06c5c 21894 case DW_TAG_namespace:
0114d602 21895 parent_type = read_type_die (parent, cu);
acebe513
UW
21896 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21897 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21898 Work around this problem here. */
21899 if (cu->language == language_cplus
7d93a1e0 21900 && strcmp (parent_type->name (), "::") == 0)
acebe513 21901 return "";
0114d602 21902 /* We give a name to even anonymous namespaces. */
7d93a1e0 21903 return parent_type->name ();
63d06c5c 21904 case DW_TAG_class_type:
680b30c7 21905 case DW_TAG_interface_type:
63d06c5c 21906 case DW_TAG_structure_type:
0114d602 21907 case DW_TAG_union_type:
f55ee35c 21908 case DW_TAG_module:
0114d602 21909 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
21910 if (parent_type->name () != NULL)
21911 return parent_type->name ();
0114d602
DJ
21912 else
21913 /* An anonymous structure is only allowed non-static data
21914 members; no typedefs, no member functions, et cetera.
21915 So it does not need a prefix. */
21916 return "";
abc72ce4 21917 case DW_TAG_compile_unit:
95554aad 21918 case DW_TAG_partial_unit:
abc72ce4
DE
21919 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21920 if (cu->language == language_cplus
976ca316 21921 && !per_objfile->per_bfd->types.empty ()
abc72ce4
DE
21922 && die->child != NULL
21923 && (die->tag == DW_TAG_class_type
21924 || die->tag == DW_TAG_structure_type
21925 || die->tag == DW_TAG_union_type))
21926 {
43816ebc 21927 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21928 if (name != NULL)
21929 return name;
21930 }
21931 return "";
0a4b0913
AB
21932 case DW_TAG_subprogram:
21933 /* Nested subroutines in Fortran get a prefix with the name
21934 of the parent's subroutine. */
21935 if (cu->language == language_fortran)
21936 {
21937 if ((die->tag == DW_TAG_subprogram)
21938 && (dwarf2_name (parent, cu) != NULL))
21939 return dwarf2_name (parent, cu);
21940 }
21941 return determine_prefix (parent, cu);
3d567982
TT
21942 case DW_TAG_enumeration_type:
21943 parent_type = read_type_die (parent, cu);
21944 if (TYPE_DECLARED_CLASS (parent_type))
21945 {
7d93a1e0
SM
21946 if (parent_type->name () != NULL)
21947 return parent_type->name ();
3d567982
TT
21948 return "";
21949 }
21950 /* Fall through. */
63d06c5c 21951 default:
8176b9b8 21952 return determine_prefix (parent, cu);
63d06c5c 21953 }
63d06c5c
DC
21954}
21955
3e43a32a
MS
21956/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21957 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21958 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21959 an obconcat, otherwise allocate storage for the result. The CU argument is
21960 used to determine the language and hence, the appropriate separator. */
987504bb 21961
f55ee35c 21962#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21963
21964static char *
f55ee35c
JK
21965typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21966 int physname, struct dwarf2_cu *cu)
63d06c5c 21967{
f55ee35c 21968 const char *lead = "";
5c315b68 21969 const char *sep;
63d06c5c 21970
3e43a32a
MS
21971 if (suffix == NULL || suffix[0] == '\0'
21972 || prefix == NULL || prefix[0] == '\0')
987504bb 21973 sep = "";
45280282
IB
21974 else if (cu->language == language_d)
21975 {
21976 /* For D, the 'main' function could be defined in any module, but it
21977 should never be prefixed. */
21978 if (strcmp (suffix, "D main") == 0)
21979 {
21980 prefix = "";
21981 sep = "";
21982 }
21983 else
21984 sep = ".";
21985 }
f55ee35c
JK
21986 else if (cu->language == language_fortran && physname)
21987 {
21988 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21989 DW_AT_MIPS_linkage_name is preferred and used instead. */
21990
21991 lead = "__";
21992 sep = "_MOD_";
21993 }
987504bb
JJ
21994 else
21995 sep = "::";
63d06c5c 21996
6dd47d34
DE
21997 if (prefix == NULL)
21998 prefix = "";
21999 if (suffix == NULL)
22000 suffix = "";
22001
987504bb
JJ
22002 if (obs == NULL)
22003 {
3e43a32a 22004 char *retval
224c3ddb
SM
22005 = ((char *)
22006 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22007
f55ee35c
JK
22008 strcpy (retval, lead);
22009 strcat (retval, prefix);
6dd47d34
DE
22010 strcat (retval, sep);
22011 strcat (retval, suffix);
63d06c5c
DC
22012 return retval;
22013 }
987504bb
JJ
22014 else
22015 {
22016 /* We have an obstack. */
f55ee35c 22017 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22018 }
63d06c5c
DC
22019}
22020
71c25dea
TT
22021/* Get name of a die, return NULL if not found. */
22022
15d034d0
TT
22023static const char *
22024dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 22025 struct objfile *objfile)
71c25dea
TT
22026{
22027 if (name && cu->language == language_cplus)
22028 {
596dc4ad
TT
22029 gdb::unique_xmalloc_ptr<char> canon_name
22030 = cp_canonicalize_string (name);
71c25dea 22031
596dc4ad
TT
22032 if (canon_name != nullptr)
22033 name = objfile->intern (canon_name.get ());
71c25dea
TT
22034 }
22035
22036 return name;
c906108c
SS
22037}
22038
96553a0c
DE
22039/* Get name of a die, return NULL if not found.
22040 Anonymous namespaces are converted to their magic string. */
9219021c 22041
15d034d0 22042static const char *
e142c38c 22043dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22044{
22045 struct attribute *attr;
5e22e966 22046 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 22047
e142c38c 22048 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22049 if ((!attr || !DW_STRING (attr))
96553a0c 22050 && die->tag != DW_TAG_namespace
53832f31
TT
22051 && die->tag != DW_TAG_class_type
22052 && die->tag != DW_TAG_interface_type
22053 && die->tag != DW_TAG_structure_type
22054 && die->tag != DW_TAG_union_type)
71c25dea
TT
22055 return NULL;
22056
22057 switch (die->tag)
22058 {
22059 case DW_TAG_compile_unit:
95554aad 22060 case DW_TAG_partial_unit:
71c25dea
TT
22061 /* Compilation units have a DW_AT_name that is a filename, not
22062 a source language identifier. */
22063 case DW_TAG_enumeration_type:
22064 case DW_TAG_enumerator:
22065 /* These tags always have simple identifiers already; no need
22066 to canonicalize them. */
22067 return DW_STRING (attr);
907af001 22068
96553a0c
DE
22069 case DW_TAG_namespace:
22070 if (attr != NULL && DW_STRING (attr) != NULL)
22071 return DW_STRING (attr);
22072 return CP_ANONYMOUS_NAMESPACE_STR;
22073
907af001
UW
22074 case DW_TAG_class_type:
22075 case DW_TAG_interface_type:
22076 case DW_TAG_structure_type:
22077 case DW_TAG_union_type:
22078 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22079 structures or unions. These were of the form "._%d" in GCC 4.1,
22080 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22081 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22082 if (attr && DW_STRING (attr)
61012eef
GB
22083 && (startswith (DW_STRING (attr), "._")
22084 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22085 return NULL;
53832f31
TT
22086
22087 /* GCC might emit a nameless typedef that has a linkage name. See
22088 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22089 if (!attr || DW_STRING (attr) == NULL)
22090 {
73b9be8b 22091 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22092 if (attr == NULL || DW_STRING (attr) == NULL)
22093 return NULL;
22094
df5c6c50
JK
22095 /* Avoid demangling DW_STRING (attr) the second time on a second
22096 call for the same DIE. */
22097 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 22098 {
43816ebc
TT
22099 gdb::unique_xmalloc_ptr<char> demangled
22100 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
22101 if (demangled == nullptr)
22102 return nullptr;
43816ebc 22103
be1e3d3e 22104 DW_STRING (attr) = objfile->intern (demangled.get ());
53832f31 22105 DW_STRING_IS_CANONICAL (attr) = 1;
53832f31 22106 }
67430cd0
TT
22107
22108 /* Strip any leading namespaces/classes, keep only the base name.
22109 DW_AT_name for named DIEs does not contain the prefixes. */
22110 const char *base = strrchr (DW_STRING (attr), ':');
22111 if (base && base > DW_STRING (attr) && base[-1] == ':')
22112 return &base[1];
22113 else
22114 return DW_STRING (attr);
53832f31 22115 }
907af001
UW
22116 break;
22117
71c25dea 22118 default:
907af001
UW
22119 break;
22120 }
22121
22122 if (!DW_STRING_IS_CANONICAL (attr))
22123 {
be1e3d3e
TT
22124 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22125 objfile);
907af001 22126 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22127 }
907af001 22128 return DW_STRING (attr);
9219021c
DC
22129}
22130
22131/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22132 is none. *EXT_CU is the CU containing DIE on input, and the CU
22133 containing the return value on output. */
9219021c
DC
22134
22135static struct die_info *
f2f0e013 22136dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22137{
22138 struct attribute *attr;
9219021c 22139
f2f0e013 22140 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22141 if (attr == NULL)
22142 return NULL;
22143
f2f0e013 22144 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22145}
22146
f9aca02d 22147static void
d97bc12b 22148dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22149{
22150 unsigned int i;
22151
d97bc12b 22152 print_spaces (indent, f);
9d8780f0 22153 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22154 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22155 sect_offset_str (die->sect_off));
d97bc12b
DE
22156
22157 if (die->parent != NULL)
22158 {
22159 print_spaces (indent, f);
9d8780f0
SM
22160 fprintf_unfiltered (f, " parent at offset: %s\n",
22161 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22162 }
22163
22164 print_spaces (indent, f);
22165 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22166 dwarf_bool_name (die->child != NULL));
c906108c 22167
d97bc12b
DE
22168 print_spaces (indent, f);
22169 fprintf_unfiltered (f, " attributes:\n");
22170
c906108c
SS
22171 for (i = 0; i < die->num_attrs; ++i)
22172 {
d97bc12b
DE
22173 print_spaces (indent, f);
22174 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22175 dwarf_attr_name (die->attrs[i].name),
22176 dwarf_form_name (die->attrs[i].form));
d97bc12b 22177
c906108c
SS
22178 switch (die->attrs[i].form)
22179 {
c906108c 22180 case DW_FORM_addr:
336d760d 22181 case DW_FORM_addrx:
3019eac3 22182 case DW_FORM_GNU_addr_index:
d97bc12b 22183 fprintf_unfiltered (f, "address: ");
5af949e3 22184 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22185 break;
22186 case DW_FORM_block2:
22187 case DW_FORM_block4:
22188 case DW_FORM_block:
22189 case DW_FORM_block1:
56eb65bd
SP
22190 fprintf_unfiltered (f, "block: size %s",
22191 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22192 break;
2dc7f7b3 22193 case DW_FORM_exprloc:
56eb65bd
SP
22194 fprintf_unfiltered (f, "expression: size %s",
22195 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22196 break;
0224619f
JK
22197 case DW_FORM_data16:
22198 fprintf_unfiltered (f, "constant of 16 bytes");
22199 break;
4568ecf9
DE
22200 case DW_FORM_ref_addr:
22201 fprintf_unfiltered (f, "ref address: ");
22202 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22203 break;
36586728
TT
22204 case DW_FORM_GNU_ref_alt:
22205 fprintf_unfiltered (f, "alt ref address: ");
22206 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22207 break;
10b3939b
DJ
22208 case DW_FORM_ref1:
22209 case DW_FORM_ref2:
22210 case DW_FORM_ref4:
4568ecf9
DE
22211 case DW_FORM_ref8:
22212 case DW_FORM_ref_udata:
d97bc12b 22213 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22214 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22215 break;
c906108c
SS
22216 case DW_FORM_data1:
22217 case DW_FORM_data2:
22218 case DW_FORM_data4:
ce5d95e1 22219 case DW_FORM_data8:
c906108c
SS
22220 case DW_FORM_udata:
22221 case DW_FORM_sdata:
43bbcdc2
PH
22222 fprintf_unfiltered (f, "constant: %s",
22223 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22224 break;
2dc7f7b3
TT
22225 case DW_FORM_sec_offset:
22226 fprintf_unfiltered (f, "section offset: %s",
22227 pulongest (DW_UNSND (&die->attrs[i])));
22228 break;
55f1336d 22229 case DW_FORM_ref_sig8:
ac9ec31b
DE
22230 fprintf_unfiltered (f, "signature: %s",
22231 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22232 break;
c906108c 22233 case DW_FORM_string:
4bdf3d34 22234 case DW_FORM_strp:
43988095 22235 case DW_FORM_line_strp:
cf532bd1 22236 case DW_FORM_strx:
3019eac3 22237 case DW_FORM_GNU_str_index:
36586728 22238 case DW_FORM_GNU_strp_alt:
8285870a 22239 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22240 DW_STRING (&die->attrs[i])
8285870a
JK
22241 ? DW_STRING (&die->attrs[i]) : "",
22242 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22243 break;
22244 case DW_FORM_flag:
22245 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22246 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22247 else
d97bc12b 22248 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22249 break;
2dc7f7b3
TT
22250 case DW_FORM_flag_present:
22251 fprintf_unfiltered (f, "flag: TRUE");
22252 break;
a8329558 22253 case DW_FORM_indirect:
0963b4bd
MS
22254 /* The reader will have reduced the indirect form to
22255 the "base form" so this form should not occur. */
5f48f8f3 22256 fprintf_unfiltered (f,
3e43a32a 22257 "unexpected attribute form: DW_FORM_indirect");
a8329558 22258 break;
663c44ac
JK
22259 case DW_FORM_implicit_const:
22260 fprintf_unfiltered (f, "constant: %s",
22261 plongest (DW_SND (&die->attrs[i])));
22262 break;
c906108c 22263 default:
d97bc12b 22264 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22265 die->attrs[i].form);
d97bc12b 22266 break;
c906108c 22267 }
d97bc12b 22268 fprintf_unfiltered (f, "\n");
c906108c
SS
22269 }
22270}
22271
f9aca02d 22272static void
d97bc12b 22273dump_die_for_error (struct die_info *die)
c906108c 22274{
d97bc12b
DE
22275 dump_die_shallow (gdb_stderr, 0, die);
22276}
22277
22278static void
22279dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22280{
22281 int indent = level * 4;
22282
22283 gdb_assert (die != NULL);
22284
22285 if (level >= max_level)
22286 return;
22287
22288 dump_die_shallow (f, indent, die);
22289
22290 if (die->child != NULL)
c906108c 22291 {
d97bc12b
DE
22292 print_spaces (indent, f);
22293 fprintf_unfiltered (f, " Children:");
22294 if (level + 1 < max_level)
22295 {
22296 fprintf_unfiltered (f, "\n");
22297 dump_die_1 (f, level + 1, max_level, die->child);
22298 }
22299 else
22300 {
3e43a32a
MS
22301 fprintf_unfiltered (f,
22302 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22303 }
22304 }
22305
22306 if (die->sibling != NULL && level > 0)
22307 {
22308 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22309 }
22310}
22311
d97bc12b
DE
22312/* This is called from the pdie macro in gdbinit.in.
22313 It's not static so gcc will keep a copy callable from gdb. */
22314
22315void
22316dump_die (struct die_info *die, int max_level)
22317{
22318 dump_die_1 (gdb_stdlog, 0, max_level, die);
22319}
22320
f9aca02d 22321static void
51545339 22322store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22323{
51545339 22324 void **slot;
c906108c 22325
9c541725
PA
22326 slot = htab_find_slot_with_hash (cu->die_hash, die,
22327 to_underlying (die->sect_off),
b64f50a1 22328 INSERT);
51545339
DJ
22329
22330 *slot = die;
c906108c
SS
22331}
22332
348e048f
DE
22333/* Follow reference or signature attribute ATTR of SRC_DIE.
22334 On entry *REF_CU is the CU of SRC_DIE.
22335 On exit *REF_CU is the CU of the result. */
22336
22337static struct die_info *
ff39bb5e 22338follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22339 struct dwarf2_cu **ref_cu)
22340{
22341 struct die_info *die;
22342
cd6c91b4 22343 if (attr->form_is_ref ())
348e048f 22344 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22345 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22346 die = follow_die_sig (src_die, attr, ref_cu);
22347 else
22348 {
22349 dump_die_for_error (src_die);
22350 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 22351 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
22352 }
22353
22354 return die;
03dd20cc
DJ
22355}
22356
5c631832 22357/* Follow reference OFFSET.
673bfd45
DE
22358 On entry *REF_CU is the CU of the source die referencing OFFSET.
22359 On exit *REF_CU is the CU of the result.
22360 Returns NULL if OFFSET is invalid. */
f504f079 22361
f9aca02d 22362static struct die_info *
9c541725 22363follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22364 struct dwarf2_cu **ref_cu)
c906108c 22365{
10b3939b 22366 struct die_info temp_die;
f2f0e013 22367 struct dwarf2_cu *target_cu, *cu = *ref_cu;
976ca316 22368 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10b3939b 22369
348e048f
DE
22370 gdb_assert (cu->per_cu != NULL);
22371
98bfdba5
PA
22372 target_cu = cu;
22373
3019eac3 22374 if (cu->per_cu->is_debug_types)
348e048f
DE
22375 {
22376 /* .debug_types CUs cannot reference anything outside their CU.
22377 If they need to, they have to reference a signatured type via
55f1336d 22378 DW_FORM_ref_sig8. */
4057dfde 22379 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22380 return NULL;
348e048f 22381 }
36586728 22382 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22383 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22384 {
22385 struct dwarf2_per_cu_data *per_cu;
9a619af0 22386
9c541725 22387 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 22388 per_objfile);
03dd20cc
DJ
22389
22390 /* If necessary, add it to the queue and load its DIEs. */
976ca316
SM
22391 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
22392 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
03dd20cc 22393
976ca316 22394 target_cu = per_objfile->get_cu (per_cu);
10b3939b 22395 }
98bfdba5
PA
22396 else if (cu->dies == NULL)
22397 {
22398 /* We're loading full DIEs during partial symbol reading. */
976ca316
SM
22399 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
22400 load_full_comp_unit (cu->per_cu, per_objfile, false, language_minimal);
98bfdba5 22401 }
c906108c 22402
f2f0e013 22403 *ref_cu = target_cu;
9c541725 22404 temp_die.sect_off = sect_off;
c24bdb02
KS
22405
22406 if (target_cu != cu)
22407 target_cu->ancestor = cu;
22408
9a3c8263 22409 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22410 &temp_die,
22411 to_underlying (sect_off));
5c631832 22412}
10b3939b 22413
5c631832
JK
22414/* Follow reference attribute ATTR of SRC_DIE.
22415 On entry *REF_CU is the CU of SRC_DIE.
22416 On exit *REF_CU is the CU of the result. */
22417
22418static struct die_info *
ff39bb5e 22419follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22420 struct dwarf2_cu **ref_cu)
22421{
0826b30a 22422 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
22423 struct dwarf2_cu *cu = *ref_cu;
22424 struct die_info *die;
22425
9c541725 22426 die = follow_die_offset (sect_off,
36586728
TT
22427 (attr->form == DW_FORM_GNU_ref_alt
22428 || cu->per_cu->is_dwz),
22429 ref_cu);
5c631832 22430 if (!die)
9d8780f0
SM
22431 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22432 "at %s [in module %s]"),
22433 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 22434 objfile_name (cu->per_objfile->objfile));
348e048f 22435
5c631832
JK
22436 return die;
22437}
22438
d4c9a4f8 22439/* See read.h. */
5c631832
JK
22440
22441struct dwarf2_locexpr_baton
9c541725 22442dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 22443 dwarf2_per_cu_data *per_cu,
976ca316 22444 dwarf2_per_objfile *per_objfile,
8b9737bf 22445 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22446 void *baton, bool resolve_abstract_p)
5c631832 22447{
5c631832
JK
22448 struct die_info *die;
22449 struct attribute *attr;
22450 struct dwarf2_locexpr_baton retval;
976ca316 22451 struct objfile *objfile = per_objfile->objfile;
8cf6f0b1 22452
976ca316 22453 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17 22454 if (cu == nullptr)
976ca316 22455 cu = load_cu (per_cu, per_objfile, false);
1b555f17
SM
22456
22457 if (cu == nullptr)
cc12ce38
DE
22458 {
22459 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22460 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22461 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22462 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22463 }
918dd910 22464
9c541725 22465 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22466 if (!die)
9d8780f0
SM
22467 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22468 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22469
22470 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22471 if (!attr && resolve_abstract_p
976ca316
SM
22472 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22473 != per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65
TV
22474 {
22475 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22476 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 22477 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 22478
3360b6e7 22479 for (const auto &cand_off
976ca316 22480 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 22481 {
3360b6e7
TV
22482 struct dwarf2_cu *cand_cu = cu;
22483 struct die_info *cand
22484 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22485 if (!cand
22486 || !cand->parent
e4a62c65
TV
22487 || cand->parent->tag != DW_TAG_subprogram)
22488 continue;
22489
22490 CORE_ADDR pc_low, pc_high;
22491 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22492 if (pc_low == ((CORE_ADDR) -1))
22493 continue;
22494 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22495 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22496 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22497 continue;
22498
22499 die = cand;
22500 attr = dwarf2_attr (die, DW_AT_location, cu);
22501 break;
22502 }
22503 }
22504
5c631832
JK
22505 if (!attr)
22506 {
e103e986
JK
22507 /* DWARF: "If there is no such attribute, then there is no effect.".
22508 DATA is ignored if SIZE is 0. */
5c631832 22509
e103e986 22510 retval.data = NULL;
5c631832
JK
22511 retval.size = 0;
22512 }
cd6c91b4 22513 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22514 {
22515 struct dwarf2_loclist_baton loclist_baton;
22516 CORE_ADDR pc = (*get_frame_pc) (baton);
22517 size_t size;
22518
22519 fill_in_loclist_baton (cu, &loclist_baton, attr);
22520
22521 retval.data = dwarf2_find_location_expression (&loclist_baton,
22522 &size, pc);
22523 retval.size = size;
22524 }
5c631832
JK
22525 else
22526 {
4fc6c0d5 22527 if (!attr->form_is_block ())
9d8780f0 22528 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22529 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22530 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22531
22532 retval.data = DW_BLOCK (attr)->data;
22533 retval.size = DW_BLOCK (attr)->size;
22534 }
976ca316 22535 retval.per_objfile = per_objfile;
5c631832 22536 retval.per_cu = cu->per_cu;
918dd910 22537
976ca316 22538 per_objfile->age_comp_units ();
918dd910 22539
5c631832 22540 return retval;
348e048f
DE
22541}
22542
d4c9a4f8 22543/* See read.h. */
8b9737bf
TT
22544
22545struct dwarf2_locexpr_baton
22546dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 22547 dwarf2_per_cu_data *per_cu,
14095eb3 22548 dwarf2_per_objfile *per_objfile,
8b9737bf
TT
22549 CORE_ADDR (*get_frame_pc) (void *baton),
22550 void *baton)
22551{
9c541725 22552 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22553
14095eb3
SM
22554 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22555 get_frame_pc, baton);
8b9737bf
TT
22556}
22557
b6807d98
TT
22558/* Write a constant of a given type as target-ordered bytes into
22559 OBSTACK. */
22560
22561static const gdb_byte *
22562write_constant_as_bytes (struct obstack *obstack,
22563 enum bfd_endian byte_order,
22564 struct type *type,
22565 ULONGEST value,
22566 LONGEST *len)
22567{
22568 gdb_byte *result;
22569
22570 *len = TYPE_LENGTH (type);
224c3ddb 22571 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22572 store_unsigned_integer (result, *len, byte_order, value);
22573
22574 return result;
22575}
22576
d4c9a4f8 22577/* See read.h. */
b6807d98
TT
22578
22579const gdb_byte *
9c541725 22580dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 22581 dwarf2_per_cu_data *per_cu,
14095eb3 22582 dwarf2_per_objfile *per_objfile,
d4c9a4f8 22583 obstack *obstack,
b6807d98
TT
22584 LONGEST *len)
22585{
b6807d98
TT
22586 struct die_info *die;
22587 struct attribute *attr;
22588 const gdb_byte *result = NULL;
22589 struct type *type;
22590 LONGEST value;
22591 enum bfd_endian byte_order;
14095eb3 22592 struct objfile *objfile = per_objfile->objfile;
b6807d98 22593
7188ed02 22594 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
22595 if (cu == nullptr)
22596 cu = load_cu (per_cu, per_objfile, false);
22597
22598 if (cu == nullptr)
cc12ce38
DE
22599 {
22600 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22601 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22602 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22603 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22604 }
b6807d98 22605
9c541725 22606 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22607 if (!die)
9d8780f0
SM
22608 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22609 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22610
22611 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22612 if (attr == NULL)
22613 return NULL;
22614
e3b94546 22615 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22616 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22617
22618 switch (attr->form)
22619 {
22620 case DW_FORM_addr:
336d760d 22621 case DW_FORM_addrx:
b6807d98
TT
22622 case DW_FORM_GNU_addr_index:
22623 {
22624 gdb_byte *tem;
22625
22626 *len = cu->header.addr_size;
224c3ddb 22627 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22628 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22629 result = tem;
22630 }
22631 break;
22632 case DW_FORM_string:
22633 case DW_FORM_strp:
cf532bd1 22634 case DW_FORM_strx:
b6807d98
TT
22635 case DW_FORM_GNU_str_index:
22636 case DW_FORM_GNU_strp_alt:
22637 /* DW_STRING is already allocated on the objfile obstack, point
22638 directly to it. */
22639 result = (const gdb_byte *) DW_STRING (attr);
22640 *len = strlen (DW_STRING (attr));
22641 break;
22642 case DW_FORM_block1:
22643 case DW_FORM_block2:
22644 case DW_FORM_block4:
22645 case DW_FORM_block:
22646 case DW_FORM_exprloc:
0224619f 22647 case DW_FORM_data16:
b6807d98
TT
22648 result = DW_BLOCK (attr)->data;
22649 *len = DW_BLOCK (attr)->size;
22650 break;
22651
22652 /* The DW_AT_const_value attributes are supposed to carry the
22653 symbol's value "represented as it would be on the target
22654 architecture." By the time we get here, it's already been
22655 converted to host endianness, so we just need to sign- or
22656 zero-extend it as appropriate. */
22657 case DW_FORM_data1:
22658 type = die_type (die, cu);
22659 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22660 if (result == NULL)
22661 result = write_constant_as_bytes (obstack, byte_order,
22662 type, value, len);
22663 break;
22664 case DW_FORM_data2:
22665 type = die_type (die, cu);
22666 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22667 if (result == NULL)
22668 result = write_constant_as_bytes (obstack, byte_order,
22669 type, value, len);
22670 break;
22671 case DW_FORM_data4:
22672 type = die_type (die, cu);
22673 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22674 if (result == NULL)
22675 result = write_constant_as_bytes (obstack, byte_order,
22676 type, value, len);
22677 break;
22678 case DW_FORM_data8:
22679 type = die_type (die, cu);
22680 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22681 if (result == NULL)
22682 result = write_constant_as_bytes (obstack, byte_order,
22683 type, value, len);
22684 break;
22685
22686 case DW_FORM_sdata:
663c44ac 22687 case DW_FORM_implicit_const:
b6807d98
TT
22688 type = die_type (die, cu);
22689 result = write_constant_as_bytes (obstack, byte_order,
22690 type, DW_SND (attr), len);
22691 break;
22692
22693 case DW_FORM_udata:
22694 type = die_type (die, cu);
22695 result = write_constant_as_bytes (obstack, byte_order,
22696 type, DW_UNSND (attr), len);
22697 break;
22698
22699 default:
b98664d3 22700 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22701 dwarf_form_name (attr->form));
22702 break;
22703 }
22704
22705 return result;
22706}
22707
d4c9a4f8 22708/* See read.h. */
7942e96e
AA
22709
22710struct type *
9c541725 22711dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
22712 dwarf2_per_cu_data *per_cu,
22713 dwarf2_per_objfile *per_objfile)
7942e96e 22714{
7942e96e
AA
22715 struct die_info *die;
22716
7188ed02 22717 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
22718 if (cu == nullptr)
22719 cu = load_cu (per_cu, per_objfile, false);
22720
22721 if (cu == nullptr)
22722 return nullptr;
7942e96e 22723
9c541725 22724 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22725 if (!die)
22726 return NULL;
22727
22728 return die_type (die, cu);
22729}
22730
8cb5117c 22731/* See read.h. */
8a9b8146
TT
22732
22733struct type *
b64f50a1 22734dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
22735 dwarf2_per_cu_data *per_cu,
22736 dwarf2_per_objfile *per_objfile)
8a9b8146 22737{
9c541725 22738 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 22739 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
22740}
22741
ac9ec31b 22742/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22743 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22744 On exit *REF_CU is the CU of the result.
22745 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22746
22747static struct die_info *
ac9ec31b
DE
22748follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22749 struct dwarf2_cu **ref_cu)
348e048f 22750{
348e048f 22751 struct die_info temp_die;
c24bdb02 22752 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f 22753 struct die_info *die;
976ca316 22754 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
120ce1b5 22755
348e048f 22756
ac9ec31b
DE
22757 /* While it might be nice to assert sig_type->type == NULL here,
22758 we can get here for DW_AT_imported_declaration where we need
22759 the DIE not the type. */
348e048f
DE
22760
22761 /* If necessary, add it to the queue and load its DIEs. */
22762
976ca316 22763 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
120ce1b5 22764 language_minimal))
976ca316 22765 read_signatured_type (sig_type, per_objfile);
348e048f 22766
976ca316 22767 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
69d751e3 22768 gdb_assert (sig_cu != NULL);
9c541725
PA
22769 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22770 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22771 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22772 to_underlying (temp_die.sect_off));
348e048f
DE
22773 if (die)
22774 {
796a7ff8
DE
22775 /* For .gdb_index version 7 keep track of included TUs.
22776 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
976ca316
SM
22777 if (per_objfile->per_bfd->index_table != NULL
22778 && per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 22779 {
ae640021 22780 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22781 }
22782
348e048f 22783 *ref_cu = sig_cu;
c24bdb02
KS
22784 if (sig_cu != cu)
22785 sig_cu->ancestor = cu;
22786
348e048f
DE
22787 return die;
22788 }
22789
ac9ec31b
DE
22790 return NULL;
22791}
22792
22793/* Follow signatured type referenced by ATTR in SRC_DIE.
22794 On entry *REF_CU is the CU of SRC_DIE.
22795 On exit *REF_CU is the CU of the result.
22796 The result is the DIE of the type.
22797 If the referenced type cannot be found an error is thrown. */
22798
22799static struct die_info *
ff39bb5e 22800follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22801 struct dwarf2_cu **ref_cu)
22802{
22803 ULONGEST signature = DW_SIGNATURE (attr);
22804 struct signatured_type *sig_type;
22805 struct die_info *die;
22806
22807 gdb_assert (attr->form == DW_FORM_ref_sig8);
22808
a2ce51a0 22809 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22810 /* sig_type will be NULL if the signatured type is missing from
22811 the debug info. */
22812 if (sig_type == NULL)
22813 {
22814 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22815 " from DIE at %s [in module %s]"),
22816 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22817 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22818 }
22819
22820 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22821 if (die == NULL)
22822 {
22823 dump_die_for_error (src_die);
22824 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22825 " from DIE at %s [in module %s]"),
22826 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22827 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22828 }
22829
22830 return die;
22831}
22832
22833/* Get the type specified by SIGNATURE referenced in DIE/CU,
22834 reading in and processing the type unit if necessary. */
22835
22836static struct type *
22837get_signatured_type (struct die_info *die, ULONGEST signature,
22838 struct dwarf2_cu *cu)
22839{
976ca316 22840 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ac9ec31b
DE
22841 struct signatured_type *sig_type;
22842 struct dwarf2_cu *type_cu;
22843 struct die_info *type_die;
22844 struct type *type;
22845
a2ce51a0 22846 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22847 /* sig_type will be NULL if the signatured type is missing from
22848 the debug info. */
22849 if (sig_type == NULL)
22850 {
b98664d3 22851 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22852 " from DIE at %s [in module %s]"),
22853 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 22854 objfile_name (per_objfile->objfile));
ac9ec31b
DE
22855 return build_error_marker_type (cu, die);
22856 }
22857
22858 /* If we already know the type we're done. */
976ca316 22859 type = per_objfile->get_type_for_signatured_type (sig_type);
e286671b
TT
22860 if (type != nullptr)
22861 return type;
ac9ec31b
DE
22862
22863 type_cu = cu;
22864 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22865 if (type_die != NULL)
22866 {
22867 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22868 is created. This is important, for example, because for c++ classes
22869 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22870 type = read_type_die (type_die, type_cu);
22871 if (type == NULL)
22872 {
b98664d3 22873 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22874 " referenced from DIE at %s [in module %s]"),
22875 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 22876 objfile_name (per_objfile->objfile));
ac9ec31b
DE
22877 type = build_error_marker_type (cu, die);
22878 }
22879 }
22880 else
22881 {
b98664d3 22882 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22883 " from DIE at %s [in module %s]"),
22884 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 22885 objfile_name (per_objfile->objfile));
ac9ec31b
DE
22886 type = build_error_marker_type (cu, die);
22887 }
e286671b 22888
976ca316 22889 per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
22890
22891 return type;
22892}
22893
22894/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22895 reading in and processing the type unit if necessary. */
22896
22897static struct type *
ff39bb5e 22898get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22899 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22900{
22901 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22902 if (attr->form_is_ref ())
ac9ec31b
DE
22903 {
22904 struct dwarf2_cu *type_cu = cu;
22905 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22906
22907 return read_type_die (type_die, type_cu);
22908 }
22909 else if (attr->form == DW_FORM_ref_sig8)
22910 {
22911 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22912 }
22913 else
22914 {
976ca316 22915 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 22916
b98664d3 22917 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22918 " at %s [in module %s]"),
22919 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
976ca316 22920 objfile_name (per_objfile->objfile));
ac9ec31b
DE
22921 return build_error_marker_type (cu, die);
22922 }
348e048f
DE
22923}
22924
e5fe5e75 22925/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22926
22927static void
ab432490
SM
22928load_full_type_unit (dwarf2_per_cu_data *per_cu,
22929 dwarf2_per_objfile *per_objfile)
348e048f 22930{
52dc124a 22931 struct signatured_type *sig_type;
348e048f 22932
f4dc4d17 22933 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22934 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22935
6721b2ec
DE
22936 /* We have the per_cu, but we need the signatured_type.
22937 Fortunately this is an easy translation. */
22938 gdb_assert (per_cu->is_debug_types);
22939 sig_type = (struct signatured_type *) per_cu;
348e048f 22940
7188ed02 22941 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 22942
ab432490 22943 read_signatured_type (sig_type, per_objfile);
348e048f 22944
7188ed02 22945 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
348e048f
DE
22946}
22947
3019eac3
DE
22948/* Read in a signatured type and build its CU and DIEs.
22949 If the type is a stub for the real type in a DWO file,
22950 read in the real type from the DWO file as well. */
dee91e82
DE
22951
22952static void
ab432490
SM
22953read_signatured_type (signatured_type *sig_type,
22954 dwarf2_per_objfile *per_objfile)
dee91e82
DE
22955{
22956 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22957
3019eac3 22958 gdb_assert (per_cu->is_debug_types);
7188ed02 22959 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 22960
2e671100 22961 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
22962
22963 if (!reader.dummy_p)
22964 {
22965 struct dwarf2_cu *cu = reader.cu;
22966 const gdb_byte *info_ptr = reader.info_ptr;
22967
22968 gdb_assert (cu->die_hash == NULL);
22969 cu->die_hash =
22970 htab_create_alloc_ex (cu->header.length / 12,
22971 die_hash,
22972 die_eq,
22973 NULL,
22974 &cu->comp_unit_obstack,
22975 hashtab_obstack_allocate,
22976 dummy_obstack_deallocate);
22977
3e225074 22978 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22979 reader.comp_unit_die->child
22980 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22981 reader.comp_unit_die);
22982 cu->dies = reader.comp_unit_die;
22983 /* comp_unit_die is not stored in die_hash, no need. */
22984
22985 /* We try not to read any attributes in this function, because
22986 not all CUs needed for references have been loaded yet, and
22987 symbol table processing isn't initialized. But we have to
22988 set the CU language, or we won't be able to build types
22989 correctly. Similarly, if we do not read the producer, we can
22990 not apply producer-specific interpretation. */
22991 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22992
22993 reader.keep ();
c0ab21c2
TT
22994 }
22995
7ee85ab1 22996 sig_type->per_cu.tu_read = 1;
c906108c
SS
22997}
22998
c906108c
SS
22999/* Decode simple location descriptions.
23000 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
23001 the location and return the value. If COMPUTED is non-null, it is
23002 set to true to indicate that decoding was successful, and false
23003 otherwise. If COMPUTED is null, then this function may emit a
23004 complaint. */
c906108c
SS
23005
23006static CORE_ADDR
7d79de9a 23007decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 23008{
5e22e966 23009 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
23010 size_t i;
23011 size_t size = blk->size;
d521ce57 23012 const gdb_byte *data = blk->data;
21ae7a4d
JK
23013 CORE_ADDR stack[64];
23014 int stacki;
23015 unsigned int bytes_read, unsnd;
23016 gdb_byte op;
c906108c 23017
7d79de9a
TT
23018 if (computed != nullptr)
23019 *computed = false;
23020
21ae7a4d
JK
23021 i = 0;
23022 stacki = 0;
23023 stack[stacki] = 0;
23024 stack[++stacki] = 0;
23025
23026 while (i < size)
23027 {
23028 op = data[i++];
23029 switch (op)
23030 {
23031 case DW_OP_lit0:
23032 case DW_OP_lit1:
23033 case DW_OP_lit2:
23034 case DW_OP_lit3:
23035 case DW_OP_lit4:
23036 case DW_OP_lit5:
23037 case DW_OP_lit6:
23038 case DW_OP_lit7:
23039 case DW_OP_lit8:
23040 case DW_OP_lit9:
23041 case DW_OP_lit10:
23042 case DW_OP_lit11:
23043 case DW_OP_lit12:
23044 case DW_OP_lit13:
23045 case DW_OP_lit14:
23046 case DW_OP_lit15:
23047 case DW_OP_lit16:
23048 case DW_OP_lit17:
23049 case DW_OP_lit18:
23050 case DW_OP_lit19:
23051 case DW_OP_lit20:
23052 case DW_OP_lit21:
23053 case DW_OP_lit22:
23054 case DW_OP_lit23:
23055 case DW_OP_lit24:
23056 case DW_OP_lit25:
23057 case DW_OP_lit26:
23058 case DW_OP_lit27:
23059 case DW_OP_lit28:
23060 case DW_OP_lit29:
23061 case DW_OP_lit30:
23062 case DW_OP_lit31:
23063 stack[++stacki] = op - DW_OP_lit0;
23064 break;
f1bea926 23065
21ae7a4d
JK
23066 case DW_OP_reg0:
23067 case DW_OP_reg1:
23068 case DW_OP_reg2:
23069 case DW_OP_reg3:
23070 case DW_OP_reg4:
23071 case DW_OP_reg5:
23072 case DW_OP_reg6:
23073 case DW_OP_reg7:
23074 case DW_OP_reg8:
23075 case DW_OP_reg9:
23076 case DW_OP_reg10:
23077 case DW_OP_reg11:
23078 case DW_OP_reg12:
23079 case DW_OP_reg13:
23080 case DW_OP_reg14:
23081 case DW_OP_reg15:
23082 case DW_OP_reg16:
23083 case DW_OP_reg17:
23084 case DW_OP_reg18:
23085 case DW_OP_reg19:
23086 case DW_OP_reg20:
23087 case DW_OP_reg21:
23088 case DW_OP_reg22:
23089 case DW_OP_reg23:
23090 case DW_OP_reg24:
23091 case DW_OP_reg25:
23092 case DW_OP_reg26:
23093 case DW_OP_reg27:
23094 case DW_OP_reg28:
23095 case DW_OP_reg29:
23096 case DW_OP_reg30:
23097 case DW_OP_reg31:
23098 stack[++stacki] = op - DW_OP_reg0;
23099 if (i < size)
7d79de9a
TT
23100 {
23101 if (computed == nullptr)
23102 dwarf2_complex_location_expr_complaint ();
23103 else
23104 return 0;
23105 }
21ae7a4d 23106 break;
c906108c 23107
21ae7a4d
JK
23108 case DW_OP_regx:
23109 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23110 i += bytes_read;
23111 stack[++stacki] = unsnd;
23112 if (i < size)
7d79de9a
TT
23113 {
23114 if (computed == nullptr)
23115 dwarf2_complex_location_expr_complaint ();
23116 else
23117 return 0;
23118 }
21ae7a4d 23119 break;
c906108c 23120
21ae7a4d 23121 case DW_OP_addr:
c8a7a66f
TT
23122 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23123 &bytes_read);
21ae7a4d
JK
23124 i += bytes_read;
23125 break;
d53d4ac5 23126
21ae7a4d
JK
23127 case DW_OP_const1u:
23128 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23129 i += 1;
23130 break;
23131
23132 case DW_OP_const1s:
23133 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23134 i += 1;
23135 break;
23136
23137 case DW_OP_const2u:
23138 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23139 i += 2;
23140 break;
23141
23142 case DW_OP_const2s:
23143 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23144 i += 2;
23145 break;
d53d4ac5 23146
21ae7a4d
JK
23147 case DW_OP_const4u:
23148 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23149 i += 4;
23150 break;
23151
23152 case DW_OP_const4s:
23153 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23154 i += 4;
23155 break;
23156
585861ea
JK
23157 case DW_OP_const8u:
23158 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23159 i += 8;
23160 break;
23161
21ae7a4d
JK
23162 case DW_OP_constu:
23163 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23164 &bytes_read);
23165 i += bytes_read;
23166 break;
23167
23168 case DW_OP_consts:
23169 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23170 i += bytes_read;
23171 break;
23172
23173 case DW_OP_dup:
23174 stack[stacki + 1] = stack[stacki];
23175 stacki++;
23176 break;
23177
23178 case DW_OP_plus:
23179 stack[stacki - 1] += stack[stacki];
23180 stacki--;
23181 break;
23182
23183 case DW_OP_plus_uconst:
23184 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23185 &bytes_read);
23186 i += bytes_read;
23187 break;
23188
23189 case DW_OP_minus:
23190 stack[stacki - 1] -= stack[stacki];
23191 stacki--;
23192 break;
23193
23194 case DW_OP_deref:
23195 /* If we're not the last op, then we definitely can't encode
23196 this using GDB's address_class enum. This is valid for partial
23197 global symbols, although the variable's address will be bogus
23198 in the psymtab. */
23199 if (i < size)
7d79de9a
TT
23200 {
23201 if (computed == nullptr)
23202 dwarf2_complex_location_expr_complaint ();
23203 else
23204 return 0;
23205 }
21ae7a4d
JK
23206 break;
23207
23208 case DW_OP_GNU_push_tls_address:
4aa4e28b 23209 case DW_OP_form_tls_address:
21ae7a4d
JK
23210 /* The top of the stack has the offset from the beginning
23211 of the thread control block at which the variable is located. */
23212 /* Nothing should follow this operator, so the top of stack would
23213 be returned. */
23214 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23215 address will be bogus in the psymtab. Make it always at least
23216 non-zero to not look as a variable garbage collected by linker
23217 which have DW_OP_addr 0. */
21ae7a4d 23218 if (i < size)
7d79de9a
TT
23219 {
23220 if (computed == nullptr)
23221 dwarf2_complex_location_expr_complaint ();
23222 else
23223 return 0;
23224 }
585861ea 23225 stack[stacki]++;
21ae7a4d
JK
23226 break;
23227
23228 case DW_OP_GNU_uninit:
7d79de9a
TT
23229 if (computed != nullptr)
23230 return 0;
21ae7a4d
JK
23231 break;
23232
336d760d 23233 case DW_OP_addrx:
3019eac3 23234 case DW_OP_GNU_addr_index:
49f6c839 23235 case DW_OP_GNU_const_index:
3019eac3
DE
23236 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23237 &bytes_read);
23238 i += bytes_read;
23239 break;
23240
21ae7a4d 23241 default:
7d79de9a
TT
23242 if (computed == nullptr)
23243 {
23244 const char *name = get_DW_OP_name (op);
21ae7a4d 23245
7d79de9a
TT
23246 if (name)
23247 complaint (_("unsupported stack op: '%s'"),
23248 name);
23249 else
23250 complaint (_("unsupported stack op: '%02x'"),
23251 op);
23252 }
21ae7a4d
JK
23253
23254 return (stack[stacki]);
d53d4ac5 23255 }
3c6e0cb3 23256
21ae7a4d
JK
23257 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23258 outside of the allocated space. Also enforce minimum>0. */
23259 if (stacki >= ARRAY_SIZE (stack) - 1)
23260 {
7d79de9a
TT
23261 if (computed == nullptr)
23262 complaint (_("location description stack overflow"));
21ae7a4d
JK
23263 return 0;
23264 }
23265
23266 if (stacki <= 0)
23267 {
7d79de9a
TT
23268 if (computed == nullptr)
23269 complaint (_("location description stack underflow"));
21ae7a4d
JK
23270 return 0;
23271 }
23272 }
7d79de9a
TT
23273
23274 if (computed != nullptr)
23275 *computed = true;
21ae7a4d 23276 return (stack[stacki]);
c906108c
SS
23277}
23278
23279/* memory allocation interface */
23280
c906108c 23281static struct dwarf_block *
7b5a2f43 23282dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23283{
8d749320 23284 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23285}
23286
c906108c 23287static struct die_info *
b60c80d6 23288dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23289{
23290 struct die_info *die;
b60c80d6
DJ
23291 size_t size = sizeof (struct die_info);
23292
23293 if (num_attrs > 1)
23294 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23295
b60c80d6 23296 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23297 memset (die, 0, sizeof (struct die_info));
23298 return (die);
23299}
2e276125
JB
23300
23301\f
a036ba48 23302
c90ec28a 23303/* Macro support. */
cf2c3c16 23304
9eac9650
TT
23305/* An overload of dwarf_decode_macros that finds the correct section
23306 and ensures it is read in before calling the other overload. */
23307
23308static void
23309dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23310 int section_is_gnu)
23311{
976ca316
SM
23312 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23313 struct objfile *objfile = per_objfile->objfile;
5a0e026f 23314 const struct line_header *lh = cu->line_header;
9eac9650
TT
23315 unsigned int offset_size = cu->header.offset_size;
23316 struct dwarf2_section_info *section;
23317 const char *section_name;
23318
23319 if (cu->dwo_unit != nullptr)
23320 {
23321 if (section_is_gnu)
23322 {
23323 section = &cu->dwo_unit->dwo_file->sections.macro;
23324 section_name = ".debug_macro.dwo";
23325 }
23326 else
23327 {
23328 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23329 section_name = ".debug_macinfo.dwo";
23330 }
23331 }
23332 else
23333 {
23334 if (section_is_gnu)
23335 {
976ca316 23336 section = &per_objfile->per_bfd->macro;
9eac9650
TT
23337 section_name = ".debug_macro";
23338 }
23339 else
23340 {
976ca316 23341 section = &per_objfile->per_bfd->macinfo;
9eac9650
TT
23342 section_name = ".debug_macinfo";
23343 }
23344 }
23345
23346 section->read (objfile);
23347 if (section->buffer == nullptr)
23348 {
23349 complaint (_("missing %s section"), section_name);
23350 return;
23351 }
23352
23353 buildsym_compunit *builder = cu->get_builder ();
23354
976ca316 23355 dwarf_decode_macros (per_objfile, builder, section, lh,
9eac9650
TT
23356 offset_size, offset, section_is_gnu);
23357}
23358
3019eac3
DE
23359/* Return the .debug_loc section to use for CU.
23360 For DWO files use .debug_loc.dwo. */
23361
23362static struct dwarf2_section_info *
23363cu_debug_loc_section (struct dwarf2_cu *cu)
23364{
976ca316 23365 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 23366
3019eac3 23367 if (cu->dwo_unit)
43988095
JK
23368 {
23369 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23370
43988095
JK
23371 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23372 }
976ca316
SM
23373 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
23374 : &per_objfile->per_bfd->loc);
3019eac3
DE
23375}
23376
8cf6f0b1
TT
23377/* A helper function that fills in a dwarf2_loclist_baton. */
23378
23379static void
23380fill_in_loclist_baton (struct dwarf2_cu *cu,
23381 struct dwarf2_loclist_baton *baton,
ff39bb5e 23382 const struct attribute *attr)
8cf6f0b1 23383{
976ca316 23384 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3
DE
23385 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23386
976ca316 23387 section->read (per_objfile->objfile);
8cf6f0b1 23388
976ca316 23389 baton->per_objfile = per_objfile;
8cf6f0b1
TT
23390 baton->per_cu = cu->per_cu;
23391 gdb_assert (baton->per_cu);
23392 /* We don't know how long the location list is, but make sure we
23393 don't run off the edge of the section. */
3019eac3
DE
23394 baton->size = section->size - DW_UNSND (attr);
23395 baton->data = section->buffer + DW_UNSND (attr);
2b24b6e4
TT
23396 if (cu->base_address.has_value ())
23397 baton->base_address = *cu->base_address;
23398 else
23399 baton->base_address = 0;
f664829e 23400 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23401}
23402
4c2df51b 23403static void
ff39bb5e 23404dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23405 struct dwarf2_cu *cu, int is_block)
4c2df51b 23406{
976ca316
SM
23407 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23408 struct objfile *objfile = per_objfile->objfile;
3019eac3 23409 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23410
cd6c91b4 23411 if (attr->form_is_section_offset ()
3019eac3 23412 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23413 the section. If so, fall through to the complaint in the
23414 other branch. */
2c7d5afc 23415 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 23416 {
0d53c4c4 23417 struct dwarf2_loclist_baton *baton;
4c2df51b 23418
8d749320 23419 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23420
8cf6f0b1 23421 fill_in_loclist_baton (cu, baton, attr);
be391dca 23422
2b24b6e4 23423 if (!cu->base_address.has_value ())
b98664d3 23424 complaint (_("Location list used without "
3e43a32a 23425 "specifying the CU base address."));
4c2df51b 23426
f1e6e072
TT
23427 SYMBOL_ACLASS_INDEX (sym) = (is_block
23428 ? dwarf2_loclist_block_index
23429 : dwarf2_loclist_index);
0d53c4c4
DJ
23430 SYMBOL_LOCATION_BATON (sym) = baton;
23431 }
23432 else
23433 {
23434 struct dwarf2_locexpr_baton *baton;
23435
8d749320 23436 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
976ca316 23437 baton->per_objfile = per_objfile;
ae0d2f24
UW
23438 baton->per_cu = cu->per_cu;
23439 gdb_assert (baton->per_cu);
0d53c4c4 23440
4fc6c0d5 23441 if (attr->form_is_block ())
0d53c4c4
DJ
23442 {
23443 /* Note that we're just copying the block's data pointer
23444 here, not the actual data. We're still pointing into the
6502dd73
DJ
23445 info_buffer for SYM's objfile; right now we never release
23446 that buffer, but when we do clean up properly this may
23447 need to change. */
0d53c4c4
DJ
23448 baton->size = DW_BLOCK (attr)->size;
23449 baton->data = DW_BLOCK (attr)->data;
23450 }
23451 else
23452 {
23453 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 23454 sym->natural_name ());
0d53c4c4 23455 baton->size = 0;
0d53c4c4 23456 }
6e70227d 23457
f1e6e072
TT
23458 SYMBOL_ACLASS_INDEX (sym) = (is_block
23459 ? dwarf2_locexpr_block_index
23460 : dwarf2_locexpr_index);
0d53c4c4
DJ
23461 SYMBOL_LOCATION_BATON (sym) = baton;
23462 }
4c2df51b 23463}
6502dd73 23464
2e6a9f79 23465/* See read.h. */
96408a79 23466
2e6a9f79
SM
23467const comp_unit_head *
23468dwarf2_per_cu_data::get_header () const
96408a79 23469{
2e6a9f79
SM
23470 if (!m_header_read_in)
23471 {
23472 const gdb_byte *info_ptr
23473 = this->section->buffer + to_underlying (this->sect_off);
96408a79 23474
2e6a9f79 23475 memset (&m_header, 0, sizeof (m_header));
96408a79 23476
2e6a9f79
SM
23477 read_comp_unit_head (&m_header, info_ptr, this->section,
23478 rcuh_kind::COMPILE);
23479 }
96408a79 23480
2e6a9f79 23481 return &m_header;
96408a79
SA
23482}
23483
09ba997f 23484/* See read.h. */
ae0d2f24 23485
98714339 23486int
09ba997f 23487dwarf2_per_cu_data::addr_size () const
ae0d2f24 23488{
2e6a9f79 23489 return this->get_header ()->addr_size;
ae0d2f24
UW
23490}
23491
09ba997f 23492/* See read.h. */
9eae7c52
TT
23493
23494int
09ba997f 23495dwarf2_per_cu_data::offset_size () const
9eae7c52 23496{
2e6a9f79 23497 return this->get_header ()->offset_size;
96408a79
SA
23498}
23499
09ba997f 23500/* See read.h. */
96408a79
SA
23501
23502int
09ba997f 23503dwarf2_per_cu_data::ref_addr_size () const
96408a79 23504{
2e6a9f79 23505 const comp_unit_head *header = this->get_header ();
96408a79 23506
2e6a9f79
SM
23507 if (header->version == 2)
23508 return header->addr_size;
96408a79 23509 else
2e6a9f79 23510 return header->offset_size;
181cebd4
JK
23511}
23512
09ba997f 23513/* See read.h. */
9aa1f1e3 23514
09ba997f 23515struct type *
293e7e51 23516dwarf2_cu::addr_type () const
9a49df9d 23517{
293e7e51 23518 struct objfile *objfile = this->per_objfile->objfile;
9a49df9d
AB
23519 struct type *void_type = objfile_type (objfile)->builtin_void;
23520 struct type *addr_type = lookup_pointer_type (void_type);
293e7e51 23521 int addr_size = this->per_cu->addr_size ();
9a49df9d
AB
23522
23523 if (TYPE_LENGTH (addr_type) == addr_size)
23524 return addr_type;
23525
09ba997f 23526 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
23527 return addr_type;
23528}
23529
22b6cd70
TT
23530/* A helper function for dwarf2_find_containing_comp_unit that returns
23531 the index of the result, and that searches a vector. It will
23532 return a result even if the offset in question does not actually
23533 occur in any CU. This is separate so that it can be unit
23534 tested. */
ae038cb0 23535
22b6cd70
TT
23536static int
23537dwarf2_find_containing_comp_unit
23538 (sect_offset sect_off,
23539 unsigned int offset_in_dwz,
23540 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 23541{
ae038cb0
DJ
23542 int low, high;
23543
ae038cb0 23544 low = 0;
22b6cd70 23545 high = all_comp_units.size () - 1;
ae038cb0
DJ
23546 while (high > low)
23547 {
36586728 23548 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 23549 int mid = low + (high - low) / 2;
9a619af0 23550
22b6cd70 23551 mid_cu = all_comp_units[mid];
36586728 23552 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 23553 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 23554 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
23555 high = mid;
23556 else
23557 low = mid + 1;
23558 }
23559 gdb_assert (low == high);
22b6cd70
TT
23560 return low;
23561}
23562
23563/* Locate the .debug_info compilation unit from CU's objfile which contains
23564 the DIE at OFFSET. Raises an error on failure. */
23565
23566static struct dwarf2_per_cu_data *
23567dwarf2_find_containing_comp_unit (sect_offset sect_off,
23568 unsigned int offset_in_dwz,
976ca316 23569 dwarf2_per_objfile *per_objfile)
22b6cd70 23570{
976ca316
SM
23571 int low = dwarf2_find_containing_comp_unit
23572 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
23573 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
22b6cd70 23574
45b8ae0c 23575 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 23576 {
36586728 23577 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 23578 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
23579 "offset %s [in module %s]"),
23580 sect_offset_str (sect_off),
976ca316 23581 bfd_get_filename (per_objfile->objfile->obfd));
10b3939b 23582
976ca316 23583 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 23584 <= sect_off);
976ca316 23585 return per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
23586 }
23587 else
23588 {
976ca316 23589 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 23590 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 23591 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 23592 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
23593 return this_cu;
23594 }
23595}
23596
22b6cd70
TT
23597#if GDB_SELF_TEST
23598
23599namespace selftests {
23600namespace find_containing_comp_unit {
23601
23602static void
23603run_test ()
23604{
23605 struct dwarf2_per_cu_data one {};
23606 struct dwarf2_per_cu_data two {};
23607 struct dwarf2_per_cu_data three {};
23608 struct dwarf2_per_cu_data four {};
23609
23610 one.length = 5;
23611 two.sect_off = sect_offset (one.length);
23612 two.length = 7;
23613
23614 three.length = 5;
23615 three.is_dwz = 1;
23616 four.sect_off = sect_offset (three.length);
23617 four.length = 7;
23618 four.is_dwz = 1;
23619
23620 std::vector<dwarf2_per_cu_data *> units;
23621 units.push_back (&one);
23622 units.push_back (&two);
23623 units.push_back (&three);
23624 units.push_back (&four);
23625
23626 int result;
23627
23628 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23629 SELF_CHECK (units[result] == &one);
23630 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23631 SELF_CHECK (units[result] == &one);
23632 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23633 SELF_CHECK (units[result] == &two);
23634
23635 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23636 SELF_CHECK (units[result] == &three);
23637 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23638 SELF_CHECK (units[result] == &three);
23639 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23640 SELF_CHECK (units[result] == &four);
23641}
23642
23643}
23644}
23645
23646#endif /* GDB_SELF_TEST */
23647
9e021579 23648/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 23649
9e021579
SM
23650dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23651 dwarf2_per_objfile *per_objfile)
23652 : per_cu (per_cu),
23653 per_objfile (per_objfile),
9068261f
AB
23654 mark (false),
23655 has_loclist (false),
23656 checked_producer (false),
23657 producer_is_gxx_lt_4_6 (false),
23658 producer_is_gcc_lt_4_3 (false),
eb77c9df 23659 producer_is_icc (false),
9068261f 23660 producer_is_icc_lt_14 (false),
c258c396 23661 producer_is_codewarrior (false),
9068261f 23662 processing_has_namespace_info (false)
93311388 23663{
9816fde3
JK
23664}
23665
23666/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23667
23668static void
95554aad
TT
23669prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23670 enum language pretend_language)
9816fde3
JK
23671{
23672 struct attribute *attr;
23673
23674 /* Set the language we're debugging. */
23675 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 23676 if (attr != nullptr)
9816fde3
JK
23677 set_cu_language (DW_UNSND (attr), cu);
23678 else
9cded63f 23679 {
95554aad 23680 cu->language = pretend_language;
9cded63f
TT
23681 cu->language_defn = language_def (cu->language);
23682 }
dee91e82 23683
7d45c7c3 23684 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
23685}
23686
7188ed02 23687/* See read.h. */
ae038cb0 23688
7188ed02
SM
23689dwarf2_cu *
23690dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 23691{
7188ed02
SM
23692 auto it = m_dwarf2_cus.find (per_cu);
23693 if (it == m_dwarf2_cus.end ())
23694 return nullptr;
ae038cb0 23695
7188ed02
SM
23696 return it->second;
23697}
23698
23699/* See read.h. */
23700
23701void
23702dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
23703{
23704 gdb_assert (this->get_cu (per_cu) == nullptr);
23705
23706 m_dwarf2_cus[per_cu] = cu;
23707}
23708
23709/* See read.h. */
23710
23711void
23712dwarf2_per_objfile::age_comp_units ()
23713{
23714 /* Start by clearing all marks. */
23715 for (auto pair : m_dwarf2_cus)
23716 pair.second->mark = false;
23717
23718 /* Traverse all CUs, mark them and their dependencies if used recently
23719 enough. */
23720 for (auto pair : m_dwarf2_cus)
ae038cb0 23721 {
7188ed02
SM
23722 dwarf2_cu *cu = pair.second;
23723
23724 cu->last_used++;
23725 if (cu->last_used <= dwarf_max_cache_age)
23726 dwarf2_mark (cu);
ae038cb0
DJ
23727 }
23728
7188ed02
SM
23729 /* Delete all CUs still not marked. */
23730 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
ae038cb0 23731 {
7188ed02 23732 dwarf2_cu *cu = it->second;
ae038cb0 23733
7188ed02 23734 if (!cu->mark)
ae038cb0 23735 {
7188ed02
SM
23736 delete cu;
23737 it = m_dwarf2_cus.erase (it);
ae038cb0
DJ
23738 }
23739 else
7188ed02 23740 it++;
ae038cb0
DJ
23741 }
23742}
23743
7188ed02 23744/* See read.h. */
ae038cb0 23745
7188ed02
SM
23746void
23747dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 23748{
7188ed02
SM
23749 auto it = m_dwarf2_cus.find (per_cu);
23750 if (it == m_dwarf2_cus.end ())
23751 return;
ae038cb0 23752
7188ed02 23753 delete it->second;
ae038cb0 23754
7188ed02
SM
23755 m_dwarf2_cus.erase (it);
23756}
ae038cb0 23757
7188ed02
SM
23758dwarf2_per_objfile::~dwarf2_per_objfile ()
23759{
23760 remove_all_cus ();
ae038cb0
DJ
23761}
23762
dee91e82
DE
23763/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23764 We store these in a hash table separate from the DIEs, and preserve them
23765 when the DIEs are flushed out of cache.
23766
23767 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23768 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23769 or the type may come from a DWO file. Furthermore, while it's more logical
23770 to use per_cu->section+offset, with Fission the section with the data is in
23771 the DWO file but we don't know that section at the point we need it.
23772 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23773 because we can enter the lookup routine, get_die_type_at_offset, from
23774 outside this file, and thus won't necessarily have PER_CU->cu.
23775 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23776
dee91e82 23777struct dwarf2_per_cu_offset_and_type
1c379e20 23778{
dee91e82 23779 const struct dwarf2_per_cu_data *per_cu;
9c541725 23780 sect_offset sect_off;
1c379e20
DJ
23781 struct type *type;
23782};
23783
dee91e82 23784/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23785
23786static hashval_t
dee91e82 23787per_cu_offset_and_type_hash (const void *item)
1c379e20 23788{
9a3c8263
SM
23789 const struct dwarf2_per_cu_offset_and_type *ofs
23790 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23791
9c541725 23792 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23793}
23794
dee91e82 23795/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23796
23797static int
dee91e82 23798per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23799{
9a3c8263
SM
23800 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23801 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23802 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23803 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23804
dee91e82 23805 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23806 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23807}
23808
23809/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23810 table if necessary. For convenience, return TYPE.
23811
23812 The DIEs reading must have careful ordering to:
85102364 23813 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
23814 reading current DIE.
23815 * Not trying to dereference contents of still incompletely read in types
23816 while reading in other DIEs.
23817 * Enable referencing still incompletely read in types just by a pointer to
23818 the type without accessing its fields.
23819
23820 Therefore caller should follow these rules:
23821 * Try to fetch any prerequisite types we may need to build this DIE type
23822 before building the type and calling set_die_type.
e71ec853 23823 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23824 possible before fetching more types to complete the current type.
23825 * Make the type as complete as possible before fetching more types. */
1c379e20 23826
f792889a 23827static struct type *
1c379e20
DJ
23828set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23829{
976ca316 23830 dwarf2_per_objfile *per_objfile = cu->per_objfile;
dee91e82 23831 struct dwarf2_per_cu_offset_and_type **slot, ofs;
976ca316 23832 struct objfile *objfile = per_objfile->objfile;
3cdcd0ce
JB
23833 struct attribute *attr;
23834 struct dynamic_prop prop;
1c379e20 23835
b4ba55a1
JB
23836 /* For Ada types, make sure that the gnat-specific data is always
23837 initialized (if not already set). There are a few types where
23838 we should not be doing so, because the type-specific area is
23839 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23840 where the type-specific area is used to store the floatformat).
23841 But this is not a problem, because the gnat-specific information
23842 is actually not needed for these types. */
23843 if (need_gnat_info (cu)
78134374
SM
23844 && type->code () != TYPE_CODE_FUNC
23845 && type->code () != TYPE_CODE_FLT
23846 && type->code () != TYPE_CODE_METHODPTR
23847 && type->code () != TYPE_CODE_MEMBERPTR
23848 && type->code () != TYPE_CODE_METHOD
b4ba55a1
JB
23849 && !HAVE_GNAT_AUX_INFO (type))
23850 INIT_GNAT_SPECIFIC (type);
23851
3f2f83dd
KB
23852 /* Read DW_AT_allocated and set in type. */
23853 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 23854 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23855 {
293e7e51 23856 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 23857 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23858 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd
KB
23859 }
23860 else if (attr != NULL)
23861 {
b98664d3 23862 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 23863 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23864 sect_offset_str (die->sect_off));
3f2f83dd
KB
23865 }
23866
23867 /* Read DW_AT_associated and set in type. */
23868 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 23869 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23870 {
293e7e51 23871 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 23872 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23873 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd
KB
23874 }
23875 else if (attr != NULL)
23876 {
b98664d3 23877 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 23878 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23879 sect_offset_str (die->sect_off));
3f2f83dd
KB
23880 }
23881
3cdcd0ce
JB
23882 /* Read DW_AT_data_location and set in type. */
23883 attr = dwarf2_attr (die, DW_AT_data_location, cu);
293e7e51 23884 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 23885 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
3cdcd0ce 23886
976ca316
SM
23887 if (per_objfile->die_type_hash == NULL)
23888 per_objfile->die_type_hash
0335378b
TT
23889 = htab_up (htab_create_alloc (127,
23890 per_cu_offset_and_type_hash,
23891 per_cu_offset_and_type_eq,
23892 NULL, xcalloc, xfree));
1c379e20 23893
dee91e82 23894 ofs.per_cu = cu->per_cu;
9c541725 23895 ofs.sect_off = die->sect_off;
1c379e20 23896 ofs.type = type;
dee91e82 23897 slot = (struct dwarf2_per_cu_offset_and_type **)
976ca316 23898 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 23899 if (*slot)
b98664d3 23900 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 23901 sect_offset_str (die->sect_off));
8d749320
SM
23902 *slot = XOBNEW (&objfile->objfile_obstack,
23903 struct dwarf2_per_cu_offset_and_type);
1c379e20 23904 **slot = ofs;
f792889a 23905 return type;
1c379e20
DJ
23906}
23907
9c541725 23908/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23909 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23910
23911static struct type *
9c541725 23912get_die_type_at_offset (sect_offset sect_off,
aa66c379 23913 dwarf2_per_cu_data *per_cu,
976ca316 23914 dwarf2_per_objfile *per_objfile)
1c379e20 23915{
dee91e82 23916 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 23917
976ca316 23918 if (per_objfile->die_type_hash == NULL)
f792889a 23919 return NULL;
1c379e20 23920
dee91e82 23921 ofs.per_cu = per_cu;
9c541725 23922 ofs.sect_off = sect_off;
9a3c8263 23923 slot = ((struct dwarf2_per_cu_offset_and_type *)
976ca316 23924 htab_find (per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
23925 if (slot)
23926 return slot->type;
23927 else
23928 return NULL;
23929}
23930
02142a6c 23931/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23932 or return NULL if DIE does not have a saved type. */
23933
23934static struct type *
23935get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23936{
aa66c379 23937 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
23938}
23939
10b3939b
DJ
23940/* Add a dependence relationship from CU to REF_PER_CU. */
23941
23942static void
23943dwarf2_add_dependence (struct dwarf2_cu *cu,
23944 struct dwarf2_per_cu_data *ref_per_cu)
23945{
23946 void **slot;
23947
23948 if (cu->dependencies == NULL)
23949 cu->dependencies
23950 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23951 NULL, &cu->comp_unit_obstack,
23952 hashtab_obstack_allocate,
23953 dummy_obstack_deallocate);
23954
23955 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23956 if (*slot == NULL)
23957 *slot = ref_per_cu;
23958}
1c379e20 23959
f504f079
DE
23960/* Subroutine of dwarf2_mark to pass to htab_traverse.
23961 Set the mark field in every compilation unit in the
7188ed02
SM
23962 cache that we must keep because we are keeping CU.
23963
23964 DATA is the dwarf2_per_objfile object in which to look up CUs. */
ae038cb0 23965
10b3939b
DJ
23966static int
23967dwarf2_mark_helper (void **slot, void *data)
23968{
7188ed02
SM
23969 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
23970 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
23971 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
d07ed419
JK
23972
23973 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23974 reading of the chain. As such dependencies remain valid it is not much
23975 useful to track and undo them during QUIT cleanups. */
7188ed02 23976 if (cu == nullptr)
d07ed419
JK
23977 return 1;
23978
7188ed02 23979 if (cu->mark)
10b3939b 23980 return 1;
10b3939b 23981
7188ed02
SM
23982 cu->mark = true;
23983
23984 if (cu->dependencies != nullptr)
23985 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
10b3939b
DJ
23986
23987 return 1;
23988}
23989
f504f079
DE
23990/* Set the mark field in CU and in every other compilation unit in the
23991 cache that we must keep because we are keeping CU. */
23992
ae038cb0
DJ
23993static void
23994dwarf2_mark (struct dwarf2_cu *cu)
23995{
23996 if (cu->mark)
23997 return;
7188ed02 23998
9068261f 23999 cu->mark = true;
ae038cb0 24000
7188ed02
SM
24001 if (cu->dependencies != nullptr)
24002 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
72bf9492
DJ
24003}
24004
72bf9492
DJ
24005/* Trivial hash function for partial_die_info: the hash value of a DIE
24006 is its offset in .debug_info for this objfile. */
24007
24008static hashval_t
24009partial_die_hash (const void *item)
24010{
9a3c8263
SM
24011 const struct partial_die_info *part_die
24012 = (const struct partial_die_info *) item;
9a619af0 24013
9c541725 24014 return to_underlying (part_die->sect_off);
72bf9492
DJ
24015}
24016
24017/* Trivial comparison function for partial_die_info structures: two DIEs
24018 are equal if they have the same offset. */
24019
24020static int
24021partial_die_eq (const void *item_lhs, const void *item_rhs)
24022{
9a3c8263
SM
24023 const struct partial_die_info *part_die_lhs
24024 = (const struct partial_die_info *) item_lhs;
24025 const struct partial_die_info *part_die_rhs
24026 = (const struct partial_die_info *) item_rhs;
9a619af0 24027
9c541725 24028 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
24029}
24030
3c3bb058
AB
24031struct cmd_list_element *set_dwarf_cmdlist;
24032struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 24033
9291a0cd 24034static void
cd4fb1b2
SM
24035show_check_physname (struct ui_file *file, int from_tty,
24036 struct cmd_list_element *c, const char *value)
9291a0cd 24037{
cd4fb1b2
SM
24038 fprintf_filtered (file,
24039 _("Whether to check \"physname\" is %s.\n"),
24040 value);
9291a0cd
TT
24041}
24042
6c265988 24043void _initialize_dwarf2_read ();
cd4fb1b2 24044void
6c265988 24045_initialize_dwarf2_read ()
9291a0cd 24046{
0743fc83 24047 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 24048Set DWARF specific variables.\n\
590042fc 24049Configure DWARF variables such as the cache size."),
0743fc83
TT
24050 &set_dwarf_cmdlist, "maintenance set dwarf ",
24051 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 24052
0743fc83 24053 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
24054Show DWARF specific variables.\n\
24055Show DWARF variables such as the cache size."),
0743fc83
TT
24056 &show_dwarf_cmdlist, "maintenance show dwarf ",
24057 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 24058
cd4fb1b2
SM
24059 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24060 &dwarf_max_cache_age, _("\
24061Set the upper bound on the age of cached DWARF compilation units."), _("\
24062Show the upper bound on the age of cached DWARF compilation units."), _("\
24063A higher limit means that cached compilation units will be stored\n\
24064in memory longer, and more total memory will be used. Zero disables\n\
24065caching, which can slow down startup."),
24066 NULL,
24067 show_dwarf_max_cache_age,
24068 &set_dwarf_cmdlist,
24069 &show_dwarf_cmdlist);
156942c7 24070
cd4fb1b2
SM
24071 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24072Set debugging of the DWARF reader."), _("\
24073Show debugging of the DWARF reader."), _("\
24074When enabled (non-zero), debugging messages are printed during DWARF\n\
24075reading and symtab expansion. A value of 1 (one) provides basic\n\
24076information. A value greater than 1 provides more verbose information."),
24077 NULL,
24078 NULL,
24079 &setdebuglist, &showdebuglist);
9291a0cd 24080
cd4fb1b2
SM
24081 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24082Set debugging of the DWARF DIE reader."), _("\
24083Show debugging of the DWARF DIE reader."), _("\
24084When enabled (non-zero), DIEs are dumped after they are read in.\n\
24085The value is the maximum depth to print."),
24086 NULL,
24087 NULL,
24088 &setdebuglist, &showdebuglist);
9291a0cd 24089
cd4fb1b2
SM
24090 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24091Set debugging of the dwarf line reader."), _("\
24092Show debugging of the dwarf line reader."), _("\
24093When enabled (non-zero), line number entries are dumped as they are read in.\n\
24094A value of 1 (one) provides basic information.\n\
24095A value greater than 1 provides more verbose information."),
24096 NULL,
24097 NULL,
24098 &setdebuglist, &showdebuglist);
437afbb8 24099
cd4fb1b2
SM
24100 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24101Set cross-checking of \"physname\" code against demangler."), _("\
24102Show cross-checking of \"physname\" code against demangler."), _("\
24103When enabled, GDB's internal \"physname\" code is checked against\n\
24104the demangler."),
24105 NULL, show_check_physname,
24106 &setdebuglist, &showdebuglist);
900e11f9 24107
e615022a
DE
24108 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24109 no_class, &use_deprecated_index_sections, _("\
24110Set whether to use deprecated gdb_index sections."), _("\
24111Show whether to use deprecated gdb_index sections."), _("\
24112When enabled, deprecated .gdb_index sections are used anyway.\n\
24113Normally they are ignored either because of a missing feature or\n\
24114performance issue.\n\
24115Warning: This option must be enabled before gdb reads the file."),
24116 NULL,
24117 NULL,
24118 &setlist, &showlist);
24119
f1e6e072
TT
24120 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24121 &dwarf2_locexpr_funcs);
24122 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24123 &dwarf2_loclist_funcs);
24124
24125 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24126 &dwarf2_block_frame_base_locexpr_funcs);
24127 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24128 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24129
24130#if GDB_SELF_TEST
24131 selftests::register_test ("dw2_expand_symtabs_matching",
24132 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
24133 selftests::register_test ("dwarf2_find_containing_comp_unit",
24134 selftests::find_containing_comp_unit::run_test);
c62446b1 24135#endif
6502dd73 24136}
This page took 6.539197 seconds and 4 git commands to generate.