[gdb/symtab] Fix name lookup in dw2_map_matching_symbols
[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
TV
3780 {
3781 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3782 here though if the current language is Ada for a non-Ada objfile
9a0bacfb 3783 using GNU index. */
976ca316 3784 mapped_index &index = *per_objfile->per_bfd->index_table;
1aa98955 3785
9a0bacfb
TV
3786 const char *match_name = name.ada ().lookup_name ().c_str ();
3787 auto matcher = [&] (const char *symname)
3788 {
3789 if (ordered_compare == nullptr)
3790 return true;
3791 return ordered_compare (symname, match_name) == 0;
3792 };
3793
3794 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3795 [&] (offset_type namei)
3796 {
3797 struct dw2_symtab_iterator iter;
3798 struct dwarf2_per_cu_data *per_cu;
3799
976ca316 3800 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
e5f3ece2 3801 namei);
9a0bacfb 3802 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3803 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 3804 nullptr);
9a0bacfb 3805 return true;
976ca316 3806 }, per_objfile);
9a0bacfb
TV
3807 }
3808 else
3809 {
3810 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3811 proceed assuming all symtabs have been read in. */
3812 }
1aa98955
TV
3813
3814 for (compunit_symtab *cust : objfile->compunits ())
3815 {
3816 const struct block *block;
3817
3818 if (cust == NULL)
3819 continue;
3820 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3821 if (!iterate_over_symbols_terminated (block, name,
3822 domain, callback))
3823 return;
3824 }
9291a0cd
TT
3825}
3826
e1ef7d7a
PA
3827/* Starting from a search name, return the string that finds the upper
3828 bound of all strings that start with SEARCH_NAME in a sorted name
3829 list. Returns the empty string to indicate that the upper bound is
3830 the end of the list. */
3831
3832static std::string
3833make_sort_after_prefix_name (const char *search_name)
3834{
3835 /* When looking to complete "func", we find the upper bound of all
3836 symbols that start with "func" by looking for where we'd insert
3837 the closest string that would follow "func" in lexicographical
3838 order. Usually, that's "func"-with-last-character-incremented,
3839 i.e. "fund". Mind non-ASCII characters, though. Usually those
3840 will be UTF-8 multi-byte sequences, but we can't be certain.
3841 Especially mind the 0xff character, which is a valid character in
3842 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3843 rule out compilers allowing it in identifiers. Note that
3844 conveniently, strcmp/strcasecmp are specified to compare
3845 characters interpreted as unsigned char. So what we do is treat
3846 the whole string as a base 256 number composed of a sequence of
3847 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3848 to 0, and carries 1 to the following more-significant position.
3849 If the very first character in SEARCH_NAME ends up incremented
3850 and carries/overflows, then the upper bound is the end of the
3851 list. The string after the empty string is also the empty
3852 string.
3853
3854 Some examples of this operation:
3855
3856 SEARCH_NAME => "+1" RESULT
3857
3858 "abc" => "abd"
3859 "ab\xff" => "ac"
3860 "\xff" "a" "\xff" => "\xff" "b"
3861 "\xff" => ""
3862 "\xff\xff" => ""
3863 "" => ""
3864
3865 Then, with these symbols for example:
3866
3867 func
3868 func1
3869 fund
3870
3871 completing "func" looks for symbols between "func" and
3872 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3873 which finds "func" and "func1", but not "fund".
3874
3875 And with:
3876
3877 funcÿ (Latin1 'ÿ' [0xff])
3878 funcÿ1
3879 fund
3880
3881 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3882 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3883
3884 And with:
3885
3886 ÿÿ (Latin1 'ÿ' [0xff])
3887 ÿÿ1
3888
3889 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3890 the end of the list.
3891 */
3892 std::string after = search_name;
3893 while (!after.empty () && (unsigned char) after.back () == 0xff)
3894 after.pop_back ();
3895 if (!after.empty ())
3896 after.back () = (unsigned char) after.back () + 1;
3897 return after;
3898}
3899
5c58de74 3900/* See declaration. */
61d96d7e 3901
5c58de74
PA
3902std::pair<std::vector<name_component>::const_iterator,
3903 std::vector<name_component>::const_iterator>
44ed8f3e 3904mapped_index_base::find_name_components_bounds
fcf23d5b
SM
3905 (const lookup_name_info &lookup_name_without_params, language lang,
3906 dwarf2_per_objfile *per_objfile) const
3f563c84 3907{
5c58de74
PA
3908 auto *name_cmp
3909 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3910
3b00ef10 3911 const char *lang_name
e0802d59 3912 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3913
3f563c84
PA
3914 /* Comparison function object for lower_bound that matches against a
3915 given symbol name. */
3916 auto lookup_compare_lower = [&] (const name_component &elem,
3917 const char *name)
3918 {
fcf23d5b 3919 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3920 const char *elem_name = elem_qualified + elem.name_offset;
3921 return name_cmp (elem_name, name) < 0;
3922 };
3923
3924 /* Comparison function object for upper_bound that matches against a
3925 given symbol name. */
3926 auto lookup_compare_upper = [&] (const char *name,
3927 const name_component &elem)
3928 {
fcf23d5b 3929 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3930 const char *elem_name = elem_qualified + elem.name_offset;
3931 return name_cmp (name, elem_name) < 0;
3932 };
3933
5c58de74
PA
3934 auto begin = this->name_components.begin ();
3935 auto end = this->name_components.end ();
3f563c84
PA
3936
3937 /* Find the lower bound. */
3938 auto lower = [&] ()
3939 {
3b00ef10 3940 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3941 return begin;
3942 else
3b00ef10 3943 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3944 } ();
3945
3946 /* Find the upper bound. */
3947 auto upper = [&] ()
3948 {
5c58de74 3949 if (lookup_name_without_params.completion_mode ())
3f563c84 3950 {
e1ef7d7a
PA
3951 /* In completion mode, we want UPPER to point past all
3952 symbols names that have the same prefix. I.e., with
3953 these symbols, and completing "func":
3954
3955 function << lower bound
3956 function1
3957 other_function << upper bound
3958
3959 We find the upper bound by looking for the insertion
3960 point of "func"-with-last-character-incremented,
3961 i.e. "fund". */
3b00ef10 3962 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3963 if (after.empty ())
3f563c84 3964 return end;
e6b2f5ef
PA
3965 return std::lower_bound (lower, end, after.c_str (),
3966 lookup_compare_lower);
3f563c84
PA
3967 }
3968 else
3b00ef10 3969 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3970 } ();
3971
5c58de74
PA
3972 return {lower, upper};
3973}
3974
3975/* See declaration. */
3976
3977void
fcf23d5b 3978mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
5c58de74
PA
3979{
3980 if (!this->name_components.empty ())
3981 return;
3982
3983 this->name_components_casing = case_sensitivity;
3984 auto *name_cmp
3985 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3986
3987 /* The code below only knows how to break apart components of C++
3988 symbol names (and other languages that use '::' as
3b00ef10 3989 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3990 auto count = this->symbol_name_count ();
3991 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3992 {
44ed8f3e 3993 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3994 continue;
3995
fcf23d5b 3996 const char *name = this->symbol_name_at (idx, per_objfile);
5c58de74
PA
3997
3998 /* Add each name component to the name component table. */
3999 unsigned int previous_len = 0;
3b00ef10
TT
4000
4001 if (strstr (name, "::") != nullptr)
4002 {
4003 for (unsigned int current_len = cp_find_first_component (name);
4004 name[current_len] != '\0';
4005 current_len += cp_find_first_component (name + current_len))
4006 {
4007 gdb_assert (name[current_len] == ':');
4008 this->name_components.push_back ({previous_len, idx});
4009 /* Skip the '::'. */
4010 current_len += 2;
4011 previous_len = current_len;
4012 }
4013 }
4014 else
5c58de74 4015 {
3b00ef10
TT
4016 /* Handle the Ada encoded (aka mangled) form here. */
4017 for (const char *iter = strstr (name, "__");
4018 iter != nullptr;
4019 iter = strstr (iter, "__"))
4020 {
4021 this->name_components.push_back ({previous_len, idx});
4022 iter += 2;
4023 previous_len = iter - name;
4024 }
5c58de74 4025 }
3b00ef10 4026
5c58de74
PA
4027 this->name_components.push_back ({previous_len, idx});
4028 }
4029
4030 /* Sort name_components elements by name. */
4031 auto name_comp_compare = [&] (const name_component &left,
4032 const name_component &right)
4033 {
fcf23d5b
SM
4034 const char *left_qualified
4035 = this->symbol_name_at (left.idx, per_objfile);
4036 const char *right_qualified
4037 = this->symbol_name_at (right.idx, per_objfile);
5c58de74
PA
4038
4039 const char *left_name = left_qualified + left.name_offset;
4040 const char *right_name = right_qualified + right.name_offset;
4041
4042 return name_cmp (left_name, right_name) < 0;
4043 };
4044
4045 std::sort (this->name_components.begin (),
4046 this->name_components.end (),
4047 name_comp_compare);
4048}
4049
4050/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4051 mapped_index_base instead of the containing objfile. This is split
4052 to a separate function in order to be able to unit test the
4053 name_components matching using a mock mapped_index_base. For each
5c58de74 4054 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4055 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4056
4057static void
4058dw2_expand_symtabs_matching_symbol
44ed8f3e 4059 (mapped_index_base &index,
5c58de74
PA
4060 const lookup_name_info &lookup_name_in,
4061 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4062 enum search_domain kind,
fcf23d5b
SM
4063 gdb::function_view<bool (offset_type)> match_callback,
4064 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4065{
4066 lookup_name_info lookup_name_without_params
4067 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4068
4069 /* Build the symbol name component sorted vector, if we haven't
4070 yet. */
fcf23d5b 4071 index.build_name_components (per_objfile);
5c58de74 4072
3f563c84
PA
4073 /* The same symbol may appear more than once in the range though.
4074 E.g., if we're looking for symbols that complete "w", and we have
4075 a symbol named "w1::w2", we'll find the two name components for
4076 that same symbol in the range. To be sure we only call the
4077 callback once per symbol, we first collect the symbol name
4078 indexes that matched in a temporary vector and ignore
4079 duplicates. */
4080 std::vector<offset_type> matches;
3f563c84 4081
3b00ef10
TT
4082 struct name_and_matcher
4083 {
4084 symbol_name_matcher_ftype *matcher;
ecc6c606 4085 const char *name;
3b00ef10
TT
4086
4087 bool operator== (const name_and_matcher &other) const
3f563c84 4088 {
ecc6c606 4089 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
4090 }
4091 };
4092
4093 /* A vector holding all the different symbol name matchers, for all
4094 languages. */
4095 std::vector<name_and_matcher> matchers;
4096
4097 for (int i = 0; i < nr_languages; i++)
4098 {
4099 enum language lang_e = (enum language) i;
4100
4101 const language_defn *lang = language_def (lang_e);
4102 symbol_name_matcher_ftype *name_matcher
4103 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4104
3b00ef10
TT
4105 name_and_matcher key {
4106 name_matcher,
4107 lookup_name_without_params.language_lookup_name (lang_e)
4108 };
4109
4110 /* Don't insert the same comparison routine more than once.
4111 Note that we do this linear walk. This is not a problem in
4112 practice because the number of supported languages is
4113 low. */
4114 if (std::find (matchers.begin (), matchers.end (), key)
4115 != matchers.end ())
9291a0cd 4116 continue;
3b00ef10
TT
4117 matchers.push_back (std::move (key));
4118
4119 auto bounds
4120 = index.find_name_components_bounds (lookup_name_without_params,
fcf23d5b 4121 lang_e, per_objfile);
3b00ef10
TT
4122
4123 /* Now for each symbol name in range, check to see if we have a name
4124 match, and if so, call the MATCH_CALLBACK callback. */
4125
4126 for (; bounds.first != bounds.second; ++bounds.first)
4127 {
fcf23d5b
SM
4128 const char *qualified
4129 = index.symbol_name_at (bounds.first->idx, per_objfile);
3b00ef10
TT
4130
4131 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4132 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4133 continue;
9291a0cd 4134
3b00ef10
TT
4135 matches.push_back (bounds.first->idx);
4136 }
3f563c84
PA
4137 }
4138
4139 std::sort (matches.begin (), matches.end ());
4140
4141 /* Finally call the callback, once per match. */
4142 ULONGEST prev = -1;
4143 for (offset_type idx : matches)
4144 {
4145 if (prev != idx)
4146 {
3b00ef10
TT
4147 if (!match_callback (idx))
4148 break;
3f563c84
PA
4149 prev = idx;
4150 }
4151 }
4152
4153 /* Above we use a type wider than idx's for 'prev', since 0 and
4154 (offset_type)-1 are both possible values. */
4155 static_assert (sizeof (prev) > sizeof (offset_type), "");
4156}
4157
c62446b1
PA
4158#if GDB_SELF_TEST
4159
4160namespace selftests { namespace dw2_expand_symtabs_matching {
4161
a3c5fafd
PA
4162/* A mock .gdb_index/.debug_names-like name index table, enough to
4163 exercise dw2_expand_symtabs_matching_symbol, which works with the
4164 mapped_index_base interface. Builds an index from the symbol list
4165 passed as parameter to the constructor. */
4166class mock_mapped_index : public mapped_index_base
c62446b1
PA
4167{
4168public:
a3c5fafd
PA
4169 mock_mapped_index (gdb::array_view<const char *> symbols)
4170 : m_symbol_table (symbols)
c62446b1
PA
4171 {}
4172
a3c5fafd 4173 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4174
a3c5fafd 4175 /* Return the number of names in the symbol table. */
632e107b 4176 size_t symbol_name_count () const override
c62446b1 4177 {
a3c5fafd 4178 return m_symbol_table.size ();
c62446b1
PA
4179 }
4180
a3c5fafd 4181 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
4182 const char *symbol_name_at
4183 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
a3c5fafd
PA
4184 {
4185 return m_symbol_table[idx];
4186 }
c62446b1 4187
a3c5fafd
PA
4188private:
4189 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4190};
4191
4192/* Convenience function that converts a NULL pointer to a "<null>"
4193 string, to pass to print routines. */
4194
4195static const char *
4196string_or_null (const char *str)
4197{
4198 return str != NULL ? str : "<null>";
4199}
4200
4201/* Check if a lookup_name_info built from
4202 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4203 index. EXPECTED_LIST is the list of expected matches, in expected
4204 matching order. If no match expected, then an empty list is
4205 specified. Returns true on success. On failure prints a warning
4206 indicating the file:line that failed, and returns false. */
4207
4208static bool
4209check_match (const char *file, int line,
4210 mock_mapped_index &mock_index,
4211 const char *name, symbol_name_match_type match_type,
4212 bool completion_mode,
fcf23d5b
SM
4213 std::initializer_list<const char *> expected_list,
4214 dwarf2_per_objfile *per_objfile)
c62446b1
PA
4215{
4216 lookup_name_info lookup_name (name, match_type, completion_mode);
4217
4218 bool matched = true;
4219
4220 auto mismatch = [&] (const char *expected_str,
4221 const char *got)
4222 {
4223 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4224 "expected=\"%s\", got=\"%s\"\n"),
4225 file, line,
4226 (match_type == symbol_name_match_type::FULL
4227 ? "FULL" : "WILD"),
4228 name, string_or_null (expected_str), string_or_null (got));
4229 matched = false;
4230 };
4231
4232 auto expected_it = expected_list.begin ();
4233 auto expected_end = expected_list.end ();
4234
a3c5fafd 4235 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4236 NULL, ALL_DOMAIN,
4237 [&] (offset_type idx)
4238 {
fcf23d5b 4239 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
c62446b1
PA
4240 const char *expected_str
4241 = expected_it == expected_end ? NULL : *expected_it++;
4242
4243 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4244 mismatch (expected_str, matched_name);
3b00ef10 4245 return true;
fcf23d5b 4246 }, per_objfile);
c62446b1
PA
4247
4248 const char *expected_str
4249 = expected_it == expected_end ? NULL : *expected_it++;
4250 if (expected_str != NULL)
4251 mismatch (expected_str, NULL);
4252
4253 return matched;
4254}
4255
4256/* The symbols added to the mock mapped_index for testing (in
4257 canonical form). */
4258static const char *test_symbols[] = {
4259 "function",
4260 "std::bar",
4261 "std::zfunction",
4262 "std::zfunction2",
4263 "w1::w2",
4264 "ns::foo<char*>",
4265 "ns::foo<int>",
4266 "ns::foo<long>",
a20714ff
PA
4267 "ns2::tmpl<int>::foo2",
4268 "(anonymous namespace)::A::B::C",
c62446b1 4269
e1ef7d7a
PA
4270 /* These are used to check that the increment-last-char in the
4271 matching algorithm for completion doesn't match "t1_fund" when
4272 completing "t1_func". */
4273 "t1_func",
4274 "t1_func1",
4275 "t1_fund",
4276 "t1_fund1",
4277
4278 /* A UTF-8 name with multi-byte sequences to make sure that
4279 cp-name-parser understands this as a single identifier ("função"
4280 is "function" in PT). */
4281 u8"u8função",
4282
4283 /* \377 (0xff) is Latin1 'ÿ'. */
4284 "yfunc\377",
4285
4286 /* \377 (0xff) is Latin1 'ÿ'. */
4287 "\377",
4288 "\377\377123",
4289
c62446b1
PA
4290 /* A name with all sorts of complications. Starts with "z" to make
4291 it easier for the completion tests below. */
4292#define Z_SYM_NAME \
4293 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4294 "::tuple<(anonymous namespace)::ui*, " \
4295 "std::default_delete<(anonymous namespace)::ui>, void>"
4296
4297 Z_SYM_NAME
4298};
4299
a3c5fafd
PA
4300/* Returns true if the mapped_index_base::find_name_component_bounds
4301 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4302 in completion mode. */
5c58de74
PA
4303
4304static bool
a3c5fafd 4305check_find_bounds_finds (mapped_index_base &index,
5c58de74 4306 const char *search_name,
fcf23d5b
SM
4307 gdb::array_view<const char *> expected_syms,
4308 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4309{
4310 lookup_name_info lookup_name (search_name,
4311 symbol_name_match_type::FULL, true);
4312
3b00ef10 4313 auto bounds = index.find_name_components_bounds (lookup_name,
fcf23d5b
SM
4314 language_cplus,
4315 per_objfile);
5c58de74
PA
4316
4317 size_t distance = std::distance (bounds.first, bounds.second);
4318 if (distance != expected_syms.size ())
4319 return false;
4320
4321 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4322 {
4323 auto nc_elem = bounds.first + exp_elem;
fcf23d5b 4324 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
5c58de74
PA
4325 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4326 return false;
4327 }
4328
4329 return true;
4330}
4331
4332/* Test the lower-level mapped_index::find_name_component_bounds
4333 method. */
4334
c62446b1 4335static void
5c58de74
PA
4336test_mapped_index_find_name_component_bounds ()
4337{
4338 mock_mapped_index mock_index (test_symbols);
4339
fcf23d5b 4340 mock_index.build_name_components (NULL /* per_objfile */);
5c58de74
PA
4341
4342 /* Test the lower-level mapped_index::find_name_component_bounds
4343 method in completion mode. */
4344 {
4345 static const char *expected_syms[] = {
4346 "t1_func",
4347 "t1_func1",
5c58de74
PA
4348 };
4349
fcf23d5b
SM
4350 SELF_CHECK (check_find_bounds_finds
4351 (mock_index, "t1_func", expected_syms,
4352 NULL /* per_objfile */));
5c58de74
PA
4353 }
4354
4355 /* Check that the increment-last-char in the name matching algorithm
4356 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4357 {
4358 static const char *expected_syms1[] = {
4359 "\377",
4360 "\377\377123",
4361 };
fcf23d5b
SM
4362 SELF_CHECK (check_find_bounds_finds
4363 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
5c58de74
PA
4364
4365 static const char *expected_syms2[] = {
4366 "\377\377123",
4367 };
fcf23d5b
SM
4368 SELF_CHECK (check_find_bounds_finds
4369 (mock_index, "\377\377", expected_syms2,
4370 NULL /* per_objfile */));
5c58de74
PA
4371 }
4372}
4373
4374/* Test dw2_expand_symtabs_matching_symbol. */
4375
4376static void
4377test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4378{
4379 mock_mapped_index mock_index (test_symbols);
4380
4381 /* We let all tests run until the end even if some fails, for debug
4382 convenience. */
4383 bool any_mismatch = false;
4384
4385 /* Create the expected symbols list (an initializer_list). Needed
4386 because lists have commas, and we need to pass them to CHECK,
4387 which is a macro. */
4388#define EXPECT(...) { __VA_ARGS__ }
4389
4390 /* Wrapper for check_match that passes down the current
4391 __FILE__/__LINE__. */
4392#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4393 any_mismatch |= !check_match (__FILE__, __LINE__, \
4394 mock_index, \
4395 NAME, MATCH_TYPE, COMPLETION_MODE, \
fcf23d5b 4396 EXPECTED_LIST, NULL)
c62446b1
PA
4397
4398 /* Identity checks. */
4399 for (const char *sym : test_symbols)
4400 {
4401 /* Should be able to match all existing symbols. */
4402 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4403 EXPECT (sym));
4404
4405 /* Should be able to match all existing symbols with
4406 parameters. */
4407 std::string with_params = std::string (sym) + "(int)";
4408 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4409 EXPECT (sym));
4410
4411 /* Should be able to match all existing symbols with
4412 parameters and qualifiers. */
4413 with_params = std::string (sym) + " ( int ) const";
4414 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4415 EXPECT (sym));
4416
4417 /* This should really find sym, but cp-name-parser.y doesn't
4418 know about lvalue/rvalue qualifiers yet. */
4419 with_params = std::string (sym) + " ( int ) &&";
4420 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4421 {});
4422 }
4423
e1ef7d7a
PA
4424 /* Check that the name matching algorithm for completion doesn't get
4425 confused with Latin1 'ÿ' / 0xff. */
4426 {
4427 static const char str[] = "\377";
4428 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4429 EXPECT ("\377", "\377\377123"));
4430 }
4431
4432 /* Check that the increment-last-char in the matching algorithm for
4433 completion doesn't match "t1_fund" when completing "t1_func". */
4434 {
4435 static const char str[] = "t1_func";
4436 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4437 EXPECT ("t1_func", "t1_func1"));
4438 }
4439
c62446b1
PA
4440 /* Check that completion mode works at each prefix of the expected
4441 symbol name. */
4442 {
4443 static const char str[] = "function(int)";
4444 size_t len = strlen (str);
4445 std::string lookup;
4446
4447 for (size_t i = 1; i < len; i++)
4448 {
4449 lookup.assign (str, i);
4450 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4451 EXPECT ("function"));
4452 }
4453 }
4454
4455 /* While "w" is a prefix of both components, the match function
4456 should still only be called once. */
4457 {
4458 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4459 EXPECT ("w1::w2"));
a20714ff
PA
4460 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4461 EXPECT ("w1::w2"));
c62446b1
PA
4462 }
4463
4464 /* Same, with a "complicated" symbol. */
4465 {
4466 static const char str[] = Z_SYM_NAME;
4467 size_t len = strlen (str);
4468 std::string lookup;
4469
4470 for (size_t i = 1; i < len; i++)
4471 {
4472 lookup.assign (str, i);
4473 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4474 EXPECT (Z_SYM_NAME));
4475 }
4476 }
4477
4478 /* In FULL mode, an incomplete symbol doesn't match. */
4479 {
4480 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4481 {});
4482 }
4483
4484 /* A complete symbol with parameters matches any overload, since the
4485 index has no overload info. */
4486 {
4487 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4488 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4489 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4490 EXPECT ("std::zfunction", "std::zfunction2"));
4491 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4492 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4493 }
4494
4495 /* Check that whitespace is ignored appropriately. A symbol with a
4496 template argument list. */
4497 {
4498 static const char expected[] = "ns::foo<int>";
4499 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4500 EXPECT (expected));
a20714ff
PA
4501 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4502 EXPECT (expected));
c62446b1
PA
4503 }
4504
4505 /* Check that whitespace is ignored appropriately. A symbol with a
4506 template argument list that includes a pointer. */
4507 {
4508 static const char expected[] = "ns::foo<char*>";
4509 /* Try both completion and non-completion modes. */
4510 static const bool completion_mode[2] = {false, true};
4511 for (size_t i = 0; i < 2; i++)
4512 {
4513 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4514 completion_mode[i], EXPECT (expected));
a20714ff
PA
4515 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4516 completion_mode[i], EXPECT (expected));
c62446b1
PA
4517
4518 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4519 completion_mode[i], EXPECT (expected));
a20714ff
PA
4520 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4521 completion_mode[i], EXPECT (expected));
c62446b1
PA
4522 }
4523 }
4524
4525 {
4526 /* Check method qualifiers are ignored. */
4527 static const char expected[] = "ns::foo<char*>";
4528 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4529 symbol_name_match_type::FULL, true, EXPECT (expected));
4530 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4531 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4532 CHECK_MATCH ("foo < char * > ( int ) const",
4533 symbol_name_match_type::WILD, true, EXPECT (expected));
4534 CHECK_MATCH ("foo < char * > ( int ) &&",
4535 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4536 }
4537
4538 /* Test lookup names that don't match anything. */
4539 {
a20714ff
PA
4540 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4541 {});
4542
c62446b1
PA
4543 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4544 {});
4545 }
4546
a20714ff
PA
4547 /* Some wild matching tests, exercising "(anonymous namespace)",
4548 which should not be confused with a parameter list. */
4549 {
4550 static const char *syms[] = {
4551 "A::B::C",
4552 "B::C",
4553 "C",
4554 "A :: B :: C ( int )",
4555 "B :: C ( int )",
4556 "C ( int )",
4557 };
4558
4559 for (const char *s : syms)
4560 {
4561 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4562 EXPECT ("(anonymous namespace)::A::B::C"));
4563 }
4564 }
4565
4566 {
4567 static const char expected[] = "ns2::tmpl<int>::foo2";
4568 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4569 EXPECT (expected));
4570 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4571 EXPECT (expected));
4572 }
4573
c62446b1
PA
4574 SELF_CHECK (!any_mismatch);
4575
4576#undef EXPECT
4577#undef CHECK_MATCH
4578}
4579
5c58de74
PA
4580static void
4581run_test ()
4582{
4583 test_mapped_index_find_name_component_bounds ();
4584 test_dw2_expand_symtabs_matching_symbol ();
4585}
4586
c62446b1
PA
4587}} // namespace selftests::dw2_expand_symtabs_matching
4588
4589#endif /* GDB_SELF_TEST */
4590
4b514bc8
JK
4591/* If FILE_MATCHER is NULL or if PER_CU has
4592 dwarf2_per_cu_quick_data::MARK set (see
4593 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4594 EXPANSION_NOTIFY on it. */
4595
4596static void
4597dw2_expand_symtabs_matching_one
97a1449a
SM
4598 (dwarf2_per_cu_data *per_cu,
4599 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4600 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4601 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4602{
4603 if (file_matcher == NULL || per_cu->v.quick->mark)
4604 {
af758d11 4605 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4606
97a1449a
SM
4607 compunit_symtab *symtab
4608 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4609 gdb_assert (symtab != nullptr);
4b514bc8 4610
af758d11
SM
4611 if (expansion_notify != NULL && symtab_was_null)
4612 expansion_notify (symtab);
4b514bc8
JK
4613 }
4614}
4615
3f563c84
PA
4616/* Helper for dw2_expand_matching symtabs. Called on each symbol
4617 matched, to expand corresponding CUs that were marked. IDX is the
4618 index of the symbol name that matched. */
4619
4620static void
4621dw2_expand_marked_cus
976ca316 4622 (dwarf2_per_objfile *per_objfile, offset_type idx,
3f563c84
PA
4623 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4624 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4625 search_domain kind)
4626{
3f563c84
PA
4627 offset_type *vec, vec_len, vec_idx;
4628 bool global_seen = false;
976ca316 4629 mapped_index &index = *per_objfile->per_bfd->index_table;
3f563c84 4630
61920122 4631 vec = (offset_type *) (index.constant_pool
f00a2de2 4632 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4633 vec_len = MAYBE_SWAP (vec[0]);
4634 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4635 {
61920122
PA
4636 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4637 /* This value is only valid for index versions >= 7. */
4638 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4639 gdb_index_symbol_kind symbol_kind =
4640 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4641 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4642 /* Only check the symbol attributes if they're present.
4643 Indices prior to version 7 don't record them,
4644 and indices >= 7 may elide them for certain symbols
4645 (gold does this). */
4646 int attrs_valid =
4647 (index.version >= 7
4648 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4649
4650 /* Work around gold/15646. */
f030440d
TV
4651 if (attrs_valid
4652 && !is_static
4653 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
9291a0cd 4654 {
f030440d 4655 if (global_seen)
61920122 4656 continue;
f030440d
TV
4657
4658 global_seen = true;
61920122 4659 }
3190f0c6 4660
61920122
PA
4661 /* Only check the symbol's kind if it has one. */
4662 if (attrs_valid)
4663 {
4664 switch (kind)
8943b874 4665 {
61920122
PA
4666 case VARIABLES_DOMAIN:
4667 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4668 continue;
4669 break;
4670 case FUNCTIONS_DOMAIN:
4671 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4672 continue;
61920122
PA
4673 break;
4674 case TYPES_DOMAIN:
4675 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4676 continue;
4677 break;
59c35742
AB
4678 case MODULES_DOMAIN:
4679 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4680 continue;
4681 break;
61920122
PA
4682 default:
4683 break;
8943b874 4684 }
61920122 4685 }
8943b874 4686
61920122 4687 /* Don't crash on bad data. */
976ca316
SM
4688 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4689 + per_objfile->per_bfd->all_type_units.size ()))
61920122 4690 {
b98664d3 4691 complaint (_(".gdb_index entry has bad CU index"
976ca316 4692 " [in module %s]"), objfile_name (per_objfile->objfile));
61920122
PA
4693 continue;
4694 }
4695
976ca316
SM
4696 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4697 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4b514bc8 4698 expansion_notify);
61920122
PA
4699 }
4700}
4701
4b514bc8
JK
4702/* If FILE_MATCHER is non-NULL, set all the
4703 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4704 that match FILE_MATCHER. */
4705
61920122 4706static void
4b514bc8 4707dw_expand_symtabs_matching_file_matcher
976ca316 4708 (dwarf2_per_objfile *per_objfile,
ed2dc618 4709 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4710{
4b514bc8 4711 if (file_matcher == NULL)
61920122
PA
4712 return;
4713
4b514bc8
JK
4714 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4715 htab_eq_pointer,
4716 NULL, xcalloc, xfree));
4717 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4718 htab_eq_pointer,
4719 NULL, xcalloc, xfree));
61920122 4720
4b514bc8
JK
4721 /* The rule is CUs specify all the files, including those used by
4722 any TU, so there's no need to scan TUs here. */
61920122 4723
976ca316 4724 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4725 {
927aa2e7
JK
4726 QUIT;
4727
4728 per_cu->v.quick->mark = 0;
4729
4730 /* We only need to look at symtabs not already expanded. */
976ca316 4731 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4732 continue;
4733
976ca316 4734 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4735 if (file_data == NULL)
4736 continue;
4737
4738 if (htab_find (visited_not_found.get (), file_data) != NULL)
4739 continue;
4740 else if (htab_find (visited_found.get (), file_data) != NULL)
4741 {
4742 per_cu->v.quick->mark = 1;
4743 continue;
4744 }
4745
b76e467d 4746 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4747 {
4748 const char *this_real_name;
4749
4750 if (file_matcher (file_data->file_names[j], false))
4751 {
4752 per_cu->v.quick->mark = 1;
4753 break;
4754 }
4755
4756 /* Before we invoke realpath, which can get expensive when many
4757 files are involved, do a quick comparison of the basenames. */
4758 if (!basenames_may_differ
4759 && !file_matcher (lbasename (file_data->file_names[j]),
4760 true))
4761 continue;
4762
976ca316 4763 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
927aa2e7
JK
4764 if (file_matcher (this_real_name, false))
4765 {
4766 per_cu->v.quick->mark = 1;
4767 break;
4768 }
4769 }
4770
b76e467d
SM
4771 void **slot = htab_find_slot (per_cu->v.quick->mark
4772 ? visited_found.get ()
4773 : visited_not_found.get (),
4774 file_data, INSERT);
927aa2e7
JK
4775 *slot = file_data;
4776 }
4777}
4778
4779static void
4780dw2_expand_symtabs_matching
4781 (struct objfile *objfile,
4782 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4783 const lookup_name_info *lookup_name,
927aa2e7
JK
4784 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4785 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4786 enum search_domain kind)
4787{
976ca316 4788 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4789
4790 /* index_table is NULL if OBJF_READNOW. */
976ca316 4791 if (!per_objfile->per_bfd->index_table)
927aa2e7
JK
4792 return;
4793
976ca316 4794 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
927aa2e7 4795
c1a66c06
TV
4796 if (symbol_matcher == NULL && lookup_name == NULL)
4797 {
976ca316 4798 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4799 {
4800 QUIT;
4801
976ca316 4802 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
97a1449a 4803 file_matcher, expansion_notify);
c1a66c06
TV
4804 }
4805 return;
4806 }
4807
976ca316 4808 mapped_index &index = *per_objfile->per_bfd->index_table;
927aa2e7 4809
c1a66c06 4810 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4811 symbol_matcher,
4812 kind, [&] (offset_type idx)
4813 {
976ca316
SM
4814 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4815 kind);
3b00ef10 4816 return true;
976ca316 4817 }, per_objfile);
927aa2e7
JK
4818}
4819
4820/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4821 symtab. */
4822
4823static struct compunit_symtab *
4824recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4825 CORE_ADDR pc)
4826{
4827 int i;
4828
4829 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4830 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4831 return cust;
4832
4833 if (cust->includes == NULL)
4834 return NULL;
4835
4836 for (i = 0; cust->includes[i]; ++i)
4837 {
4838 struct compunit_symtab *s = cust->includes[i];
4839
4840 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4841 if (s != NULL)
4842 return s;
4843 }
4844
4845 return NULL;
4846}
4847
4848static struct compunit_symtab *
4849dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4850 struct bound_minimal_symbol msymbol,
4851 CORE_ADDR pc,
4852 struct obj_section *section,
4853 int warn_if_readin)
4854{
4855 struct dwarf2_per_cu_data *data;
4856 struct compunit_symtab *result;
4857
d320c2b5 4858 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4859 return NULL;
4860
b3b3bada 4861 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4862 data = (struct dwarf2_per_cu_data *) addrmap_find
4863 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4864 if (!data)
4865 return NULL;
4866
af758d11
SM
4867 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4868 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4869 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4870 paddress (objfile->arch (), pc));
927aa2e7 4871
97a1449a
SM
4872 result = recursively_find_pc_sect_compunit_symtab
4873 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4874
927aa2e7
JK
4875 gdb_assert (result != NULL);
4876 return result;
4877}
4878
4879static void
4880dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4881 void *data, int need_fullname)
4882{
976ca316 4883 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7 4884
976ca316 4885 if (!per_objfile->per_bfd->filenames_cache)
927aa2e7 4886 {
976ca316 4887 per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
4888
4889 htab_up visited (htab_create_alloc (10,
4890 htab_hash_pointer, htab_eq_pointer,
4891 NULL, xcalloc, xfree));
4892
4893 /* The rule is CUs specify all the files, including those used
4894 by any TU, so there's no need to scan TUs here. We can
4895 ignore file names coming from already-expanded CUs. */
4896
976ca316 4897 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4898 {
976ca316 4899 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4900 {
4901 void **slot = htab_find_slot (visited.get (),
4902 per_cu->v.quick->file_names,
4903 INSERT);
4904
4905 *slot = per_cu->v.quick->file_names;
4906 }
4907 }
4908
976ca316 4909 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4910 {
927aa2e7 4911 /* We only need to look at symtabs not already expanded. */
976ca316 4912 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4913 continue;
4914
ab432490 4915 quick_file_names *file_data
976ca316 4916 = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4917 if (file_data == NULL)
4918 continue;
4919
b76e467d 4920 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4921 if (*slot)
4922 {
4923 /* Already visited. */
4924 continue;
4925 }
4926 *slot = file_data;
4927
4928 for (int j = 0; j < file_data->num_file_names; ++j)
4929 {
4930 const char *filename = file_data->file_names[j];
976ca316 4931 per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
4932 }
4933 }
4934 }
4935
976ca316 4936 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
4937 {
4938 gdb::unique_xmalloc_ptr<char> this_real_name;
4939
4940 if (need_fullname)
4941 this_real_name = gdb_realpath (filename);
4942 (*fun) (filename, this_real_name.get (), data);
4943 });
4944}
4945
4946static int
4947dw2_has_symbols (struct objfile *objfile)
4948{
4949 return 1;
4950}
4951
4952const struct quick_symbol_functions dwarf2_gdb_index_functions =
4953{
4954 dw2_has_symbols,
4955 dw2_find_last_source_symtab,
4956 dw2_forget_cached_source_info,
4957 dw2_map_symtabs_matching_filename,
4958 dw2_lookup_symbol,
d3214198 4959 NULL,
927aa2e7
JK
4960 dw2_print_stats,
4961 dw2_dump,
927aa2e7
JK
4962 dw2_expand_symtabs_for_function,
4963 dw2_expand_all_symtabs,
4964 dw2_expand_symtabs_with_fullname,
4965 dw2_map_matching_symbols,
4966 dw2_expand_symtabs_matching,
4967 dw2_find_pc_sect_compunit_symtab,
4968 NULL,
4969 dw2_map_symbol_filenames
4970};
4971
4972/* DWARF-5 debug_names reader. */
4973
4974/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4975static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4976
4977/* A helper function that reads the .debug_names section in SECTION
4978 and fills in MAP. FILENAME is the name of the file containing the
4979 section; it is used for error reporting.
4980
4981 Returns true if all went well, false otherwise. */
4982
4983static bool
4984read_debug_names_from_section (struct objfile *objfile,
4985 const char *filename,
4986 struct dwarf2_section_info *section,
4987 mapped_debug_names &map)
4988{
96b79293 4989 if (section->empty ())
927aa2e7
JK
4990 return false;
4991
4992 /* Older elfutils strip versions could keep the section in the main
4993 executable while splitting it for the separate debug info file. */
96b79293 4994 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4995 return false;
4996
96b79293 4997 section->read (objfile);
927aa2e7 4998
08feed99 4999 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
5000
5001 const gdb_byte *addr = section->buffer;
5002
96b79293 5003 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
5004
5005 unsigned int bytes_read;
5006 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5007 addr += bytes_read;
5008
5009 map.dwarf5_is_dwarf64 = bytes_read != 4;
5010 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5011 if (bytes_read + length != section->size)
5012 {
5013 /* There may be multiple per-CU indices. */
5014 warning (_("Section .debug_names in %s length %s does not match "
5015 "section length %s, ignoring .debug_names."),
5016 filename, plongest (bytes_read + length),
5017 pulongest (section->size));
5018 return false;
5019 }
5020
5021 /* The version number. */
5022 uint16_t version = read_2_bytes (abfd, addr);
5023 addr += 2;
5024 if (version != 5)
5025 {
5026 warning (_("Section .debug_names in %s has unsupported version %d, "
5027 "ignoring .debug_names."),
5028 filename, version);
5029 return false;
5030 }
5031
5032 /* Padding. */
5033 uint16_t padding = read_2_bytes (abfd, addr);
5034 addr += 2;
5035 if (padding != 0)
5036 {
5037 warning (_("Section .debug_names in %s has unsupported padding %d, "
5038 "ignoring .debug_names."),
5039 filename, padding);
5040 return false;
5041 }
5042
5043 /* comp_unit_count - The number of CUs in the CU list. */
5044 map.cu_count = read_4_bytes (abfd, addr);
5045 addr += 4;
5046
5047 /* local_type_unit_count - The number of TUs in the local TU
5048 list. */
5049 map.tu_count = read_4_bytes (abfd, addr);
5050 addr += 4;
5051
5052 /* foreign_type_unit_count - The number of TUs in the foreign TU
5053 list. */
5054 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5055 addr += 4;
5056 if (foreign_tu_count != 0)
5057 {
5058 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5059 "ignoring .debug_names."),
5060 filename, static_cast<unsigned long> (foreign_tu_count));
5061 return false;
5062 }
5063
5064 /* bucket_count - The number of hash buckets in the hash lookup
5065 table. */
5066 map.bucket_count = read_4_bytes (abfd, addr);
5067 addr += 4;
5068
5069 /* name_count - The number of unique names in the index. */
5070 map.name_count = read_4_bytes (abfd, addr);
5071 addr += 4;
5072
5073 /* abbrev_table_size - The size in bytes of the abbreviations
5074 table. */
5075 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5076 addr += 4;
5077
5078 /* augmentation_string_size - The size in bytes of the augmentation
5079 string. This value is rounded up to a multiple of 4. */
5080 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5081 addr += 4;
5082 map.augmentation_is_gdb = ((augmentation_string_size
5083 == sizeof (dwarf5_augmentation))
5084 && memcmp (addr, dwarf5_augmentation,
5085 sizeof (dwarf5_augmentation)) == 0);
5086 augmentation_string_size += (-augmentation_string_size) & 3;
5087 addr += augmentation_string_size;
5088
5089 /* List of CUs */
5090 map.cu_table_reordered = addr;
5091 addr += map.cu_count * map.offset_size;
5092
5093 /* List of Local TUs */
5094 map.tu_table_reordered = addr;
5095 addr += map.tu_count * map.offset_size;
5096
5097 /* Hash Lookup Table */
5098 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5099 addr += map.bucket_count * 4;
5100 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5101 addr += map.name_count * 4;
5102
5103 /* Name Table */
5104 map.name_table_string_offs_reordered = addr;
5105 addr += map.name_count * map.offset_size;
5106 map.name_table_entry_offs_reordered = addr;
5107 addr += map.name_count * map.offset_size;
5108
5109 const gdb_byte *abbrev_table_start = addr;
5110 for (;;)
5111 {
927aa2e7
JK
5112 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5113 addr += bytes_read;
5114 if (index_num == 0)
5115 break;
5116
5117 const auto insertpair
5118 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5119 if (!insertpair.second)
5120 {
5121 warning (_("Section .debug_names in %s has duplicate index %s, "
5122 "ignoring .debug_names."),
5123 filename, pulongest (index_num));
5124 return false;
5125 }
5126 mapped_debug_names::index_val &indexval = insertpair.first->second;
5127 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5128 addr += bytes_read;
5129
5130 for (;;)
5131 {
5132 mapped_debug_names::index_val::attr attr;
5133 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5134 addr += bytes_read;
5135 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5136 addr += bytes_read;
5137 if (attr.form == DW_FORM_implicit_const)
5138 {
5139 attr.implicit_const = read_signed_leb128 (abfd, addr,
5140 &bytes_read);
5141 addr += bytes_read;
5142 }
5143 if (attr.dw_idx == 0 && attr.form == 0)
5144 break;
5145 indexval.attr_vec.push_back (std::move (attr));
5146 }
5147 }
5148 if (addr != abbrev_table_start + abbrev_table_size)
5149 {
5150 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5151 "of size %s vs. written as %u, ignoring .debug_names."),
5152 filename, plongest (addr - abbrev_table_start),
5153 abbrev_table_size);
927aa2e7
JK
5154 return false;
5155 }
5156 map.entry_pool = addr;
5157
5158 return true;
5159}
5160
5161/* A helper for create_cus_from_debug_names that handles the MAP's CU
5162 list. */
5163
5164static void
168c9250 5165create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5166 const mapped_debug_names &map,
5167 dwarf2_section_info &section,
b76e467d 5168 bool is_dwz)
927aa2e7 5169{
3ee6bb11
TV
5170 if (!map.augmentation_is_gdb)
5171 {
5172 for (uint32_t i = 0; i < map.cu_count; ++i)
5173 {
5174 sect_offset sect_off
5175 = (sect_offset) (extract_unsigned_integer
5176 (map.cu_table_reordered + i * map.offset_size,
5177 map.offset_size,
5178 map.dwarf5_byte_order));
5179 /* We don't know the length of the CU, because the CU list in a
5180 .debug_names index can be incomplete, so we can't use the start of
5181 the next CU as end of this CU. We create the CUs here with length 0,
5182 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5183 dwarf2_per_cu_data *per_cu
168c9250
SM
5184 = create_cu_from_index_list (per_bfd, &section, is_dwz, sect_off, 0);
5185 per_bfd->all_comp_units.push_back (per_cu);
3ee6bb11
TV
5186 }
5187 }
5188
927aa2e7
JK
5189 sect_offset sect_off_prev;
5190 for (uint32_t i = 0; i <= map.cu_count; ++i)
5191 {
5192 sect_offset sect_off_next;
5193 if (i < map.cu_count)
5194 {
5195 sect_off_next
5196 = (sect_offset) (extract_unsigned_integer
5197 (map.cu_table_reordered + i * map.offset_size,
5198 map.offset_size,
5199 map.dwarf5_byte_order));
5200 }
5201 else
5202 sect_off_next = (sect_offset) section.size;
5203 if (i >= 1)
5204 {
5205 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5206 dwarf2_per_cu_data *per_cu
168c9250 5207 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 5208 sect_off_prev, length);
168c9250 5209 per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5210 }
5211 sect_off_prev = sect_off_next;
5212 }
5213}
5214
5215/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5216 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5217
5218static void
168c9250 5219create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5220 const mapped_debug_names &map,
5221 const mapped_debug_names &dwz_map)
5222{
168c9250
SM
5223 gdb_assert (per_bfd->all_comp_units.empty ());
5224 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5225
168c9250 5226 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 5227 false /* is_dwz */);
927aa2e7
JK
5228
5229 if (dwz_map.cu_count == 0)
5230 return;
5231
168c9250
SM
5232 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5233 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 5234 true /* is_dwz */);
927aa2e7
JK
5235}
5236
5237/* Read .debug_names. If everything went ok, initialize the "quick"
5238 elements of all the CUs and return true. Otherwise, return false. */
5239
5240static bool
976ca316 5241dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
927aa2e7 5242{
fcf23d5b
SM
5243 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5244 mapped_debug_names dwz_map;
976ca316 5245 struct objfile *objfile = per_objfile->objfile;
f8c41851 5246 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
5247
5248 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
976ca316 5249 &per_objfile->per_bfd->debug_names, *map))
927aa2e7
JK
5250 return false;
5251
5252 /* Don't use the index if it's empty. */
22ca247e 5253 if (map->name_count == 0)
927aa2e7
JK
5254 return false;
5255
5256 /* If there is a .dwz file, read it so we can get its CU list as
5257 well. */
f8c41851 5258 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
927aa2e7
JK
5259 if (dwz != NULL)
5260 {
5261 if (!read_debug_names_from_section (objfile,
00f93c44 5262 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5263 &dwz->debug_names, dwz_map))
5264 {
5265 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5266 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5267 return false;
5268 }
5269 }
5270
f8c41851 5271 create_cus_from_debug_names (per_bfd, *map, dwz_map);
927aa2e7 5272
22ca247e 5273 if (map->tu_count != 0)
927aa2e7
JK
5274 {
5275 /* We can only handle a single .debug_types when we have an
5276 index. */
f8c41851 5277 if (per_bfd->types.size () != 1)
927aa2e7
JK
5278 return false;
5279
f8c41851 5280 dwarf2_section_info *section = &per_bfd->types[0];
927aa2e7
JK
5281
5282 create_signatured_type_table_from_debug_names
f8c41851 5283 (per_objfile, *map, section, &per_bfd->abbrev);
927aa2e7
JK
5284 }
5285
f8c41851 5286 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
927aa2e7 5287
f8c41851
SM
5288 per_bfd->debug_names_table = std::move (map);
5289 per_bfd->using_index = 1;
5290 per_bfd->quick_file_names_table =
976ca316 5291 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
927aa2e7 5292
f8c41851
SM
5293 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
5294 objfiles using the same BFD. */
5295 gdb_assert (per_bfd->partial_symtabs == nullptr);
5296 per_bfd->partial_symtabs = objfile->partial_symtabs;
5297
927aa2e7
JK
5298 return true;
5299}
5300
927aa2e7
JK
5301/* Type used to manage iterating over all CUs looking for a symbol for
5302 .debug_names. */
5303
5304class dw2_debug_names_iterator
5305{
5306public:
927aa2e7 5307 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5308 gdb::optional<block_enum> block_index,
5309 domain_enum domain,
fcf23d5b 5310 const char *name, dwarf2_per_objfile *per_objfile)
2b79f376 5311 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5312 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5313 m_per_objfile (per_objfile)
927aa2e7
JK
5314 {}
5315
5316 dw2_debug_names_iterator (const mapped_debug_names &map,
fcf23d5b 5317 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5318 : m_map (map),
5319 m_search (search),
fcf23d5b
SM
5320 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5321 m_per_objfile (per_objfile)
927aa2e7
JK
5322 {}
5323
3b00ef10
TT
5324 dw2_debug_names_iterator (const mapped_debug_names &map,
5325 block_enum block_index, domain_enum domain,
fcf23d5b 5326 uint32_t namei, dwarf2_per_objfile *per_objfile)
3b00ef10 5327 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5328 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5329 m_per_objfile (per_objfile)
3b00ef10
TT
5330 {}
5331
927aa2e7
JK
5332 /* Return the next matching CU or NULL if there are no more. */
5333 dwarf2_per_cu_data *next ();
5334
5335private:
5336 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5337 const char *name,
5338 dwarf2_per_objfile *per_objfile);
927aa2e7 5339 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5340 uint32_t namei,
5341 dwarf2_per_objfile *per_objfile);
927aa2e7
JK
5342
5343 /* The internalized form of .debug_names. */
5344 const mapped_debug_names &m_map;
5345
2b79f376
SM
5346 /* If set, only look for symbols that match that block. Valid values are
5347 GLOBAL_BLOCK and STATIC_BLOCK. */
5348 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5349
5350 /* The kind of symbol we're looking for. */
5351 const domain_enum m_domain = UNDEF_DOMAIN;
5352 const search_domain m_search = ALL_DOMAIN;
5353
5354 /* The list of CUs from the index entry of the symbol, or NULL if
5355 not found. */
5356 const gdb_byte *m_addr;
fcf23d5b
SM
5357
5358 dwarf2_per_objfile *m_per_objfile;
927aa2e7
JK
5359};
5360
5361const char *
fcf23d5b 5362mapped_debug_names::namei_to_name
976ca316 5363 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
927aa2e7
JK
5364{
5365 const ULONGEST namei_string_offs
5366 = extract_unsigned_integer ((name_table_string_offs_reordered
5367 + namei * offset_size),
5368 offset_size,
5369 dwarf5_byte_order);
976ca316 5370 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
927aa2e7
JK
5371}
5372
5373/* Find a slot in .debug_names for the object named NAME. If NAME is
5374 found, return pointer to its pool data. If NAME cannot be found,
5375 return NULL. */
5376
5377const gdb_byte *
5378dw2_debug_names_iterator::find_vec_in_debug_names
976ca316
SM
5379 (const mapped_debug_names &map, const char *name,
5380 dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5381{
5382 int (*cmp) (const char *, const char *);
5383
54ee4252 5384 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5385 if (current_language->la_language == language_cplus
5386 || current_language->la_language == language_fortran
5387 || current_language->la_language == language_d)
5388 {
5389 /* NAME is already canonical. Drop any qualifiers as
5390 .debug_names does not contain any. */
5391
5392 if (strchr (name, '(') != NULL)
5393 {
54ee4252 5394 without_params = cp_remove_params (name);
927aa2e7 5395 if (without_params != NULL)
54ee4252 5396 name = without_params.get ();
927aa2e7
JK
5397 }
5398 }
5399
5400 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5401
5402 const uint32_t full_hash = dwarf5_djb_hash (name);
5403 uint32_t namei
5404 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5405 (map.bucket_table_reordered
5406 + (full_hash % map.bucket_count)), 4,
5407 map.dwarf5_byte_order);
5408 if (namei == 0)
5409 return NULL;
5410 --namei;
5411 if (namei >= map.name_count)
5412 {
b98664d3 5413 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5414 "[in module %s]"),
5415 namei, map.name_count,
fcf23d5b 5416 objfile_name (per_objfile->objfile));
927aa2e7
JK
5417 return NULL;
5418 }
5419
5420 for (;;)
5421 {
5422 const uint32_t namei_full_hash
5423 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5424 (map.hash_table_reordered + namei), 4,
5425 map.dwarf5_byte_order);
5426 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5427 return NULL;
5428
5429 if (full_hash == namei_full_hash)
5430 {
fcf23d5b 5431 const char *const namei_string = map.namei_to_name (namei, per_objfile);
927aa2e7
JK
5432
5433#if 0 /* An expensive sanity check. */
5434 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5435 {
b98664d3 5436 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5437 "[in module %s]"),
5438 namei, objfile_name (dwarf2_per_objfile->objfile));
5439 return NULL;
5440 }
5441#endif
5442
5443 if (cmp (namei_string, name) == 0)
5444 {
5445 const ULONGEST namei_entry_offs
5446 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5447 + namei * map.offset_size),
5448 map.offset_size, map.dwarf5_byte_order);
5449 return map.entry_pool + namei_entry_offs;
5450 }
5451 }
5452
5453 ++namei;
5454 if (namei >= map.name_count)
5455 return NULL;
5456 }
5457}
5458
5459const gdb_byte *
5460dw2_debug_names_iterator::find_vec_in_debug_names
fcf23d5b 5461 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5462{
5463 if (namei >= map.name_count)
5464 {
b98664d3 5465 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5466 "[in module %s]"),
5467 namei, map.name_count,
fcf23d5b 5468 objfile_name (per_objfile->objfile));
927aa2e7
JK
5469 return NULL;
5470 }
5471
5472 const ULONGEST namei_entry_offs
5473 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5474 + namei * map.offset_size),
5475 map.offset_size, map.dwarf5_byte_order);
5476 return map.entry_pool + namei_entry_offs;
5477}
5478
5479/* See dw2_debug_names_iterator. */
5480
5481dwarf2_per_cu_data *
5482dw2_debug_names_iterator::next ()
5483{
5484 if (m_addr == NULL)
5485 return NULL;
5486
fcf23d5b
SM
5487 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5488 struct objfile *objfile = m_per_objfile->objfile;
ed2dc618 5489 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5490
5491 again:
5492
5493 unsigned int bytes_read;
5494 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5495 m_addr += bytes_read;
5496 if (abbrev == 0)
5497 return NULL;
5498
5499 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5500 if (indexval_it == m_map.abbrev_map.cend ())
5501 {
b98664d3 5502 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5503 "[in module %s]"),
ed2dc618 5504 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5505 return NULL;
5506 }
5507 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5508 enum class symbol_linkage {
5509 unknown,
5510 static_,
5511 extern_,
23c13d42 5512 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5513 dwarf2_per_cu_data *per_cu = NULL;
5514 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5515 {
5516 ULONGEST ull;
5517 switch (attr.form)
5518 {
5519 case DW_FORM_implicit_const:
5520 ull = attr.implicit_const;
5521 break;
5522 case DW_FORM_flag_present:
5523 ull = 1;
5524 break;
5525 case DW_FORM_udata:
5526 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5527 m_addr += bytes_read;
5528 break;
6dc55ce9 5529 case DW_FORM_ref4:
5530 ull = read_4_bytes (abfd, m_addr);
5531 m_addr += 4;
5532 break;
5533 case DW_FORM_ref8:
5534 ull = read_8_bytes (abfd, m_addr);
5535 m_addr += 8;
5536 break;
5537 case DW_FORM_ref_sig8:
5538 ull = read_8_bytes (abfd, m_addr);
5539 m_addr += 8;
5540 break;
927aa2e7 5541 default:
b98664d3 5542 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5543 dwarf_form_name (attr.form),
ed2dc618 5544 objfile_name (objfile));
927aa2e7
JK
5545 return NULL;
5546 }
5547 switch (attr.dw_idx)
5548 {
5549 case DW_IDX_compile_unit:
5550 /* Don't crash on bad data. */
fcf23d5b 5551 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
927aa2e7 5552 {
b98664d3 5553 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5554 " [in module %s]"),
5555 pulongest (ull),
fcf23d5b 5556 objfile_name (objfile));
927aa2e7
JK
5557 continue;
5558 }
fcf23d5b 5559 per_cu = per_bfd->get_cutu (ull);
927aa2e7 5560 break;
8af5c486
JK
5561 case DW_IDX_type_unit:
5562 /* Don't crash on bad data. */
fcf23d5b 5563 if (ull >= per_bfd->all_type_units.size ())
8af5c486 5564 {
b98664d3 5565 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5566 " [in module %s]"),
5567 pulongest (ull),
fcf23d5b 5568 objfile_name (objfile));
8af5c486
JK
5569 continue;
5570 }
fcf23d5b 5571 per_cu = &per_bfd->get_tu (ull)->per_cu;
8af5c486 5572 break;
6dc55ce9 5573 case DW_IDX_die_offset:
5574 /* In a per-CU index (as opposed to a per-module index), index
5575 entries without CU attribute implicitly refer to the single CU. */
5576 if (per_cu == NULL)
fcf23d5b 5577 per_cu = per_bfd->get_cu (0);
6dc55ce9 5578 break;
927aa2e7
JK
5579 case DW_IDX_GNU_internal:
5580 if (!m_map.augmentation_is_gdb)
5581 break;
23c13d42 5582 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5583 break;
5584 case DW_IDX_GNU_external:
5585 if (!m_map.augmentation_is_gdb)
5586 break;
23c13d42 5587 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5588 break;
5589 }
5590 }
5591
5592 /* Skip if already read in. */
fcf23d5b 5593 if (m_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5594 goto again;
5595
5596 /* Check static vs global. */
23c13d42 5597 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5598 {
2b79f376 5599 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5600 const bool symbol_is_static =
5601 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5602 if (want_static != symbol_is_static)
2b79f376 5603 goto again;
927aa2e7
JK
5604 }
5605
5606 /* Match dw2_symtab_iter_next, symbol_kind
5607 and debug_names::psymbol_tag. */
5608 switch (m_domain)
5609 {
5610 case VAR_DOMAIN:
5611 switch (indexval.dwarf_tag)
5612 {
5613 case DW_TAG_variable:
5614 case DW_TAG_subprogram:
5615 /* Some types are also in VAR_DOMAIN. */
5616 case DW_TAG_typedef:
5617 case DW_TAG_structure_type:
5618 break;
5619 default:
5620 goto again;
5621 }
5622 break;
5623 case STRUCT_DOMAIN:
5624 switch (indexval.dwarf_tag)
5625 {
5626 case DW_TAG_typedef:
5627 case DW_TAG_structure_type:
5628 break;
5629 default:
5630 goto again;
5631 }
5632 break;
5633 case LABEL_DOMAIN:
5634 switch (indexval.dwarf_tag)
5635 {
5636 case 0:
5637 case DW_TAG_variable:
5638 break;
5639 default:
5640 goto again;
5641 }
5642 break;
59c35742
AB
5643 case MODULE_DOMAIN:
5644 switch (indexval.dwarf_tag)
5645 {
5646 case DW_TAG_module:
5647 break;
5648 default:
5649 goto again;
5650 }
5651 break;
927aa2e7
JK
5652 default:
5653 break;
5654 }
5655
5656 /* Match dw2_expand_symtabs_matching, symbol_kind and
5657 debug_names::psymbol_tag. */
5658 switch (m_search)
4b514bc8 5659 {
927aa2e7
JK
5660 case VARIABLES_DOMAIN:
5661 switch (indexval.dwarf_tag)
4b514bc8 5662 {
927aa2e7
JK
5663 case DW_TAG_variable:
5664 break;
5665 default:
5666 goto again;
4b514bc8 5667 }
927aa2e7
JK
5668 break;
5669 case FUNCTIONS_DOMAIN:
5670 switch (indexval.dwarf_tag)
4b514bc8 5671 {
927aa2e7
JK
5672 case DW_TAG_subprogram:
5673 break;
5674 default:
5675 goto again;
4b514bc8 5676 }
927aa2e7
JK
5677 break;
5678 case TYPES_DOMAIN:
5679 switch (indexval.dwarf_tag)
5680 {
5681 case DW_TAG_typedef:
5682 case DW_TAG_structure_type:
5683 break;
5684 default:
5685 goto again;
5686 }
5687 break;
59c35742
AB
5688 case MODULES_DOMAIN:
5689 switch (indexval.dwarf_tag)
5690 {
5691 case DW_TAG_module:
5692 break;
5693 default:
5694 goto again;
5695 }
927aa2e7
JK
5696 default:
5697 break;
4b514bc8 5698 }
927aa2e7
JK
5699
5700 return per_cu;
4b514bc8 5701}
61920122 5702
927aa2e7 5703static struct compunit_symtab *
c7f839cb 5704dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5705 const char *name, domain_enum domain)
4b514bc8 5706{
976ca316 5707 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
61920122 5708
976ca316 5709 const auto &mapp = per_objfile->per_bfd->debug_names_table;
927aa2e7 5710 if (!mapp)
61920122 5711 {
927aa2e7
JK
5712 /* index is NULL if OBJF_READNOW. */
5713 return NULL;
5714 }
5715 const auto &map = *mapp;
9291a0cd 5716
976ca316 5717 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
9703b513 5718
927aa2e7
JK
5719 struct compunit_symtab *stab_best = NULL;
5720 struct dwarf2_per_cu_data *per_cu;
5721 while ((per_cu = iter.next ()) != NULL)
5722 {
5723 struct symbol *sym, *with_opaque = NULL;
97a1449a 5724 compunit_symtab *stab
976ca316 5725 = dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7 5726 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5727 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5728
927aa2e7
JK
5729 sym = block_find_symbol (block, name, domain,
5730 block_find_non_opaque_type_preferred,
5731 &with_opaque);
9703b513 5732
927aa2e7
JK
5733 /* Some caution must be observed with overloaded functions and
5734 methods, since the index will not contain any overload
5735 information (but NAME might contain it). */
a3ec0bb1 5736
927aa2e7 5737 if (sym != NULL
987012b8 5738 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5739 return stab;
5740 if (with_opaque != NULL
987012b8 5741 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5742 stab_best = stab;
9703b513 5743
927aa2e7 5744 /* Keep looking through other CUs. */
9703b513
TT
5745 }
5746
927aa2e7 5747 return stab_best;
9703b513
TT
5748}
5749
927aa2e7
JK
5750/* This dumps minimal information about .debug_names. It is called
5751 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5752 uses this to verify that .debug_names has been loaded. */
9291a0cd 5753
927aa2e7
JK
5754static void
5755dw2_debug_names_dump (struct objfile *objfile)
5756{
976ca316 5757 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 5758
976ca316 5759 gdb_assert (per_objfile->per_bfd->using_index);
927aa2e7 5760 printf_filtered (".debug_names:");
976ca316 5761 if (per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5762 printf_filtered (" exists\n");
5763 else
5764 printf_filtered (" faked for \"readnow\"\n");
5765 printf_filtered ("\n");
9291a0cd
TT
5766}
5767
9291a0cd 5768static void
927aa2e7
JK
5769dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5770 const char *func_name)
9291a0cd 5771{
976ca316 5772 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 5773
976ca316
SM
5774 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5775 if (per_objfile->per_bfd->debug_names_table)
24c79950 5776 {
976ca316 5777 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
24c79950 5778
fcf23d5b 5779 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
976ca316 5780 per_objfile);
24c79950 5781
927aa2e7
JK
5782 struct dwarf2_per_cu_data *per_cu;
5783 while ((per_cu = iter.next ()) != NULL)
976ca316 5784 dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7
JK
5785 }
5786}
24c79950 5787
3b00ef10
TT
5788static void
5789dw2_debug_names_map_matching_symbols
5790 (struct objfile *objfile,
5791 const lookup_name_info &name, domain_enum domain,
5792 int global,
5793 gdb::function_view<symbol_found_callback_ftype> callback,
5794 symbol_compare_ftype *ordered_compare)
5795{
976ca316 5796 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3b00ef10
TT
5797
5798 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5799 if (!per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5800 return;
5801
976ca316 5802 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
3b00ef10
TT
5803 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5804
5805 const char *match_name = name.ada ().lookup_name ().c_str ();
5806 auto matcher = [&] (const char *symname)
5807 {
5808 if (ordered_compare == nullptr)
5809 return true;
5810 return ordered_compare (symname, match_name) == 0;
5811 };
5812
5813 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5814 [&] (offset_type namei)
5815 {
5816 /* The name was matched, now expand corresponding CUs that were
5817 marked. */
fcf23d5b 5818 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
976ca316 5819 per_objfile);
3b00ef10
TT
5820
5821 struct dwarf2_per_cu_data *per_cu;
5822 while ((per_cu = iter.next ()) != NULL)
976ca316 5823 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 5824 nullptr);
3b00ef10 5825 return true;
976ca316 5826 }, per_objfile);
3b00ef10
TT
5827
5828 /* It's a shame we couldn't do this inside the
5829 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5830 that have already been expanded. Instead, this loop matches what
5831 the psymtab code does. */
976ca316 5832 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3b00ef10 5833 {
976ca316 5834 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
af758d11 5835 if (symtab != nullptr)
3b00ef10
TT
5836 {
5837 const struct block *block
af758d11 5838 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
3b00ef10
TT
5839 if (!iterate_over_symbols_terminated (block, name,
5840 domain, callback))
5841 break;
5842 }
5843 }
5844}
5845
927aa2e7
JK
5846static void
5847dw2_debug_names_expand_symtabs_matching
5848 (struct objfile *objfile,
5849 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5850 const lookup_name_info *lookup_name,
927aa2e7
JK
5851 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5852 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5853 enum search_domain kind)
5854{
976ca316 5855 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 5856
927aa2e7 5857 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5858 if (!per_objfile->per_bfd->debug_names_table)
927aa2e7 5859 return;
9291a0cd 5860
976ca316 5861 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
24c79950 5862
c1a66c06
TV
5863 if (symbol_matcher == NULL && lookup_name == NULL)
5864 {
976ca316 5865 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5866 {
5867 QUIT;
5868
976ca316
SM
5869 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5870 expansion_notify);
c1a66c06
TV
5871 }
5872 return;
5873 }
5874
976ca316 5875 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
bbf2f4df 5876
c1a66c06 5877 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
5878 symbol_matcher,
5879 kind, [&] (offset_type namei)
927aa2e7 5880 {
927aa2e7
JK
5881 /* The name was matched, now expand corresponding CUs that were
5882 marked. */
976ca316 5883 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
bbf2f4df 5884
927aa2e7
JK
5885 struct dwarf2_per_cu_data *per_cu;
5886 while ((per_cu = iter.next ()) != NULL)
976ca316
SM
5887 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5888 expansion_notify);
3b00ef10 5889 return true;
976ca316 5890 }, per_objfile);
9291a0cd
TT
5891}
5892
927aa2e7 5893const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5894{
5895 dw2_has_symbols,
5896 dw2_find_last_source_symtab,
5897 dw2_forget_cached_source_info,
f8eba3c6 5898 dw2_map_symtabs_matching_filename,
927aa2e7 5899 dw2_debug_names_lookup_symbol,
d3214198 5900 NULL,
9291a0cd 5901 dw2_print_stats,
927aa2e7 5902 dw2_debug_names_dump,
927aa2e7 5903 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5904 dw2_expand_all_symtabs,
652a8996 5905 dw2_expand_symtabs_with_fullname,
3b00ef10 5906 dw2_debug_names_map_matching_symbols,
927aa2e7 5907 dw2_debug_names_expand_symtabs_matching,
43f3e411 5908 dw2_find_pc_sect_compunit_symtab,
71a3c369 5909 NULL,
9291a0cd
TT
5910 dw2_map_symbol_filenames
5911};
5912
4485a1c1 5913/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5914 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5915
5916template <typename T>
5917static gdb::array_view<const gdb_byte>
5918get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5919{
5920 dwarf2_section_info *section = &section_owner->gdb_index;
5921
96b79293 5922 if (section->empty ())
4485a1c1
SM
5923 return {};
5924
5925 /* Older elfutils strip versions could keep the section in the main
5926 executable while splitting it for the separate debug info file. */
96b79293 5927 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5928 return {};
5929
96b79293 5930 section->read (obj);
4485a1c1 5931
8bebfcda
PA
5932 /* dwarf2_section_info::size is a bfd_size_type, while
5933 gdb::array_view works with size_t. On 32-bit hosts, with
5934 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5935 is 32-bit. So we need an explicit narrowing conversion here.
5936 This is fine, because it's impossible to allocate or mmap an
5937 array/buffer larger than what size_t can represent. */
5938 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5939}
5940
87d6a7aa
SM
5941/* Lookup the index cache for the contents of the index associated to
5942 DWARF2_OBJ. */
5943
5944static gdb::array_view<const gdb_byte>
5989a64e 5945get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5946{
5947 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5948 if (build_id == nullptr)
5949 return {};
5950
5951 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5952 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5953}
5954
5955/* Same as the above, but for DWZ. */
5956
5957static gdb::array_view<const gdb_byte>
5958get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5959{
5960 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5961 if (build_id == nullptr)
5962 return {};
5963
5964 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5965}
5966
3c0aa29a 5967/* See symfile.h. */
9291a0cd 5968
3c0aa29a
PA
5969bool
5970dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5971{
976ca316
SM
5972 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5973 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
ed2dc618 5974
9291a0cd
TT
5975 /* If we're about to read full symbols, don't bother with the
5976 indices. In this case we also don't care if some other debug
5977 format is making psymtabs, because they are all about to be
5978 expanded anyway. */
5979 if ((objfile->flags & OBJF_READNOW))
5980 {
17ee85fc
TT
5981 /* When using READNOW, the using_index flag (set below) indicates that
5982 PER_BFD was already initialized, when we loaded some other objfile. */
5983 if (per_bfd->using_index)
5984 {
5985 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 5986 per_objfile->resize_symtabs ();
17ee85fc
TT
5987 return true;
5988 }
5989
5990 per_bfd->using_index = 1;
976ca316
SM
5991 create_all_comp_units (per_objfile);
5992 create_all_type_units (per_objfile);
17ee85fc
TT
5993 per_bfd->quick_file_names_table
5994 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
976ca316 5995 per_objfile->resize_symtabs ();
9291a0cd 5996
17ee85fc
TT
5997 for (int i = 0; i < (per_bfd->all_comp_units.size ()
5998 + per_bfd->all_type_units.size ()); ++i)
9291a0cd 5999 {
17ee85fc 6000 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
9291a0cd 6001
17ee85fc 6002 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
e254ef6a 6003 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6004 }
6005
6006 /* Return 1 so that gdb sees the "quick" functions. However,
6007 these functions will be no-ops because we will have expanded
6008 all symtabs. */
3c0aa29a
PA
6009 *index_kind = dw_index_kind::GDB_INDEX;
6010 return true;
9291a0cd
TT
6011 }
6012
17ee85fc
TT
6013 /* Was a debug names index already read when we processed an objfile sharing
6014 PER_BFD? */
6015 if (per_bfd->debug_names_table != nullptr)
6016 {
6017 *index_kind = dw_index_kind::DEBUG_NAMES;
f8c41851 6018 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6019 per_objfile->resize_symtabs ();
17ee85fc
TT
6020 return true;
6021 }
6022
6023 /* Was a GDB index already read when we processed an objfile sharing
6024 PER_BFD? */
6025 if (per_bfd->index_table != nullptr)
6026 {
6027 *index_kind = dw_index_kind::GDB_INDEX;
f8c41851 6028 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6029 per_objfile->resize_symtabs ();
17ee85fc
TT
6030 return true;
6031 }
6032
976ca316 6033 if (dwarf2_read_debug_names (per_objfile))
3c0aa29a
PA
6034 {
6035 *index_kind = dw_index_kind::DEBUG_NAMES;
976ca316 6036 per_objfile->resize_symtabs ();
3c0aa29a
PA
6037 return true;
6038 }
927aa2e7 6039
976ca316 6040 if (dwarf2_read_gdb_index (per_objfile,
5989a64e 6041 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 6042 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6043 {
6044 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6045 per_objfile->resize_symtabs ();
3c0aa29a
PA
6046 return true;
6047 }
9291a0cd 6048
87d6a7aa 6049 /* ... otherwise, try to find the index in the index cache. */
976ca316 6050 if (dwarf2_read_gdb_index (per_objfile,
87d6a7aa
SM
6051 get_gdb_index_contents_from_cache,
6052 get_gdb_index_contents_from_cache_dwz))
6053 {
6054 global_index_cache.hit ();
6055 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6056 per_objfile->resize_symtabs ();
87d6a7aa
SM
6057 return true;
6058 }
6059
6060 global_index_cache.miss ();
3c0aa29a 6061 return false;
9291a0cd
TT
6062}
6063
6064\f
6065
dce234bc
PP
6066/* Build a partial symbol table. */
6067
6068void
f29dff0a 6069dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6070{
976ca316
SM
6071 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6072 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
17ee85fc
TT
6073
6074 if (per_bfd->partial_symtabs != nullptr)
6075 {
6076 /* Partial symbols were already read, so now we can simply
6077 attach them. */
6078 objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6079 per_objfile->resize_symtabs ();
17ee85fc
TT
6080 return;
6081 }
c9bf0622 6082
6eee24ce 6083 init_psymbol_list (objfile, 1024);
c906108c 6084
a70b8144 6085 try
c9bf0622
TT
6086 {
6087 /* This isn't really ideal: all the data we allocate on the
6088 objfile's obstack is still uselessly kept around. However,
6089 freeing it seems unsafe. */
906768f9 6090 psymtab_discarder psymtabs (objfile);
976ca316 6091 dwarf2_build_psymtabs_hard (per_objfile);
906768f9 6092 psymtabs.keep ();
87d6a7aa 6093
976ca316 6094 per_objfile->resize_symtabs ();
af758d11 6095
87d6a7aa 6096 /* (maybe) store an index in the cache. */
976ca316 6097 global_index_cache.store (per_objfile);
c9bf0622 6098 }
230d2906 6099 catch (const gdb_exception_error &except)
492d29ea
PA
6100 {
6101 exception_print (gdb_stderr, except);
6102 }
17ee85fc
TT
6103
6104 /* Finish by setting the local reference to partial symtabs, so that
6105 we don't try to read them again if reading another objfile with the same
6106 BFD. If we can't in fact share, this won't make a difference anyway as
6107 the dwarf2_per_bfd object won't be shared. */
6108 per_bfd->partial_symtabs = objfile->partial_symtabs;
c906108c 6109}
c906108c 6110
3b80fe9b
DE
6111/* Find the base address of the compilation unit for range lists and
6112 location lists. It will normally be specified by DW_AT_low_pc.
6113 In DWARF-3 draft 4, the base address could be overridden by
6114 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6115 compilation units with discontinuous ranges. */
6116
6117static void
6118dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6119{
6120 struct attribute *attr;
6121
2b24b6e4 6122 cu->base_address.reset ();
3b80fe9b
DE
6123
6124 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6125 if (attr != nullptr)
2b24b6e4 6126 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6127 else
6128 {
6129 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6130 if (attr != nullptr)
2b24b6e4 6131 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6132 }
6133}
6134
36586728
TT
6135/* Helper function that returns the proper abbrev section for
6136 THIS_CU. */
6137
6138static struct dwarf2_section_info *
6139get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6140{
6141 struct dwarf2_section_info *abbrev;
c3699833 6142 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
6143
6144 if (this_cu->is_dwz)
c3699833 6145 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
36586728 6146 else
c3699833 6147 abbrev = &per_bfd->abbrev;
36586728
TT
6148
6149 return abbrev;
6150}
6151
f4dc4d17
DE
6152/* Fetch the abbreviation table offset from a comp or type unit header. */
6153
6154static sect_offset
976ca316 6155read_abbrev_offset (dwarf2_per_objfile *per_objfile,
ed2dc618 6156 struct dwarf2_section_info *section,
9c541725 6157 sect_offset sect_off)
f4dc4d17 6158{
96b79293 6159 bfd *abfd = section->get_bfd_owner ();
d521ce57 6160 const gdb_byte *info_ptr;
ac298888 6161 unsigned int initial_length_size, offset_size;
43988095 6162 uint16_t version;
f4dc4d17 6163
976ca316 6164 section->read (per_objfile->objfile);
9c541725 6165 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6166 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6167 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6168 info_ptr += initial_length_size;
6169
6170 version = read_2_bytes (abfd, info_ptr);
6171 info_ptr += 2;
6172 if (version >= 5)
6173 {
6174 /* Skip unit type and address size. */
6175 info_ptr += 2;
6176 }
6177
24aa364d 6178 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6179}
6180
b83470bf
TT
6181/* A partial symtab that is used only for include files. */
6182struct dwarf2_include_psymtab : public partial_symtab
6183{
6184 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6185 : partial_symtab (filename, objfile)
6186 {
6187 }
6188
6189 void read_symtab (struct objfile *objfile) override
6190 {
194d088f
TV
6191 /* It's an include file, no symbols to read for it.
6192 Everything is in the includer symtab. */
6193
6194 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6195 expansion of the includer psymtab. We use the dependencies[0] field to
6196 model the includer. But if we go the regular route of calling
6197 expand_psymtab here, and having expand_psymtab call expand_dependencies
6198 to expand the includer, we'll only use expand_psymtab on the includer
6199 (making it a non-toplevel psymtab), while if we expand the includer via
6200 another path, we'll use read_symtab (making it a toplevel psymtab).
6201 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6202 psymtab, and trigger read_symtab on the includer here directly. */
6203 includer ()->read_symtab (objfile);
b83470bf
TT
6204 }
6205
6206 void expand_psymtab (struct objfile *objfile) override
6207 {
194d088f
TV
6208 /* This is not called by read_symtab, and should not be called by any
6209 expand_dependencies. */
6210 gdb_assert (false);
b83470bf
TT
6211 }
6212
5717c425 6213 bool readin_p (struct objfile *objfile) const override
b83470bf 6214 {
5717c425 6215 return includer ()->readin_p (objfile);
b83470bf
TT
6216 }
6217
5717c425 6218 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6219 {
6220 return nullptr;
6221 }
6222
6223private:
194d088f
TV
6224 partial_symtab *includer () const
6225 {
6226 /* An include psymtab has exactly one dependency: the psymtab that
6227 includes it. */
6228 gdb_assert (this->number_of_dependencies == 1);
6229 return this->dependencies[0];
6230 }
b83470bf
TT
6231};
6232
aaa75496
JB
6233/* Allocate a new partial symtab for file named NAME and mark this new
6234 partial symtab as being an include of PST. */
6235
6236static void
891813be 6237dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
6238 struct objfile *objfile)
6239{
b83470bf 6240 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6241
fbd9ab74 6242 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6243 subpst->dirname = pst->dirname;
fbd9ab74 6244
a9342b62 6245 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6246 subpst->dependencies[0] = pst;
6247 subpst->number_of_dependencies = 1;
aaa75496
JB
6248}
6249
6250/* Read the Line Number Program data and extract the list of files
6251 included by the source file represented by PST. Build an include
d85a05f0 6252 partial symtab for each of these included files. */
aaa75496
JB
6253
6254static void
6255dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6256 struct die_info *die,
891813be 6257 dwarf2_psymtab *pst)
aaa75496 6258{
fff8551c 6259 line_header_up lh;
d85a05f0 6260 struct attribute *attr;
aaa75496 6261
d85a05f0 6262 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6263 if (attr != nullptr)
9c541725 6264 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6265 if (lh == NULL)
6266 return; /* No linetable, so no includes. */
6267
79748972
TT
6268 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6269 that we pass in the raw text_low here; that is ok because we're
6270 only decoding the line table to make include partial symtabs, and
6271 so the addresses aren't really used. */
4ae976d1 6272 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6273 pst->raw_text_low (), 1);
aaa75496
JB
6274}
6275
348e048f 6276static hashval_t
52dc124a 6277hash_signatured_type (const void *item)
348e048f 6278{
9a3c8263
SM
6279 const struct signatured_type *sig_type
6280 = (const struct signatured_type *) item;
9a619af0 6281
348e048f 6282 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6283 return sig_type->signature;
348e048f
DE
6284}
6285
6286static int
52dc124a 6287eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6288{
9a3c8263
SM
6289 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6290 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6291
348e048f
DE
6292 return lhs->signature == rhs->signature;
6293}
6294
1fd400ff
TT
6295/* Allocate a hash table for signatured types. */
6296
b0b6a987 6297static htab_up
298e9637 6298allocate_signatured_type_table ()
1fd400ff 6299{
b0b6a987
TT
6300 return htab_up (htab_create_alloc (41,
6301 hash_signatured_type,
6302 eq_signatured_type,
6303 NULL, xcalloc, xfree));
1fd400ff
TT
6304}
6305
d467dd73 6306/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6307
6308static int
d467dd73 6309add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6310{
9a3c8263 6311 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6312 std::vector<signatured_type *> *all_type_units
6313 = (std::vector<signatured_type *> *) datum;
1fd400ff 6314
b2bdb8cf 6315 all_type_units->push_back (sigt);
1fd400ff
TT
6316
6317 return 1;
6318}
6319
78d4d2c5 6320/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6321 and fill them into TYPES_HTAB. It will process only type units,
6322 therefore DW_UT_type. */
c88ee1f0 6323
78d4d2c5 6324static void
976ca316 6325create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6326 struct dwo_file *dwo_file,
b0b6a987 6327 dwarf2_section_info *section, htab_up &types_htab,
43988095 6328 rcuh_kind section_kind)
348e048f 6329{
976ca316 6330 struct objfile *objfile = per_objfile->objfile;
4bdcc0c1 6331 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6332 bfd *abfd;
6333 const gdb_byte *info_ptr, *end_ptr;
348e048f 6334
4bdcc0c1
DE
6335 abbrev_section = (dwo_file != NULL
6336 ? &dwo_file->sections.abbrev
976ca316 6337 : &per_objfile->per_bfd->abbrev);
4bdcc0c1 6338
b4f54984 6339 if (dwarf_read_debug)
43988095 6340 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6341 section->get_name (),
6342 abbrev_section->get_file_name ());
09406207 6343
96b79293 6344 section->read (objfile);
78d4d2c5 6345 info_ptr = section->buffer;
348e048f 6346
78d4d2c5
JK
6347 if (info_ptr == NULL)
6348 return;
348e048f 6349
78d4d2c5
JK
6350 /* We can't set abfd until now because the section may be empty or
6351 not present, in which case the bfd is unknown. */
96b79293 6352 abfd = section->get_bfd_owner ();
348e048f 6353
c0ab21c2
TT
6354 /* We don't use cutu_reader here because we don't need to read
6355 any dies: the signature is in the header. */
3019eac3 6356
78d4d2c5
JK
6357 end_ptr = info_ptr + section->size;
6358 while (info_ptr < end_ptr)
6359 {
78d4d2c5
JK
6360 struct signatured_type *sig_type;
6361 struct dwo_unit *dwo_tu;
6362 void **slot;
6363 const gdb_byte *ptr = info_ptr;
6364 struct comp_unit_head header;
6365 unsigned int length;
8b70b953 6366
9c541725 6367 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6368
a49dd8dd
JK
6369 /* Initialize it due to a false compiler warning. */
6370 header.signature = -1;
9c541725 6371 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6372
78d4d2c5
JK
6373 /* We need to read the type's signature in order to build the hash
6374 table, but we don't need anything else just yet. */
348e048f 6375
976ca316 6376 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
43988095 6377 abbrev_section, ptr, section_kind);
348e048f 6378
4057dfde 6379 length = header.get_length ();
6caca83c 6380
78d4d2c5
JK
6381 /* Skip dummy type units. */
6382 if (ptr >= info_ptr + length
43988095
JK
6383 || peek_abbrev_code (abfd, ptr) == 0
6384 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6385 {
6386 info_ptr += length;
6387 continue;
6388 }
dee91e82 6389
78d4d2c5
JK
6390 if (types_htab == NULL)
6391 {
6392 if (dwo_file)
298e9637 6393 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6394 else
298e9637 6395 types_htab = allocate_signatured_type_table ();
78d4d2c5 6396 }
8b70b953 6397
78d4d2c5
JK
6398 if (dwo_file)
6399 {
6400 sig_type = NULL;
976ca316 6401 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
78d4d2c5 6402 dwo_tu->dwo_file = dwo_file;
43988095 6403 dwo_tu->signature = header.signature;
9c541725 6404 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6405 dwo_tu->section = section;
9c541725 6406 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6407 dwo_tu->length = length;
6408 }
6409 else
6410 {
6411 /* N.B.: type_offset is not usable if this type uses a DWO file.
6412 The real type_offset is in the DWO file. */
6413 dwo_tu = NULL;
976ca316 6414 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
43988095 6415 sig_type->signature = header.signature;
9c541725 6416 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
6417 sig_type->per_cu.is_debug_types = 1;
6418 sig_type->per_cu.section = section;
9c541725 6419 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6420 sig_type->per_cu.length = length;
6421 }
6422
b0b6a987 6423 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6424 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6425 INSERT);
6426 gdb_assert (slot != NULL);
6427 if (*slot != NULL)
6428 {
9c541725 6429 sect_offset dup_sect_off;
0349ea22 6430
3019eac3
DE
6431 if (dwo_file)
6432 {
78d4d2c5
JK
6433 const struct dwo_unit *dup_tu
6434 = (const struct dwo_unit *) *slot;
6435
9c541725 6436 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6437 }
6438 else
6439 {
78d4d2c5
JK
6440 const struct signatured_type *dup_tu
6441 = (const struct signatured_type *) *slot;
6442
9c541725 6443 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6444 }
8b70b953 6445
b98664d3 6446 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6447 " the entry at offset %s, signature %s"),
6448 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6449 hex_string (header.signature));
78d4d2c5
JK
6450 }
6451 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6452
78d4d2c5 6453 if (dwarf_read_debug > 1)
9d8780f0
SM
6454 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6455 sect_offset_str (sect_off),
43988095 6456 hex_string (header.signature));
3019eac3 6457
78d4d2c5
JK
6458 info_ptr += length;
6459 }
6460}
3019eac3 6461
78d4d2c5
JK
6462/* Create the hash table of all entries in the .debug_types
6463 (or .debug_types.dwo) section(s).
6464 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6465 otherwise it is NULL.
b3c8eb43 6466
78d4d2c5 6467 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6468
78d4d2c5 6469 Note: This function processes DWO files only, not DWP files. */
348e048f 6470
78d4d2c5 6471static void
976ca316 6472create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6473 struct dwo_file *dwo_file,
fd5866f6 6474 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6475 htab_up &types_htab)
78d4d2c5 6476{
fd5866f6 6477 for (dwarf2_section_info &section : type_sections)
976ca316
SM
6478 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6479 rcuh_kind::TYPE);
3019eac3
DE
6480}
6481
6482/* Create the hash table of all entries in the .debug_types section,
6483 and initialize all_type_units.
6484 The result is zero if there is an error (e.g. missing .debug_types section),
6485 otherwise non-zero. */
6486
6487static int
976ca316 6488create_all_type_units (dwarf2_per_objfile *per_objfile)
3019eac3 6489{
b0b6a987 6490 htab_up types_htab;
3019eac3 6491
976ca316
SM
6492 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6493 types_htab, rcuh_kind::COMPILE);
6494 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6495 types_htab);
3019eac3
DE
6496 if (types_htab == NULL)
6497 {
976ca316 6498 per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6499 return 0;
6500 }
6501
976ca316 6502 per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6503
976ca316
SM
6504 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6505 per_objfile->per_bfd->all_type_units.reserve
6506 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6507
976ca316 6508 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6509 add_signatured_type_cu_to_table,
976ca316 6510 &per_objfile->per_bfd->all_type_units);
1fd400ff 6511
348e048f
DE
6512 return 1;
6513}
6514
5989a64e 6515/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6516 If SLOT is non-NULL, it is the entry to use in the hash table.
6517 Otherwise we find one. */
6518
6519static struct signatured_type *
976ca316 6520add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6aa5f3a6 6521{
976ca316
SM
6522 if (per_objfile->per_bfd->all_type_units.size ()
6523 == per_objfile->per_bfd->all_type_units.capacity ())
6524 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6525
976ca316 6526 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6527
976ca316 6528 per_objfile->resize_symtabs ();
af758d11 6529
976ca316 6530 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6531 sig_type->signature = sig;
6532 sig_type->per_cu.is_debug_types = 1;
976ca316 6533 if (per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6534 {
6535 sig_type->per_cu.v.quick =
976ca316 6536 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6537 struct dwarf2_per_cu_quick_data);
6538 }
6539
6540 if (slot == NULL)
6541 {
976ca316 6542 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6543 sig_type, INSERT);
6544 }
6545 gdb_assert (*slot == NULL);
6546 *slot = sig_type;
6547 /* The rest of sig_type must be filled in by the caller. */
6548 return sig_type;
6549}
6550
a2ce51a0
DE
6551/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6552 Fill in SIG_ENTRY with DWO_ENTRY. */
6553
6554static void
976ca316 6555fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
a2ce51a0
DE
6556 struct signatured_type *sig_entry,
6557 struct dwo_unit *dwo_entry)
6558{
976ca316 6559 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1859c670 6560
7ee85ab1 6561 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0 6562 gdb_assert (! sig_entry->per_cu.queued);
976ca316 6563 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
1859c670 6564 if (per_bfd->using_index)
6aa5f3a6
DE
6565 {
6566 gdb_assert (sig_entry->per_cu.v.quick != NULL);
976ca316 6567 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6568 }
6569 else
6570 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6571 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6572 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6573 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6574 gdb_assert (sig_entry->dwo_unit == NULL);
6575
6576 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6577 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6578 sig_entry->per_cu.length = dwo_entry->length;
6579 sig_entry->per_cu.reading_dwo_directly = 1;
1859c670 6580 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6581 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6582 sig_entry->dwo_unit = dwo_entry;
6583}
6584
6585/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6586 If we haven't read the TU yet, create the signatured_type data structure
6587 for a TU to be read in directly from a DWO file, bypassing the stub.
6588 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6589 using .gdb_index, then when reading a CU we want to stay in the DWO file
6590 containing that CU. Otherwise we could end up reading several other DWO
6591 files (due to comdat folding) to process the transitive closure of all the
6592 mentioned TUs, and that can be slow. The current DWO file will have every
6593 type signature that it needs.
a2ce51a0
DE
6594 We only do this for .gdb_index because in the psymtab case we already have
6595 to read all the DWOs to build the type unit groups. */
6596
6597static struct signatured_type *
6598lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6599{
976ca316 6600 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a2ce51a0
DE
6601 struct dwo_file *dwo_file;
6602 struct dwo_unit find_dwo_entry, *dwo_entry;
6603 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6604 void **slot;
a2ce51a0 6605
976ca316 6606 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0 6607
6aa5f3a6
DE
6608 /* If TU skeletons have been removed then we may not have read in any
6609 TUs yet. */
976ca316
SM
6610 if (per_objfile->per_bfd->signatured_types == NULL)
6611 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6612
6613 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6614 Use the global signatured_types array to do our own comdat-folding
6615 of types. If this is the first time we're reading this TU, and
6616 the TU has an entry in .gdb_index, replace the recorded data from
6617 .gdb_index with this TU. */
a2ce51a0 6618
a2ce51a0 6619 find_sig_entry.signature = sig;
976ca316 6620 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6621 &find_sig_entry, INSERT);
9a3c8263 6622 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6623
6624 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6625 read. Don't reassign the global entry to point to this DWO if that's
6626 the case. Also note that if the TU is already being read, it may not
6627 have come from a DWO, the program may be a mix of Fission-compiled
6628 code and non-Fission-compiled code. */
6629
6630 /* Have we already tried to read this TU?
6631 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6632 needn't exist in the global table yet). */
6633 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6634 return sig_entry;
6635
6aa5f3a6
DE
6636 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6637 dwo_unit of the TU itself. */
6638 dwo_file = cu->dwo_unit->dwo_file;
6639
a2ce51a0
DE
6640 /* Ok, this is the first time we're reading this TU. */
6641 if (dwo_file->tus == NULL)
6642 return NULL;
6643 find_dwo_entry.signature = sig;
b0b6a987
TT
6644 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6645 &find_dwo_entry);
a2ce51a0
DE
6646 if (dwo_entry == NULL)
6647 return NULL;
6648
6aa5f3a6
DE
6649 /* If the global table doesn't have an entry for this TU, add one. */
6650 if (sig_entry == NULL)
976ca316 6651 sig_entry = add_type_unit (per_objfile, sig, slot);
6aa5f3a6 6652
976ca316 6653 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
89e63ee4 6654 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6655 return sig_entry;
6656}
6657
a2ce51a0
DE
6658/* Subroutine of lookup_signatured_type.
6659 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6660 then try the DWP file. If the TU stub (skeleton) has been removed then
6661 it won't be in .gdb_index. */
a2ce51a0
DE
6662
6663static struct signatured_type *
6664lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6665{
976ca316
SM
6666 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6667 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
a2ce51a0
DE
6668 struct dwo_unit *dwo_entry;
6669 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6670 void **slot;
a2ce51a0 6671
976ca316 6672 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0
DE
6673 gdb_assert (dwp_file != NULL);
6674
6aa5f3a6
DE
6675 /* If TU skeletons have been removed then we may not have read in any
6676 TUs yet. */
976ca316
SM
6677 if (per_objfile->per_bfd->signatured_types == NULL)
6678 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6679
6aa5f3a6 6680 find_sig_entry.signature = sig;
976ca316 6681 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6682 &find_sig_entry, INSERT);
9a3c8263 6683 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6684
6685 /* Have we already tried to read this TU?
6686 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6687 needn't exist in the global table yet). */
6688 if (sig_entry != NULL)
6689 return sig_entry;
6690
a2ce51a0
DE
6691 if (dwp_file->tus == NULL)
6692 return NULL;
976ca316
SM
6693 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6694 1 /* is_debug_types */);
a2ce51a0
DE
6695 if (dwo_entry == NULL)
6696 return NULL;
6697
976ca316
SM
6698 sig_entry = add_type_unit (per_objfile, sig, slot);
6699 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
a2ce51a0 6700
a2ce51a0
DE
6701 return sig_entry;
6702}
6703
380bca97 6704/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6705 Returns NULL if signature SIG is not present in the table.
6706 It is up to the caller to complain about this. */
348e048f
DE
6707
6708static struct signatured_type *
a2ce51a0 6709lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6710{
976ca316 6711 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 6712
976ca316 6713 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
a2ce51a0
DE
6714 {
6715 /* We're in a DWO/DWP file, and we're using .gdb_index.
6716 These cases require special processing. */
976ca316 6717 if (get_dwp_file (per_objfile) == NULL)
a2ce51a0
DE
6718 return lookup_dwo_signatured_type (cu, sig);
6719 else
6720 return lookup_dwp_signatured_type (cu, sig);
6721 }
6722 else
6723 {
6724 struct signatured_type find_entry, *entry;
348e048f 6725
976ca316 6726 if (per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6727 return NULL;
6728 find_entry.signature = sig;
9a3c8263 6729 entry = ((struct signatured_type *)
976ca316 6730 htab_find (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6731 &find_entry));
a2ce51a0
DE
6732 return entry;
6733 }
348e048f 6734}
18a8505e 6735
42e7ad6c 6736/* Low level DIE reading support. */
348e048f 6737
d85a05f0
DJ
6738/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6739
6740static void
6741init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6742 struct dwarf2_cu *cu,
3019eac3 6743 struct dwarf2_section_info *section,
685af9cd
TT
6744 struct dwo_file *dwo_file,
6745 struct abbrev_table *abbrev_table)
d85a05f0 6746{
fceca515 6747 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6748 reader->abfd = section->get_bfd_owner ();
d85a05f0 6749 reader->cu = cu;
3019eac3 6750 reader->dwo_file = dwo_file;
dee91e82
DE
6751 reader->die_section = section;
6752 reader->buffer = section->buffer;
f664829e 6753 reader->buffer_end = section->buffer + section->size;
685af9cd 6754 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6755}
6756
c0ab21c2 6757/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6758 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6759 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6760 already.
6761
6762 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6763 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6764 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6765 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6766 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6767 STUB_COMP_DIR may be non-NULL.
3e225074 6768 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6769 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6770 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6771 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6772 kept around for at least as long as *RESULT_READER.
6773
b0c7bfa9
DE
6774 The result is non-zero if a valid (non-dummy) DIE was found. */
6775
6776static int
4ab09049 6777read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6778 struct dwo_unit *dwo_unit,
b0c7bfa9 6779 struct die_info *stub_comp_unit_die,
a2ce51a0 6780 const char *stub_comp_dir,
b0c7bfa9 6781 struct die_reader_specs *result_reader,
d521ce57 6782 const gdb_byte **result_info_ptr,
b0c7bfa9 6783 struct die_info **result_comp_unit_die,
685af9cd 6784 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6785{
976ca316 6786 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4ab09049 6787 dwarf2_per_cu_data *per_cu = cu->per_cu;
976ca316 6788 struct objfile *objfile = per_objfile->objfile;
b0c7bfa9 6789 bfd *abfd;
d521ce57 6790 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6791 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6792 int i,num_extra_attrs;
6793 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6794 struct die_info *comp_unit_die;
6795
b0aeadb3
DE
6796 /* At most one of these may be provided. */
6797 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6798
b0c7bfa9
DE
6799 /* These attributes aren't processed until later:
6800 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6801 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6802 referenced later. However, these attributes are found in the stub
6803 which we won't have later. In order to not impose this complication
6804 on the rest of the code, we read them here and copy them to the
6805 DWO CU/TU die. */
b0c7bfa9
DE
6806
6807 stmt_list = NULL;
6808 low_pc = NULL;
6809 high_pc = NULL;
6810 ranges = NULL;
6811 comp_dir = NULL;
6812
6813 if (stub_comp_unit_die != NULL)
6814 {
6815 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6816 DWO file. */
4ab09049 6817 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6818 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6819 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6820 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6821 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6822 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6823
a39fdb41 6824 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6825
18a8505e
AT
6826 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6827 here (if needed). We need the value before we can process
6828 DW_AT_ranges. */
a39fdb41 6829 cu->ranges_base = stub_comp_unit_die->ranges_base ();
b0c7bfa9 6830 }
a2ce51a0
DE
6831 else if (stub_comp_dir != NULL)
6832 {
6833 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6834 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6835 comp_dir->name = DW_AT_comp_dir;
6836 comp_dir->form = DW_FORM_string;
6837 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6838 DW_STRING (comp_dir) = stub_comp_dir;
6839 }
b0c7bfa9
DE
6840
6841 /* Set up for reading the DWO CU/TU. */
6842 cu->dwo_unit = dwo_unit;
685af9cd 6843 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6844 section->read (objfile);
6845 abfd = section->get_bfd_owner ();
9c541725
PA
6846 begin_info_ptr = info_ptr = (section->buffer
6847 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6848 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6849
4ab09049 6850 if (per_cu->is_debug_types)
b0c7bfa9 6851 {
4ab09049 6852 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6853
976ca316
SM
6854 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6855 section, dwo_abbrev_section,
43988095 6856 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6857 /* This is not an assert because it can be caused by bad debug info. */
43988095 6858 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6859 {
6860 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6861 " TU at offset %s [in module %s]"),
a2ce51a0 6862 hex_string (sig_type->signature),
43988095 6863 hex_string (cu->header.signature),
9d8780f0 6864 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6865 bfd_get_filename (abfd));
6866 }
9c541725 6867 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6868 /* For DWOs coming from DWP files, we don't know the CU length
6869 nor the type's offset in the TU until now. */
4057dfde 6870 dwo_unit->length = cu->header.get_length ();
9c541725 6871 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6872
6873 /* Establish the type offset that can be used to lookup the type.
6874 For DWO files, we don't know it until now. */
9c541725
PA
6875 sig_type->type_offset_in_section
6876 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6877 }
6878 else
6879 {
976ca316
SM
6880 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6881 section, dwo_abbrev_section,
43988095 6882 info_ptr, rcuh_kind::COMPILE);
9c541725 6883 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6884 /* For DWOs coming from DWP files, we don't know the CU length
6885 until now. */
4057dfde 6886 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6887 }
6888
685af9cd 6889 *result_dwo_abbrev_table
86de1d91
TT
6890 = abbrev_table::read (objfile, dwo_abbrev_section,
6891 cu->header.abbrev_sect_off);
685af9cd
TT
6892 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6893 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6894
6895 /* Read in the die, but leave space to copy over the attributes
6896 from the stub. This has the benefit of simplifying the rest of
6897 the code - all the work to maintain the illusion of a single
6898 DW_TAG_{compile,type}_unit DIE is done here. */
6899 num_extra_attrs = ((stmt_list != NULL)
6900 + (low_pc != NULL)
6901 + (high_pc != NULL)
6902 + (ranges != NULL)
6903 + (comp_dir != NULL));
6904 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6905 num_extra_attrs);
b0c7bfa9
DE
6906
6907 /* Copy over the attributes from the stub to the DIE we just read in. */
6908 comp_unit_die = *result_comp_unit_die;
6909 i = comp_unit_die->num_attrs;
6910 if (stmt_list != NULL)
6911 comp_unit_die->attrs[i++] = *stmt_list;
6912 if (low_pc != NULL)
6913 comp_unit_die->attrs[i++] = *low_pc;
6914 if (high_pc != NULL)
6915 comp_unit_die->attrs[i++] = *high_pc;
6916 if (ranges != NULL)
6917 comp_unit_die->attrs[i++] = *ranges;
6918 if (comp_dir != NULL)
6919 comp_unit_die->attrs[i++] = *comp_dir;
6920 comp_unit_die->num_attrs += num_extra_attrs;
6921
b4f54984 6922 if (dwarf_die_debug)
bf6af496
DE
6923 {
6924 fprintf_unfiltered (gdb_stdlog,
6925 "Read die from %s@0x%x of %s:\n",
96b79293 6926 section->get_name (),
bf6af496
DE
6927 (unsigned) (begin_info_ptr - section->buffer),
6928 bfd_get_filename (abfd));
b4f54984 6929 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6930 }
6931
b0c7bfa9
DE
6932 /* Skip dummy compilation units. */
6933 if (info_ptr >= begin_info_ptr + dwo_unit->length
6934 || peek_abbrev_code (abfd, info_ptr) == 0)
6935 return 0;
6936
6937 *result_info_ptr = info_ptr;
6938 return 1;
6939}
6940
a084a2a6
AT
6941/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6942 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6943 signature is part of the header. */
6944static gdb::optional<ULONGEST>
6945lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6946{
6947 if (cu->header.version >= 5)
6948 return cu->header.signature;
6949 struct attribute *attr;
6950 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6951 if (attr == nullptr)
6952 return gdb::optional<ULONGEST> ();
6953 return DW_UNSND (attr);
6954}
6955
c0ab21c2 6956/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6957 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6958 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6959
6960static struct dwo_unit *
4ab09049 6961lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 6962{
4ab09049 6963 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6964 struct dwo_unit *dwo_unit;
c0ab21c2 6965 const char *comp_dir;
b0c7bfa9 6966
a2ce51a0
DE
6967 gdb_assert (cu != NULL);
6968
b0c7bfa9 6969 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6970 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6971 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 6972
4ab09049
SM
6973 if (per_cu->is_debug_types)
6974 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
6975 else
6976 {
a084a2a6 6977 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 6978
a084a2a6 6979 if (!signature.has_value ())
b0c7bfa9
DE
6980 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6981 " [in module %s]"),
4ab09049
SM
6982 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6983
6984 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
6985 }
6986
b0c7bfa9
DE
6987 return dwo_unit;
6988}
6989
c0ab21c2 6990/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6991 See it for a description of the parameters.
fcd3b13d 6992 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6993
c0ab21c2 6994void
9e021579
SM
6995cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6996 dwarf2_per_objfile *per_objfile,
2e671100 6997 dwarf2_cu *existing_cu)
a2ce51a0 6998{
a2ce51a0 6999 struct signatured_type *sig_type;
a2ce51a0
DE
7000
7001 /* Verify we can do the following downcast, and that we have the
7002 data we need. */
7003 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7004 sig_type = (struct signatured_type *) this_cu;
7005 gdb_assert (sig_type->dwo_unit != NULL);
7006
2e671100
SM
7007 dwarf2_cu *cu;
7008
7009 if (existing_cu != nullptr)
6aa5f3a6 7010 {
2e671100
SM
7011 cu = existing_cu;
7012 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 7013 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 7014 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
7015 }
7016 else
7017 {
7188ed02
SM
7018 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7019 in per_objfile yet. */
7020 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
9e021579 7021 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
2e671100 7022 cu = m_new_cu.get ();
6aa5f3a6
DE
7023 }
7024
7025 /* A future optimization, if needed, would be to use an existing
7026 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7027 could share abbrev tables. */
a2ce51a0 7028
2e671100 7029 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
a2ce51a0
DE
7030 NULL /* stub_comp_unit_die */,
7031 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 7032 this, &info_ptr,
3e225074 7033 &comp_unit_die,
c0ab21c2 7034 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
7035 {
7036 /* Dummy die. */
c0ab21c2 7037 dummy_p = true;
a2ce51a0 7038 }
a2ce51a0
DE
7039}
7040
fd820528 7041/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7042 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7043
f4dc4d17
DE
7044 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7045 Otherwise the table specified in the comp unit header is read in and used.
7046 This is an optimization for when we already have the abbrev table.
7047
2e671100
SM
7048 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7049 allocated. */
aaa75496 7050
ab432490 7051cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7052 dwarf2_per_objfile *per_objfile,
c0ab21c2 7053 struct abbrev_table *abbrev_table,
2e671100 7054 dwarf2_cu *existing_cu,
c0ab21c2
TT
7055 bool skip_partial)
7056 : die_reader_specs {},
6751ebae 7057 m_this_cu (this_cu)
c906108c 7058{
976ca316 7059 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7060 struct dwarf2_section_info *section = this_cu->section;
96b79293 7061 bfd *abfd = section->get_bfd_owner ();
c0ab21c2 7062 const gdb_byte *begin_info_ptr;
dee91e82 7063 struct signatured_type *sig_type = NULL;
4bdcc0c1 7064 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7065 /* Non-zero if CU currently points to a DWO file and we need to
7066 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7067 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7068 int rereading_dwo_cu = 0;
c906108c 7069
b4f54984 7070 if (dwarf_die_debug)
9d8780f0 7071 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7072 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7073 sect_offset_str (this_cu->sect_off));
09406207 7074
a2ce51a0
DE
7075 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7076 file (instead of going through the stub), short-circuit all of this. */
7077 if (this_cu->reading_dwo_directly)
7078 {
7079 /* Narrow down the scope of possibilities to have to understand. */
7080 gdb_assert (this_cu->is_debug_types);
7081 gdb_assert (abbrev_table == NULL);
976ca316 7082 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
a2ce51a0
DE
7083 return;
7084 }
7085
dee91e82 7086 /* This is cheap if the section is already read in. */
96b79293 7087 section->read (objfile);
dee91e82 7088
9c541725 7089 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7090
7091 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7092
2e671100
SM
7093 dwarf2_cu *cu;
7094
7095 if (existing_cu != nullptr)
dee91e82 7096 {
2e671100 7097 cu = existing_cu;
42e7ad6c
DE
7098 /* If this CU is from a DWO file we need to start over, we need to
7099 refetch the attributes from the skeleton CU.
7100 This could be optimized by retrieving those attributes from when we
7101 were here the first time: the previous comp_unit_die was stored in
7102 comp_unit_obstack. But there's no data yet that we need this
7103 optimization. */
7104 if (cu->dwo_unit != NULL)
7105 rereading_dwo_cu = 1;
dee91e82
DE
7106 }
7107 else
7108 {
7188ed02
SM
7109 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7110 in per_objfile yet. */
976ca316
SM
7111 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7112 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
c0ab21c2 7113 cu = m_new_cu.get ();
42e7ad6c 7114 }
dee91e82 7115
b0c7bfa9 7116 /* Get the header. */
9c541725 7117 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7118 {
7119 /* We already have the header, there's no need to read it in again. */
9c541725 7120 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7121 }
7122 else
7123 {
3019eac3 7124 if (this_cu->is_debug_types)
dee91e82 7125 {
976ca316
SM
7126 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7127 section, abbrev_section,
7128 info_ptr, rcuh_kind::TYPE);
dee91e82 7129
42e7ad6c
DE
7130 /* Since per_cu is the first member of struct signatured_type,
7131 we can go from a pointer to one to a pointer to the other. */
7132 sig_type = (struct signatured_type *) this_cu;
43988095 7133 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7134 gdb_assert (sig_type->type_offset_in_tu
7135 == cu->header.type_cu_offset_in_tu);
7136 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7137
42e7ad6c
DE
7138 /* LENGTH has not been set yet for type units if we're
7139 using .gdb_index. */
4057dfde 7140 this_cu->length = cu->header.get_length ();
3019eac3
DE
7141
7142 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7143 sig_type->type_offset_in_section =
7144 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7145
7146 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7147 }
7148 else
7149 {
976ca316
SM
7150 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7151 section, abbrev_section,
43988095
JK
7152 info_ptr,
7153 rcuh_kind::COMPILE);
dee91e82 7154
9c541725 7155 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
7156 if (this_cu->length == 0)
7157 this_cu->length = cu->header.get_length ();
7158 else
7159 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7160 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7161 }
7162 }
10b3939b 7163
6caca83c 7164 /* Skip dummy compilation units. */
dee91e82 7165 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7166 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7167 {
7168 dummy_p = true;
7169 return;
7170 }
6caca83c 7171
433df2d4
DE
7172 /* If we don't have them yet, read the abbrevs for this compilation unit.
7173 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7174 done. */
f4dc4d17 7175 if (abbrev_table != NULL)
685af9cd
TT
7176 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7177 else
f4dc4d17 7178 {
c0ab21c2 7179 m_abbrev_table_holder
86de1d91
TT
7180 = abbrev_table::read (objfile, abbrev_section,
7181 cu->header.abbrev_sect_off);
c0ab21c2 7182 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7183 }
af703f96 7184
dee91e82 7185 /* Read the top level CU/TU die. */
c0ab21c2 7186 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7187 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7188
58f0c718 7189 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7190 {
7191 dummy_p = true;
7192 return;
7193 }
58f0c718 7194
b0c7bfa9 7195 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7196 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7197 table from the DWO file and pass the ownership over to us. It will be
7198 referenced from READER, so we must make sure to free it after we're done
7199 with READER.
7200
b0c7bfa9
DE
7201 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7202 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7203 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7204 if (dwo_name != nullptr)
3019eac3 7205 {
3019eac3 7206 struct dwo_unit *dwo_unit;
b0c7bfa9 7207 struct die_info *dwo_comp_unit_die;
3019eac3 7208
3e225074 7209 if (comp_unit_die->has_children)
6a506a2d 7210 {
b98664d3 7211 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7212 " has children (offset %s) [in module %s]"),
7213 sect_offset_str (this_cu->sect_off),
7214 bfd_get_filename (abfd));
6a506a2d 7215 }
4ab09049 7216 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 7217 if (dwo_unit != NULL)
3019eac3 7218 {
4ab09049 7219 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 7220 comp_unit_die, NULL,
c0ab21c2 7221 this, &info_ptr,
3e225074 7222 &dwo_comp_unit_die,
c0ab21c2 7223 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7224 {
7225 /* Dummy die. */
c0ab21c2 7226 dummy_p = true;
6a506a2d
DE
7227 return;
7228 }
7229 comp_unit_die = dwo_comp_unit_die;
7230 }
7231 else
7232 {
7233 /* Yikes, we couldn't find the rest of the DIE, we only have
7234 the stub. A complaint has already been logged. There's
7235 not much more we can do except pass on the stub DIE to
7236 die_reader_func. We don't want to throw an error on bad
7237 debug info. */
3019eac3
DE
7238 }
7239 }
c0ab21c2 7240}
3019eac3 7241
6751ebae
TT
7242void
7243cutu_reader::keep ()
c0ab21c2 7244{
b0c7bfa9 7245 /* Done, clean up. */
6751ebae
TT
7246 gdb_assert (!dummy_p);
7247 if (m_new_cu != NULL)
348e048f 7248 {
7188ed02
SM
7249 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7250 now. */
7251 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7252 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
348e048f 7253 }
dee91e82
DE
7254}
7255
18a8505e
AT
7256/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7257 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7258 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7259
7260 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7261 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7262
7263 We fill in THIS_CU->length.
7264
dee91e82 7265 THIS_CU->cu is always freed when done.
3019eac3 7266 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7267 to care whether it refers to the "main" CU or the DWO CU.
7268
7269 When parent_cu is passed, it is used to provide a default value for
7270 str_offsets_base and addr_base from the parent. */
dee91e82 7271
ab432490 7272cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7273 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
7274 struct dwarf2_cu *parent_cu,
7275 struct dwo_file *dwo_file)
7276 : die_reader_specs {},
7277 m_this_cu (this_cu)
dee91e82 7278{
976ca316 7279 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7280 struct dwarf2_section_info *section = this_cu->section;
96b79293 7281 bfd *abfd = section->get_bfd_owner ();
33e80786 7282 struct dwarf2_section_info *abbrev_section;
d521ce57 7283 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7284
b4f54984 7285 if (dwarf_die_debug)
9d8780f0 7286 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7287 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7288 sect_offset_str (this_cu->sect_off));
09406207 7289
976ca316 7290 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
dee91e82 7291
33e80786
DE
7292 abbrev_section = (dwo_file != NULL
7293 ? &dwo_file->sections.abbrev
7294 : get_abbrev_section_for_cu (this_cu));
7295
dee91e82 7296 /* This is cheap if the section is already read in. */
96b79293 7297 section->read (objfile);
dee91e82 7298
976ca316 7299 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
dee91e82 7300
9c541725 7301 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
976ca316
SM
7302 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7303 section, abbrev_section, info_ptr,
43988095
JK
7304 (this_cu->is_debug_types
7305 ? rcuh_kind::TYPE
7306 : rcuh_kind::COMPILE));
dee91e82 7307
18a8505e
AT
7308 if (parent_cu != nullptr)
7309 {
c0ab21c2
TT
7310 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7311 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7312 }
4057dfde 7313 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7314
7315 /* Skip dummy compilation units. */
7316 if (info_ptr >= begin_info_ptr + this_cu->length
7317 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7318 {
7319 dummy_p = true;
7320 return;
7321 }
72bf9492 7322
c0ab21c2 7323 m_abbrev_table_holder
86de1d91
TT
7324 = abbrev_table::read (objfile, abbrev_section,
7325 m_new_cu->header.abbrev_sect_off);
dee91e82 7326
c0ab21c2
TT
7327 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7328 m_abbrev_table_holder.get ());
3e225074 7329 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7330}
7331
0018ea6f
DE
7332\f
7333/* Type Unit Groups.
dee91e82 7334
0018ea6f
DE
7335 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7336 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7337 so that all types coming from the same compilation (.o file) are grouped
7338 together. A future step could be to put the types in the same symtab as
7339 the CU the types ultimately came from. */
ff013f42 7340
f4dc4d17
DE
7341static hashval_t
7342hash_type_unit_group (const void *item)
7343{
9a3c8263
SM
7344 const struct type_unit_group *tu_group
7345 = (const struct type_unit_group *) item;
f4dc4d17 7346
094b34ac 7347 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7348}
348e048f
DE
7349
7350static int
f4dc4d17 7351eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7352{
9a3c8263
SM
7353 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7354 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7355
094b34ac 7356 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7357}
348e048f 7358
f4dc4d17
DE
7359/* Allocate a hash table for type unit groups. */
7360
eaa5fa8b 7361static htab_up
298e9637 7362allocate_type_unit_groups_table ()
f4dc4d17 7363{
eaa5fa8b
TT
7364 return htab_up (htab_create_alloc (3,
7365 hash_type_unit_group,
7366 eq_type_unit_group,
7367 NULL, xcalloc, xfree));
f4dc4d17 7368}
dee91e82 7369
f4dc4d17
DE
7370/* Type units that don't have DW_AT_stmt_list are grouped into their own
7371 partial symtabs. We combine several TUs per psymtab to not let the size
7372 of any one psymtab grow too big. */
7373#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7374#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7375
094b34ac 7376/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7377 Create the type_unit_group object used to hold one or more TUs. */
7378
7379static struct type_unit_group *
094b34ac 7380create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7381{
976ca316
SM
7382 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7383 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
094b34ac 7384 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7385 struct type_unit_group *tu_group;
f4dc4d17 7386
976ca316 7387 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
094b34ac 7388 per_cu = &tu_group->per_cu;
1859c670 7389 per_cu->per_bfd = per_bfd;
f4dc4d17 7390
1859c670 7391 if (per_bfd->using_index)
094b34ac 7392 {
1859c670 7393 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7394 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7395 }
7396 else
7397 {
9c541725 7398 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7399 dwarf2_psymtab *pst;
528e1572 7400 std::string name;
094b34ac
DE
7401
7402 /* Give the symtab a useful name for debug purposes. */
7403 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7404 name = string_printf ("<type_units_%d>",
7405 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7406 else
528e1572 7407 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7408
976ca316 7409 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
6d94535f 7410 pst->anonymous = true;
094b34ac 7411 }
f4dc4d17 7412
094b34ac 7413 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7414 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7415
7416 return tu_group;
7417}
7418
094b34ac
DE
7419/* Look up the type_unit_group for type unit CU, and create it if necessary.
7420 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7421
7422static struct type_unit_group *
ff39bb5e 7423get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7424{
976ca316
SM
7425 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7426 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7427 struct type_unit_group *tu_group;
7428 void **slot;
7429 unsigned int line_offset;
7430 struct type_unit_group type_unit_group_for_lookup;
7431
976ca316
SM
7432 if (per_objfile->per_bfd->type_unit_groups == NULL)
7433 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7434
7435 /* Do we need to create a new group, or can we use an existing one? */
7436
7437 if (stmt_list)
7438 {
7439 line_offset = DW_UNSND (stmt_list);
7440 ++tu_stats->nr_symtab_sharers;
7441 }
7442 else
7443 {
7444 /* Ugh, no stmt_list. Rare, but we have to handle it.
7445 We can do various things here like create one group per TU or
7446 spread them over multiple groups to split up the expansion work.
7447 To avoid worst case scenarios (too many groups or too large groups)
7448 we, umm, group them in bunches. */
7449 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7450 | (tu_stats->nr_stmt_less_type_units
7451 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7452 ++tu_stats->nr_stmt_less_type_units;
7453 }
7454
094b34ac 7455 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7456 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
976ca316 7457 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7458 &type_unit_group_for_lookup, INSERT);
7459 if (*slot != NULL)
7460 {
9a3c8263 7461 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7462 gdb_assert (tu_group != NULL);
7463 }
7464 else
7465 {
9c541725 7466 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7467 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7468 *slot = tu_group;
7469 ++tu_stats->nr_symtabs;
7470 }
7471
7472 return tu_group;
7473}
0018ea6f
DE
7474\f
7475/* Partial symbol tables. */
7476
7477/* Create a psymtab named NAME and assign it to PER_CU.
7478
7479 The caller must fill in the following details:
7480 dirname, textlow, texthigh. */
7481
891813be 7482static dwarf2_psymtab *
7aa104c4
SM
7483create_partial_symtab (dwarf2_per_cu_data *per_cu,
7484 dwarf2_per_objfile *per_objfile,
7485 const char *name)
0018ea6f 7486{
7aa104c4 7487 struct objfile *objfile = per_objfile->objfile;
891813be 7488 dwarf2_psymtab *pst;
0018ea6f 7489
9f4e76a4 7490 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7491
6d94535f 7492 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7493
7494 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7495 per_cu->v.psymtab = pst;
7496
7497 return pst;
7498}
7499
c0ab21c2 7500/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7501
7502static void
7503process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7504 const gdb_byte *info_ptr,
0018ea6f 7505 struct die_info *comp_unit_die,
c0ab21c2 7506 enum language pretend_language)
0018ea6f
DE
7507{
7508 struct dwarf2_cu *cu = reader->cu;
7aa104c4
SM
7509 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7510 struct objfile *objfile = per_objfile->objfile;
08feed99 7511 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7512 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7513 CORE_ADDR baseaddr;
7514 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7515 dwarf2_psymtab *pst;
3a2b436a 7516 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7517 const char *filename;
0018ea6f 7518
0018ea6f
DE
7519 gdb_assert (! per_cu->is_debug_types);
7520
c0ab21c2 7521 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7522
0018ea6f 7523 /* Allocate a new partial symbol table structure. */
2e927613
TV
7524 gdb::unique_xmalloc_ptr<char> debug_filename;
7525 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7526 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7527 if (filename == NULL)
0018ea6f 7528 filename = "";
2e927613
TV
7529 else if (strcmp (filename, artificial) == 0)
7530 {
7531 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7532 sect_offset_str (per_cu->sect_off),
7533 (char *) NULL));
2e927613
TV
7534 filename = debug_filename.get ();
7535 }
0018ea6f 7536
7aa104c4 7537 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7538
7539 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7540 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7541
b3b3bada 7542 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7543
7544 dwarf2_find_base_address (comp_unit_die, cu);
7545
7546 /* Possibly set the default values of LOWPC and HIGHPC from
7547 `DW_AT_ranges'. */
3a2b436a
JK
7548 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7549 &best_highpc, cu, pst);
7550 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7551 {
7552 CORE_ADDR low
7553 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7554 - baseaddr);
7555 CORE_ADDR high
7556 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7557 - baseaddr - 1);
7558 /* Store the contiguous range if it is not empty; it can be
7559 empty for CUs with no code. */
d320c2b5
TT
7560 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7561 low, high, pst);
79748972 7562 }
0018ea6f
DE
7563
7564 /* Check if comp unit has_children.
7565 If so, read the rest of the partial symbols from this comp unit.
7566 If not, there's no more debug_info for this comp unit. */
3e225074 7567 if (comp_unit_die->has_children)
0018ea6f
DE
7568 {
7569 struct partial_die_info *first_die;
7570 CORE_ADDR lowpc, highpc;
7571
7572 lowpc = ((CORE_ADDR) -1);
7573 highpc = ((CORE_ADDR) 0);
7574
7575 first_die = load_partial_dies (reader, info_ptr, 1);
7576
7577 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7578 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7579
7580 /* If we didn't find a lowpc, set it to highpc to avoid
7581 complaints from `maint check'. */
7582 if (lowpc == ((CORE_ADDR) -1))
7583 lowpc = highpc;
7584
7585 /* If the compilation unit didn't have an explicit address range,
7586 then use the information extracted from its child dies. */
e385593e 7587 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7588 {
7589 best_lowpc = lowpc;
7590 best_highpc = highpc;
7591 }
7592 }
4ae976d1 7593 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7594 best_lowpc + baseaddr)
7595 - baseaddr);
4ae976d1 7596 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7597 best_highpc + baseaddr)
7598 - baseaddr);
0018ea6f 7599
8763cede 7600 end_psymtab_common (objfile, pst);
0018ea6f 7601
ae640021 7602 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7603 {
7604 int i;
ae640021 7605 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7606
7607 /* Fill in 'dependencies' here; we fill in 'users' in a
7608 post-pass. */
7609 pst->number_of_dependencies = len;
a9342b62
TT
7610 pst->dependencies
7611 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7612 for (i = 0; i < len; ++i)
7613 {
7614 pst->dependencies[i]
7615 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7616 }
0018ea6f 7617
ae640021 7618 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7619 }
7620
7621 /* Get the list of files included in the current compilation unit,
7622 and build a psymtab for each of them. */
7623 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7624
b4f54984 7625 if (dwarf_read_debug)
b926417a
TT
7626 fprintf_unfiltered (gdb_stdlog,
7627 "Psymtab for %s unit @%s: %s - %s"
7628 ", %d global, %d static syms\n",
7629 per_cu->is_debug_types ? "type" : "comp",
7630 sect_offset_str (per_cu->sect_off),
7631 paddress (gdbarch, pst->text_low (objfile)),
7632 paddress (gdbarch, pst->text_high (objfile)),
7633 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7634}
7635
7636/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7637 Process compilation unit THIS_CU for a psymtab. */
7638
7639static void
ab432490
SM
7640process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7641 dwarf2_per_objfile *per_objfile,
135f5437 7642 bool want_partial_unit,
b93601f3 7643 enum language pretend_language)
0018ea6f
DE
7644{
7645 /* If this compilation unit was already read in, free the
7646 cached copy in order to read it in again. This is
7647 necessary because we skipped some symbols when we first
7648 read in the compilation unit (see load_partial_dies).
7649 This problem could be avoided, but the benefit is unclear. */
7188ed02 7650 per_objfile->remove_cu (this_cu);
0018ea6f 7651
2e671100 7652 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2 7653
58990295
TV
7654 switch (reader.comp_unit_die->tag)
7655 {
7656 case DW_TAG_compile_unit:
7657 this_cu->unit_type = DW_UT_compile;
7658 break;
7659 case DW_TAG_partial_unit:
7660 this_cu->unit_type = DW_UT_partial;
7661 break;
7662 default:
7663 abort ();
7664 }
7665
c0ab21c2 7666 if (reader.dummy_p)
f1902523 7667 {
c0ab21c2 7668 /* Nothing. */
f1902523 7669 }
c0ab21c2 7670 else if (this_cu->is_debug_types)
3e225074
TT
7671 build_type_psymtabs_reader (&reader, reader.info_ptr,
7672 reader.comp_unit_die);
135f5437
TT
7673 else if (want_partial_unit
7674 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7675 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7676 reader.comp_unit_die,
c0ab21c2 7677 pretend_language);
0018ea6f 7678
7188ed02 7679 this_cu->lang = reader.cu->language;
58990295 7680
0018ea6f 7681 /* Age out any secondary CUs. */
7188ed02 7682 per_objfile->age_comp_units ();
0018ea6f 7683}
f4dc4d17
DE
7684
7685/* Reader function for build_type_psymtabs. */
7686
7687static void
7688build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7689 const gdb_byte *info_ptr,
3e225074 7690 struct die_info *type_unit_die)
f4dc4d17 7691{
976ca316
SM
7692 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7693 struct objfile *objfile = per_objfile->objfile;
f4dc4d17
DE
7694 struct dwarf2_cu *cu = reader->cu;
7695 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7696 struct signatured_type *sig_type;
f4dc4d17
DE
7697 struct type_unit_group *tu_group;
7698 struct attribute *attr;
7699 struct partial_die_info *first_die;
7700 CORE_ADDR lowpc, highpc;
891813be 7701 dwarf2_psymtab *pst;
f4dc4d17 7702
0186c6a7
DE
7703 gdb_assert (per_cu->is_debug_types);
7704 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7705
3e225074 7706 if (! type_unit_die->has_children)
f4dc4d17
DE
7707 return;
7708
052c8bb8 7709 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7710 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7711
df07e2c7 7712 if (tu_group->tus == nullptr)
a8b3b8e9 7713 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7714 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7715
7716 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
976ca316 7717 pst = create_partial_symtab (per_cu, per_objfile, "");
6d94535f 7718 pst->anonymous = true;
f4dc4d17
DE
7719
7720 first_die = load_partial_dies (reader, info_ptr, 1);
7721
7722 lowpc = (CORE_ADDR) -1;
7723 highpc = (CORE_ADDR) 0;
7724 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7725
8763cede 7726 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7727}
7728
73051182
DE
7729/* Struct used to sort TUs by their abbreviation table offset. */
7730
7731struct tu_abbrev_offset
7732{
b2bdb8cf
SM
7733 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7734 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7735 {}
7736
7737 signatured_type *sig_type;
73051182
DE
7738 sect_offset abbrev_offset;
7739};
7740
484cf504 7741/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7742
484cf504
TT
7743static bool
7744sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7745 const struct tu_abbrev_offset &b)
73051182 7746{
484cf504 7747 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7748}
7749
7750/* Efficiently read all the type units.
7751 This does the bulk of the work for build_type_psymtabs.
7752
7753 The efficiency is because we sort TUs by the abbrev table they use and
7754 only read each abbrev table once. In one program there are 200K TUs
7755 sharing 8K abbrev tables.
7756
7757 The main purpose of this function is to support building the
5989a64e 7758 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7759 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7760 can collapse the search space by grouping them by stmt_list.
7761 The savings can be significant, in the same program from above the 200K TUs
7762 share 8K stmt_list tables.
7763
7764 FUNC is expected to call get_type_unit_group, which will create the
7765 struct type_unit_group if necessary and add it to
5989a64e 7766 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7767
7768static void
976ca316 7769build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
73051182 7770{
976ca316 7771 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
685af9cd 7772 abbrev_table_up abbrev_table;
73051182 7773 sect_offset abbrev_offset;
73051182
DE
7774
7775 /* It's up to the caller to not call us multiple times. */
976ca316 7776 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7777
976ca316 7778 if (per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7779 return;
7780
7781 /* TUs typically share abbrev tables, and there can be way more TUs than
7782 abbrev tables. Sort by abbrev table to reduce the number of times we
7783 read each abbrev table in.
7784 Alternatives are to punt or to maintain a cache of abbrev tables.
7785 This is simpler and efficient enough for now.
7786
7787 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7788 symtab to use). Typically TUs with the same abbrev offset have the same
7789 stmt_list value too so in practice this should work well.
7790
7791 The basic algorithm here is:
7792
7793 sort TUs by abbrev table
7794 for each TU with same abbrev table:
7795 read abbrev table if first user
7796 read TU top level DIE
7797 [IWBN if DWO skeletons had DW_AT_stmt_list]
7798 call FUNC */
7799
b4f54984 7800 if (dwarf_read_debug)
73051182
DE
7801 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7802
7803 /* Sort in a separate table to maintain the order of all_type_units
7804 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7805 std::vector<tu_abbrev_offset> sorted_by_abbrev;
976ca316 7806 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7807
976ca316 7808 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
b2bdb8cf 7809 sorted_by_abbrev.emplace_back
976ca316 7810 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
b2bdb8cf 7811 sig_type->per_cu.sect_off));
73051182 7812
484cf504
TT
7813 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7814 sort_tu_by_abbrev_offset);
73051182 7815
9c541725 7816 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7817
b2bdb8cf 7818 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7819 {
73051182
DE
7820 /* Switch to the next abbrev table if necessary. */
7821 if (abbrev_table == NULL
b2bdb8cf 7822 || tu.abbrev_offset != abbrev_offset)
73051182 7823 {
b2bdb8cf 7824 abbrev_offset = tu.abbrev_offset;
73051182 7825 abbrev_table =
976ca316
SM
7826 abbrev_table::read (per_objfile->objfile,
7827 &per_objfile->per_bfd->abbrev, abbrev_offset);
73051182
DE
7828 ++tu_stats->nr_uniq_abbrev_tables;
7829 }
7830
976ca316 7831 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
2e671100 7832 abbrev_table.get (), nullptr, false);
c0ab21c2
TT
7833 if (!reader.dummy_p)
7834 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7835 reader.comp_unit_die);
73051182 7836 }
6aa5f3a6 7837}
73051182 7838
6aa5f3a6
DE
7839/* Print collected type unit statistics. */
7840
7841static void
976ca316 7842print_tu_stats (dwarf2_per_objfile *per_objfile)
6aa5f3a6 7843{
976ca316 7844 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6aa5f3a6
DE
7845
7846 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf 7847 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
976ca316 7848 per_objfile->per_bfd->all_type_units.size ());
6aa5f3a6
DE
7849 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7850 tu_stats->nr_uniq_abbrev_tables);
7851 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7852 tu_stats->nr_symtabs);
7853 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7854 tu_stats->nr_symtab_sharers);
7855 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7856 tu_stats->nr_stmt_less_type_units);
7857 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7858 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7859}
7860
f4dc4d17
DE
7861/* Traversal function for build_type_psymtabs. */
7862
7863static int
7864build_type_psymtab_dependencies (void **slot, void *info)
7865{
976ca316
SM
7866 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7867 struct objfile *objfile = per_objfile->objfile;
f4dc4d17 7868 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7869 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7870 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7871 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7872 int i;
7873
7874 gdb_assert (len > 0);
197400e8 7875 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7876
7877 pst->number_of_dependencies = len;
a9342b62 7878 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7879 for (i = 0; i < len; ++i)
f4dc4d17 7880 {
df07e2c7 7881 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7882 gdb_assert (iter->per_cu.is_debug_types);
7883 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7884 iter->type_unit_group = tu_group;
f4dc4d17
DE
7885 }
7886
df07e2c7
AB
7887 delete tu_group->tus;
7888 tu_group->tus = nullptr;
348e048f
DE
7889
7890 return 1;
7891}
7892
7893/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7894 Build partial symbol tables for the .debug_types comp-units. */
7895
7896static void
976ca316 7897build_type_psymtabs (dwarf2_per_objfile *per_objfile)
348e048f 7898{
976ca316 7899 if (! create_all_type_units (per_objfile))
348e048f
DE
7900 return;
7901
976ca316 7902 build_type_psymtabs_1 (per_objfile);
6aa5f3a6 7903}
f4dc4d17 7904
6aa5f3a6
DE
7905/* Traversal function for process_skeletonless_type_unit.
7906 Read a TU in a DWO file and build partial symbols for it. */
7907
7908static int
7909process_skeletonless_type_unit (void **slot, void *info)
7910{
7911 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
976ca316 7912 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
6aa5f3a6
DE
7913 struct signatured_type find_entry, *entry;
7914
7915 /* If this TU doesn't exist in the global table, add it and read it in. */
7916
976ca316
SM
7917 if (per_objfile->per_bfd->signatured_types == NULL)
7918 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7919
7920 find_entry.signature = dwo_unit->signature;
976ca316 7921 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7922 &find_entry, INSERT);
6aa5f3a6
DE
7923 /* If we've already seen this type there's nothing to do. What's happening
7924 is we're doing our own version of comdat-folding here. */
7925 if (*slot != NULL)
7926 return 1;
7927
7928 /* This does the job that create_all_type_units would have done for
7929 this TU. */
976ca316
SM
7930 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7931 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7932 *slot = entry;
7933
7934 /* This does the job that build_type_psymtabs_1 would have done. */
976ca316 7935 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
7936 if (!reader.dummy_p)
7937 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7938 reader.comp_unit_die);
6aa5f3a6
DE
7939
7940 return 1;
7941}
7942
7943/* Traversal function for process_skeletonless_type_units. */
7944
7945static int
7946process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7947{
7948 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7949
7950 if (dwo_file->tus != NULL)
b0b6a987
TT
7951 htab_traverse_noresize (dwo_file->tus.get (),
7952 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7953
7954 return 1;
7955}
7956
7957/* Scan all TUs of DWO files, verifying we've processed them.
7958 This is needed in case a TU was emitted without its skeleton.
7959 Note: This can't be done until we know what all the DWO files are. */
7960
7961static void
976ca316 7962process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
6aa5f3a6
DE
7963{
7964 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
976ca316
SM
7965 if (get_dwp_file (per_objfile) == NULL
7966 && per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 7967 {
976ca316 7968 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 7969 process_dwo_file_for_skeletonless_type_units,
976ca316 7970 per_objfile);
6aa5f3a6 7971 }
348e048f
DE
7972}
7973
ed2dc618 7974/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7975
7976static void
976ca316 7977set_partial_user (dwarf2_per_objfile *per_objfile)
95554aad 7978{
976ca316 7979 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
95554aad 7980 {
891813be 7981 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7982
36586728
TT
7983 if (pst == NULL)
7984 continue;
7985
b76e467d 7986 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7987 {
7988 /* Set the 'user' field only if it is not already set. */
7989 if (pst->dependencies[j]->user == NULL)
7990 pst->dependencies[j]->user = pst;
7991 }
7992 }
7993}
7994
93311388
DE
7995/* Build the partial symbol table by doing a quick pass through the
7996 .debug_info and .debug_abbrev sections. */
72bf9492 7997
93311388 7998static void
976ca316 7999dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
93311388 8000{
976ca316 8001 struct objfile *objfile = per_objfile->objfile;
93311388 8002
b4f54984 8003 if (dwarf_read_debug)
45cfd468
DE
8004 {
8005 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8006 objfile_name (objfile));
45cfd468
DE
8007 }
8008
76935768 8009 scoped_restore restore_reading_psyms
976ca316 8010 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
76935768 8011 true);
98bfdba5 8012
976ca316 8013 per_objfile->per_bfd->info.read (objfile);
91c24f0a 8014
93311388
DE
8015 /* Any cached compilation units will be linked by the per-objfile
8016 read_in_chain. Make sure to free them when we're done. */
976ca316 8017 free_cached_comp_units freer (per_objfile);
72bf9492 8018
976ca316 8019 build_type_psymtabs (per_objfile);
348e048f 8020
976ca316 8021 create_all_comp_units (per_objfile);
c906108c 8022
60606b2c
TT
8023 /* Create a temporary address map on a temporary obstack. We later
8024 copy this to the final obstack. */
8268c778 8025 auto_obstack temp_obstack;
791afaa2
TT
8026
8027 scoped_restore save_psymtabs_addrmap
d320c2b5 8028 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8029 addrmap_create_mutable (&temp_obstack));
72bf9492 8030
976ca316 8031 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3d5afab3
TV
8032 {
8033 if (per_cu->v.psymtab != NULL)
8034 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8035 continue;
976ca316 8036 process_psymtab_comp_unit (per_cu, per_objfile, false,
ab432490 8037 language_minimal);
3d5afab3 8038 }
ff013f42 8039
6aa5f3a6 8040 /* This has to wait until we read the CUs, we need the list of DWOs. */
976ca316 8041 process_skeletonless_type_units (per_objfile);
6aa5f3a6
DE
8042
8043 /* Now that all TUs have been processed we can fill in the dependencies. */
976ca316 8044 if (per_objfile->per_bfd->type_unit_groups != NULL)
6aa5f3a6 8045 {
976ca316
SM
8046 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8047 build_type_psymtab_dependencies, per_objfile);
6aa5f3a6
DE
8048 }
8049
b4f54984 8050 if (dwarf_read_debug)
976ca316 8051 print_tu_stats (per_objfile);
6aa5f3a6 8052
976ca316 8053 set_partial_user (per_objfile);
95554aad 8054
d320c2b5
TT
8055 objfile->partial_symtabs->psymtabs_addrmap
8056 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8057 objfile->partial_symtabs->obstack ());
791afaa2
TT
8058 /* At this point we want to keep the address map. */
8059 save_psymtabs_addrmap.release ();
ff013f42 8060
b4f54984 8061 if (dwarf_read_debug)
45cfd468 8062 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8063 objfile_name (objfile));
ae038cb0
DJ
8064}
8065
dee91e82
DE
8066/* Load the partial DIEs for a secondary CU into memory.
8067 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8068
dee91e82 8069static void
ab432490 8070load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
2e671100
SM
8071 dwarf2_per_objfile *per_objfile,
8072 dwarf2_cu *existing_cu)
dee91e82 8073{
2e671100 8074 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
c0ab21c2
TT
8075
8076 if (!reader.dummy_p)
8077 {
8078 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8079 language_minimal);
8080
8081 /* Check if comp unit has_children.
8082 If so, read the rest of the partial symbols from this comp unit.
8083 If not, there's no more debug_info for this comp unit. */
3e225074 8084 if (reader.comp_unit_die->has_children)
c0ab21c2 8085 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
8086
8087 reader.keep ();
c0ab21c2 8088 }
ae038cb0
DJ
8089}
8090
ae038cb0 8091static void
976ca316 8092read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
36586728 8093 struct dwarf2_section_info *section,
f1902523 8094 struct dwarf2_section_info *abbrev_section,
b76e467d 8095 unsigned int is_dwz)
ae038cb0 8096{
d521ce57 8097 const gdb_byte *info_ptr;
976ca316 8098 struct objfile *objfile = per_objfile->objfile;
be391dca 8099
b4f54984 8100 if (dwarf_read_debug)
bf6af496 8101 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
8102 section->get_name (),
8103 section->get_file_name ());
bf6af496 8104
96b79293 8105 section->read (objfile);
ae038cb0 8106
36586728 8107 info_ptr = section->buffer;
6e70227d 8108
36586728 8109 while (info_ptr < section->buffer + section->size)
ae038cb0 8110 {
ae038cb0 8111 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8112
9c541725 8113 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8114
f1902523 8115 comp_unit_head cu_header;
976ca316 8116 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618
SM
8117 abbrev_section, info_ptr,
8118 rcuh_kind::COMPILE);
ae038cb0
DJ
8119
8120 /* Save the compilation unit for later lookup. */
f1902523 8121 if (cu_header.unit_type != DW_UT_type)
976ca316 8122 this_cu = per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
8123 else
8124 {
976ca316 8125 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
8126 sig_type->signature = cu_header.signature;
8127 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8128 this_cu = &sig_type->per_cu;
8129 }
8130 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8131 this_cu->sect_off = sect_off;
f1902523 8132 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8133 this_cu->is_dwz = is_dwz;
8a0459fd 8134 this_cu->section = section;
ae038cb0 8135
976ca316 8136 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8137
8138 info_ptr = info_ptr + this_cu->length;
8139 }
36586728
TT
8140}
8141
8142/* Create a list of all compilation units in OBJFILE.
8143 This is only done for -readnow and building partial symtabs. */
8144
8145static void
976ca316 8146create_all_comp_units (dwarf2_per_objfile *per_objfile)
36586728 8147{
976ca316
SM
8148 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8149 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8150 &per_objfile->per_bfd->abbrev, 0);
36586728 8151
976ca316 8152 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
4db1a1dc 8153 if (dwz != NULL)
976ca316 8154 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
c906108c
SS
8155}
8156
5734ee8b 8157/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8158 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8159 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8160 DW_AT_ranges). See the comments of add_partial_subprogram on how
8161 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8162
72bf9492
DJ
8163static void
8164scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8165 CORE_ADDR *highpc, int set_addrmap,
8166 struct dwarf2_cu *cu)
c906108c 8167{
72bf9492 8168 struct partial_die_info *pdi;
c906108c 8169
91c24f0a
DC
8170 /* Now, march along the PDI's, descending into ones which have
8171 interesting children but skipping the children of the other ones,
8172 until we reach the end of the compilation unit. */
c906108c 8173
72bf9492 8174 pdi = first_die;
91c24f0a 8175
72bf9492
DJ
8176 while (pdi != NULL)
8177 {
52356b79 8178 pdi->fixup (cu);
c906108c 8179
f55ee35c 8180 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8181 children, so we need to look at them. Ditto for anonymous
8182 enums. */
933c6fe4 8183
7d00ffec 8184 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8185 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8186 || pdi->tag == DW_TAG_imported_unit
8187 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8188 {
72bf9492 8189 switch (pdi->tag)
c906108c
SS
8190 {
8191 case DW_TAG_subprogram:
b1dc1806 8192 case DW_TAG_inlined_subroutine:
cdc07690 8193 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
f9b5d5ea
TV
8194 if (cu->language == language_cplus)
8195 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8196 set_addrmap, cu);
c906108c 8197 break;
72929c62 8198 case DW_TAG_constant:
c906108c
SS
8199 case DW_TAG_variable:
8200 case DW_TAG_typedef:
91c24f0a 8201 case DW_TAG_union_type:
317d2668
TV
8202 if (!pdi->is_declaration
8203 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8204 {
72bf9492 8205 add_partial_symbol (pdi, cu);
63d06c5c
DC
8206 }
8207 break;
c906108c 8208 case DW_TAG_class_type:
680b30c7 8209 case DW_TAG_interface_type:
c906108c 8210 case DW_TAG_structure_type:
72bf9492 8211 if (!pdi->is_declaration)
c906108c 8212 {
72bf9492 8213 add_partial_symbol (pdi, cu);
c906108c 8214 }
b7fee5a3
KS
8215 if ((cu->language == language_rust
8216 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8217 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8218 set_addrmap, cu);
c906108c 8219 break;
91c24f0a 8220 case DW_TAG_enumeration_type:
72bf9492
DJ
8221 if (!pdi->is_declaration)
8222 add_partial_enumeration (pdi, cu);
c906108c
SS
8223 break;
8224 case DW_TAG_base_type:
a02abb62 8225 case DW_TAG_subrange_type:
c906108c 8226 /* File scope base type definitions are added to the partial
c5aa993b 8227 symbol table. */
72bf9492 8228 add_partial_symbol (pdi, cu);
c906108c 8229 break;
d9fa45fe 8230 case DW_TAG_namespace:
cdc07690 8231 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8232 break;
5d7cb8df 8233 case DW_TAG_module:
59c35742
AB
8234 if (!pdi->is_declaration)
8235 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8236 break;
95554aad
TT
8237 case DW_TAG_imported_unit:
8238 {
8239 struct dwarf2_per_cu_data *per_cu;
8240
f4dc4d17
DE
8241 /* For now we don't handle imported units in type units. */
8242 if (cu->per_cu->is_debug_types)
8243 {
8244 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8245 " supported in type units [in module %s]"),
5e22e966 8246 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
8247 }
8248
e3b94546 8249 per_cu = dwarf2_find_containing_comp_unit
5e22e966 8250 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
8251
8252 /* Go read the partial unit, if needed. */
8253 if (per_cu->v.psymtab == NULL)
ab432490
SM
8254 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8255 cu->language);
95554aad 8256
ae640021 8257 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8258 }
8259 break;
74921315
KS
8260 case DW_TAG_imported_declaration:
8261 add_partial_symbol (pdi, cu);
8262 break;
c906108c
SS
8263 default:
8264 break;
8265 }
8266 }
8267
72bf9492
DJ
8268 /* If the die has a sibling, skip to the sibling. */
8269
8270 pdi = pdi->die_sibling;
8271 }
8272}
8273
8274/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8275
72bf9492 8276 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8277 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8278 Enumerators are an exception; they use the scope of their parent
8279 enumeration type, i.e. the name of the enumeration type is not
8280 prepended to the enumerator.
91c24f0a 8281
72bf9492
DJ
8282 There are two complexities. One is DW_AT_specification; in this
8283 case "parent" means the parent of the target of the specification,
8284 instead of the direct parent of the DIE. The other is compilers
8285 which do not emit DW_TAG_namespace; in this case we try to guess
8286 the fully qualified name of structure types from their members'
8287 linkage names. This must be done using the DIE's children rather
8288 than the children of any DW_AT_specification target. We only need
8289 to do this for structures at the top level, i.e. if the target of
8290 any DW_AT_specification (if any; otherwise the DIE itself) does not
8291 have a parent. */
8292
8293/* Compute the scope prefix associated with PDI's parent, in
8294 compilation unit CU. The result will be allocated on CU's
8295 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8296 field. NULL is returned if no prefix is necessary. */
15d034d0 8297static const char *
72bf9492
DJ
8298partial_die_parent_scope (struct partial_die_info *pdi,
8299 struct dwarf2_cu *cu)
8300{
15d034d0 8301 const char *grandparent_scope;
72bf9492 8302 struct partial_die_info *parent, *real_pdi;
91c24f0a 8303
72bf9492
DJ
8304 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8305 then this means the parent of the specification DIE. */
8306
8307 real_pdi = pdi;
72bf9492 8308 while (real_pdi->has_specification)
fb816e8b 8309 {
122cf0f2
AB
8310 auto res = find_partial_die (real_pdi->spec_offset,
8311 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8312 real_pdi = res.pdi;
8313 cu = res.cu;
8314 }
72bf9492
DJ
8315
8316 parent = real_pdi->die_parent;
8317 if (parent == NULL)
8318 return NULL;
8319
8320 if (parent->scope_set)
8321 return parent->scope;
8322
52356b79 8323 parent->fixup (cu);
72bf9492 8324
10b3939b 8325 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8326
acebe513
UW
8327 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8328 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8329 Work around this problem here. */
8330 if (cu->language == language_cplus
6e70227d 8331 && parent->tag == DW_TAG_namespace
7d00ffec 8332 && strcmp (parent->name (cu), "::") == 0
acebe513
UW
8333 && grandparent_scope == NULL)
8334 {
8335 parent->scope = NULL;
8336 parent->scope_set = 1;
8337 return NULL;
8338 }
8339
0a4b0913 8340 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8341 if (pdi->tag == DW_TAG_enumerator)
8342 /* Enumerators should not get the name of the enumeration as a prefix. */
8343 parent->scope = grandparent_scope;
8344 else if (parent->tag == DW_TAG_namespace
f55ee35c 8345 || parent->tag == DW_TAG_module
72bf9492
DJ
8346 || parent->tag == DW_TAG_structure_type
8347 || parent->tag == DW_TAG_class_type
680b30c7 8348 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8349 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8350 || parent->tag == DW_TAG_enumeration_type
8351 || (cu->language == language_fortran
8352 && parent->tag == DW_TAG_subprogram
8353 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8354 {
8355 if (grandparent_scope == NULL)
7d00ffec 8356 parent->scope = parent->name (cu);
72bf9492 8357 else
3e43a32a
MS
8358 parent->scope = typename_concat (&cu->comp_unit_obstack,
8359 grandparent_scope,
7d00ffec 8360 parent->name (cu), 0, cu);
72bf9492 8361 }
72bf9492
DJ
8362 else
8363 {
8364 /* FIXME drow/2004-04-01: What should we be doing with
8365 function-local names? For partial symbols, we should probably be
8366 ignoring them. */
fa9c3fa0
TT
8367 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8368 dwarf_tag_name (parent->tag),
8369 sect_offset_str (pdi->sect_off));
72bf9492 8370 parent->scope = grandparent_scope;
c906108c
SS
8371 }
8372
72bf9492
DJ
8373 parent->scope_set = 1;
8374 return parent->scope;
8375}
8376
8377/* Return the fully scoped name associated with PDI, from compilation unit
8378 CU. The result will be allocated with malloc. */
4568ecf9 8379
43816ebc 8380static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8381partial_die_full_name (struct partial_die_info *pdi,
8382 struct dwarf2_cu *cu)
8383{
15d034d0 8384 const char *parent_scope;
72bf9492 8385
98bfdba5
PA
8386 /* If this is a template instantiation, we can not work out the
8387 template arguments from partial DIEs. So, unfortunately, we have
8388 to go through the full DIEs. At least any work we do building
8389 types here will be reused if full symbols are loaded later. */
8390 if (pdi->has_template_arguments)
8391 {
52356b79 8392 pdi->fixup (cu);
98bfdba5 8393
7d00ffec 8394 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
98bfdba5
PA
8395 {
8396 struct die_info *die;
8397 struct attribute attr;
8398 struct dwarf2_cu *ref_cu = cu;
8399
b64f50a1 8400 /* DW_FORM_ref_addr is using section offset. */
b4069958 8401 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8402 attr.form = DW_FORM_ref_addr;
9c541725 8403 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8404 die = follow_die_ref (NULL, &attr, &ref_cu);
8405
43816ebc 8406 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8407 }
8408 }
8409
72bf9492
DJ
8410 parent_scope = partial_die_parent_scope (pdi, cu);
8411 if (parent_scope == NULL)
8412 return NULL;
8413 else
43816ebc 8414 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7d00ffec
TT
8415 pdi->name (cu),
8416 0, cu));
c906108c
SS
8417}
8418
8419static void
72bf9492 8420add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8421{
976ca316
SM
8422 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8423 struct objfile *objfile = per_objfile->objfile;
08feed99 8424 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8425 CORE_ADDR addr = 0;
15d034d0 8426 const char *actual_name = NULL;
e142c38c
DJ
8427 CORE_ADDR baseaddr;
8428
b3b3bada 8429 baseaddr = objfile->text_section_offset ();
c906108c 8430
43816ebc
TT
8431 gdb::unique_xmalloc_ptr<char> built_actual_name
8432 = partial_die_full_name (pdi, cu);
15d034d0 8433 if (built_actual_name != NULL)
43816ebc 8434 actual_name = built_actual_name.get ();
63d06c5c 8435
72bf9492 8436 if (actual_name == NULL)
7d00ffec 8437 actual_name = pdi->name (cu);
72bf9492 8438
76e288d1
TT
8439 partial_symbol psymbol;
8440 memset (&psymbol, 0, sizeof (psymbol));
8441 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8442 psymbol.ginfo.section = -1;
8443
8444 /* The code below indicates that the psymbol should be installed by
8445 setting this. */
8446 gdb::optional<psymbol_placement> where;
8447
c906108c
SS
8448 switch (pdi->tag)
8449 {
b1dc1806 8450 case DW_TAG_inlined_subroutine:
c906108c 8451 case DW_TAG_subprogram:
79748972
TT
8452 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8453 - baseaddr);
0a4b0913
AB
8454 if (pdi->is_external
8455 || cu->language == language_ada
8456 || (cu->language == language_fortran
8457 && pdi->die_parent != NULL
8458 && pdi->die_parent->tag == DW_TAG_subprogram))
8459 {
8460 /* Normally, only "external" DIEs are part of the global scope.
8461 But in Ada and Fortran, we want to be able to access nested
8462 procedures globally. So all Ada and Fortran subprograms are
8463 stored in the global scope. */
76e288d1 8464 where = psymbol_placement::GLOBAL;
c906108c
SS
8465 }
8466 else
76e288d1
TT
8467 where = psymbol_placement::STATIC;
8468
8469 psymbol.domain = VAR_DOMAIN;
8470 psymbol.aclass = LOC_BLOCK;
8471 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8472 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8473
8474 if (pdi->main_subprogram && actual_name != NULL)
8475 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8476 break;
72929c62 8477 case DW_TAG_constant:
76e288d1
TT
8478 psymbol.domain = VAR_DOMAIN;
8479 psymbol.aclass = LOC_STATIC;
8480 where = (pdi->is_external
8481 ? psymbol_placement::GLOBAL
8482 : psymbol_placement::STATIC);
72929c62 8483 break;
c906108c 8484 case DW_TAG_variable:
95554aad
TT
8485 if (pdi->d.locdesc)
8486 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8487
95554aad 8488 if (pdi->d.locdesc
caac4577 8489 && addr == 0
976ca316 8490 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8491 {
8492 /* A global or static variable may also have been stripped
8493 out by the linker if unused, in which case its address
8494 will be nullified; do not add such variables into partial
8495 symbol table then. */
8496 }
8497 else if (pdi->is_external)
c906108c
SS
8498 {
8499 /* Global Variable.
8500 Don't enter into the minimal symbol tables as there is
8501 a minimal symbol table entry from the ELF symbols already.
8502 Enter into partial symbol table if it has a location
8503 descriptor or a type.
8504 If the location descriptor is missing, new_symbol will create
8505 a LOC_UNRESOLVED symbol, the address of the variable will then
8506 be determined from the minimal symbol table whenever the variable
8507 is referenced.
8508 The address for the partial symbol table entry is not
8509 used by GDB, but it comes in handy for debugging partial symbol
8510 table building. */
8511
95554aad 8512 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8513 {
8514 psymbol.domain = VAR_DOMAIN;
8515 psymbol.aclass = LOC_STATIC;
8516 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8517 psymbol.ginfo.value.address = addr;
8518 where = psymbol_placement::GLOBAL;
8519 }
c906108c
SS
8520 }
8521 else
8522 {
ff908ebf
AW
8523 int has_loc = pdi->d.locdesc != NULL;
8524
8525 /* Static Variable. Skip symbols whose value we cannot know (those
8526 without location descriptors or constant values). */
8527 if (!has_loc && !pdi->has_const_value)
43816ebc 8528 return;
ff908ebf 8529
76e288d1
TT
8530 psymbol.domain = VAR_DOMAIN;
8531 psymbol.aclass = LOC_STATIC;
8532 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8533 if (has_loc)
8534 psymbol.ginfo.value.address = addr;
8535 where = psymbol_placement::STATIC;
c906108c
SS
8536 }
8537 break;
8538 case DW_TAG_typedef:
8539 case DW_TAG_base_type:
a02abb62 8540 case DW_TAG_subrange_type:
76e288d1
TT
8541 psymbol.domain = VAR_DOMAIN;
8542 psymbol.aclass = LOC_TYPEDEF;
8543 where = psymbol_placement::STATIC;
c906108c 8544 break;
74921315 8545 case DW_TAG_imported_declaration:
72bf9492 8546 case DW_TAG_namespace:
76e288d1
TT
8547 psymbol.domain = VAR_DOMAIN;
8548 psymbol.aclass = LOC_TYPEDEF;
8549 where = psymbol_placement::GLOBAL;
72bf9492 8550 break;
530e8392 8551 case DW_TAG_module:
a5fd13a9
BH
8552 /* With Fortran 77 there might be a "BLOCK DATA" module
8553 available without any name. If so, we skip the module as it
8554 doesn't bring any value. */
8555 if (actual_name != nullptr)
76e288d1
TT
8556 {
8557 psymbol.domain = MODULE_DOMAIN;
8558 psymbol.aclass = LOC_TYPEDEF;
8559 where = psymbol_placement::GLOBAL;
8560 }
530e8392 8561 break;
c906108c 8562 case DW_TAG_class_type:
680b30c7 8563 case DW_TAG_interface_type:
c906108c
SS
8564 case DW_TAG_structure_type:
8565 case DW_TAG_union_type:
8566 case DW_TAG_enumeration_type:
fa4028e9
JB
8567 /* Skip external references. The DWARF standard says in the section
8568 about "Structure, Union, and Class Type Entries": "An incomplete
8569 structure, union or class type is represented by a structure,
8570 union or class entry that does not have a byte size attribute
8571 and that has a DW_AT_declaration attribute." */
8572 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8573 return;
fa4028e9 8574
63d06c5c
DC
8575 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8576 static vs. global. */
76e288d1
TT
8577 psymbol.domain = STRUCT_DOMAIN;
8578 psymbol.aclass = LOC_TYPEDEF;
8579 where = (cu->language == language_cplus
8580 ? psymbol_placement::GLOBAL
8581 : psymbol_placement::STATIC);
c906108c
SS
8582 break;
8583 case DW_TAG_enumerator:
76e288d1
TT
8584 psymbol.domain = VAR_DOMAIN;
8585 psymbol.aclass = LOC_CONST;
8586 where = (cu->language == language_cplus
8587 ? psymbol_placement::GLOBAL
8588 : psymbol_placement::STATIC);
c906108c
SS
8589 break;
8590 default:
8591 break;
8592 }
76e288d1
TT
8593
8594 if (where.has_value ())
8595 {
f049a313
TT
8596 if (built_actual_name != nullptr)
8597 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8598 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8599 psymbol.ginfo.set_linkage_name (actual_name);
8600 else
8601 {
8602 psymbol.ginfo.set_demangled_name (actual_name,
8603 &objfile->objfile_obstack);
8604 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8605 }
76e288d1
TT
8606 add_psymbol_to_list (psymbol, *where, objfile);
8607 }
c906108c
SS
8608}
8609
5c4e30ca
DC
8610/* Read a partial die corresponding to a namespace; also, add a symbol
8611 corresponding to that namespace to the symbol table. NAMESPACE is
8612 the name of the enclosing namespace. */
91c24f0a 8613
72bf9492
DJ
8614static void
8615add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8616 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8617 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8618{
72bf9492 8619 /* Add a symbol for the namespace. */
e7c27a73 8620
72bf9492 8621 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8622
8623 /* Now scan partial symbols in that namespace. */
8624
91c24f0a 8625 if (pdi->has_children)
cdc07690 8626 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8627}
8628
5d7cb8df
JK
8629/* Read a partial die corresponding to a Fortran module. */
8630
8631static void
8632add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8633 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8634{
530e8392
KB
8635 /* Add a symbol for the namespace. */
8636
8637 add_partial_symbol (pdi, cu);
8638
f55ee35c 8639 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8640
8641 if (pdi->has_children)
cdc07690 8642 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8643}
8644
b1dc1806
XR
8645/* Read a partial die corresponding to a subprogram or an inlined
8646 subprogram and create a partial symbol for that subprogram.
8647 When the CU language allows it, this routine also defines a partial
8648 symbol for each nested subprogram that this subprogram contains.
8649 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8650 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8651
cdc07690
YQ
8652 PDI may also be a lexical block, in which case we simply search
8653 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8654 Again, this is only performed when the CU language allows this
8655 type of definitions. */
8656
8657static void
8658add_partial_subprogram (struct partial_die_info *pdi,
8659 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8660 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8661{
b1dc1806 8662 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8663 {
8664 if (pdi->has_pc_info)
8665 {
8666 if (pdi->lowpc < *lowpc)
8667 *lowpc = pdi->lowpc;
8668 if (pdi->highpc > *highpc)
8669 *highpc = pdi->highpc;
cdc07690 8670 if (set_addrmap)
5734ee8b 8671 {
5e22e966 8672 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 8673 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8674 CORE_ADDR baseaddr;
b926417a
TT
8675 CORE_ADDR this_highpc;
8676 CORE_ADDR this_lowpc;
5734ee8b 8677
b3b3bada 8678 baseaddr = objfile->text_section_offset ();
b926417a
TT
8679 this_lowpc
8680 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8681 pdi->lowpc + baseaddr)
8682 - baseaddr);
8683 this_highpc
8684 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8685 pdi->highpc + baseaddr)
8686 - baseaddr);
d320c2b5 8687 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8688 this_lowpc, this_highpc - 1,
9291a0cd 8689 cu->per_cu->v.psymtab);
5734ee8b 8690 }
481860b3
GB
8691 }
8692
8693 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8694 {
bc30ff58 8695 if (!pdi->is_declaration)
e8d05480
JB
8696 /* Ignore subprogram DIEs that do not have a name, they are
8697 illegal. Do not emit a complaint at this point, we will
8698 do so when we convert this psymtab into a symtab. */
7d00ffec 8699 if (pdi->name (cu))
e8d05480 8700 add_partial_symbol (pdi, cu);
bc30ff58
JB
8701 }
8702 }
6e70227d 8703
bc30ff58
JB
8704 if (! pdi->has_children)
8705 return;
8706
0a4b0913 8707 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8708 {
8709 pdi = pdi->die_child;
8710 while (pdi != NULL)
8711 {
52356b79 8712 pdi->fixup (cu);
bc30ff58 8713 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8714 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8715 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8716 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8717 pdi = pdi->die_sibling;
8718 }
8719 }
8720}
8721
91c24f0a
DC
8722/* Read a partial die corresponding to an enumeration type. */
8723
72bf9492
DJ
8724static void
8725add_partial_enumeration (struct partial_die_info *enum_pdi,
8726 struct dwarf2_cu *cu)
91c24f0a 8727{
72bf9492 8728 struct partial_die_info *pdi;
91c24f0a 8729
7d00ffec 8730 if (enum_pdi->name (cu) != NULL)
72bf9492
DJ
8731 add_partial_symbol (enum_pdi, cu);
8732
8733 pdi = enum_pdi->die_child;
8734 while (pdi)
91c24f0a 8735 {
7d00ffec 8736 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
b98664d3 8737 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8738 else
72bf9492
DJ
8739 add_partial_symbol (pdi, cu);
8740 pdi = pdi->die_sibling;
91c24f0a 8741 }
91c24f0a
DC
8742}
8743
6caca83c
CC
8744/* Return the initial uleb128 in the die at INFO_PTR. */
8745
8746static unsigned int
d521ce57 8747peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8748{
8749 unsigned int bytes_read;
8750
8751 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8752}
8753
685af9cd
TT
8754/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8755 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8756
4bb7a0a7
DJ
8757 Return the corresponding abbrev, or NULL if the number is zero (indicating
8758 an empty DIE). In either case *BYTES_READ will be set to the length of
8759 the initial number. */
8760
8761static struct abbrev_info *
685af9cd
TT
8762peek_die_abbrev (const die_reader_specs &reader,
8763 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8764{
685af9cd 8765 dwarf2_cu *cu = reader.cu;
5e22e966 8766 bfd *abfd = cu->per_objfile->objfile->obfd;
685af9cd
TT
8767 unsigned int abbrev_number
8768 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8769
8770 if (abbrev_number == 0)
8771 return NULL;
8772
685af9cd 8773 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8774 if (!abbrev)
8775 {
422b9917 8776 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8777 " at offset %s [in module %s]"),
422b9917 8778 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8779 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8780 }
8781
8782 return abbrev;
8783}
8784
93311388
DE
8785/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8786 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8787 DIE. Any children of the skipped DIEs will also be skipped. */
8788
d521ce57
TT
8789static const gdb_byte *
8790skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8791{
4bb7a0a7
DJ
8792 while (1)
8793 {
685af9cd
TT
8794 unsigned int bytes_read;
8795 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8796
4bb7a0a7
DJ
8797 if (abbrev == NULL)
8798 return info_ptr + bytes_read;
8799 else
dee91e82 8800 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8801 }
8802}
8803
93311388
DE
8804/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8805 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8806 abbrev corresponding to that skipped uleb128 should be passed in
8807 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8808 children. */
8809
d521ce57
TT
8810static const gdb_byte *
8811skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8812 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8813{
8814 unsigned int bytes_read;
8815 struct attribute attr;
dee91e82
DE
8816 bfd *abfd = reader->abfd;
8817 struct dwarf2_cu *cu = reader->cu;
d521ce57 8818 const gdb_byte *buffer = reader->buffer;
f664829e 8819 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8820 unsigned int form, i;
8821
8822 for (i = 0; i < abbrev->num_attrs; i++)
8823 {
8824 /* The only abbrev we care about is DW_AT_sibling. */
8825 if (abbrev->attrs[i].name == DW_AT_sibling)
8826 {
18a8505e
AT
8827 bool ignored;
8828 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8829 &ignored);
4bb7a0a7 8830 if (attr.form == DW_FORM_ref_addr)
b98664d3 8831 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8832 else
b9502d3f 8833 {
0826b30a 8834 sect_offset off = attr.get_ref_die_offset ();
9c541725 8835 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8836
8837 if (sibling_ptr < info_ptr)
b98664d3 8838 complaint (_("DW_AT_sibling points backwards"));
22869d73 8839 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8840 reader->die_section->overflow_complaint ();
b9502d3f
WN
8841 else
8842 return sibling_ptr;
8843 }
4bb7a0a7
DJ
8844 }
8845
8846 /* If it isn't DW_AT_sibling, skip this attribute. */
8847 form = abbrev->attrs[i].form;
8848 skip_attribute:
8849 switch (form)
8850 {
4bb7a0a7 8851 case DW_FORM_ref_addr:
ae411497
TT
8852 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8853 and later it is offset sized. */
8854 if (cu->header.version == 2)
8855 info_ptr += cu->header.addr_size;
8856 else
8857 info_ptr += cu->header.offset_size;
8858 break;
36586728
TT
8859 case DW_FORM_GNU_ref_alt:
8860 info_ptr += cu->header.offset_size;
8861 break;
ae411497 8862 case DW_FORM_addr:
4bb7a0a7
DJ
8863 info_ptr += cu->header.addr_size;
8864 break;
8865 case DW_FORM_data1:
8866 case DW_FORM_ref1:
8867 case DW_FORM_flag:
8fe0f950 8868 case DW_FORM_strx1:
4bb7a0a7
DJ
8869 info_ptr += 1;
8870 break;
2dc7f7b3 8871 case DW_FORM_flag_present:
43988095 8872 case DW_FORM_implicit_const:
2dc7f7b3 8873 break;
4bb7a0a7
DJ
8874 case DW_FORM_data2:
8875 case DW_FORM_ref2:
8fe0f950 8876 case DW_FORM_strx2:
4bb7a0a7
DJ
8877 info_ptr += 2;
8878 break;
8fe0f950
AT
8879 case DW_FORM_strx3:
8880 info_ptr += 3;
8881 break;
4bb7a0a7
DJ
8882 case DW_FORM_data4:
8883 case DW_FORM_ref4:
8fe0f950 8884 case DW_FORM_strx4:
4bb7a0a7
DJ
8885 info_ptr += 4;
8886 break;
8887 case DW_FORM_data8:
8888 case DW_FORM_ref8:
55f1336d 8889 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8890 info_ptr += 8;
8891 break;
0224619f
JK
8892 case DW_FORM_data16:
8893 info_ptr += 16;
8894 break;
4bb7a0a7 8895 case DW_FORM_string:
9b1c24c8 8896 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8897 info_ptr += bytes_read;
8898 break;
2dc7f7b3 8899 case DW_FORM_sec_offset:
4bb7a0a7 8900 case DW_FORM_strp:
36586728 8901 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8902 info_ptr += cu->header.offset_size;
8903 break;
2dc7f7b3 8904 case DW_FORM_exprloc:
4bb7a0a7
DJ
8905 case DW_FORM_block:
8906 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8907 info_ptr += bytes_read;
8908 break;
8909 case DW_FORM_block1:
8910 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8911 break;
8912 case DW_FORM_block2:
8913 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8914 break;
8915 case DW_FORM_block4:
8916 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8917 break;
336d760d 8918 case DW_FORM_addrx:
cf532bd1 8919 case DW_FORM_strx:
4bb7a0a7
DJ
8920 case DW_FORM_sdata:
8921 case DW_FORM_udata:
8922 case DW_FORM_ref_udata:
3019eac3
DE
8923 case DW_FORM_GNU_addr_index:
8924 case DW_FORM_GNU_str_index:
18a8505e 8925 case DW_FORM_rnglistx:
41144253 8926 case DW_FORM_loclistx:
d521ce57 8927 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8928 break;
8929 case DW_FORM_indirect:
8930 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8931 info_ptr += bytes_read;
8932 /* We need to continue parsing from here, so just go back to
8933 the top. */
8934 goto skip_attribute;
8935
8936 default:
3e43a32a
MS
8937 error (_("Dwarf Error: Cannot handle %s "
8938 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8939 dwarf_form_name (form),
8940 bfd_get_filename (abfd));
8941 }
8942 }
8943
8944 if (abbrev->has_children)
dee91e82 8945 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8946 else
8947 return info_ptr;
8948}
8949
93311388 8950/* Locate ORIG_PDI's sibling.
dee91e82 8951 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8952
d521ce57 8953static const gdb_byte *
dee91e82
DE
8954locate_pdi_sibling (const struct die_reader_specs *reader,
8955 struct partial_die_info *orig_pdi,
d521ce57 8956 const gdb_byte *info_ptr)
91c24f0a
DC
8957{
8958 /* Do we know the sibling already? */
72bf9492 8959
91c24f0a
DC
8960 if (orig_pdi->sibling)
8961 return orig_pdi->sibling;
8962
8963 /* Are there any children to deal with? */
8964
8965 if (!orig_pdi->has_children)
8966 return info_ptr;
8967
4bb7a0a7 8968 /* Skip the children the long way. */
91c24f0a 8969
dee91e82 8970 return skip_children (reader, info_ptr);
91c24f0a
DC
8971}
8972
257e7a09 8973/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8974 not NULL. */
c906108c 8975
891813be
TT
8976void
8977dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8978{
976ca316 8979 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 8980
976ca316 8981 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
af758d11 8982
077cbab2
TT
8983 /* If this psymtab is constructed from a debug-only objfile, the
8984 has_section_at_zero flag will not necessarily be correct. We
8985 can get the correct value for this flag by looking at the data
8986 associated with the (presumably stripped) associated objfile. */
8987 if (objfile->separate_debug_objfile_backlink)
c906108c 8988 {
976ca316 8989 dwarf2_per_objfile *per_objfile_backlink
077cbab2 8990 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8991
976ca316
SM
8992 per_objfile->per_bfd->has_section_at_zero
8993 = per_objfile_backlink->per_bfd->has_section_at_zero;
077cbab2 8994 }
98bfdba5 8995
8566b89b 8996 expand_psymtab (objfile);
95554aad 8997
976ca316 8998 process_cu_includes (per_objfile);
c906108c 8999}
9cdd5dbd
DE
9000\f
9001/* Reading in full CUs. */
c906108c 9002
10b3939b
DJ
9003/* Add PER_CU to the queue. */
9004
9005static void
120ce1b5
SM
9006queue_comp_unit (dwarf2_per_cu_data *per_cu,
9007 dwarf2_per_objfile *per_objfile,
95554aad 9008 enum language pretend_language)
10b3939b 9009{
10b3939b 9010 per_cu->queued = 1;
120ce1b5 9011 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
9012}
9013
89e63ee4
DE
9014/* If PER_CU is not yet queued, add it to the queue.
9015 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9016 dependency.
0907af0c 9017 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9018 meaning either PER_CU is already queued or it is already loaded.
9019
9020 N.B. There is an invariant here that if a CU is queued then it is loaded.
9021 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9022
9023static int
89e63ee4 9024maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
9025 dwarf2_per_cu_data *per_cu,
9026 dwarf2_per_objfile *per_objfile,
0907af0c
DE
9027 enum language pretend_language)
9028{
9029 /* We may arrive here during partial symbol reading, if we need full
9030 DIEs to process an unusual case (e.g. template arguments). Do
9031 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 9032 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c 9033 {
7188ed02
SM
9034 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9035
9036 if (cu == NULL || cu->dies == NULL)
0907af0c
DE
9037 return 1;
9038 return 0;
9039 }
9040
9041 /* Mark the dependence relation so that we don't flush PER_CU
9042 too early. */
89e63ee4
DE
9043 if (dependent_cu != NULL)
9044 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9045
9046 /* If it's already on the queue, we have nothing to do. */
9047 if (per_cu->queued)
9048 return 0;
9049
9050 /* If the compilation unit is already loaded, just mark it as
9051 used. */
7188ed02
SM
9052 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9053 if (cu != nullptr)
0907af0c 9054 {
7188ed02 9055 cu->last_used = 0;
0907af0c
DE
9056 return 0;
9057 }
9058
9059 /* Add it to the queue. */
120ce1b5 9060 queue_comp_unit (per_cu, per_objfile, pretend_language);
0907af0c
DE
9061
9062 return 1;
9063}
9064
10b3939b
DJ
9065/* Process the queue. */
9066
9067static void
976ca316 9068process_queue (dwarf2_per_objfile *per_objfile)
10b3939b 9069{
b4f54984 9070 if (dwarf_read_debug)
45cfd468
DE
9071 {
9072 fprintf_unfiltered (gdb_stdlog,
9073 "Expanding one or more symtabs of objfile %s ...\n",
976ca316 9074 objfile_name (per_objfile->objfile));
45cfd468
DE
9075 }
9076
03dd20cc
DJ
9077 /* The queue starts out with one item, but following a DIE reference
9078 may load a new CU, adding it to the end of the queue. */
976ca316 9079 while (!per_objfile->per_bfd->queue.empty ())
10b3939b 9080 {
976ca316 9081 dwarf2_queue_item &item = per_objfile->per_bfd->queue.front ();
7188ed02 9082 dwarf2_per_cu_data *per_cu = item.per_cu;
39856def 9083
976ca316 9084 if (!per_objfile->symtab_set_p (per_cu))
f4dc4d17 9085 {
976ca316 9086 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
f4dc4d17 9087
7188ed02
SM
9088 /* Skip dummy CUs. */
9089 if (cu != nullptr)
73be47f5 9090 {
7188ed02
SM
9091 unsigned int debug_print_threshold;
9092 char buf[100];
9093
9094 if (per_cu->is_debug_types)
9095 {
9096 struct signatured_type *sig_type =
9097 (struct signatured_type *) per_cu;
9098
9099 sprintf (buf, "TU %s at offset %s",
9100 hex_string (sig_type->signature),
9101 sect_offset_str (per_cu->sect_off));
9102 /* There can be 100s of TUs.
9103 Only print them in verbose mode. */
9104 debug_print_threshold = 2;
9105 }
9106 else
9107 {
9108 sprintf (buf, "CU at offset %s",
9109 sect_offset_str (per_cu->sect_off));
9110 debug_print_threshold = 1;
9111 }
247f5c4f 9112
7188ed02
SM
9113 if (dwarf_read_debug >= debug_print_threshold)
9114 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17 9115
7188ed02
SM
9116 if (per_cu->is_debug_types)
9117 process_full_type_unit (cu, item.pretend_language);
9118 else
9119 process_full_comp_unit (cu, item.pretend_language);
f4dc4d17 9120
7188ed02
SM
9121 if (dwarf_read_debug >= debug_print_threshold)
9122 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9123 }
f4dc4d17 9124 }
10b3939b 9125
7188ed02 9126 per_cu->queued = 0;
976ca316 9127 per_objfile->per_bfd->queue.pop ();
10b3939b
DJ
9128 }
9129
b4f54984 9130 if (dwarf_read_debug)
45cfd468
DE
9131 {
9132 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
976ca316 9133 objfile_name (per_objfile->objfile));
45cfd468 9134 }
10b3939b
DJ
9135}
9136
10b3939b
DJ
9137/* Read in full symbols for PST, and anything it depends on. */
9138
8566b89b
TT
9139void
9140dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9141{
af758d11 9142 gdb_assert (!readin_p (objfile));
95554aad 9143
17ee85fc
TT
9144 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9145 free_cached_comp_units freer (per_objfile);
48993951 9146 expand_dependencies (objfile);
aaa75496 9147
97a1449a 9148 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 9149 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
9150}
9151
af758d11
SM
9152/* See psympriv.h. */
9153
9154bool
9155dwarf2_psymtab::readin_p (struct objfile *objfile) const
9156{
9157 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9158 return per_objfile->symtab_set_p (per_cu_data);
9159}
9160
9161/* See psympriv.h. */
9162
9163compunit_symtab *
9164dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9165{
9166 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9167 return per_objfile->get_symtab (per_cu_data);
9168}
9169
dee91e82
DE
9170/* Trivial hash function for die_info: the hash value of a DIE
9171 is its offset in .debug_info for this objfile. */
10b3939b 9172
dee91e82
DE
9173static hashval_t
9174die_hash (const void *item)
10b3939b 9175{
9a3c8263 9176 const struct die_info *die = (const struct die_info *) item;
6502dd73 9177
9c541725 9178 return to_underlying (die->sect_off);
dee91e82 9179}
63d06c5c 9180
dee91e82
DE
9181/* Trivial comparison function for die_info structures: two DIEs
9182 are equal if they have the same offset. */
98bfdba5 9183
dee91e82
DE
9184static int
9185die_eq (const void *item_lhs, const void *item_rhs)
9186{
9a3c8263
SM
9187 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9188 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9189
9c541725 9190 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9191}
c906108c 9192
c0ab21c2 9193/* Load the DIEs associated with PER_CU into memory. */
c906108c 9194
dee91e82 9195static void
ab432490
SM
9196load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9197 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
9198 bool skip_partial,
9199 enum language pretend_language)
dee91e82 9200{
c0ab21c2
TT
9201 gdb_assert (! this_cu->is_debug_types);
9202
7188ed02
SM
9203 dwarf2_cu *existing_cu = per_objfile->get_cu (this_cu);
9204 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
c0ab21c2
TT
9205 if (reader.dummy_p)
9206 return;
9207
9208 struct dwarf2_cu *cu = reader.cu;
9209 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9210
dee91e82
DE
9211 gdb_assert (cu->die_hash == NULL);
9212 cu->die_hash =
9213 htab_create_alloc_ex (cu->header.length / 12,
9214 die_hash,
9215 die_eq,
9216 NULL,
9217 &cu->comp_unit_obstack,
9218 hashtab_obstack_allocate,
9219 dummy_obstack_deallocate);
e142c38c 9220
3e225074 9221 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9222 reader.comp_unit_die->child
9223 = read_die_and_siblings (&reader, reader.info_ptr,
9224 &info_ptr, reader.comp_unit_die);
9225 cu->dies = reader.comp_unit_die;
dee91e82 9226 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9227
9228 /* We try not to read any attributes in this function, because not
9cdd5dbd 9229 all CUs needed for references have been loaded yet, and symbol
10b3939b 9230 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9231 or we won't be able to build types correctly.
9232 Similarly, if we do not read the producer, we can not apply
9233 producer-specific interpretation. */
c0ab21c2 9234 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9235
9236 reader.keep ();
10b3939b
DJ
9237}
9238
3da10d80
KS
9239/* Add a DIE to the delayed physname list. */
9240
9241static void
9242add_to_method_list (struct type *type, int fnfield_index, int index,
9243 const char *name, struct die_info *die,
9244 struct dwarf2_cu *cu)
9245{
9246 struct delayed_method_info mi;
9247 mi.type = type;
9248 mi.fnfield_index = fnfield_index;
9249 mi.index = index;
9250 mi.name = name;
9251 mi.die = die;
c89b44cd 9252 cu->method_list.push_back (mi);
3da10d80
KS
9253}
9254
3693fdb3
PA
9255/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9256 "const" / "volatile". If so, decrements LEN by the length of the
9257 modifier and return true. Otherwise return false. */
9258
9259template<size_t N>
9260static bool
9261check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9262{
9263 size_t mod_len = sizeof (mod) - 1;
9264 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9265 {
9266 len -= mod_len;
9267 return true;
9268 }
9269 return false;
9270}
9271
3da10d80
KS
9272/* Compute the physnames of any methods on the CU's method list.
9273
9274 The computation of method physnames is delayed in order to avoid the
9275 (bad) condition that one of the method's formal parameters is of an as yet
9276 incomplete type. */
9277
9278static void
9279compute_delayed_physnames (struct dwarf2_cu *cu)
9280{
3693fdb3 9281 /* Only C++ delays computing physnames. */
c89b44cd 9282 if (cu->method_list.empty ())
3693fdb3
PA
9283 return;
9284 gdb_assert (cu->language == language_cplus);
9285
52941706 9286 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9287 {
1d06ead6 9288 const char *physname;
3da10d80 9289 struct fn_fieldlist *fn_flp
c89b44cd
TT
9290 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9291 physname = dwarf2_physname (mi.name, mi.die, cu);
9292 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9293 = physname ? physname : "";
3693fdb3
PA
9294
9295 /* Since there's no tag to indicate whether a method is a
9296 const/volatile overload, extract that information out of the
9297 demangled name. */
9298 if (physname != NULL)
9299 {
9300 size_t len = strlen (physname);
9301
9302 while (1)
9303 {
9304 if (physname[len] == ')') /* shortcut */
9305 break;
9306 else if (check_modifier (physname, len, " const"))
c89b44cd 9307 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9308 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9309 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9310 else
9311 break;
9312 }
9313 }
3da10d80 9314 }
c89b44cd
TT
9315
9316 /* The list is no longer needed. */
9317 cu->method_list.clear ();
3da10d80
KS
9318}
9319
a766d390
DE
9320/* Go objects should be embedded in a DW_TAG_module DIE,
9321 and it's not clear if/how imported objects will appear.
9322 To keep Go support simple until that's worked out,
9323 go back through what we've read and create something usable.
9324 We could do this while processing each DIE, and feels kinda cleaner,
9325 but that way is more invasive.
9326 This is to, for example, allow the user to type "p var" or "b main"
9327 without having to specify the package name, and allow lookups
9328 of module.object to work in contexts that use the expression
9329 parser. */
9330
9331static void
9332fixup_go_packaging (struct dwarf2_cu *cu)
9333{
421d1616 9334 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9335 struct pending *list;
9336 int i;
9337
c24bdb02 9338 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9339 list != NULL;
9340 list = list->next)
a766d390
DE
9341 {
9342 for (i = 0; i < list->nsyms; ++i)
9343 {
9344 struct symbol *sym = list->symbol[i];
9345
c1b5c1eb 9346 if (sym->language () == language_go
a766d390
DE
9347 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9348 {
421d1616
TT
9349 gdb::unique_xmalloc_ptr<char> this_package_name
9350 (go_symbol_package_name (sym));
a766d390
DE
9351
9352 if (this_package_name == NULL)
9353 continue;
9354 if (package_name == NULL)
421d1616 9355 package_name = std::move (this_package_name);
a766d390
DE
9356 else
9357 {
5e22e966 9358 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9359 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9360 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9361 (symbol_symtab (sym) != NULL
9362 ? symtab_to_filename_for_display
9363 (symbol_symtab (sym))
e3b94546 9364 : objfile_name (objfile)),
421d1616 9365 this_package_name.get (), package_name.get ());
a766d390
DE
9366 }
9367 }
9368 }
9369 }
9370
9371 if (package_name != NULL)
9372 {
5e22e966 9373 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9374 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9375 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9376 saved_package_name);
a766d390
DE
9377 struct symbol *sym;
9378
8c14c3a3 9379 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9380 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9381 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9382 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9383 e.g., "main" finds the "main" module and not C's main(). */
9384 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9385 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9386 SYMBOL_TYPE (sym) = type;
9387
c24bdb02 9388 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9389 }
9390}
9391
c9317f21
TT
9392/* Allocate a fully-qualified name consisting of the two parts on the
9393 obstack. */
9394
9395static const char *
9396rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9397{
9398 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9399}
9400
9c6a1327
TT
9401/* A helper that allocates a variant part to attach to a Rust enum
9402 type. OBSTACK is where the results should be allocated. TYPE is
9403 the type we're processing. DISCRIMINANT_INDEX is the index of the
9404 discriminant. It must be the index of one of the fields of TYPE.
9405 DEFAULT_INDEX is the index of the default field; or -1 if there is
9406 no default. RANGES is indexed by "effective" field number (the
9407 field index, but omitting the discriminant and default fields) and
9408 must hold the discriminant values used by the variants. Note that
9409 RANGES must have a lifetime at least as long as OBSTACK -- either
9410 already allocated on it, or static. */
c9317f21 9411
9c6a1327
TT
9412static void
9413alloc_rust_variant (struct obstack *obstack, struct type *type,
9414 int discriminant_index, int default_index,
9415 gdb::array_view<discriminant_range> ranges)
9416{
9417 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9418 must be handled by the caller. */
9419 gdb_assert (discriminant_index >= 0
1f704f76 9420 && discriminant_index < type->num_fields ());
c9317f21 9421 gdb_assert (default_index == -1
1f704f76 9422 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9423
9c6a1327 9424 /* We have one variant for each non-discriminant field. */
1f704f76 9425 int n_variants = type->num_fields () - 1;
c9317f21 9426
9c6a1327
TT
9427 variant *variants = new (obstack) variant[n_variants];
9428 int var_idx = 0;
9429 int range_idx = 0;
1f704f76 9430 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9431 {
9432 if (i == discriminant_index)
9433 continue;
c9317f21 9434
9c6a1327
TT
9435 variants[var_idx].first_field = i;
9436 variants[var_idx].last_field = i + 1;
9437
9438 /* The default field does not need a range, but other fields do.
9439 We skipped the discriminant above. */
9440 if (i != default_index)
9441 {
9442 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9443 ++range_idx;
9444 }
c9317f21 9445
9c6a1327
TT
9446 ++var_idx;
9447 }
9448
9449 gdb_assert (range_idx == ranges.size ());
9450 gdb_assert (var_idx == n_variants);
9451
9452 variant_part *part = new (obstack) variant_part;
9453 part->discriminant_index = discriminant_index;
940da03e 9454 part->is_unsigned = TYPE_UNSIGNED (type->field (discriminant_index).type ());
9c6a1327
TT
9455 part->variants = gdb::array_view<variant> (variants, n_variants);
9456
9457 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9458 gdb::array_view<variant_part> *prop_value
9459 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9460
9c6a1327
TT
9461 struct dynamic_prop prop;
9462 prop.kind = PROP_VARIANT_PARTS;
9463 prop.data.variant_parts = prop_value;
9464
5c54719c 9465 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9466}
9467
9468/* Some versions of rustc emitted enums in an unusual way.
9469
9470 Ordinary enums were emitted as unions. The first element of each
9471 structure in the union was named "RUST$ENUM$DISR". This element
9472 held the discriminant.
9473
9474 These versions of Rust also implemented the "non-zero"
9475 optimization. When the enum had two values, and one is empty and
9476 the other holds a pointer that cannot be zero, the pointer is used
9477 as the discriminant, with a zero value meaning the empty variant.
9478 Here, the union's first member is of the form
9479 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9480 where the fieldnos are the indices of the fields that should be
9481 traversed in order to find the field (which may be several fields deep)
9482 and the variantname is the name of the variant of the case when the
9483 field is zero.
9484
9485 This function recognizes whether TYPE is of one of these forms,
9486 and, if so, smashes it to be a variant type. */
9487
9488static void
9489quirk_rust_enum (struct type *type, struct objfile *objfile)
9490{
78134374 9491 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9492
9493 /* We don't need to deal with empty enums. */
1f704f76 9494 if (type->num_fields () == 0)
c9317f21
TT
9495 return;
9496
9497#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9498 if (type->num_fields () == 1
c9317f21
TT
9499 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9500 {
9501 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9502
9503 /* Decode the field name to find the offset of the
9504 discriminant. */
9505 ULONGEST bit_offset = 0;
940da03e 9506 struct type *field_type = type->field (0).type ();
c9317f21
TT
9507 while (name[0] >= '0' && name[0] <= '9')
9508 {
9509 char *tail;
9510 unsigned long index = strtoul (name, &tail, 10);
9511 name = tail;
9512 if (*name != '$'
1f704f76 9513 || index >= field_type->num_fields ()
c9317f21
TT
9514 || (TYPE_FIELD_LOC_KIND (field_type, index)
9515 != FIELD_LOC_KIND_BITPOS))
9516 {
b98664d3 9517 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9518 "[in module %s]"),
9519 TYPE_FIELD_NAME (type, 0),
9520 objfile_name (objfile));
9521 return;
9522 }
9523 ++name;
9524
9525 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
940da03e 9526 field_type = field_type->field (index).type ();
c9317f21
TT
9527 }
9528
9c6a1327
TT
9529 /* Smash this type to be a structure type. We have to do this
9530 because the type has already been recorded. */
67607e24 9531 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9532 type->set_num_fields (3);
9c6a1327 9533 /* Save the field we care about. */
ceacbf6e 9534 struct field saved_field = type->field (0);
3cabb6b0
SM
9535 type->set_fields
9536 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9537
9c6a1327 9538 /* Put the discriminant at index 0. */
5d14b6e5 9539 type->field (0).set_type (field_type);
9c6a1327
TT
9540 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9541 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9542 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9543
9544 /* The order of fields doesn't really matter, so put the real
9545 field at index 1 and the data-less field at index 2. */
ceacbf6e 9546 type->field (1) = saved_field;
9c6a1327 9547 TYPE_FIELD_NAME (type, 1)
940da03e
SM
9548 = rust_last_path_segment (type->field (1).type ()->name ());
9549 type->field (1).type ()->set_name
7d93a1e0 9550 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9551 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9552
9553 const char *dataless_name
7d93a1e0 9554 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9555 name);
9556 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9557 dataless_name);
5d14b6e5 9558 type->field (2).set_type (dataless_type);
c9317f21
TT
9559 /* NAME points into the original discriminant name, which
9560 already has the correct lifetime. */
9c6a1327 9561 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9562 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9563
9c6a1327
TT
9564 /* Indicate that this is a variant type. */
9565 static discriminant_range ranges[1] = { { 0, 0 } };
9566 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9567 }
77c2dba3
TT
9568 /* A union with a single anonymous field is probably an old-style
9569 univariant enum. */
1f704f76 9570 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9571 {
c9317f21
TT
9572 /* Smash this type to be a structure type. We have to do this
9573 because the type has already been recorded. */
67607e24 9574 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9575
940da03e 9576 struct type *field_type = type->field (0).type ();
c9317f21 9577 const char *variant_name
7d93a1e0 9578 = rust_last_path_segment (field_type->name ());
9c6a1327 9579 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9580 field_type->set_name
9581 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9582 type->name (), variant_name));
c9317f21
TT
9583 }
9584 else
9585 {
9586 struct type *disr_type = nullptr;
1f704f76 9587 for (int i = 0; i < type->num_fields (); ++i)
c9317f21 9588 {
940da03e 9589 disr_type = type->field (i).type ();
c9317f21 9590
78134374 9591 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9592 {
9593 /* All fields of a true enum will be structs. */
9594 return;
9595 }
1f704f76 9596 else if (disr_type->num_fields () == 0)
c9317f21
TT
9597 {
9598 /* Could be data-less variant, so keep going. */
a037790e 9599 disr_type = nullptr;
c9317f21
TT
9600 }
9601 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9602 "RUST$ENUM$DISR") != 0)
9603 {
9604 /* Not a Rust enum. */
9605 return;
9606 }
9607 else
9608 {
9609 /* Found one. */
9610 break;
9611 }
9612 }
9613
9614 /* If we got here without a discriminant, then it's probably
9615 just a union. */
9616 if (disr_type == nullptr)
9617 return;
9618
9619 /* Smash this type to be a structure type. We have to do this
9620 because the type has already been recorded. */
67607e24 9621 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9622
9c6a1327 9623 /* Make space for the discriminant field. */
ceacbf6e 9624 struct field *disr_field = &disr_type->field (0);
9c6a1327 9625 field *new_fields
1f704f76 9626 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9627 * sizeof (struct field)));
80fc5e77 9628 memcpy (new_fields + 1, type->fields (),
1f704f76 9629 type->num_fields () * sizeof (struct field));
3cabb6b0 9630 type->set_fields (new_fields);
1f704f76 9631 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9632
9633 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9634 type->field (0) = *disr_field;
9c6a1327
TT
9635 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9636 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9637
9638 /* We need a way to find the correct discriminant given a
9639 variant name. For convenience we build a map here. */
b6cdac4b 9640 struct type *enum_type = disr_field->type ();
c9317f21 9641 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9642 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9643 {
9644 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9645 {
9646 const char *name
9647 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9648 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9649 }
9650 }
9651
1f704f76 9652 int n_fields = type->num_fields ();
9c6a1327
TT
9653 /* We don't need a range entry for the discriminant, but we do
9654 need one for every other field, as there is no default
9655 variant. */
9656 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9657 discriminant_range,
9658 n_fields - 1);
c9317f21
TT
9659 /* Skip the discriminant here. */
9660 for (int i = 1; i < n_fields; ++i)
9661 {
9662 /* Find the final word in the name of this variant's type.
9663 That name can be used to look up the correct
9664 discriminant. */
9665 const char *variant_name
940da03e 9666 = rust_last_path_segment (type->field (i).type ()->name ());
c9317f21
TT
9667
9668 auto iter = discriminant_map.find (variant_name);
9669 if (iter != discriminant_map.end ())
9c6a1327
TT
9670 {
9671 ranges[i].low = iter->second;
9672 ranges[i].high = iter->second;
9673 }
c9317f21 9674
bedda9ac 9675 /* Remove the discriminant field, if it exists. */
940da03e 9676 struct type *sub_type = type->field (i).type ();
1f704f76 9677 if (sub_type->num_fields () > 0)
bedda9ac 9678 {
5e33d5f4 9679 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9680 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9681 }
9c6a1327 9682 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9683 sub_type->set_name
9684 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9685 type->name (), variant_name));
c9317f21 9686 }
9c6a1327
TT
9687
9688 /* Indicate that this is a variant type. */
9689 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9690 gdb::array_view<discriminant_range> (ranges,
9691 n_fields - 1));
c9317f21
TT
9692 }
9693}
9694
9695/* Rewrite some Rust unions to be structures with variants parts. */
9696
9697static void
9698rust_union_quirks (struct dwarf2_cu *cu)
9699{
9700 gdb_assert (cu->language == language_rust);
52941706 9701 for (type *type_ : cu->rust_unions)
5e22e966 9702 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9703 /* We don't need this any more. */
9704 cu->rust_unions.clear ();
c9317f21
TT
9705}
9706
8adb8487
TT
9707/* See read.h. */
9708
9709type_unit_group_unshareable *
9710dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9711{
9712 auto iter = this->m_type_units.find (tu_group);
9713 if (iter != this->m_type_units.end ())
9714 return iter->second.get ();
9715
9716 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9717 type_unit_group_unshareable *result = uniq.get ();
9718 this->m_type_units[tu_group] = std::move (uniq);
9719 return result;
9720}
9721
e286671b
TT
9722struct type *
9723dwarf2_per_objfile::get_type_for_signatured_type
9724 (signatured_type *sig_type) const
9725{
9726 auto iter = this->m_type_map.find (sig_type);
9727 if (iter == this->m_type_map.end ())
9728 return nullptr;
9729
9730 return iter->second;
9731}
9732
9733void dwarf2_per_objfile::set_type_for_signatured_type
9734 (signatured_type *sig_type, struct type *type)
9735{
9736 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9737
9738 this->m_type_map[sig_type] = type;
9739}
9740
95554aad
TT
9741/* A helper function for computing the list of all symbol tables
9742 included by PER_CU. */
9743
9744static void
4c39bc03 9745recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9746 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9747 dwarf2_per_cu_data *per_cu,
9748 dwarf2_per_objfile *per_objfile,
43f3e411 9749 struct compunit_symtab *immediate_parent)
95554aad 9750{
af758d11 9751 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9752 if (*slot != NULL)
9753 {
9754 /* This inclusion and its children have been processed. */
9755 return;
9756 }
9757
9758 *slot = per_cu;
af758d11 9759
95554aad 9760 /* Only add a CU if it has a symbol table. */
43182c09 9761 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9762 if (cust != NULL)
ec94af83
DE
9763 {
9764 /* If this is a type unit only add its symbol table if we haven't
9765 seen it yet (type unit per_cu's can share symtabs). */
9766 if (per_cu->is_debug_types)
9767 {
43f3e411 9768 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9769 if (*slot == NULL)
9770 {
43f3e411 9771 *slot = cust;
4c39bc03 9772 result->push_back (cust);
43f3e411
DE
9773 if (cust->user == NULL)
9774 cust->user = immediate_parent;
ec94af83
DE
9775 }
9776 }
9777 else
f9125b6c 9778 {
4c39bc03 9779 result->push_back (cust);
43f3e411
DE
9780 if (cust->user == NULL)
9781 cust->user = immediate_parent;
f9125b6c 9782 }
ec94af83 9783 }
95554aad 9784
ae640021
AB
9785 if (!per_cu->imported_symtabs_empty ())
9786 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9787 {
9788 recursively_compute_inclusions (result, all_children,
43182c09
SM
9789 all_type_symtabs, ptr, per_objfile,
9790 cust);
ae640021 9791 }
95554aad
TT
9792}
9793
43f3e411 9794/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9795 PER_CU. */
9796
9797static void
43182c09
SM
9798compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9799 dwarf2_per_objfile *per_objfile)
95554aad 9800{
f4dc4d17
DE
9801 gdb_assert (! per_cu->is_debug_types);
9802
ae640021 9803 if (!per_cu->imported_symtabs_empty ())
95554aad 9804 {
ae640021 9805 int len;
4c39bc03 9806 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9807 htab_t all_children, all_type_symtabs;
43182c09 9808 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9809
9810 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9811 if (cust == NULL)
95554aad
TT
9812 return;
9813
9814 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9815 NULL, xcalloc, xfree);
ec94af83
DE
9816 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9817 NULL, xcalloc, xfree);
95554aad 9818
ae640021 9819 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9820 {
9821 recursively_compute_inclusions (&result_symtabs, all_children,
43182c09
SM
9822 all_type_symtabs, ptr, per_objfile,
9823 cust);
ec94af83 9824 }
95554aad 9825
ec94af83 9826 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9827 len = result_symtabs.size ();
43f3e411 9828 cust->includes
f6e649dd 9829 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9830 struct compunit_symtab *, len + 1);
4c39bc03
TT
9831 memcpy (cust->includes, result_symtabs.data (),
9832 len * sizeof (compunit_symtab *));
43f3e411 9833 cust->includes[len] = NULL;
95554aad 9834
95554aad 9835 htab_delete (all_children);
ec94af83 9836 htab_delete (all_type_symtabs);
95554aad
TT
9837 }
9838}
9839
9840/* Compute the 'includes' field for the symtabs of all the CUs we just
9841 read. */
9842
9843static void
976ca316 9844process_cu_includes (dwarf2_per_objfile *per_objfile)
95554aad 9845{
976ca316 9846 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9847 {
9848 if (! iter->is_debug_types)
976ca316 9849 compute_compunit_symtab_includes (iter, per_objfile);
f4dc4d17 9850 }
95554aad 9851
976ca316 9852 per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9853}
9854
8fc0b21d 9855/* Generate full symbol information for CU, whose DIEs have
10b3939b
DJ
9856 already been loaded into memory. */
9857
9858static void
8fc0b21d 9859process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10b3939b 9860{
976ca316
SM
9861 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9862 struct objfile *objfile = per_objfile->objfile;
08feed99 9863 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9864 CORE_ADDR lowpc, highpc;
43f3e411 9865 struct compunit_symtab *cust;
10b3939b 9866 CORE_ADDR baseaddr;
4359dff1 9867 struct block *static_block;
3e29f34a 9868 CORE_ADDR addr;
10b3939b 9869
b3b3bada 9870 baseaddr = objfile->text_section_offset ();
10b3939b 9871
c89b44cd
TT
9872 /* Clear the list here in case something was left over. */
9873 cu->method_list.clear ();
10b3939b 9874
95554aad
TT
9875 cu->language = pretend_language;
9876 cu->language_defn = language_def (cu->language);
9877
c906108c 9878 /* Do line number decoding in read_file_scope () */
10b3939b 9879 process_die (cu->dies, cu);
c906108c 9880
a766d390
DE
9881 /* For now fudge the Go package. */
9882 if (cu->language == language_go)
9883 fixup_go_packaging (cu);
9884
5f48f8f3 9885 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9886 should be complete, and it should now be safe to compute all of the
9887 physnames. */
9888 compute_delayed_physnames (cu);
3da10d80 9889
c9317f21
TT
9890 if (cu->language == language_rust)
9891 rust_union_quirks (cu);
9892
fae299cd
DC
9893 /* Some compilers don't define a DW_AT_high_pc attribute for the
9894 compilation unit. If the DW_AT_high_pc is missing, synthesize
9895 it, by scanning the DIE's below the compilation unit. */
10b3939b 9896 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9897
3e29f34a 9898 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9899 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9900
9901 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9902 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9903 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9904 addrmap to help ensure it has an accurate map of pc values belonging to
9905 this comp unit. */
9906 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9907
c24bdb02 9908 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9909 SECT_OFF_TEXT (objfile),
9910 0);
c906108c 9911
43f3e411 9912 if (cust != NULL)
c906108c 9913 {
df15bd07 9914 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9915
8be455d7
JK
9916 /* Set symtab language to language from DW_AT_language. If the
9917 compilation is from a C file generated by language preprocessors, do
9918 not set the language if it was already deduced by start_subfile. */
43f3e411 9919 if (!(cu->language == language_c
40e3ad0e 9920 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9921 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9922
9923 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9924 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9925 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9926 there were bugs in prologue debug info, fixed later in GCC-4.5
9927 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9928
9929 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9930 needed, it would be wrong due to missing DW_AT_producer there.
9931
9932 Still one can confuse GDB by using non-standard GCC compilation
9933 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9934 */
ab260dad 9935 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9936 cust->locations_valid = 1;
e0d00bc7
JK
9937
9938 if (gcc_4_minor >= 5)
43f3e411 9939 cust->epilogue_unwind_valid = 1;
96408a79 9940
43f3e411 9941 cust->call_site_htab = cu->call_site_htab;
c906108c 9942 }
9291a0cd 9943
976ca316 9944 per_objfile->set_symtab (cu->per_cu, cust);
c906108c 9945
95554aad 9946 /* Push it for inclusion processing later. */
976ca316 9947 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
804d2729
TT
9948
9949 /* Not needed any more. */
c24bdb02 9950 cu->reset_builder ();
f4dc4d17 9951}
45cfd468 9952
8fc0b21d 9953/* Generate full symbol information for type unit CU, whose DIEs have
f4dc4d17
DE
9954 already been loaded into memory. */
9955
9956static void
8fc0b21d 9957process_full_type_unit (dwarf2_cu *cu,
f4dc4d17
DE
9958 enum language pretend_language)
9959{
976ca316
SM
9960 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9961 struct objfile *objfile = per_objfile->objfile;
43f3e411 9962 struct compunit_symtab *cust;
0186c6a7
DE
9963 struct signatured_type *sig_type;
9964
8fc0b21d
SM
9965 gdb_assert (cu->per_cu->is_debug_types);
9966 sig_type = (struct signatured_type *) cu->per_cu;
f4dc4d17 9967
c89b44cd
TT
9968 /* Clear the list here in case something was left over. */
9969 cu->method_list.clear ();
f4dc4d17 9970
f4dc4d17
DE
9971 cu->language = pretend_language;
9972 cu->language_defn = language_def (cu->language);
9973
9974 /* The symbol tables are set up in read_type_unit_scope. */
9975 process_die (cu->dies, cu);
9976
9977 /* For now fudge the Go package. */
9978 if (cu->language == language_go)
9979 fixup_go_packaging (cu);
9980
5f48f8f3 9981 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9982 should be complete, and it should now be safe to compute all of the
9983 physnames. */
9984 compute_delayed_physnames (cu);
f4dc4d17 9985
c9317f21
TT
9986 if (cu->language == language_rust)
9987 rust_union_quirks (cu);
9988
f4dc4d17
DE
9989 /* TUs share symbol tables.
9990 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9991 of it with end_expandable_symtab. Otherwise, complete the addition of
9992 this TU's symbols to the existing symtab. */
8adb8487 9993 type_unit_group_unshareable *tug_unshare =
976ca316 9994 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8adb8487 9995 if (tug_unshare->compunit_symtab == NULL)
45cfd468 9996 {
c24bdb02
KS
9997 buildsym_compunit *builder = cu->get_builder ();
9998 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 9999 tug_unshare->compunit_symtab = cust;
f4dc4d17 10000
43f3e411 10001 if (cust != NULL)
f4dc4d17
DE
10002 {
10003 /* Set symtab language to language from DW_AT_language. If the
10004 compilation is from a C file generated by language preprocessors,
10005 do not set the language if it was already deduced by
10006 start_subfile. */
43f3e411
DE
10007 if (!(cu->language == language_c
10008 && COMPUNIT_FILETABS (cust)->language != language_c))
10009 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10010 }
10011 }
10012 else
10013 {
c24bdb02 10014 cu->get_builder ()->augment_type_symtab ();
8adb8487 10015 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
10016 }
10017
976ca316 10018 per_objfile->set_symtab (cu->per_cu, cust);
804d2729
TT
10019
10020 /* Not needed any more. */
c24bdb02 10021 cu->reset_builder ();
c906108c
SS
10022}
10023
95554aad
TT
10024/* Process an imported unit DIE. */
10025
10026static void
10027process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10028{
10029 struct attribute *attr;
10030
f4dc4d17
DE
10031 /* For now we don't handle imported units in type units. */
10032 if (cu->per_cu->is_debug_types)
10033 {
10034 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10035 " supported in type units [in module %s]"),
5e22e966 10036 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
10037 }
10038
95554aad
TT
10039 attr = dwarf2_attr (die, DW_AT_import, cu);
10040 if (attr != NULL)
10041 {
0826b30a 10042 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 10043 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 10044 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 10045 dwarf2_per_cu_data *per_cu
ab432490 10046 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 10047
58990295
TV
10048 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10049 into another compilation unit, at root level. Regard this as a hint,
10050 and ignore it. */
10051 if (die->parent && die->parent->parent == NULL
10052 && per_cu->unit_type == DW_UT_compile
10053 && per_cu->lang == language_cplus)
10054 return;
10055
69d751e3 10056 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 10057 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
ab432490 10058 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
95554aad 10059
ae640021 10060 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10061 }
10062}
10063
4c8aa72d
PA
10064/* RAII object that represents a process_die scope: i.e.,
10065 starts/finishes processing a DIE. */
10066class process_die_scope
adde2bff 10067{
4c8aa72d
PA
10068public:
10069 process_die_scope (die_info *die, dwarf2_cu *cu)
10070 : m_die (die), m_cu (cu)
10071 {
10072 /* We should only be processing DIEs not already in process. */
10073 gdb_assert (!m_die->in_process);
10074 m_die->in_process = true;
10075 }
8c3cb9fa 10076
4c8aa72d
PA
10077 ~process_die_scope ()
10078 {
10079 m_die->in_process = false;
10080
10081 /* If we're done processing the DIE for the CU that owns the line
10082 header, we don't need the line header anymore. */
10083 if (m_cu->line_header_die_owner == m_die)
10084 {
10085 delete m_cu->line_header;
10086 m_cu->line_header = NULL;
10087 m_cu->line_header_die_owner = NULL;
10088 }
10089 }
10090
10091private:
10092 die_info *m_die;
10093 dwarf2_cu *m_cu;
10094};
adde2bff 10095
c906108c
SS
10096/* Process a die and its children. */
10097
10098static void
e7c27a73 10099process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10100{
4c8aa72d 10101 process_die_scope scope (die, cu);
adde2bff 10102
c906108c
SS
10103 switch (die->tag)
10104 {
10105 case DW_TAG_padding:
10106 break;
10107 case DW_TAG_compile_unit:
95554aad 10108 case DW_TAG_partial_unit:
e7c27a73 10109 read_file_scope (die, cu);
c906108c 10110 break;
348e048f
DE
10111 case DW_TAG_type_unit:
10112 read_type_unit_scope (die, cu);
10113 break;
c906108c 10114 case DW_TAG_subprogram:
0a4b0913
AB
10115 /* Nested subprograms in Fortran get a prefix. */
10116 if (cu->language == language_fortran
10117 && die->parent != NULL
10118 && die->parent->tag == DW_TAG_subprogram)
10119 cu->processing_has_namespace_info = true;
10120 /* Fall through. */
c906108c 10121 case DW_TAG_inlined_subroutine:
edb3359d 10122 read_func_scope (die, cu);
c906108c
SS
10123 break;
10124 case DW_TAG_lexical_block:
14898363
L
10125 case DW_TAG_try_block:
10126 case DW_TAG_catch_block:
e7c27a73 10127 read_lexical_block_scope (die, cu);
c906108c 10128 break;
216f72a1 10129 case DW_TAG_call_site:
96408a79
SA
10130 case DW_TAG_GNU_call_site:
10131 read_call_site_scope (die, cu);
10132 break;
c906108c 10133 case DW_TAG_class_type:
680b30c7 10134 case DW_TAG_interface_type:
c906108c
SS
10135 case DW_TAG_structure_type:
10136 case DW_TAG_union_type:
134d01f1 10137 process_structure_scope (die, cu);
c906108c
SS
10138 break;
10139 case DW_TAG_enumeration_type:
134d01f1 10140 process_enumeration_scope (die, cu);
c906108c 10141 break;
134d01f1 10142
f792889a
DJ
10143 /* These dies have a type, but processing them does not create
10144 a symbol or recurse to process the children. Therefore we can
10145 read them on-demand through read_type_die. */
c906108c 10146 case DW_TAG_subroutine_type:
72019c9c 10147 case DW_TAG_set_type:
c906108c 10148 case DW_TAG_array_type:
c906108c 10149 case DW_TAG_pointer_type:
c906108c 10150 case DW_TAG_ptr_to_member_type:
c906108c 10151 case DW_TAG_reference_type:
4297a3f0 10152 case DW_TAG_rvalue_reference_type:
c906108c 10153 case DW_TAG_string_type:
c906108c 10154 break;
134d01f1 10155
c906108c 10156 case DW_TAG_base_type:
a02abb62 10157 case DW_TAG_subrange_type:
cb249c71 10158 case DW_TAG_typedef:
134d01f1
DJ
10159 /* Add a typedef symbol for the type definition, if it has a
10160 DW_AT_name. */
f792889a 10161 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10162 break;
c906108c 10163 case DW_TAG_common_block:
e7c27a73 10164 read_common_block (die, cu);
c906108c
SS
10165 break;
10166 case DW_TAG_common_inclusion:
10167 break;
d9fa45fe 10168 case DW_TAG_namespace:
9068261f 10169 cu->processing_has_namespace_info = true;
e7c27a73 10170 read_namespace (die, cu);
d9fa45fe 10171 break;
5d7cb8df 10172 case DW_TAG_module:
9068261f 10173 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10174 read_module (die, cu);
10175 break;
d9fa45fe 10176 case DW_TAG_imported_declaration:
9068261f 10177 cu->processing_has_namespace_info = true;
74921315
KS
10178 if (read_namespace_alias (die, cu))
10179 break;
86a73007
TT
10180 /* The declaration is not a global namespace alias. */
10181 /* Fall through. */
d9fa45fe 10182 case DW_TAG_imported_module:
9068261f 10183 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10184 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10185 || cu->language != language_fortran))
b98664d3 10186 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10187 dwarf_tag_name (die->tag));
10188 read_import_statement (die, cu);
d9fa45fe 10189 break;
95554aad
TT
10190
10191 case DW_TAG_imported_unit:
10192 process_imported_unit_die (die, cu);
10193 break;
10194
71a3c369
TT
10195 case DW_TAG_variable:
10196 read_variable (die, cu);
10197 break;
10198
c906108c 10199 default:
e7c27a73 10200 new_symbol (die, NULL, cu);
c906108c
SS
10201 break;
10202 }
10203}
ca69b9e6
DE
10204\f
10205/* DWARF name computation. */
c906108c 10206
94af9270
KS
10207/* A helper function for dwarf2_compute_name which determines whether DIE
10208 needs to have the name of the scope prepended to the name listed in the
10209 die. */
10210
10211static int
10212die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10213{
1c809c68
TT
10214 struct attribute *attr;
10215
94af9270
KS
10216 switch (die->tag)
10217 {
10218 case DW_TAG_namespace:
10219 case DW_TAG_typedef:
10220 case DW_TAG_class_type:
10221 case DW_TAG_interface_type:
10222 case DW_TAG_structure_type:
10223 case DW_TAG_union_type:
10224 case DW_TAG_enumeration_type:
10225 case DW_TAG_enumerator:
10226 case DW_TAG_subprogram:
08a76f8a 10227 case DW_TAG_inlined_subroutine:
94af9270 10228 case DW_TAG_member:
74921315 10229 case DW_TAG_imported_declaration:
94af9270
KS
10230 return 1;
10231
10232 case DW_TAG_variable:
c2b0a229 10233 case DW_TAG_constant:
94af9270
KS
10234 /* We only need to prefix "globally" visible variables. These include
10235 any variable marked with DW_AT_external or any variable that
10236 lives in a namespace. [Variables in anonymous namespaces
10237 require prefixing, but they are not DW_AT_external.] */
10238
10239 if (dwarf2_attr (die, DW_AT_specification, cu))
10240 {
10241 struct dwarf2_cu *spec_cu = cu;
9a619af0 10242
94af9270
KS
10243 return die_needs_namespace (die_specification (die, &spec_cu),
10244 spec_cu);
10245 }
10246
1c809c68 10247 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10248 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10249 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10250 return 0;
10251 /* A variable in a lexical block of some kind does not need a
10252 namespace, even though in C++ such variables may be external
10253 and have a mangled name. */
10254 if (die->parent->tag == DW_TAG_lexical_block
10255 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10256 || die->parent->tag == DW_TAG_catch_block
10257 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10258 return 0;
10259 return 1;
94af9270
KS
10260
10261 default:
10262 return 0;
10263 }
10264}
10265
73b9be8b
KS
10266/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10267 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10268 defined for the given DIE. */
10269
10270static struct attribute *
10271dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10272{
10273 struct attribute *attr;
10274
10275 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10276 if (attr == NULL)
10277 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10278
10279 return attr;
10280}
10281
10282/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10283 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10284 defined for the given DIE. */
10285
10286static const char *
10287dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10288{
10289 const char *linkage_name;
10290
10291 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10292 if (linkage_name == NULL)
10293 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10294
787de330
TT
10295 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10296 See https://github.com/rust-lang/rust/issues/32925. */
10297 if (cu->language == language_rust && linkage_name != NULL
10298 && strchr (linkage_name, '{') != NULL)
10299 linkage_name = NULL;
10300
73b9be8b
KS
10301 return linkage_name;
10302}
10303
94af9270 10304/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10305 compute the physname for the object, which include a method's:
9c37b5ae 10306 - formal parameters (C++),
a766d390 10307 - receiver type (Go),
a766d390
DE
10308
10309 The term "physname" is a bit confusing.
10310 For C++, for example, it is the demangled name.
10311 For Go, for example, it's the mangled name.
94af9270 10312
af6b7be1
JB
10313 For Ada, return the DIE's linkage name rather than the fully qualified
10314 name. PHYSNAME is ignored..
10315
5989a64e 10316 The result is allocated on the objfile->per_bfd's obstack and
45940949 10317 canonicalized. */
94af9270
KS
10318
10319static const char *
15d034d0
TT
10320dwarf2_compute_name (const char *name,
10321 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10322 int physname)
10323{
5e22e966 10324 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10325
94af9270
KS
10326 if (name == NULL)
10327 name = dwarf2_name (die, cu);
10328
2ee7123e
DE
10329 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10330 but otherwise compute it by typename_concat inside GDB.
10331 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10332 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10333 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10334 will set the demangled name to the result of dwarf2_full_name, and it is
10335 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10336 if (cu->language == language_ada
10337 || (cu->language == language_fortran && physname))
10338 {
10339 /* For Ada unit, we prefer the linkage name over the name, as
10340 the former contains the exported name, which the user expects
10341 to be able to reference. Ideally, we want the user to be able
10342 to reference this entity using either natural or linkage name,
10343 but we haven't started looking at this enhancement yet. */
73b9be8b 10344 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10345
2ee7123e
DE
10346 if (linkage_name != NULL)
10347 return linkage_name;
f55ee35c
JK
10348 }
10349
94af9270
KS
10350 /* These are the only languages we know how to qualify names in. */
10351 if (name != NULL
9c37b5ae 10352 && (cu->language == language_cplus
c44af4eb
TT
10353 || cu->language == language_fortran || cu->language == language_d
10354 || cu->language == language_rust))
94af9270
KS
10355 {
10356 if (die_needs_namespace (die, cu))
10357 {
0d5cff50 10358 const char *prefix;
34a68019 10359 const char *canonical_name = NULL;
94af9270 10360
d7e74731
PA
10361 string_file buf;
10362
94af9270 10363 prefix = determine_prefix (die, cu);
94af9270
KS
10364 if (*prefix != '\0')
10365 {
43816ebc
TT
10366 gdb::unique_xmalloc_ptr<char> prefixed_name
10367 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10368
43816ebc 10369 buf.puts (prefixed_name.get ());
94af9270
KS
10370 }
10371 else
d7e74731 10372 buf.puts (name);
94af9270 10373
98bfdba5
PA
10374 /* Template parameters may be specified in the DIE's DW_AT_name, or
10375 as children with DW_TAG_template_type_param or
10376 DW_TAG_value_type_param. If the latter, add them to the name
10377 here. If the name already has template parameters, then
10378 skip this step; some versions of GCC emit both, and
10379 it is more efficient to use the pre-computed name.
10380
10381 Something to keep in mind about this process: it is very
10382 unlikely, or in some cases downright impossible, to produce
10383 something that will match the mangled name of a function.
10384 If the definition of the function has the same debug info,
10385 we should be able to match up with it anyway. But fallbacks
10386 using the minimal symbol, for instance to find a method
10387 implemented in a stripped copy of libstdc++, will not work.
10388 If we do not have debug info for the definition, we will have to
10389 match them up some other way.
10390
10391 When we do name matching there is a related problem with function
10392 templates; two instantiated function templates are allowed to
10393 differ only by their return types, which we do not add here. */
10394
10395 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10396 {
10397 struct attribute *attr;
10398 struct die_info *child;
10399 int first = 1;
10400
10401 die->building_fullname = 1;
10402
10403 for (child = die->child; child != NULL; child = child->sibling)
10404 {
10405 struct type *type;
12df843f 10406 LONGEST value;
d521ce57 10407 const gdb_byte *bytes;
98bfdba5
PA
10408 struct dwarf2_locexpr_baton *baton;
10409 struct value *v;
10410
10411 if (child->tag != DW_TAG_template_type_param
10412 && child->tag != DW_TAG_template_value_param)
10413 continue;
10414
10415 if (first)
10416 {
d7e74731 10417 buf.puts ("<");
98bfdba5
PA
10418 first = 0;
10419 }
10420 else
d7e74731 10421 buf.puts (", ");
98bfdba5
PA
10422
10423 attr = dwarf2_attr (child, DW_AT_type, cu);
10424 if (attr == NULL)
10425 {
b98664d3 10426 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10427 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10428 continue;
10429 }
10430 type = die_type (child, cu);
10431
10432 if (child->tag == DW_TAG_template_type_param)
10433 {
c1ec8cea
TT
10434 c_print_type (type, "", &buf, -1, 0, cu->language,
10435 &type_print_raw_options);
98bfdba5
PA
10436 continue;
10437 }
10438
10439 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10440 if (attr == NULL)
10441 {
b98664d3 10442 complaint (_("template parameter missing "
3e43a32a 10443 "DW_AT_const_value"));
d7e74731 10444 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10445 continue;
10446 }
10447
10448 dwarf2_const_value_attr (attr, type, name,
10449 &cu->comp_unit_obstack, cu,
10450 &value, &bytes, &baton);
10451
10452 if (TYPE_NOSIGN (type))
10453 /* GDB prints characters as NUMBER 'CHAR'. If that's
10454 changed, this can use value_print instead. */
d7e74731 10455 c_printchar (value, type, &buf);
98bfdba5
PA
10456 else
10457 {
10458 struct value_print_options opts;
10459
10460 if (baton != NULL)
10461 v = dwarf2_evaluate_loc_desc (type, NULL,
10462 baton->data,
10463 baton->size,
9f47c707
SM
10464 baton->per_cu,
10465 baton->per_objfile);
98bfdba5
PA
10466 else if (bytes != NULL)
10467 {
10468 v = allocate_value (type);
10469 memcpy (value_contents_writeable (v), bytes,
10470 TYPE_LENGTH (type));
10471 }
10472 else
10473 v = value_from_longest (type, value);
10474
3e43a32a
MS
10475 /* Specify decimal so that we do not depend on
10476 the radix. */
98bfdba5
PA
10477 get_formatted_print_options (&opts, 'd');
10478 opts.raw = 1;
d7e74731 10479 value_print (v, &buf, &opts);
98bfdba5 10480 release_value (v);
98bfdba5
PA
10481 }
10482 }
10483
10484 die->building_fullname = 0;
10485
10486 if (!first)
10487 {
10488 /* Close the argument list, with a space if necessary
10489 (nested templates). */
d7e74731
PA
10490 if (!buf.empty () && buf.string ().back () == '>')
10491 buf.puts (" >");
98bfdba5 10492 else
d7e74731 10493 buf.puts (">");
98bfdba5
PA
10494 }
10495 }
10496
9c37b5ae 10497 /* For C++ methods, append formal parameter type
94af9270 10498 information, if PHYSNAME. */
6e70227d 10499
94af9270 10500 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10501 && cu->language == language_cplus)
94af9270
KS
10502 {
10503 struct type *type = read_type_die (die, cu);
10504
d7e74731 10505 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10506 &type_print_raw_options);
94af9270 10507
9c37b5ae 10508 if (cu->language == language_cplus)
94af9270 10509 {
60430eff
DJ
10510 /* Assume that an artificial first parameter is
10511 "this", but do not crash if it is not. RealView
10512 marks unnamed (and thus unused) parameters as
10513 artificial; there is no way to differentiate
10514 the two cases. */
1f704f76 10515 if (type->num_fields () > 0
94af9270 10516 && TYPE_FIELD_ARTIFICIAL (type, 0)
940da03e
SM
10517 && type->field (0).type ()->code () == TYPE_CODE_PTR
10518 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
d7e74731 10519 buf.puts (" const");
94af9270
KS
10520 }
10521 }
10522
d7e74731 10523 const std::string &intermediate_name = buf.string ();
94af9270
KS
10524
10525 if (cu->language == language_cplus)
34a68019 10526 canonical_name
322a8516 10527 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10528 objfile);
34a68019
TT
10529
10530 /* If we only computed INTERMEDIATE_NAME, or if
10531 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10532 intern it. */
322a8516 10533 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10534 name = objfile->intern (intermediate_name);
34a68019
TT
10535 else
10536 name = canonical_name;
94af9270
KS
10537 }
10538 }
10539
10540 return name;
10541}
10542
0114d602
DJ
10543/* Return the fully qualified name of DIE, based on its DW_AT_name.
10544 If scope qualifiers are appropriate they will be added. The result
34a68019 10545 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10546 not have a name. NAME may either be from a previous call to
10547 dwarf2_name or NULL.
10548
9c37b5ae 10549 The output string will be canonicalized (if C++). */
0114d602
DJ
10550
10551static const char *
15d034d0 10552dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10553{
94af9270
KS
10554 return dwarf2_compute_name (name, die, cu, 0);
10555}
0114d602 10556
94af9270
KS
10557/* Construct a physname for the given DIE in CU. NAME may either be
10558 from a previous call to dwarf2_name or NULL. The result will be
10559 allocated on the objfile_objstack or NULL if the DIE does not have a
10560 name.
0114d602 10561
9c37b5ae 10562 The output string will be canonicalized (if C++). */
0114d602 10563
94af9270 10564static const char *
15d034d0 10565dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10566{
5e22e966 10567 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10568 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10569 int need_copy = 1;
10570
10571 /* In this case dwarf2_compute_name is just a shortcut not building anything
10572 on its own. */
10573 if (!die_needs_namespace (die, cu))
10574 return dwarf2_compute_name (name, die, cu, 1);
10575
906bb4c5
TT
10576 if (cu->language != language_rust)
10577 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10578
10579 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10580 has computed. */
791afaa2 10581 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10582 if (mangled != NULL)
900e11f9 10583 {
900e11f9 10584
59cc4834
JB
10585 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10586 {
10587 /* Do nothing (do not demangle the symbol name). */
10588 }
10589 else if (cu->language == language_go)
a766d390 10590 {
5e2db402
TT
10591 /* This is a lie, but we already lie to the caller new_symbol.
10592 new_symbol assumes we return the mangled name.
a766d390 10593 This just undoes that lie until things are cleaned up. */
a766d390
DE
10594 }
10595 else
10596 {
0eb876f5
JB
10597 /* Use DMGL_RET_DROP for C++ template functions to suppress
10598 their return type. It is easier for GDB users to search
10599 for such functions as `name(params)' than `long name(params)'.
10600 In such case the minimal symbol names do not match the full
10601 symbol names but for template functions there is never a need
10602 to look up their definition from their declaration so
10603 the only disadvantage remains the minimal symbol variant
10604 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10605 demangled.reset (gdb_demangle (mangled,
10606 (DMGL_PARAMS | DMGL_ANSI
10607 | DMGL_RET_DROP)));
a766d390 10608 }
900e11f9 10609 if (demangled)
791afaa2 10610 canon = demangled.get ();
900e11f9
JK
10611 else
10612 {
10613 canon = mangled;
10614 need_copy = 0;
10615 }
10616 }
10617
10618 if (canon == NULL || check_physname)
10619 {
10620 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10621
10622 if (canon != NULL && strcmp (physname, canon) != 0)
10623 {
10624 /* It may not mean a bug in GDB. The compiler could also
10625 compute DW_AT_linkage_name incorrectly. But in such case
10626 GDB would need to be bug-to-bug compatible. */
10627
b98664d3 10628 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10629 "(from linkage <%s>) - DIE at %s [in module %s]"),
10630 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10631 objfile_name (objfile));
900e11f9
JK
10632
10633 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10634 is available here - over computed PHYSNAME. It is safer
10635 against both buggy GDB and buggy compilers. */
10636
10637 retval = canon;
10638 }
10639 else
10640 {
10641 retval = physname;
10642 need_copy = 0;
10643 }
10644 }
10645 else
10646 retval = canon;
10647
10648 if (need_copy)
be1e3d3e 10649 retval = objfile->intern (retval);
900e11f9 10650
900e11f9 10651 return retval;
0114d602
DJ
10652}
10653
74921315
KS
10654/* Inspect DIE in CU for a namespace alias. If one exists, record
10655 a new symbol for it.
10656
10657 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10658
10659static int
10660read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10661{
10662 struct attribute *attr;
10663
10664 /* If the die does not have a name, this is not a namespace
10665 alias. */
10666 attr = dwarf2_attr (die, DW_AT_name, cu);
10667 if (attr != NULL)
10668 {
10669 int num;
10670 struct die_info *d = die;
10671 struct dwarf2_cu *imported_cu = cu;
10672
10673 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10674 keep inspecting DIEs until we hit the underlying import. */
10675#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10676 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10677 {
10678 attr = dwarf2_attr (d, DW_AT_import, cu);
10679 if (attr == NULL)
10680 break;
10681
10682 d = follow_die_ref (d, attr, &imported_cu);
10683 if (d->tag != DW_TAG_imported_declaration)
10684 break;
10685 }
10686
10687 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10688 {
b98664d3 10689 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10690 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10691 return 0;
10692 }
10693
10694 if (attr != NULL)
10695 {
10696 struct type *type;
0826b30a 10697 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10698
aa66c379 10699 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10700 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10701 {
10702 /* This declaration is a global namespace alias. Add
10703 a symbol for it whose type is the aliased namespace. */
10704 new_symbol (die, type, cu);
10705 return 1;
10706 }
10707 }
10708 }
10709
10710 return 0;
10711}
10712
22cee43f 10713/* Return the using directives repository (global or local?) to use in the
804d2729 10714 current context for CU.
22cee43f
PMR
10715
10716 For Ada, imported declarations can materialize renamings, which *may* be
10717 global. However it is impossible (for now?) in DWARF to distinguish
10718 "external" imported declarations and "static" ones. As all imported
10719 declarations seem to be static in all other languages, make them all CU-wide
10720 global only in Ada. */
10721
10722static struct using_direct **
804d2729 10723using_directives (struct dwarf2_cu *cu)
22cee43f 10724{
c24bdb02
KS
10725 if (cu->language == language_ada
10726 && cu->get_builder ()->outermost_context_p ())
10727 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10728 else
c24bdb02 10729 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10730}
10731
27aa8d6a
SW
10732/* Read the import statement specified by the given die and record it. */
10733
10734static void
10735read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10736{
5e22e966 10737 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10738 struct attribute *import_attr;
32019081 10739 struct die_info *imported_die, *child_die;
de4affc9 10740 struct dwarf2_cu *imported_cu;
27aa8d6a 10741 const char *imported_name;
794684b6 10742 const char *imported_name_prefix;
13387711
SW
10743 const char *canonical_name;
10744 const char *import_alias;
10745 const char *imported_declaration = NULL;
794684b6 10746 const char *import_prefix;
eb1e02fd 10747 std::vector<const char *> excludes;
13387711 10748
27aa8d6a
SW
10749 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10750 if (import_attr == NULL)
10751 {
b98664d3 10752 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10753 dwarf_tag_name (die->tag));
10754 return;
10755 }
10756
de4affc9
CC
10757 imported_cu = cu;
10758 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10759 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10760 if (imported_name == NULL)
10761 {
10762 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10763
10764 The import in the following code:
10765 namespace A
10766 {
10767 typedef int B;
10768 }
10769
10770 int main ()
10771 {
10772 using A::B;
10773 B b;
10774 return b;
10775 }
10776
10777 ...
10778 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10779 <52> DW_AT_decl_file : 1
10780 <53> DW_AT_decl_line : 6
10781 <54> DW_AT_import : <0x75>
10782 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10783 <59> DW_AT_name : B
10784 <5b> DW_AT_decl_file : 1
10785 <5c> DW_AT_decl_line : 2
10786 <5d> DW_AT_type : <0x6e>
10787 ...
10788 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10789 <76> DW_AT_byte_size : 4
10790 <77> DW_AT_encoding : 5 (signed)
10791
10792 imports the wrong die ( 0x75 instead of 0x58 ).
10793 This case will be ignored until the gcc bug is fixed. */
10794 return;
10795 }
10796
82856980
SW
10797 /* Figure out the local name after import. */
10798 import_alias = dwarf2_name (die, cu);
27aa8d6a 10799
794684b6
SW
10800 /* Figure out where the statement is being imported to. */
10801 import_prefix = determine_prefix (die, cu);
10802
10803 /* Figure out what the scope of the imported die is and prepend it
10804 to the name of the imported die. */
de4affc9 10805 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10806
f55ee35c
JK
10807 if (imported_die->tag != DW_TAG_namespace
10808 && imported_die->tag != DW_TAG_module)
794684b6 10809 {
13387711
SW
10810 imported_declaration = imported_name;
10811 canonical_name = imported_name_prefix;
794684b6 10812 }
13387711 10813 else if (strlen (imported_name_prefix) > 0)
12aaed36 10814 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10815 imported_name_prefix,
10816 (cu->language == language_d ? "." : "::"),
10817 imported_name, (char *) NULL);
13387711
SW
10818 else
10819 canonical_name = imported_name;
794684b6 10820
32019081
JK
10821 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10822 for (child_die = die->child; child_die && child_die->tag;
436c571c 10823 child_die = child_die->sibling)
32019081
JK
10824 {
10825 /* DWARF-4: A Fortran use statement with a “rename list” may be
10826 represented by an imported module entry with an import attribute
10827 referring to the module and owned entries corresponding to those
10828 entities that are renamed as part of being imported. */
10829
10830 if (child_die->tag != DW_TAG_imported_declaration)
10831 {
b98664d3 10832 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10833 "- DIE at %s [in module %s]"),
10834 sect_offset_str (child_die->sect_off),
10835 objfile_name (objfile));
32019081
JK
10836 continue;
10837 }
10838
10839 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10840 if (import_attr == NULL)
10841 {
b98664d3 10842 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10843 dwarf_tag_name (child_die->tag));
10844 continue;
10845 }
10846
10847 imported_cu = cu;
10848 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10849 &imported_cu);
10850 imported_name = dwarf2_name (imported_die, imported_cu);
10851 if (imported_name == NULL)
10852 {
b98664d3 10853 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10854 "imported name - DIE at %s [in module %s]"),
10855 sect_offset_str (child_die->sect_off),
10856 objfile_name (objfile));
32019081
JK
10857 continue;
10858 }
10859
eb1e02fd 10860 excludes.push_back (imported_name);
32019081
JK
10861
10862 process_die (child_die, cu);
10863 }
10864
804d2729 10865 add_using_directive (using_directives (cu),
22cee43f
PMR
10866 import_prefix,
10867 canonical_name,
10868 import_alias,
10869 imported_declaration,
10870 excludes,
10871 0,
10872 &objfile->objfile_obstack);
27aa8d6a
SW
10873}
10874
5230b05a
WT
10875/* ICC<14 does not output the required DW_AT_declaration on incomplete
10876 types, but gives them a size of zero. Starting with version 14,
10877 ICC is compatible with GCC. */
10878
9068261f 10879static bool
5230b05a
WT
10880producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10881{
10882 if (!cu->checked_producer)
10883 check_producer (cu);
10884
10885 return cu->producer_is_icc_lt_14;
10886}
10887
eb77c9df
AB
10888/* ICC generates a DW_AT_type for C void functions. This was observed on
10889 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10890 which says that void functions should not have a DW_AT_type. */
10891
10892static bool
10893producer_is_icc (struct dwarf2_cu *cu)
10894{
10895 if (!cu->checked_producer)
10896 check_producer (cu);
10897
10898 return cu->producer_is_icc;
10899}
10900
1b80a9fa
JK
10901/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10902 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10903 this, it was first present in GCC release 4.3.0. */
10904
9068261f 10905static bool
1b80a9fa
JK
10906producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10907{
10908 if (!cu->checked_producer)
10909 check_producer (cu);
10910
10911 return cu->producer_is_gcc_lt_4_3;
10912}
10913
d721ba37
PA
10914static file_and_directory
10915find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10916{
d721ba37
PA
10917 file_and_directory res;
10918
9291a0cd
TT
10919 /* Find the filename. Do not use dwarf2_name here, since the filename
10920 is not a source language identifier. */
d721ba37
PA
10921 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10922 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10923
d721ba37
PA
10924 if (res.comp_dir == NULL
10925 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10926 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10927 {
d721ba37
PA
10928 res.comp_dir_storage = ldirname (res.name);
10929 if (!res.comp_dir_storage.empty ())
10930 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10931 }
d721ba37 10932 if (res.comp_dir != NULL)
9291a0cd
TT
10933 {
10934 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10935 directory, get rid of it. */
d721ba37 10936 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10937
d721ba37
PA
10938 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10939 res.comp_dir = cp + 1;
9291a0cd
TT
10940 }
10941
d721ba37
PA
10942 if (res.name == NULL)
10943 res.name = "<unknown>";
10944
10945 return res;
9291a0cd
TT
10946}
10947
f4dc4d17
DE
10948/* Handle DW_AT_stmt_list for a compilation unit.
10949 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10950 COMP_DIR is the compilation directory. LOWPC is passed to
10951 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10952
10953static void
10954handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10955 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10956{
976ca316 10957 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2ab95328 10958 struct attribute *attr;
527f3840
JK
10959 struct line_header line_header_local;
10960 hashval_t line_header_local_hash;
527f3840
JK
10961 void **slot;
10962 int decode_mapping;
2ab95328 10963
f4dc4d17
DE
10964 gdb_assert (! cu->per_cu->is_debug_types);
10965
2ab95328 10966 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10967 if (attr == NULL)
10968 return;
10969
9c541725 10970 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10971
10972 /* The line header hash table is only created if needed (it exists to
10973 prevent redundant reading of the line table for partial_units).
10974 If we're given a partial_unit, we'll need it. If we're given a
10975 compile_unit, then use the line header hash table if it's already
10976 created, but don't create one just yet. */
10977
976ca316 10978 if (per_objfile->line_header_hash == NULL
527f3840 10979 && die->tag == DW_TAG_partial_unit)
2ab95328 10980 {
976ca316 10981 per_objfile->line_header_hash
d15acc42
TT
10982 .reset (htab_create_alloc (127, line_header_hash_voidp,
10983 line_header_eq_voidp,
10984 free_line_header_voidp,
10985 xcalloc, xfree));
527f3840 10986 }
2ab95328 10987
9c541725 10988 line_header_local.sect_off = line_offset;
527f3840
JK
10989 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10990 line_header_local_hash = line_header_hash (&line_header_local);
976ca316 10991 if (per_objfile->line_header_hash != NULL)
527f3840 10992 {
976ca316 10993 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
10994 &line_header_local,
10995 line_header_local_hash, NO_INSERT);
10996
10997 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10998 is not present in *SLOT (since if there is something in *SLOT then
10999 it will be for a partial_unit). */
11000 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11001 {
527f3840 11002 gdb_assert (*slot != NULL);
9a3c8263 11003 cu->line_header = (struct line_header *) *slot;
527f3840 11004 return;
dee91e82 11005 }
2ab95328 11006 }
527f3840
JK
11007
11008 /* dwarf_decode_line_header does not yet provide sufficient information.
11009 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11010 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11011 if (lh == NULL)
527f3840 11012 return;
4c8aa72d
PA
11013
11014 cu->line_header = lh.release ();
11015 cu->line_header_die_owner = die;
527f3840 11016
976ca316 11017 if (per_objfile->line_header_hash == NULL)
527f3840
JK
11018 slot = NULL;
11019 else
11020 {
976ca316 11021 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
11022 &line_header_local,
11023 line_header_local_hash, INSERT);
11024 gdb_assert (slot != NULL);
11025 }
11026 if (slot != NULL && *slot == NULL)
11027 {
11028 /* This newly decoded line number information unit will be owned
11029 by line_header_hash hash table. */
11030 *slot = cu->line_header;
4c8aa72d 11031 cu->line_header_die_owner = NULL;
527f3840
JK
11032 }
11033 else
11034 {
11035 /* We cannot free any current entry in (*slot) as that struct line_header
11036 may be already used by multiple CUs. Create only temporary decoded
11037 line_header for this CU - it may happen at most once for each line
11038 number information unit. And if we're not using line_header_hash
11039 then this is what we want as well. */
11040 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11041 }
11042 decode_mapping = (die->tag != DW_TAG_partial_unit);
11043 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11044 decode_mapping);
fff8551c 11045
2ab95328
TT
11046}
11047
95554aad 11048/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11049
c906108c 11050static void
e7c27a73 11051read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11052{
976ca316
SM
11053 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11054 struct objfile *objfile = per_objfile->objfile;
08feed99 11055 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 11056 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11057 CORE_ADDR highpc = ((CORE_ADDR) 0);
11058 struct attribute *attr;
c906108c 11059 struct die_info *child_die;
e142c38c 11060 CORE_ADDR baseaddr;
6e70227d 11061
380618d6 11062 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 11063 baseaddr = objfile->text_section_offset ();
c906108c 11064
fae299cd 11065 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11066
11067 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11068 from finish_block. */
2acceee2 11069 if (lowpc == ((CORE_ADDR) -1))
c906108c 11070 lowpc = highpc;
3e29f34a 11071 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11072
d721ba37 11073 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11074
f4b8a18d
KW
11075 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11076 standardised yet. As a workaround for the language detection we fall
11077 back to the DW_AT_producer string. */
11078 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11079 cu->language = language_opencl;
11080
3019eac3
DE
11081 /* Similar hack for Go. */
11082 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11083 set_cu_language (DW_LANG_Go, cu);
11084
c24bdb02 11085 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11086
11087 /* Decode line number information if present. We do this before
11088 processing child DIEs, so that the line header table is available
11089 for DW_AT_decl_file. */
d721ba37 11090 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11091
11092 /* Process all dies in compilation unit. */
11093 if (die->child != NULL)
11094 {
11095 child_die = die->child;
11096 while (child_die && child_die->tag)
11097 {
11098 process_die (child_die, cu);
436c571c 11099 child_die = child_die->sibling;
3019eac3
DE
11100 }
11101 }
11102
11103 /* Decode macro information, if present. Dwarf 2 macro information
11104 refers to information in the line number info statement program
11105 header, so we can only read it if we've read the header
11106 successfully. */
0af92d60
JK
11107 attr = dwarf2_attr (die, DW_AT_macros, cu);
11108 if (attr == NULL)
11109 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11110 if (attr && cu->line_header)
11111 {
11112 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11113 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11114
43f3e411 11115 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11116 }
11117 else
11118 {
11119 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11120 if (attr && cu->line_header)
11121 {
11122 unsigned int macro_offset = DW_UNSND (attr);
11123
43f3e411 11124 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11125 }
11126 }
3019eac3
DE
11127}
11128
c24bdb02
KS
11129void
11130dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11131{
f4dc4d17
DE
11132 struct type_unit_group *tu_group;
11133 int first_time;
3019eac3 11134 struct attribute *attr;
9c541725 11135 unsigned int i;
0186c6a7 11136 struct signatured_type *sig_type;
3019eac3 11137
f4dc4d17 11138 gdb_assert (per_cu->is_debug_types);
0186c6a7 11139 sig_type = (struct signatured_type *) per_cu;
3019eac3 11140
c24bdb02 11141 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11142
f4dc4d17 11143 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11144 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11145 if (sig_type->type_unit_group == NULL)
c24bdb02 11146 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11147 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11148
11149 /* If we've already processed this stmt_list there's no real need to
11150 do it again, we could fake it and just recreate the part we need
11151 (file name,index -> symtab mapping). If data shows this optimization
11152 is useful we can do it then. */
8adb8487
TT
11153 type_unit_group_unshareable *tug_unshare
11154 = per_objfile->get_type_unit_group_unshareable (tu_group);
11155 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
11156
11157 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11158 debug info. */
fff8551c 11159 line_header_up lh;
f4dc4d17 11160 if (attr != NULL)
3019eac3 11161 {
9c541725 11162 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11163 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11164 }
11165 if (lh == NULL)
11166 {
11167 if (first_time)
c24bdb02 11168 start_symtab ("", NULL, 0);
f4dc4d17
DE
11169 else
11170 {
8adb8487 11171 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 11172 gdb_assert (m_builder == nullptr);
8adb8487 11173 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11174 m_builder.reset (new struct buildsym_compunit
11175 (COMPUNIT_OBJFILE (cust), "",
11176 COMPUNIT_DIRNAME (cust),
11177 compunit_language (cust),
11178 0, cust));
770479f2 11179 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11180 }
f4dc4d17 11181 return;
3019eac3
DE
11182 }
11183
c24bdb02
KS
11184 line_header = lh.release ();
11185 line_header_die_owner = die;
3019eac3 11186
f4dc4d17
DE
11187 if (first_time)
11188 {
c24bdb02 11189 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11190
1fd60fc0
DE
11191 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11192 still initializing it, and our caller (a few levels up)
11193 process_full_type_unit still needs to know if this is the first
11194 time. */
11195
8adb8487 11196 tug_unshare->symtabs
4ac93832
TT
11197 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11198 struct symtab *, line_header->file_names_size ());
3019eac3 11199
7ba99d21
AT
11200 auto &file_names = line_header->file_names ();
11201 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11202 {
7ba99d21 11203 file_entry &fe = file_names[i];
c24bdb02
KS
11204 dwarf2_start_subfile (this, fe.name,
11205 fe.include_dir (line_header));
11206 buildsym_compunit *b = get_builder ();
11207 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11208 {
4c8aa72d
PA
11209 /* NOTE: start_subfile will recognize when it's been
11210 passed a file it has already seen. So we can't
11211 assume there's a simple mapping from
11212 cu->line_header->file_names to subfiles, plus
11213 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11214 b->get_current_subfile ()->symtab
11215 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11216 }
11217
c24bdb02 11218 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 11219 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
11220 }
11221 }
11222 else
3019eac3 11223 {
c24bdb02 11224 gdb_assert (m_builder == nullptr);
8adb8487 11225 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11226 m_builder.reset (new struct buildsym_compunit
11227 (COMPUNIT_OBJFILE (cust), "",
11228 COMPUNIT_DIRNAME (cust),
11229 compunit_language (cust),
11230 0, cust));
770479f2 11231 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11232
7ba99d21
AT
11233 auto &file_names = line_header->file_names ();
11234 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11235 {
7ba99d21 11236 file_entry &fe = file_names[i];
8adb8487 11237 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 11238 }
3019eac3
DE
11239 }
11240
f4dc4d17
DE
11241 /* The main symtab is allocated last. Type units don't have DW_AT_name
11242 so they don't have a "real" (so to speak) symtab anyway.
11243 There is later code that will assign the main symtab to all symbols
11244 that don't have one. We need to handle the case of a symbol with a
11245 missing symtab (DW_AT_decl_file) anyway. */
11246}
3019eac3 11247
f4dc4d17
DE
11248/* Process DW_TAG_type_unit.
11249 For TUs we want to skip the first top level sibling if it's not the
11250 actual type being defined by this TU. In this case the first top
11251 level sibling is there to provide context only. */
3019eac3 11252
f4dc4d17
DE
11253static void
11254read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11255{
11256 struct die_info *child_die;
3019eac3 11257
f4dc4d17
DE
11258 prepare_one_comp_unit (cu, die, language_minimal);
11259
11260 /* Initialize (or reinitialize) the machinery for building symtabs.
11261 We do this before processing child DIEs, so that the line header table
11262 is available for DW_AT_decl_file. */
c24bdb02 11263 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11264
11265 if (die->child != NULL)
11266 {
11267 child_die = die->child;
11268 while (child_die && child_die->tag)
11269 {
11270 process_die (child_die, cu);
436c571c 11271 child_die = child_die->sibling;
f4dc4d17
DE
11272 }
11273 }
3019eac3
DE
11274}
11275\f
80626a55
DE
11276/* DWO/DWP files.
11277
11278 http://gcc.gnu.org/wiki/DebugFission
11279 http://gcc.gnu.org/wiki/DebugFissionDWP
11280
11281 To simplify handling of both DWO files ("object" files with the DWARF info)
11282 and DWP files (a file with the DWOs packaged up into one file), we treat
11283 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11284
11285static hashval_t
11286hash_dwo_file (const void *item)
11287{
9a3c8263 11288 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11289 hashval_t hash;
3019eac3 11290
a2ce51a0
DE
11291 hash = htab_hash_string (dwo_file->dwo_name);
11292 if (dwo_file->comp_dir != NULL)
11293 hash += htab_hash_string (dwo_file->comp_dir);
11294 return hash;
3019eac3
DE
11295}
11296
11297static int
11298eq_dwo_file (const void *item_lhs, const void *item_rhs)
11299{
9a3c8263
SM
11300 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11301 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11302
a2ce51a0
DE
11303 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11304 return 0;
11305 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11306 return lhs->comp_dir == rhs->comp_dir;
11307 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11308}
11309
11310/* Allocate a hash table for DWO files. */
11311
51ac9db5 11312static htab_up
298e9637 11313allocate_dwo_file_hash_table ()
3019eac3 11314{
51ac9db5
SM
11315 auto delete_dwo_file = [] (void *item)
11316 {
11317 struct dwo_file *dwo_file = (struct dwo_file *) item;
11318
11319 delete dwo_file;
11320 };
11321
bc68fb19
TT
11322 return htab_up (htab_create_alloc (41,
11323 hash_dwo_file,
11324 eq_dwo_file,
11325 delete_dwo_file,
11326 xcalloc, xfree));
3019eac3
DE
11327}
11328
80626a55
DE
11329/* Lookup DWO file DWO_NAME. */
11330
11331static void **
976ca316 11332lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
ed2dc618
SM
11333 const char *dwo_name,
11334 const char *comp_dir)
80626a55
DE
11335{
11336 struct dwo_file find_entry;
11337 void **slot;
11338
976ca316
SM
11339 if (per_objfile->per_bfd->dwo_files == NULL)
11340 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11341
0ac5b59e
DE
11342 find_entry.dwo_name = dwo_name;
11343 find_entry.comp_dir = comp_dir;
976ca316 11344 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11345 INSERT);
80626a55
DE
11346
11347 return slot;
11348}
11349
3019eac3
DE
11350static hashval_t
11351hash_dwo_unit (const void *item)
11352{
9a3c8263 11353 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11354
11355 /* This drops the top 32 bits of the id, but is ok for a hash. */
11356 return dwo_unit->signature;
11357}
11358
11359static int
11360eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11361{
9a3c8263
SM
11362 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11363 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11364
11365 /* The signature is assumed to be unique within the DWO file.
11366 So while object file CU dwo_id's always have the value zero,
11367 that's OK, assuming each object file DWO file has only one CU,
11368 and that's the rule for now. */
11369 return lhs->signature == rhs->signature;
11370}
11371
11372/* Allocate a hash table for DWO CUs,TUs.
11373 There is one of these tables for each of CUs,TUs for each DWO file. */
11374
b0b6a987 11375static htab_up
298e9637 11376allocate_dwo_unit_table ()
3019eac3
DE
11377{
11378 /* Start out with a pretty small number.
11379 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11380 return htab_up (htab_create_alloc (3,
11381 hash_dwo_unit,
11382 eq_dwo_unit,
11383 NULL, xcalloc, xfree));
3019eac3
DE
11384}
11385
19c3d4c9 11386/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11387
11388static void
19c3d4c9
DE
11389create_dwo_cu_reader (const struct die_reader_specs *reader,
11390 const gdb_byte *info_ptr,
11391 struct die_info *comp_unit_die,
c0ab21c2
TT
11392 struct dwo_file *dwo_file,
11393 struct dwo_unit *dwo_unit)
3019eac3
DE
11394{
11395 struct dwarf2_cu *cu = reader->cu;
9c541725 11396 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11397 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11398
a084a2a6
AT
11399 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11400 if (!signature.has_value ())
3019eac3 11401 {
b98664d3 11402 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11403 " its dwo_id [in module %s]"),
9d8780f0 11404 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11405 return;
11406 }
11407
3019eac3 11408 dwo_unit->dwo_file = dwo_file;
a084a2a6 11409 dwo_unit->signature = *signature;
8a0459fd 11410 dwo_unit->section = section;
9c541725 11411 dwo_unit->sect_off = sect_off;
3019eac3
DE
11412 dwo_unit->length = cu->per_cu->length;
11413
b4f54984 11414 if (dwarf_read_debug)
9d8780f0
SM
11415 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11416 sect_offset_str (sect_off),
9c541725 11417 hex_string (dwo_unit->signature));
3019eac3
DE
11418}
11419
33c5cd75 11420/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11421 Note: This function processes DWO files only, not DWP files. */
3019eac3 11422
33c5cd75 11423static void
976ca316 11424create_cus_hash_table (dwarf2_per_objfile *per_objfile,
18a8505e 11425 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11426 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3 11427{
976ca316
SM
11428 struct objfile *objfile = per_objfile->objfile;
11429 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
d521ce57 11430 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11431
96b79293 11432 section.read (objfile);
33c5cd75 11433 info_ptr = section.buffer;
3019eac3
DE
11434
11435 if (info_ptr == NULL)
33c5cd75 11436 return;
3019eac3 11437
b4f54984 11438 if (dwarf_read_debug)
19c3d4c9
DE
11439 {
11440 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11441 section.get_name (),
11442 section.get_file_name ());
19c3d4c9 11443 }
3019eac3 11444
33c5cd75 11445 end_ptr = info_ptr + section.size;
3019eac3
DE
11446 while (info_ptr < end_ptr)
11447 {
11448 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11449 struct dwo_unit read_unit {};
33c5cd75
DB
11450 struct dwo_unit *dwo_unit;
11451 void **slot;
11452 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11453
11454 memset (&per_cu, 0, sizeof (per_cu));
1859c670 11455 per_cu.per_bfd = per_bfd;
3019eac3 11456 per_cu.is_debug_types = 0;
33c5cd75
DB
11457 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11458 per_cu.section = &section;
11459
976ca316 11460 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
c0ab21c2
TT
11461 if (!reader.dummy_p)
11462 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11463 &dwo_file, &read_unit);
33c5cd75
DB
11464 info_ptr += per_cu.length;
11465
11466 // If the unit could not be parsed, skip it.
c0ab21c2 11467 if (read_unit.dwo_file == NULL)
33c5cd75 11468 continue;
3019eac3 11469
33c5cd75 11470 if (cus_htab == NULL)
298e9637 11471 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11472
1859c670 11473 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11474 struct dwo_unit);
c0ab21c2 11475 *dwo_unit = read_unit;
b0b6a987 11476 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11477 gdb_assert (slot != NULL);
11478 if (*slot != NULL)
19c3d4c9 11479 {
33c5cd75
DB
11480 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11481 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11482
b98664d3 11483 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11484 " the entry at offset %s, signature %s"),
11485 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11486 hex_string (dwo_unit->signature));
19c3d4c9 11487 }
33c5cd75 11488 *slot = (void *)dwo_unit;
3019eac3 11489 }
3019eac3
DE
11490}
11491
80626a55
DE
11492/* DWP file .debug_{cu,tu}_index section format:
11493 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11494
d2415c6c
DE
11495 DWP Version 1:
11496
80626a55
DE
11497 Both index sections have the same format, and serve to map a 64-bit
11498 signature to a set of section numbers. Each section begins with a header,
11499 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11500 indexes, and a pool of 32-bit section numbers. The index sections will be
11501 aligned at 8-byte boundaries in the file.
11502
d2415c6c
DE
11503 The index section header consists of:
11504
11505 V, 32 bit version number
11506 -, 32 bits unused
11507 N, 32 bit number of compilation units or type units in the index
11508 M, 32 bit number of slots in the hash table
80626a55 11509
d2415c6c 11510 Numbers are recorded using the byte order of the application binary.
80626a55 11511
d2415c6c
DE
11512 The hash table begins at offset 16 in the section, and consists of an array
11513 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11514 order of the application binary). Unused slots in the hash table are 0.
11515 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11516
d2415c6c
DE
11517 The parallel table begins immediately after the hash table
11518 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11519 array of 32-bit indexes (using the byte order of the application binary),
11520 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11521 table contains a 32-bit index into the pool of section numbers. For unused
11522 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11523
73869dc2
DE
11524 The pool of section numbers begins immediately following the hash table
11525 (at offset 16 + 12 * M from the beginning of the section). The pool of
11526 section numbers consists of an array of 32-bit words (using the byte order
11527 of the application binary). Each item in the array is indexed starting
11528 from 0. The hash table entry provides the index of the first section
11529 number in the set. Additional section numbers in the set follow, and the
11530 set is terminated by a 0 entry (section number 0 is not used in ELF).
11531
11532 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11533 section must be the first entry in the set, and the .debug_abbrev.dwo must
11534 be the second entry. Other members of the set may follow in any order.
11535
11536 ---
11537
11538 DWP Version 2:
11539
11540 DWP Version 2 combines all the .debug_info, etc. sections into one,
11541 and the entries in the index tables are now offsets into these sections.
11542 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11543 section.
11544
11545 Index Section Contents:
11546 Header
11547 Hash Table of Signatures dwp_hash_table.hash_table
11548 Parallel Table of Indices dwp_hash_table.unit_table
11549 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11550 Table of Section Sizes dwp_hash_table.v2.sizes
11551
11552 The index section header consists of:
11553
11554 V, 32 bit version number
11555 L, 32 bit number of columns in the table of section offsets
11556 N, 32 bit number of compilation units or type units in the index
11557 M, 32 bit number of slots in the hash table
11558
11559 Numbers are recorded using the byte order of the application binary.
11560
11561 The hash table has the same format as version 1.
11562 The parallel table of indices has the same format as version 1,
11563 except that the entries are origin-1 indices into the table of sections
11564 offsets and the table of section sizes.
11565
11566 The table of offsets begins immediately following the parallel table
11567 (at offset 16 + 12 * M from the beginning of the section). The table is
11568 a two-dimensional array of 32-bit words (using the byte order of the
11569 application binary), with L columns and N+1 rows, in row-major order.
11570 Each row in the array is indexed starting from 0. The first row provides
11571 a key to the remaining rows: each column in this row provides an identifier
11572 for a debug section, and the offsets in the same column of subsequent rows
11573 refer to that section. The section identifiers are:
11574
11575 DW_SECT_INFO 1 .debug_info.dwo
11576 DW_SECT_TYPES 2 .debug_types.dwo
11577 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11578 DW_SECT_LINE 4 .debug_line.dwo
11579 DW_SECT_LOC 5 .debug_loc.dwo
11580 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11581 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11582 DW_SECT_MACRO 8 .debug_macro.dwo
11583
11584 The offsets provided by the CU and TU index sections are the base offsets
11585 for the contributions made by each CU or TU to the corresponding section
11586 in the package file. Each CU and TU header contains an abbrev_offset
11587 field, used to find the abbreviations table for that CU or TU within the
11588 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11589 be interpreted as relative to the base offset given in the index section.
11590 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11591 should be interpreted as relative to the base offset for .debug_line.dwo,
11592 and offsets into other debug sections obtained from DWARF attributes should
11593 also be interpreted as relative to the corresponding base offset.
11594
11595 The table of sizes begins immediately following the table of offsets.
11596 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11597 with L columns and N rows, in row-major order. Each row in the array is
11598 indexed starting from 1 (row 0 is shared by the two tables).
11599
11600 ---
11601
11602 Hash table lookup is handled the same in version 1 and 2:
11603
11604 We assume that N and M will not exceed 2^32 - 1.
11605 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11606
d2415c6c
DE
11607 Given a 64-bit compilation unit signature or a type signature S, an entry
11608 in the hash table is located as follows:
80626a55 11609
d2415c6c
DE
11610 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11611 the low-order k bits all set to 1.
80626a55 11612
d2415c6c 11613 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11614
d2415c6c
DE
11615 3) If the hash table entry at index H matches the signature, use that
11616 entry. If the hash table entry at index H is unused (all zeroes),
11617 terminate the search: the signature is not present in the table.
80626a55 11618
d2415c6c 11619 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11620
d2415c6c 11621 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11622 to stop at an unused slot or find the match. */
80626a55
DE
11623
11624/* Create a hash table to map DWO IDs to their CU/TU entry in
11625 .debug_{info,types}.dwo in DWP_FILE.
11626 Returns NULL if there isn't one.
11627 Note: This function processes DWP files only, not DWO files. */
11628
11629static struct dwp_hash_table *
976ca316 11630create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 11631 struct dwp_file *dwp_file, int is_debug_types)
80626a55 11632{
976ca316 11633 struct objfile *objfile = per_objfile->objfile;
400174b1 11634 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11635 const gdb_byte *index_ptr, *index_end;
80626a55 11636 struct dwarf2_section_info *index;
73869dc2 11637 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11638 struct dwp_hash_table *htab;
11639
11640 if (is_debug_types)
11641 index = &dwp_file->sections.tu_index;
11642 else
11643 index = &dwp_file->sections.cu_index;
11644
96b79293 11645 if (index->empty ())
80626a55 11646 return NULL;
96b79293 11647 index->read (objfile);
80626a55
DE
11648
11649 index_ptr = index->buffer;
11650 index_end = index_ptr + index->size;
11651
11652 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11653 index_ptr += 4;
11654 if (version == 2)
11655 nr_columns = read_4_bytes (dbfd, index_ptr);
11656 else
11657 nr_columns = 0;
11658 index_ptr += 4;
80626a55
DE
11659 nr_units = read_4_bytes (dbfd, index_ptr);
11660 index_ptr += 4;
11661 nr_slots = read_4_bytes (dbfd, index_ptr);
11662 index_ptr += 4;
11663
73869dc2 11664 if (version != 1 && version != 2)
80626a55 11665 {
21aa081e 11666 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11667 " [in module %s]"),
21aa081e 11668 pulongest (version), dwp_file->name);
80626a55
DE
11669 }
11670 if (nr_slots != (nr_slots & -nr_slots))
11671 {
21aa081e 11672 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11673 " is not power of 2 [in module %s]"),
21aa081e 11674 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11675 }
11676
976ca316 11677 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11678 htab->version = version;
11679 htab->nr_columns = nr_columns;
80626a55
DE
11680 htab->nr_units = nr_units;
11681 htab->nr_slots = nr_slots;
11682 htab->hash_table = index_ptr;
11683 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11684
11685 /* Exit early if the table is empty. */
11686 if (nr_slots == 0 || nr_units == 0
11687 || (version == 2 && nr_columns == 0))
11688 {
11689 /* All must be zero. */
11690 if (nr_slots != 0 || nr_units != 0
11691 || (version == 2 && nr_columns != 0))
11692 {
b98664d3 11693 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11694 " all zero [in modules %s]"),
11695 dwp_file->name);
11696 }
11697 return htab;
11698 }
11699
11700 if (version == 1)
11701 {
11702 htab->section_pool.v1.indices =
11703 htab->unit_table + sizeof (uint32_t) * nr_slots;
11704 /* It's harder to decide whether the section is too small in v1.
11705 V1 is deprecated anyway so we punt. */
11706 }
11707 else
11708 {
11709 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11710 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11711 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11712 /* Reverse map for error checking. */
11713 int ids_seen[DW_SECT_MAX + 1];
11714 int i;
11715
11716 if (nr_columns < 2)
11717 {
11718 error (_("Dwarf Error: bad DWP hash table, too few columns"
11719 " in section table [in module %s]"),
11720 dwp_file->name);
11721 }
11722 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11723 {
11724 error (_("Dwarf Error: bad DWP hash table, too many columns"
11725 " in section table [in module %s]"),
11726 dwp_file->name);
11727 }
04fd5eed
GB
11728 memset (ids, 255, sizeof_ids);
11729 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11730 for (i = 0; i < nr_columns; ++i)
11731 {
11732 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11733
11734 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11735 {
11736 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11737 " in section table [in module %s]"),
11738 id, dwp_file->name);
11739 }
11740 if (ids_seen[id] != -1)
11741 {
11742 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11743 " id %d in section table [in module %s]"),
11744 id, dwp_file->name);
11745 }
11746 ids_seen[id] = i;
11747 ids[i] = id;
11748 }
11749 /* Must have exactly one info or types section. */
11750 if (((ids_seen[DW_SECT_INFO] != -1)
11751 + (ids_seen[DW_SECT_TYPES] != -1))
11752 != 1)
11753 {
11754 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11755 " DWO info/types section [in module %s]"),
11756 dwp_file->name);
11757 }
11758 /* Must have an abbrev section. */
11759 if (ids_seen[DW_SECT_ABBREV] == -1)
11760 {
11761 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11762 " section [in module %s]"),
11763 dwp_file->name);
11764 }
11765 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11766 htab->section_pool.v2.sizes =
11767 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11768 * nr_units * nr_columns);
11769 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11770 * nr_units * nr_columns))
11771 > index_end)
11772 {
11773 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11774 " [in module %s]"),
11775 dwp_file->name);
11776 }
11777 }
80626a55
DE
11778
11779 return htab;
11780}
11781
11782/* Update SECTIONS with the data from SECTP.
11783
11784 This function is like the other "locate" section routines that are
11785 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11786 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11787
11788 The result is non-zero for success, or zero if an error was found. */
11789
11790static int
73869dc2
DE
11791locate_v1_virtual_dwo_sections (asection *sectp,
11792 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11793{
11794 const struct dwop_section_names *names = &dwop_section_names;
11795
11796 if (section_is_p (sectp->name, &names->abbrev_dwo))
11797 {
11798 /* There can be only one. */
049412e3 11799 if (sections->abbrev.s.section != NULL)
80626a55 11800 return 0;
049412e3 11801 sections->abbrev.s.section = sectp;
fd361982 11802 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11803 }
11804 else if (section_is_p (sectp->name, &names->info_dwo)
11805 || section_is_p (sectp->name, &names->types_dwo))
11806 {
11807 /* There can be only one. */
049412e3 11808 if (sections->info_or_types.s.section != NULL)
80626a55 11809 return 0;
049412e3 11810 sections->info_or_types.s.section = sectp;
fd361982 11811 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11812 }
11813 else if (section_is_p (sectp->name, &names->line_dwo))
11814 {
11815 /* There can be only one. */
049412e3 11816 if (sections->line.s.section != NULL)
80626a55 11817 return 0;
049412e3 11818 sections->line.s.section = sectp;
fd361982 11819 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11820 }
11821 else if (section_is_p (sectp->name, &names->loc_dwo))
11822 {
11823 /* There can be only one. */
049412e3 11824 if (sections->loc.s.section != NULL)
80626a55 11825 return 0;
049412e3 11826 sections->loc.s.section = sectp;
fd361982 11827 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11828 }
11829 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11830 {
11831 /* There can be only one. */
049412e3 11832 if (sections->macinfo.s.section != NULL)
80626a55 11833 return 0;
049412e3 11834 sections->macinfo.s.section = sectp;
fd361982 11835 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11836 }
11837 else if (section_is_p (sectp->name, &names->macro_dwo))
11838 {
11839 /* There can be only one. */
049412e3 11840 if (sections->macro.s.section != NULL)
80626a55 11841 return 0;
049412e3 11842 sections->macro.s.section = sectp;
fd361982 11843 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11844 }
11845 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11846 {
11847 /* There can be only one. */
049412e3 11848 if (sections->str_offsets.s.section != NULL)
80626a55 11849 return 0;
049412e3 11850 sections->str_offsets.s.section = sectp;
fd361982 11851 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11852 }
11853 else
11854 {
11855 /* No other kind of section is valid. */
11856 return 0;
11857 }
11858
11859 return 1;
11860}
11861
73869dc2
DE
11862/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11863 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11864 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11865 This is for DWP version 1 files. */
80626a55
DE
11866
11867static struct dwo_unit *
976ca316 11868create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
ed2dc618 11869 struct dwp_file *dwp_file,
73869dc2
DE
11870 uint32_t unit_index,
11871 const char *comp_dir,
11872 ULONGEST signature, int is_debug_types)
80626a55 11873{
73869dc2
DE
11874 const struct dwp_hash_table *dwp_htab =
11875 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11876 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11877 const char *kind = is_debug_types ? "TU" : "CU";
11878 struct dwo_file *dwo_file;
11879 struct dwo_unit *dwo_unit;
73869dc2 11880 struct virtual_v1_dwo_sections sections;
80626a55 11881 void **dwo_file_slot;
80626a55
DE
11882 int i;
11883
73869dc2
DE
11884 gdb_assert (dwp_file->version == 1);
11885
b4f54984 11886 if (dwarf_read_debug)
80626a55 11887 {
73869dc2 11888 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11889 kind,
73869dc2 11890 pulongest (unit_index), hex_string (signature),
80626a55
DE
11891 dwp_file->name);
11892 }
11893
19ac8c2e 11894 /* Fetch the sections of this DWO unit.
80626a55
DE
11895 Put a limit on the number of sections we look for so that bad data
11896 doesn't cause us to loop forever. */
11897
73869dc2 11898#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11899 (1 /* .debug_info or .debug_types */ \
11900 + 1 /* .debug_abbrev */ \
11901 + 1 /* .debug_line */ \
11902 + 1 /* .debug_loc */ \
11903 + 1 /* .debug_str_offsets */ \
19ac8c2e 11904 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11905 + 1 /* trailing zero */)
11906
11907 memset (&sections, 0, sizeof (sections));
80626a55 11908
73869dc2 11909 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11910 {
11911 asection *sectp;
11912 uint32_t section_nr =
11913 read_4_bytes (dbfd,
73869dc2
DE
11914 dwp_htab->section_pool.v1.indices
11915 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11916
11917 if (section_nr == 0)
11918 break;
11919 if (section_nr >= dwp_file->num_sections)
11920 {
11921 error (_("Dwarf Error: bad DWP hash table, section number too large"
11922 " [in module %s]"),
11923 dwp_file->name);
11924 }
11925
11926 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11927 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11928 {
11929 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11930 " [in module %s]"),
11931 dwp_file->name);
11932 }
11933 }
11934
11935 if (i < 2
96b79293
TT
11936 || sections.info_or_types.empty ()
11937 || sections.abbrev.empty ())
80626a55
DE
11938 {
11939 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11940 " [in module %s]"),
11941 dwp_file->name);
11942 }
73869dc2 11943 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11944 {
11945 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11946 " [in module %s]"),
11947 dwp_file->name);
11948 }
11949
11950 /* It's easier for the rest of the code if we fake a struct dwo_file and
11951 have dwo_unit "live" in that. At least for now.
11952
11953 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11954 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11955 file, we can combine them back into a virtual DWO file to save space
11956 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11957 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11958
791afaa2
TT
11959 std::string virtual_dwo_name =
11960 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11961 sections.abbrev.get_id (),
11962 sections.line.get_id (),
11963 sections.loc.get_id (),
11964 sections.str_offsets.get_id ());
80626a55 11965 /* Can we use an existing virtual DWO file? */
976ca316 11966 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 11967 comp_dir);
80626a55
DE
11968 /* Create one if necessary. */
11969 if (*dwo_file_slot == NULL)
11970 {
b4f54984 11971 if (dwarf_read_debug)
80626a55
DE
11972 {
11973 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11974 virtual_dwo_name.c_str ());
80626a55 11975 }
51ac9db5 11976 dwo_file = new struct dwo_file;
976ca316 11977 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 11978 dwo_file->comp_dir = comp_dir;
80626a55
DE
11979 dwo_file->sections.abbrev = sections.abbrev;
11980 dwo_file->sections.line = sections.line;
11981 dwo_file->sections.loc = sections.loc;
11982 dwo_file->sections.macinfo = sections.macinfo;
11983 dwo_file->sections.macro = sections.macro;
11984 dwo_file->sections.str_offsets = sections.str_offsets;
11985 /* The "str" section is global to the entire DWP file. */
11986 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11987 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11988 there's no need to record it in dwo_file.
11989 Also, we can't simply record type sections in dwo_file because
11990 we record a pointer into the vector in dwo_unit. As we collect more
11991 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11992 for it, invalidating all copies of pointers into the previous
11993 contents. */
80626a55
DE
11994 *dwo_file_slot = dwo_file;
11995 }
11996 else
11997 {
b4f54984 11998 if (dwarf_read_debug)
80626a55
DE
11999 {
12000 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12001 virtual_dwo_name.c_str ());
80626a55 12002 }
9a3c8263 12003 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12004 }
80626a55 12005
976ca316 12006 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
12007 dwo_unit->dwo_file = dwo_file;
12008 dwo_unit->signature = signature;
8d749320 12009 dwo_unit->section =
976ca316 12010 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 12011 *dwo_unit->section = sections.info_or_types;
57d63ce2 12012 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12013
12014 return dwo_unit;
12015}
12016
73869dc2
DE
12017/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12018 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12019 piece within that section used by a TU/CU, return a virtual section
12020 of just that piece. */
12021
12022static struct dwarf2_section_info
976ca316 12023create_dwp_v2_section (dwarf2_per_objfile *per_objfile,
ed2dc618 12024 struct dwarf2_section_info *section,
73869dc2
DE
12025 bfd_size_type offset, bfd_size_type size)
12026{
12027 struct dwarf2_section_info result;
12028 asection *sectp;
12029
12030 gdb_assert (section != NULL);
12031 gdb_assert (!section->is_virtual);
12032
12033 memset (&result, 0, sizeof (result));
12034 result.s.containing_section = section;
dc4ccb6f 12035 result.is_virtual = true;
73869dc2
DE
12036
12037 if (size == 0)
12038 return result;
12039
96b79293 12040 sectp = section->get_bfd_section ();
73869dc2
DE
12041
12042 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12043 bounds of the real section. This is a pretty-rare event, so just
12044 flag an error (easier) instead of a warning and trying to cope. */
12045 if (sectp == NULL
fd361982 12046 || offset + size > bfd_section_size (sectp))
73869dc2 12047 {
73869dc2
DE
12048 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12049 " in section %s [in module %s]"),
fd361982 12050 sectp ? bfd_section_name (sectp) : "<unknown>",
976ca316 12051 objfile_name (per_objfile->objfile));
73869dc2
DE
12052 }
12053
12054 result.virtual_offset = offset;
12055 result.size = size;
12056 return result;
12057}
12058
12059/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12060 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12061 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12062 This is for DWP version 2 files. */
12063
12064static struct dwo_unit *
976ca316 12065create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
ed2dc618 12066 struct dwp_file *dwp_file,
73869dc2
DE
12067 uint32_t unit_index,
12068 const char *comp_dir,
12069 ULONGEST signature, int is_debug_types)
12070{
73869dc2
DE
12071 const struct dwp_hash_table *dwp_htab =
12072 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12073 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12074 const char *kind = is_debug_types ? "TU" : "CU";
12075 struct dwo_file *dwo_file;
12076 struct dwo_unit *dwo_unit;
12077 struct virtual_v2_dwo_sections sections;
12078 void **dwo_file_slot;
73869dc2
DE
12079 int i;
12080
12081 gdb_assert (dwp_file->version == 2);
12082
b4f54984 12083 if (dwarf_read_debug)
73869dc2
DE
12084 {
12085 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12086 kind,
12087 pulongest (unit_index), hex_string (signature),
12088 dwp_file->name);
12089 }
12090
12091 /* Fetch the section offsets of this DWO unit. */
12092
12093 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12094
12095 for (i = 0; i < dwp_htab->nr_columns; ++i)
12096 {
12097 uint32_t offset = read_4_bytes (dbfd,
12098 dwp_htab->section_pool.v2.offsets
12099 + (((unit_index - 1) * dwp_htab->nr_columns
12100 + i)
12101 * sizeof (uint32_t)));
12102 uint32_t size = read_4_bytes (dbfd,
12103 dwp_htab->section_pool.v2.sizes
12104 + (((unit_index - 1) * dwp_htab->nr_columns
12105 + i)
12106 * sizeof (uint32_t)));
12107
12108 switch (dwp_htab->section_pool.v2.section_ids[i])
12109 {
12110 case DW_SECT_INFO:
12111 case DW_SECT_TYPES:
12112 sections.info_or_types_offset = offset;
12113 sections.info_or_types_size = size;
12114 break;
12115 case DW_SECT_ABBREV:
12116 sections.abbrev_offset = offset;
12117 sections.abbrev_size = size;
12118 break;
12119 case DW_SECT_LINE:
12120 sections.line_offset = offset;
12121 sections.line_size = size;
12122 break;
12123 case DW_SECT_LOC:
12124 sections.loc_offset = offset;
12125 sections.loc_size = size;
12126 break;
12127 case DW_SECT_STR_OFFSETS:
12128 sections.str_offsets_offset = offset;
12129 sections.str_offsets_size = size;
12130 break;
12131 case DW_SECT_MACINFO:
12132 sections.macinfo_offset = offset;
12133 sections.macinfo_size = size;
12134 break;
12135 case DW_SECT_MACRO:
12136 sections.macro_offset = offset;
12137 sections.macro_size = size;
12138 break;
12139 }
12140 }
12141
12142 /* It's easier for the rest of the code if we fake a struct dwo_file and
12143 have dwo_unit "live" in that. At least for now.
12144
12145 The DWP file can be made up of a random collection of CUs and TUs.
12146 However, for each CU + set of TUs that came from the same original DWO
12147 file, we can combine them back into a virtual DWO file to save space
12148 (fewer struct dwo_file objects to allocate). Remember that for really
12149 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12150
791afaa2
TT
12151 std::string virtual_dwo_name =
12152 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12153 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12154 (long) (sections.line_size ? sections.line_offset : 0),
12155 (long) (sections.loc_size ? sections.loc_offset : 0),
12156 (long) (sections.str_offsets_size
12157 ? sections.str_offsets_offset : 0));
73869dc2 12158 /* Can we use an existing virtual DWO file? */
976ca316 12159 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12160 comp_dir);
73869dc2
DE
12161 /* Create one if necessary. */
12162 if (*dwo_file_slot == NULL)
12163 {
b4f54984 12164 if (dwarf_read_debug)
73869dc2
DE
12165 {
12166 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12167 virtual_dwo_name.c_str ());
73869dc2 12168 }
51ac9db5 12169 dwo_file = new struct dwo_file;
976ca316 12170 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12171 dwo_file->comp_dir = comp_dir;
12172 dwo_file->sections.abbrev =
976ca316 12173 create_dwp_v2_section (per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12174 sections.abbrev_offset, sections.abbrev_size);
12175 dwo_file->sections.line =
976ca316 12176 create_dwp_v2_section (per_objfile, &dwp_file->sections.line,
73869dc2
DE
12177 sections.line_offset, sections.line_size);
12178 dwo_file->sections.loc =
976ca316 12179 create_dwp_v2_section (per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12180 sections.loc_offset, sections.loc_size);
12181 dwo_file->sections.macinfo =
976ca316 12182 create_dwp_v2_section (per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12183 sections.macinfo_offset, sections.macinfo_size);
12184 dwo_file->sections.macro =
976ca316 12185 create_dwp_v2_section (per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12186 sections.macro_offset, sections.macro_size);
12187 dwo_file->sections.str_offsets =
976ca316 12188 create_dwp_v2_section (per_objfile,
ed2dc618 12189 &dwp_file->sections.str_offsets,
73869dc2
DE
12190 sections.str_offsets_offset,
12191 sections.str_offsets_size);
12192 /* The "str" section is global to the entire DWP file. */
12193 dwo_file->sections.str = dwp_file->sections.str;
12194 /* The info or types section is assigned below to dwo_unit,
12195 there's no need to record it in dwo_file.
12196 Also, we can't simply record type sections in dwo_file because
12197 we record a pointer into the vector in dwo_unit. As we collect more
12198 types we'll grow the vector and eventually have to reallocate space
12199 for it, invalidating all copies of pointers into the previous
12200 contents. */
12201 *dwo_file_slot = dwo_file;
12202 }
12203 else
12204 {
b4f54984 12205 if (dwarf_read_debug)
73869dc2
DE
12206 {
12207 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12208 virtual_dwo_name.c_str ());
73869dc2 12209 }
9a3c8263 12210 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12211 }
73869dc2 12212
976ca316 12213 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12214 dwo_unit->dwo_file = dwo_file;
12215 dwo_unit->signature = signature;
8d749320 12216 dwo_unit->section =
976ca316
SM
12217 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12218 *dwo_unit->section = create_dwp_v2_section (per_objfile,
ed2dc618 12219 is_debug_types
73869dc2
DE
12220 ? &dwp_file->sections.types
12221 : &dwp_file->sections.info,
12222 sections.info_or_types_offset,
12223 sections.info_or_types_size);
12224 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12225
12226 return dwo_unit;
12227}
12228
57d63ce2
DE
12229/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12230 Returns NULL if the signature isn't found. */
80626a55
DE
12231
12232static struct dwo_unit *
976ca316 12233lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
ed2dc618 12234 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12235 ULONGEST signature, int is_debug_types)
80626a55 12236{
57d63ce2
DE
12237 const struct dwp_hash_table *dwp_htab =
12238 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12239 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12240 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12241 uint32_t hash = signature & mask;
12242 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12243 unsigned int i;
12244 void **slot;
870f88f7 12245 struct dwo_unit find_dwo_cu;
80626a55
DE
12246
12247 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12248 find_dwo_cu.signature = signature;
19ac8c2e 12249 slot = htab_find_slot (is_debug_types
48b490f2
TT
12250 ? dwp_file->loaded_tus.get ()
12251 : dwp_file->loaded_cus.get (),
19ac8c2e 12252 &find_dwo_cu, INSERT);
80626a55
DE
12253
12254 if (*slot != NULL)
9a3c8263 12255 return (struct dwo_unit *) *slot;
80626a55
DE
12256
12257 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12258 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12259 {
12260 ULONGEST signature_in_table;
12261
12262 signature_in_table =
57d63ce2 12263 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12264 if (signature_in_table == signature)
12265 {
57d63ce2
DE
12266 uint32_t unit_index =
12267 read_4_bytes (dbfd,
12268 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12269
73869dc2
DE
12270 if (dwp_file->version == 1)
12271 {
976ca316
SM
12272 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12273 unit_index, comp_dir,
12274 signature, is_debug_types);
73869dc2
DE
12275 }
12276 else
12277 {
976ca316
SM
12278 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12279 unit_index, comp_dir,
12280 signature, is_debug_types);
73869dc2 12281 }
9a3c8263 12282 return (struct dwo_unit *) *slot;
80626a55
DE
12283 }
12284 if (signature_in_table == 0)
12285 return NULL;
12286 hash = (hash + hash2) & mask;
12287 }
12288
12289 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12290 " [in module %s]"),
12291 dwp_file->name);
12292}
12293
ab5088bf 12294/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12295 Open the file specified by FILE_NAME and hand it off to BFD for
12296 preliminary analysis. Return a newly initialized bfd *, which
12297 includes a canonicalized copy of FILE_NAME.
80626a55 12298 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12299 SEARCH_CWD is true if the current directory is to be searched.
12300 It will be searched before debug-file-directory.
13aaf454
DE
12301 If successful, the file is added to the bfd include table of the
12302 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12303 If unable to find/open the file, return NULL.
3019eac3
DE
12304 NOTE: This function is derived from symfile_bfd_open. */
12305
192b62ce 12306static gdb_bfd_ref_ptr
976ca316 12307try_open_dwop_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12308 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12309{
24b9144d 12310 int desc;
9c02c129
DE
12311 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12312 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12313 to debug_file_directory. */
e0cc99a6 12314 const char *search_path;
9c02c129
DE
12315 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12316
e0cc99a6 12317 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12318 if (search_cwd)
12319 {
12320 if (*debug_file_directory != '\0')
e0cc99a6
TT
12321 {
12322 search_path_holder.reset (concat (".", dirname_separator_string,
12323 debug_file_directory,
12324 (char *) NULL));
12325 search_path = search_path_holder.get ();
12326 }
6ac97d4c 12327 else
e0cc99a6 12328 search_path = ".";
6ac97d4c 12329 }
9c02c129 12330 else
e0cc99a6 12331 search_path = debug_file_directory;
3019eac3 12332
24b9144d 12333 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12334 if (is_dwp)
12335 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12336
12337 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12338 desc = openp (search_path, flags, file_name,
3019eac3
DE
12339 O_RDONLY | O_BINARY, &absolute_name);
12340 if (desc < 0)
12341 return NULL;
12342
e0cc99a6
TT
12343 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12344 gnutarget, desc));
9c02c129
DE
12345 if (sym_bfd == NULL)
12346 return NULL;
192b62ce 12347 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12348
192b62ce
TT
12349 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12350 return NULL;
3019eac3 12351
13aaf454
DE
12352 /* Success. Record the bfd as having been included by the objfile's bfd.
12353 This is important because things like demangled_names_hash lives in the
12354 objfile's per_bfd space and may have references to things like symbol
12355 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
976ca316 12356 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12357
3019eac3
DE
12358 return sym_bfd;
12359}
12360
ab5088bf 12361/* Try to open DWO file FILE_NAME.
3019eac3
DE
12362 COMP_DIR is the DW_AT_comp_dir attribute.
12363 The result is the bfd handle of the file.
12364 If there is a problem finding or opening the file, return NULL.
12365 Upon success, the canonicalized path of the file is stored in the bfd,
12366 same as symfile_bfd_open. */
12367
192b62ce 12368static gdb_bfd_ref_ptr
976ca316 12369open_dwo_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12370 const char *file_name, const char *comp_dir)
3019eac3 12371{
80626a55 12372 if (IS_ABSOLUTE_PATH (file_name))
976ca316 12373 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12374 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12375
12376 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12377
12378 if (comp_dir != NULL)
12379 {
43816ebc
TT
12380 gdb::unique_xmalloc_ptr<char> path_to_try
12381 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12382
12383 /* NOTE: If comp_dir is a relative path, this will also try the
12384 search path, which seems useful. */
976ca316 12385 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
ed2dc618 12386 0 /*is_dwp*/,
192b62ce 12387 1 /*search_cwd*/));
3019eac3
DE
12388 if (abfd != NULL)
12389 return abfd;
12390 }
12391
12392 /* That didn't work, try debug-file-directory, which, despite its name,
12393 is a list of paths. */
12394
12395 if (*debug_file_directory == '\0')
12396 return NULL;
12397
976ca316 12398 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12399 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12400}
12401
80626a55
DE
12402/* This function is mapped across the sections and remembers the offset and
12403 size of each of the DWO debugging sections we are interested in. */
12404
12405static void
12406dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12407{
9a3c8263 12408 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12409 const struct dwop_section_names *names = &dwop_section_names;
12410
12411 if (section_is_p (sectp->name, &names->abbrev_dwo))
12412 {
049412e3 12413 dwo_sections->abbrev.s.section = sectp;
fd361982 12414 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12415 }
12416 else if (section_is_p (sectp->name, &names->info_dwo))
12417 {
049412e3 12418 dwo_sections->info.s.section = sectp;
fd361982 12419 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12420 }
12421 else if (section_is_p (sectp->name, &names->line_dwo))
12422 {
049412e3 12423 dwo_sections->line.s.section = sectp;
fd361982 12424 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12425 }
12426 else if (section_is_p (sectp->name, &names->loc_dwo))
12427 {
049412e3 12428 dwo_sections->loc.s.section = sectp;
fd361982 12429 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12430 }
41144253 12431 else if (section_is_p (sectp->name, &names->loclists_dwo))
12432 {
12433 dwo_sections->loclists.s.section = sectp;
12434 dwo_sections->loclists.size = bfd_section_size (sectp);
12435 }
80626a55
DE
12436 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12437 {
049412e3 12438 dwo_sections->macinfo.s.section = sectp;
fd361982 12439 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12440 }
12441 else if (section_is_p (sectp->name, &names->macro_dwo))
12442 {
049412e3 12443 dwo_sections->macro.s.section = sectp;
fd361982 12444 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12445 }
12446 else if (section_is_p (sectp->name, &names->str_dwo))
12447 {
049412e3 12448 dwo_sections->str.s.section = sectp;
fd361982 12449 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12450 }
12451 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12452 {
049412e3 12453 dwo_sections->str_offsets.s.section = sectp;
fd361982 12454 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12455 }
12456 else if (section_is_p (sectp->name, &names->types_dwo))
12457 {
12458 struct dwarf2_section_info type_section;
12459
12460 memset (&type_section, 0, sizeof (type_section));
049412e3 12461 type_section.s.section = sectp;
fd361982 12462 type_section.size = bfd_section_size (sectp);
fd5866f6 12463 dwo_sections->types.push_back (type_section);
80626a55
DE
12464 }
12465}
12466
ab5088bf 12467/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12468 by PER_CU. This is for the non-DWP case.
80626a55 12469 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12470
12471static struct dwo_file *
4ab09049
SM
12472open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12473 const char *comp_dir)
3019eac3 12474{
976ca316 12475 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 12476
976ca316 12477 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
80626a55
DE
12478 if (dbfd == NULL)
12479 {
b4f54984 12480 if (dwarf_read_debug)
80626a55
DE
12481 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12482 return NULL;
12483 }
263db9a1 12484
51ac9db5 12485 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12486 dwo_file->dwo_name = dwo_name;
12487 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12488 dwo_file->dbfd = std::move (dbfd);
3019eac3 12489
fb1eb2f9 12490 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12491 &dwo_file->sections);
3019eac3 12492
976ca316
SM
12493 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12494 dwo_file->cus);
3019eac3 12495
976ca316 12496 create_debug_types_hash_table (per_objfile, dwo_file.get (),
ed2dc618 12497 dwo_file->sections.types, dwo_file->tus);
3019eac3 12498
b4f54984 12499 if (dwarf_read_debug)
80626a55
DE
12500 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12501
263db9a1 12502 return dwo_file.release ();
3019eac3
DE
12503}
12504
80626a55 12505/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12506 size of each of the DWP debugging sections common to version 1 and 2 that
12507 we are interested in. */
3019eac3 12508
80626a55 12509static void
73869dc2
DE
12510dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12511 void *dwp_file_ptr)
3019eac3 12512{
9a3c8263 12513 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12514 const struct dwop_section_names *names = &dwop_section_names;
12515 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12516
80626a55 12517 /* Record the ELF section number for later lookup: this is what the
73869dc2 12518 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12519 gdb_assert (elf_section_nr < dwp_file->num_sections);
12520 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12521
80626a55
DE
12522 /* Look for specific sections that we need. */
12523 if (section_is_p (sectp->name, &names->str_dwo))
12524 {
049412e3 12525 dwp_file->sections.str.s.section = sectp;
fd361982 12526 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12527 }
12528 else if (section_is_p (sectp->name, &names->cu_index))
12529 {
049412e3 12530 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12531 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12532 }
12533 else if (section_is_p (sectp->name, &names->tu_index))
12534 {
049412e3 12535 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12536 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12537 }
12538}
3019eac3 12539
73869dc2
DE
12540/* This function is mapped across the sections and remembers the offset and
12541 size of each of the DWP version 2 debugging sections that we are interested
12542 in. This is split into a separate function because we don't know if we
12543 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12544
12545static void
12546dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12547{
9a3c8263 12548 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12549 const struct dwop_section_names *names = &dwop_section_names;
12550 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12551
12552 /* Record the ELF section number for later lookup: this is what the
12553 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12554 gdb_assert (elf_section_nr < dwp_file->num_sections);
12555 dwp_file->elf_sections[elf_section_nr] = sectp;
12556
12557 /* Look for specific sections that we need. */
12558 if (section_is_p (sectp->name, &names->abbrev_dwo))
12559 {
049412e3 12560 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12561 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12562 }
12563 else if (section_is_p (sectp->name, &names->info_dwo))
12564 {
049412e3 12565 dwp_file->sections.info.s.section = sectp;
fd361982 12566 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12567 }
12568 else if (section_is_p (sectp->name, &names->line_dwo))
12569 {
049412e3 12570 dwp_file->sections.line.s.section = sectp;
fd361982 12571 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12572 }
12573 else if (section_is_p (sectp->name, &names->loc_dwo))
12574 {
049412e3 12575 dwp_file->sections.loc.s.section = sectp;
fd361982 12576 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12577 }
12578 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12579 {
049412e3 12580 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12581 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12582 }
12583 else if (section_is_p (sectp->name, &names->macro_dwo))
12584 {
049412e3 12585 dwp_file->sections.macro.s.section = sectp;
fd361982 12586 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12587 }
12588 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12589 {
049412e3 12590 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12591 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12592 }
12593 else if (section_is_p (sectp->name, &names->types_dwo))
12594 {
049412e3 12595 dwp_file->sections.types.s.section = sectp;
fd361982 12596 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12597 }
12598}
12599
80626a55 12600/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12601
80626a55
DE
12602static hashval_t
12603hash_dwp_loaded_cutus (const void *item)
12604{
9a3c8263 12605 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12606
80626a55
DE
12607 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12608 return dwo_unit->signature;
3019eac3
DE
12609}
12610
80626a55 12611/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12612
80626a55
DE
12613static int
12614eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12615{
9a3c8263
SM
12616 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12617 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12618
80626a55
DE
12619 return dua->signature == dub->signature;
12620}
3019eac3 12621
80626a55 12622/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12623
48b490f2 12624static htab_up
298e9637 12625allocate_dwp_loaded_cutus_table ()
80626a55 12626{
48b490f2
TT
12627 return htab_up (htab_create_alloc (3,
12628 hash_dwp_loaded_cutus,
12629 eq_dwp_loaded_cutus,
12630 NULL, xcalloc, xfree));
80626a55 12631}
3019eac3 12632
ab5088bf
DE
12633/* Try to open DWP file FILE_NAME.
12634 The result is the bfd handle of the file.
12635 If there is a problem finding or opening the file, return NULL.
12636 Upon success, the canonicalized path of the file is stored in the bfd,
12637 same as symfile_bfd_open. */
12638
192b62ce 12639static gdb_bfd_ref_ptr
976ca316 12640open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
ab5088bf 12641{
976ca316 12642 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
ed2dc618 12643 1 /*is_dwp*/,
192b62ce 12644 1 /*search_cwd*/));
6ac97d4c
DE
12645 if (abfd != NULL)
12646 return abfd;
12647
12648 /* Work around upstream bug 15652.
12649 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12650 [Whether that's a "bug" is debatable, but it is getting in our way.]
12651 We have no real idea where the dwp file is, because gdb's realpath-ing
12652 of the executable's path may have discarded the needed info.
12653 [IWBN if the dwp file name was recorded in the executable, akin to
12654 .gnu_debuglink, but that doesn't exist yet.]
12655 Strip the directory from FILE_NAME and search again. */
12656 if (*debug_file_directory != '\0')
12657 {
12658 /* Don't implicitly search the current directory here.
12659 If the user wants to search "." to handle this case,
12660 it must be added to debug-file-directory. */
976ca316
SM
12661 return try_open_dwop_file (per_objfile, lbasename (file_name),
12662 1 /*is_dwp*/,
6ac97d4c
DE
12663 0 /*search_cwd*/);
12664 }
12665
12666 return NULL;
ab5088bf
DE
12667}
12668
80626a55
DE
12669/* Initialize the use of the DWP file for the current objfile.
12670 By convention the name of the DWP file is ${objfile}.dwp.
12671 The result is NULL if it can't be found. */
a766d390 12672
400174b1 12673static std::unique_ptr<struct dwp_file>
976ca316 12674open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
80626a55 12675{
976ca316 12676 struct objfile *objfile = per_objfile->objfile;
80626a55 12677
82bf32bc
JK
12678 /* Try to find first .dwp for the binary file before any symbolic links
12679 resolving. */
6c447423
DE
12680
12681 /* If the objfile is a debug file, find the name of the real binary
12682 file and get the name of dwp file from there. */
d721ba37 12683 std::string dwp_name;
6c447423
DE
12684 if (objfile->separate_debug_objfile_backlink != NULL)
12685 {
12686 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12687 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12688
d721ba37 12689 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12690 }
12691 else
d721ba37
PA
12692 dwp_name = objfile->original_name;
12693
12694 dwp_name += ".dwp";
80626a55 12695
976ca316 12696 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12697 if (dbfd == NULL
12698 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12699 {
12700 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12701 dwp_name = objfile_name (objfile);
12702 dwp_name += ".dwp";
976ca316 12703 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
82bf32bc
JK
12704 }
12705
80626a55
DE
12706 if (dbfd == NULL)
12707 {
b4f54984 12708 if (dwarf_read_debug)
d721ba37 12709 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12710 return std::unique_ptr<dwp_file> ();
3019eac3 12711 }
400174b1
TT
12712
12713 const char *name = bfd_get_filename (dbfd.get ());
12714 std::unique_ptr<struct dwp_file> dwp_file
12715 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12716
0a0f4c01 12717 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 12718 dwp_file->elf_sections =
976ca316 12719 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
80626a55
DE
12720 dwp_file->num_sections, asection *);
12721
400174b1
TT
12722 bfd_map_over_sections (dwp_file->dbfd.get (),
12723 dwarf2_locate_common_dwp_sections,
12724 dwp_file.get ());
80626a55 12725
976ca316 12726 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
80626a55 12727
976ca316 12728 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
80626a55 12729
73869dc2 12730 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12731 if (dwp_file->cus && dwp_file->tus
12732 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12733 {
12734 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12735 pretty bizarre. We use pulongest here because that's the established
4d65956b 12736 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12737 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12738 " TU version %s [in DWP file %s]"),
12739 pulongest (dwp_file->cus->version),
d721ba37 12740 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12741 }
08302ed2
DE
12742
12743 if (dwp_file->cus)
12744 dwp_file->version = dwp_file->cus->version;
12745 else if (dwp_file->tus)
12746 dwp_file->version = dwp_file->tus->version;
12747 else
12748 dwp_file->version = 2;
73869dc2
DE
12749
12750 if (dwp_file->version == 2)
400174b1
TT
12751 bfd_map_over_sections (dwp_file->dbfd.get (),
12752 dwarf2_locate_v2_dwp_sections,
12753 dwp_file.get ());
73869dc2 12754
298e9637
SM
12755 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12756 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12757
b4f54984 12758 if (dwarf_read_debug)
80626a55
DE
12759 {
12760 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12761 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12762 " %s CUs, %s TUs\n",
12763 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12764 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12765 }
12766
12767 return dwp_file;
3019eac3 12768}
c906108c 12769
ab5088bf
DE
12770/* Wrapper around open_and_init_dwp_file, only open it once. */
12771
12772static struct dwp_file *
976ca316 12773get_dwp_file (dwarf2_per_objfile *per_objfile)
ab5088bf 12774{
976ca316 12775 if (!per_objfile->per_bfd->dwp_checked)
ab5088bf 12776 {
976ca316
SM
12777 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12778 per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 12779 }
976ca316 12780 return per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
12781}
12782
80626a55
DE
12783/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12784 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12785 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12786 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12787 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12788
12789 This is called, for example, when wanting to read a variable with a
12790 complex location. Therefore we don't want to do file i/o for every call.
12791 Therefore we don't want to look for a DWO file on every call.
12792 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12793 then we check if we've already seen DWO_NAME, and only THEN do we check
12794 for a DWO file.
12795
1c658ad5 12796 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12797 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12798
3019eac3 12799static struct dwo_unit *
4ab09049 12800lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 12801 ULONGEST signature, int is_debug_types)
3019eac3 12802{
976ca316
SM
12803 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12804 struct objfile *objfile = per_objfile->objfile;
80626a55
DE
12805 const char *kind = is_debug_types ? "TU" : "CU";
12806 void **dwo_file_slot;
3019eac3 12807 struct dwo_file *dwo_file;
80626a55 12808 struct dwp_file *dwp_file;
cb1df416 12809
6a506a2d
DE
12810 /* First see if there's a DWP file.
12811 If we have a DWP file but didn't find the DWO inside it, don't
12812 look for the original DWO file. It makes gdb behave differently
12813 depending on whether one is debugging in the build tree. */
cf2c3c16 12814
976ca316 12815 dwp_file = get_dwp_file (per_objfile);
80626a55 12816 if (dwp_file != NULL)
cf2c3c16 12817 {
80626a55
DE
12818 const struct dwp_hash_table *dwp_htab =
12819 is_debug_types ? dwp_file->tus : dwp_file->cus;
12820
12821 if (dwp_htab != NULL)
12822 {
12823 struct dwo_unit *dwo_cutu =
976ca316
SM
12824 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12825 is_debug_types);
80626a55
DE
12826
12827 if (dwo_cutu != NULL)
12828 {
b4f54984 12829 if (dwarf_read_debug)
80626a55
DE
12830 {
12831 fprintf_unfiltered (gdb_stdlog,
12832 "Virtual DWO %s %s found: @%s\n",
12833 kind, hex_string (signature),
12834 host_address_to_string (dwo_cutu));
12835 }
12836 return dwo_cutu;
12837 }
12838 }
12839 }
6a506a2d 12840 else
80626a55 12841 {
6a506a2d 12842 /* No DWP file, look for the DWO file. */
80626a55 12843
976ca316 12844 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
6a506a2d 12845 if (*dwo_file_slot == NULL)
80626a55 12846 {
6a506a2d 12847 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 12848 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 12849 }
6a506a2d 12850 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12851 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12852
6a506a2d 12853 if (dwo_file != NULL)
19c3d4c9 12854 {
6a506a2d
DE
12855 struct dwo_unit *dwo_cutu = NULL;
12856
12857 if (is_debug_types && dwo_file->tus)
12858 {
12859 struct dwo_unit find_dwo_cutu;
12860
12861 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12862 find_dwo_cutu.signature = signature;
9a3c8263 12863 dwo_cutu
b0b6a987
TT
12864 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12865 &find_dwo_cutu);
6a506a2d 12866 }
33c5cd75 12867 else if (!is_debug_types && dwo_file->cus)
80626a55 12868 {
33c5cd75
DB
12869 struct dwo_unit find_dwo_cutu;
12870
12871 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12872 find_dwo_cutu.signature = signature;
b0b6a987 12873 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12874 &find_dwo_cutu);
6a506a2d
DE
12875 }
12876
12877 if (dwo_cutu != NULL)
12878 {
b4f54984 12879 if (dwarf_read_debug)
6a506a2d
DE
12880 {
12881 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12882 kind, dwo_name, hex_string (signature),
12883 host_address_to_string (dwo_cutu));
12884 }
12885 return dwo_cutu;
80626a55
DE
12886 }
12887 }
2e276125 12888 }
9cdd5dbd 12889
80626a55
DE
12890 /* We didn't find it. This could mean a dwo_id mismatch, or
12891 someone deleted the DWO/DWP file, or the search path isn't set up
12892 correctly to find the file. */
12893
b4f54984 12894 if (dwarf_read_debug)
80626a55
DE
12895 {
12896 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12897 kind, dwo_name, hex_string (signature));
12898 }
3019eac3 12899
6656a72d
DE
12900 /* This is a warning and not a complaint because it can be caused by
12901 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12902 {
12903 /* Print the name of the DWP file if we looked there, helps the user
12904 better diagnose the problem. */
791afaa2 12905 std::string dwp_text;
43942612
DE
12906
12907 if (dwp_file != NULL)
791afaa2
TT
12908 dwp_text = string_printf (" [in DWP file %s]",
12909 lbasename (dwp_file->name));
43942612 12910
9d8780f0 12911 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 12912 " [in module %s]"),
4ab09049
SM
12913 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12914 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 12915 }
3019eac3 12916 return NULL;
5fb290d7
DJ
12917}
12918
80626a55
DE
12919/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12920 See lookup_dwo_cutu_unit for details. */
12921
12922static struct dwo_unit *
4ab09049 12923lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
12924 ULONGEST signature)
12925{
4ab09049
SM
12926 gdb_assert (!cu->per_cu->is_debug_types);
12927
12928 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
12929}
12930
12931/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12932 See lookup_dwo_cutu_unit for details. */
12933
12934static struct dwo_unit *
4ab09049 12935lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 12936{
4ab09049
SM
12937 gdb_assert (cu->per_cu->is_debug_types);
12938
12939 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12940
12941 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
12942}
12943
89e63ee4
DE
12944/* Traversal function for queue_and_load_all_dwo_tus. */
12945
12946static int
12947queue_and_load_dwo_tu (void **slot, void *info)
12948{
12949 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 12950 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 12951 ULONGEST signature = dwo_unit->signature;
d460f660 12952 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
12953
12954 if (sig_type != NULL)
12955 {
12956 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12957
12958 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12959 a real dependency of PER_CU on SIG_TYPE. That is detected later
12960 while processing PER_CU. */
120ce1b5 12961 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
d460f660
SM
12962 load_full_type_unit (sig_cu, cu->per_objfile);
12963 cu->per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12964 }
12965
12966 return 1;
12967}
12968
1b555f17 12969/* Queue all TUs contained in the DWO of CU to be read in.
89e63ee4
DE
12970 The DWO may have the only definition of the type, though it may not be
12971 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12972 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12973
12974static void
1b555f17 12975queue_and_load_all_dwo_tus (dwarf2_cu *cu)
89e63ee4
DE
12976{
12977 struct dwo_unit *dwo_unit;
12978 struct dwo_file *dwo_file;
12979
1b555f17
SM
12980 gdb_assert (cu != nullptr);
12981 gdb_assert (!cu->per_cu->is_debug_types);
12982 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
89e63ee4 12983
1b555f17 12984 dwo_unit = cu->dwo_unit;
89e63ee4
DE
12985 gdb_assert (dwo_unit != NULL);
12986
12987 dwo_file = dwo_unit->dwo_file;
12988 if (dwo_file->tus != NULL)
1b555f17 12989 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
89e63ee4
DE
12990}
12991
3019eac3 12992/* Read in various DIEs. */
348e048f 12993
d389af10 12994/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12995 Inherit only the children of the DW_AT_abstract_origin DIE not being
12996 already referenced by DW_AT_abstract_origin from the children of the
12997 current DIE. */
d389af10
JK
12998
12999static void
13000inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13001{
13002 struct die_info *child_die;
791afaa2 13003 sect_offset *offsetp;
d389af10
JK
13004 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13005 struct die_info *origin_die;
13006 /* Iterator of the ORIGIN_DIE children. */
13007 struct die_info *origin_child_die;
d389af10 13008 struct attribute *attr;
cd02d79d
PA
13009 struct dwarf2_cu *origin_cu;
13010 struct pending **origin_previous_list_in_scope;
d389af10
JK
13011
13012 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13013 if (!attr)
13014 return;
13015
cd02d79d
PA
13016 /* Note that following die references may follow to a die in a
13017 different cu. */
13018
13019 origin_cu = cu;
13020 origin_die = follow_die_ref (die, attr, &origin_cu);
13021
13022 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13023 symbols in. */
13024 origin_previous_list_in_scope = origin_cu->list_in_scope;
13025 origin_cu->list_in_scope = cu->list_in_scope;
13026
edb3359d
DJ
13027 if (die->tag != origin_die->tag
13028 && !(die->tag == DW_TAG_inlined_subroutine
13029 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13030 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13031 sect_offset_str (die->sect_off),
13032 sect_offset_str (origin_die->sect_off));
d389af10 13033
791afaa2 13034 std::vector<sect_offset> offsets;
d389af10 13035
3ea89b92
PMR
13036 for (child_die = die->child;
13037 child_die && child_die->tag;
436c571c 13038 child_die = child_die->sibling)
3ea89b92
PMR
13039 {
13040 struct die_info *child_origin_die;
13041 struct dwarf2_cu *child_origin_cu;
13042
13043 /* We are trying to process concrete instance entries:
216f72a1 13044 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13045 it's not relevant to our analysis here. i.e. detecting DIEs that are
13046 present in the abstract instance but not referenced in the concrete
13047 one. */
216f72a1
JK
13048 if (child_die->tag == DW_TAG_call_site
13049 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13050 continue;
13051
c38f313d
DJ
13052 /* For each CHILD_DIE, find the corresponding child of
13053 ORIGIN_DIE. If there is more than one layer of
13054 DW_AT_abstract_origin, follow them all; there shouldn't be,
13055 but GCC versions at least through 4.4 generate this (GCC PR
13056 40573). */
3ea89b92
PMR
13057 child_origin_die = child_die;
13058 child_origin_cu = cu;
c38f313d
DJ
13059 while (1)
13060 {
cd02d79d
PA
13061 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13062 child_origin_cu);
c38f313d
DJ
13063 if (attr == NULL)
13064 break;
cd02d79d
PA
13065 child_origin_die = follow_die_ref (child_origin_die, attr,
13066 &child_origin_cu);
c38f313d
DJ
13067 }
13068
d389af10
JK
13069 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13070 counterpart may exist. */
c38f313d 13071 if (child_origin_die != child_die)
d389af10 13072 {
edb3359d
DJ
13073 if (child_die->tag != child_origin_die->tag
13074 && !(child_die->tag == DW_TAG_inlined_subroutine
13075 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13076 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13077 "different tags"),
9d8780f0
SM
13078 sect_offset_str (child_die->sect_off),
13079 sect_offset_str (child_origin_die->sect_off));
c38f313d 13080 if (child_origin_die->parent != origin_die)
b98664d3 13081 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13082 "different parents"),
9d8780f0
SM
13083 sect_offset_str (child_die->sect_off),
13084 sect_offset_str (child_origin_die->sect_off));
c38f313d 13085 else
791afaa2 13086 offsets.push_back (child_origin_die->sect_off);
d389af10 13087 }
d389af10 13088 }
791afaa2
TT
13089 std::sort (offsets.begin (), offsets.end ());
13090 sect_offset *offsets_end = offsets.data () + offsets.size ();
13091 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13092 if (offsetp[-1] == *offsetp)
b98664d3 13093 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13094 "to DIE %s as their abstract origin"),
13095 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13096
791afaa2 13097 offsetp = offsets.data ();
d389af10
JK
13098 origin_child_die = origin_die->child;
13099 while (origin_child_die && origin_child_die->tag)
13100 {
13101 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13102 while (offsetp < offsets_end
9c541725 13103 && *offsetp < origin_child_die->sect_off)
d389af10 13104 offsetp++;
b64f50a1 13105 if (offsetp >= offsets_end
9c541725 13106 || *offsetp > origin_child_die->sect_off)
d389af10 13107 {
adde2bff
DE
13108 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13109 Check whether we're already processing ORIGIN_CHILD_DIE.
13110 This can happen with mutually referenced abstract_origins.
13111 PR 16581. */
13112 if (!origin_child_die->in_process)
13113 process_die (origin_child_die, origin_cu);
d389af10 13114 }
436c571c 13115 origin_child_die = origin_child_die->sibling;
d389af10 13116 }
cd02d79d 13117 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13118
13119 if (cu != origin_cu)
13120 compute_delayed_physnames (origin_cu);
d389af10
JK
13121}
13122
c906108c 13123static void
e7c27a73 13124read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13125{
5e22e966 13126 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13127 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13128 struct context_stack *newobj;
c906108c
SS
13129 CORE_ADDR lowpc;
13130 CORE_ADDR highpc;
13131 struct die_info *child_die;
edb3359d 13132 struct attribute *attr, *call_line, *call_file;
15d034d0 13133 const char *name;
e142c38c 13134 CORE_ADDR baseaddr;
801e3a5b 13135 struct block *block;
edb3359d 13136 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13137 std::vector<struct symbol *> template_args;
34eaf542 13138 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13139
13140 if (inlined_func)
13141 {
13142 /* If we do not have call site information, we can't show the
13143 caller of this inlined function. That's too confusing, so
13144 only use the scope for local variables. */
13145 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13146 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13147 if (call_line == NULL || call_file == NULL)
13148 {
13149 read_lexical_block_scope (die, cu);
13150 return;
13151 }
13152 }
c906108c 13153
b3b3bada 13154 baseaddr = objfile->text_section_offset ();
e142c38c 13155
94af9270 13156 name = dwarf2_name (die, cu);
c906108c 13157
e8d05480
JB
13158 /* Ignore functions with missing or empty names. These are actually
13159 illegal according to the DWARF standard. */
13160 if (name == NULL)
13161 {
b98664d3 13162 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13163 sect_offset_str (die->sect_off));
e8d05480
JB
13164 return;
13165 }
13166
13167 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13168 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13169 <= PC_BOUNDS_INVALID)
e8d05480 13170 {
ae4d0c03
PM
13171 attr = dwarf2_attr (die, DW_AT_external, cu);
13172 if (!attr || !DW_UNSND (attr))
b98664d3 13173 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13174 "for subprogram DIE at %s"),
13175 sect_offset_str (die->sect_off));
e8d05480
JB
13176 return;
13177 }
c906108c 13178
3e29f34a
MR
13179 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13180 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13181
34eaf542
TT
13182 /* If we have any template arguments, then we must allocate a
13183 different sort of symbol. */
436c571c 13184 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13185 {
13186 if (child_die->tag == DW_TAG_template_type_param
13187 || child_die->tag == DW_TAG_template_value_param)
13188 {
8c14c3a3 13189 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13190 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13191 break;
13192 }
13193 }
13194
c24bdb02 13195 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13196 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13197 (struct symbol *) templ_func);
4c2df51b 13198
81873cc8 13199 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13200 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13201 cu->language);
13202
4cecd739
DJ
13203 /* If there is a location expression for DW_AT_frame_base, record
13204 it. */
e142c38c 13205 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13206 if (attr != nullptr)
fe978cb0 13207 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13208
63e43d3a
PMR
13209 /* If there is a location for the static link, record it. */
13210 newobj->static_link = NULL;
13211 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13212 if (attr != nullptr)
63e43d3a 13213 {
224c3ddb
SM
13214 newobj->static_link
13215 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13216 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13217 cu->addr_type ());
63e43d3a
PMR
13218 }
13219
c24bdb02 13220 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13221
639d11d3 13222 if (die->child != NULL)
c906108c 13223 {
639d11d3 13224 child_die = die->child;
c906108c
SS
13225 while (child_die && child_die->tag)
13226 {
34eaf542
TT
13227 if (child_die->tag == DW_TAG_template_type_param
13228 || child_die->tag == DW_TAG_template_value_param)
13229 {
13230 struct symbol *arg = new_symbol (child_die, NULL, cu);
13231
f1078f66 13232 if (arg != NULL)
2f4732b0 13233 template_args.push_back (arg);
34eaf542
TT
13234 }
13235 else
13236 process_die (child_die, cu);
436c571c 13237 child_die = child_die->sibling;
c906108c
SS
13238 }
13239 }
13240
d389af10
JK
13241 inherit_abstract_dies (die, cu);
13242
4a811a97
UW
13243 /* If we have a DW_AT_specification, we might need to import using
13244 directives from the context of the specification DIE. See the
13245 comment in determine_prefix. */
13246 if (cu->language == language_cplus
13247 && dwarf2_attr (die, DW_AT_specification, cu))
13248 {
13249 struct dwarf2_cu *spec_cu = cu;
13250 struct die_info *spec_die = die_specification (die, &spec_cu);
13251
13252 while (spec_die)
13253 {
13254 child_die = spec_die->child;
13255 while (child_die && child_die->tag)
13256 {
13257 if (child_die->tag == DW_TAG_imported_module)
13258 process_die (child_die, spec_cu);
436c571c 13259 child_die = child_die->sibling;
4a811a97
UW
13260 }
13261
13262 /* In some cases, GCC generates specification DIEs that
13263 themselves contain DW_AT_specification attributes. */
13264 spec_die = die_specification (spec_die, &spec_cu);
13265 }
13266 }
13267
c24bdb02 13268 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13269 /* Make a block for the local symbols within. */
c24bdb02 13270 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13271 cstk.static_link, lowpc, highpc);
801e3a5b 13272
df8a16a1 13273 /* For C++, set the block's scope. */
45280282
IB
13274 if ((cu->language == language_cplus
13275 || cu->language == language_fortran
c44af4eb
TT
13276 || cu->language == language_d
13277 || cu->language == language_rust)
4d4ec4e5 13278 && cu->processing_has_namespace_info)
195a3f6c
TT
13279 block_set_scope (block, determine_prefix (die, cu),
13280 &objfile->objfile_obstack);
df8a16a1 13281
801e3a5b
JB
13282 /* If we have address ranges, record them. */
13283 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13284
a60f3166 13285 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13286
34eaf542 13287 /* Attach template arguments to function. */
2f4732b0 13288 if (!template_args.empty ())
34eaf542
TT
13289 {
13290 gdb_assert (templ_func != NULL);
13291
2f4732b0 13292 templ_func->n_template_arguments = template_args.size ();
34eaf542 13293 templ_func->template_arguments
8d749320
SM
13294 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13295 templ_func->n_template_arguments);
34eaf542 13296 memcpy (templ_func->template_arguments,
2f4732b0 13297 template_args.data (),
34eaf542 13298 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13299
13300 /* Make sure that the symtab is set on the new symbols. Even
13301 though they don't appear in this symtab directly, other parts
13302 of gdb assume that symbols do, and this is reasonably
13303 true. */
8634679f 13304 for (symbol *sym : template_args)
3e1d3d8c 13305 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13306 }
13307
208d8187
JB
13308 /* In C++, we can have functions nested inside functions (e.g., when
13309 a function declares a class that has methods). This means that
13310 when we finish processing a function scope, we may need to go
13311 back to building a containing block's symbol lists. */
c24bdb02
KS
13312 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13313 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13314
921e78cf
JB
13315 /* If we've finished processing a top-level function, subsequent
13316 symbols go in the file symbol list. */
c24bdb02
KS
13317 if (cu->get_builder ()->outermost_context_p ())
13318 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13319}
13320
13321/* Process all the DIES contained within a lexical block scope. Start
13322 a new scope, process the dies, and then close the scope. */
13323
13324static void
e7c27a73 13325read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13326{
5e22e966 13327 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13328 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13329 CORE_ADDR lowpc, highpc;
13330 struct die_info *child_die;
e142c38c
DJ
13331 CORE_ADDR baseaddr;
13332
b3b3bada 13333 baseaddr = objfile->text_section_offset ();
c906108c
SS
13334
13335 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13336 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13337 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13338 be nasty. Might be easier to properly extend generic blocks to
af34e669 13339 describe ranges. */
e385593e
JK
13340 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13341 {
13342 case PC_BOUNDS_NOT_PRESENT:
13343 /* DW_TAG_lexical_block has no attributes, process its children as if
13344 there was no wrapping by that DW_TAG_lexical_block.
13345 GCC does no longer produces such DWARF since GCC r224161. */
13346 for (child_die = die->child;
13347 child_die != NULL && child_die->tag;
436c571c 13348 child_die = child_die->sibling)
4f7bc5ed
TT
13349 {
13350 /* We might already be processing this DIE. This can happen
13351 in an unusual circumstance -- where a subroutine A
13352 appears lexically in another subroutine B, but A actually
13353 inlines B. The recursion is broken here, rather than in
13354 inherit_abstract_dies, because it seems better to simply
13355 drop concrete children here. */
13356 if (!child_die->in_process)
13357 process_die (child_die, cu);
13358 }
e385593e
JK
13359 return;
13360 case PC_BOUNDS_INVALID:
13361 return;
13362 }
3e29f34a
MR
13363 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13364 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13365
c24bdb02 13366 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13367 if (die->child != NULL)
c906108c 13368 {
639d11d3 13369 child_die = die->child;
c906108c
SS
13370 while (child_die && child_die->tag)
13371 {
e7c27a73 13372 process_die (child_die, cu);
436c571c 13373 child_die = child_die->sibling;
c906108c
SS
13374 }
13375 }
3ea89b92 13376 inherit_abstract_dies (die, cu);
c24bdb02 13377 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13378
c24bdb02
KS
13379 if (*cu->get_builder ()->get_local_symbols () != NULL
13380 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13381 {
801e3a5b 13382 struct block *block
c24bdb02 13383 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13384 cstk.start_addr, highpc);
801e3a5b
JB
13385
13386 /* Note that recording ranges after traversing children, as we
13387 do here, means that recording a parent's ranges entails
13388 walking across all its children's ranges as they appear in
13389 the address map, which is quadratic behavior.
13390
13391 It would be nicer to record the parent's ranges before
13392 traversing its children, simply overriding whatever you find
13393 there. But since we don't even decide whether to create a
13394 block until after we've traversed its children, that's hard
13395 to do. */
13396 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13397 }
c24bdb02
KS
13398 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13399 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13400}
13401
216f72a1 13402/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13403
13404static void
13405read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13406{
5e22e966 13407 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13408 struct objfile *objfile = per_objfile->objfile;
08feed99 13409 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13410 CORE_ADDR pc, baseaddr;
13411 struct attribute *attr;
13412 struct call_site *call_site, call_site_local;
13413 void **slot;
13414 int nparams;
13415 struct die_info *child_die;
13416
b3b3bada 13417 baseaddr = objfile->text_section_offset ();
96408a79 13418
216f72a1
JK
13419 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13420 if (attr == NULL)
13421 {
13422 /* This was a pre-DWARF-5 GNU extension alias
13423 for DW_AT_call_return_pc. */
13424 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13425 }
96408a79
SA
13426 if (!attr)
13427 {
b98664d3 13428 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13429 "DIE %s [in module %s]"),
13430 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13431 return;
13432 }
cd6c91b4 13433 pc = attr->value_as_address () + baseaddr;
3e29f34a 13434 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13435
13436 if (cu->call_site_htab == NULL)
13437 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13438 NULL, &objfile->objfile_obstack,
13439 hashtab_obstack_allocate, NULL);
13440 call_site_local.pc = pc;
13441 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13442 if (*slot != NULL)
13443 {
b98664d3 13444 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13445 "DIE %s [in module %s]"),
13446 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13447 objfile_name (objfile));
96408a79
SA
13448 return;
13449 }
13450
13451 /* Count parameters at the caller. */
13452
13453 nparams = 0;
13454 for (child_die = die->child; child_die && child_die->tag;
436c571c 13455 child_die = child_die->sibling)
96408a79 13456 {
216f72a1
JK
13457 if (child_die->tag != DW_TAG_call_site_parameter
13458 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13459 {
b98664d3 13460 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13461 "DW_TAG_call_site child DIE %s [in module %s]"),
13462 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13463 objfile_name (objfile));
96408a79
SA
13464 continue;
13465 }
13466
13467 nparams++;
13468 }
13469
224c3ddb
SM
13470 call_site
13471 = ((struct call_site *)
13472 obstack_alloc (&objfile->objfile_obstack,
13473 sizeof (*call_site)
13474 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13475 *slot = call_site;
13476 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13477 call_site->pc = pc;
13478
216f72a1
JK
13479 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13480 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13481 {
13482 struct die_info *func_die;
13483
13484 /* Skip also over DW_TAG_inlined_subroutine. */
13485 for (func_die = die->parent;
13486 func_die && func_die->tag != DW_TAG_subprogram
13487 && func_die->tag != DW_TAG_subroutine_type;
13488 func_die = func_die->parent);
13489
216f72a1
JK
13490 /* DW_AT_call_all_calls is a superset
13491 of DW_AT_call_all_tail_calls. */
96408a79 13492 if (func_die
216f72a1 13493 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13494 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13495 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13496 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13497 {
13498 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13499 not complete. But keep CALL_SITE for look ups via call_site_htab,
13500 both the initial caller containing the real return address PC and
13501 the final callee containing the current PC of a chain of tail
13502 calls do not need to have the tail call list complete. But any
13503 function candidate for a virtual tail call frame searched via
13504 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13505 determined unambiguously. */
13506 }
13507 else
13508 {
13509 struct type *func_type = NULL;
13510
13511 if (func_die)
13512 func_type = get_die_type (func_die, cu);
13513 if (func_type != NULL)
13514 {
78134374 13515 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13516
13517 /* Enlist this call site to the function. */
13518 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13519 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13520 }
13521 else
b98664d3 13522 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13523 "DIE %s [in module %s]"),
13524 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13525 }
13526 }
13527
216f72a1
JK
13528 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13529 if (attr == NULL)
13530 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13531 if (attr == NULL)
13532 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13533 if (attr == NULL)
216f72a1
JK
13534 {
13535 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13536 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13537 }
96408a79 13538 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13539 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13540 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13541 else if (attr->form_is_block ())
96408a79
SA
13542 {
13543 struct dwarf2_locexpr_baton *dlbaton;
13544
8d749320 13545 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13546 dlbaton->data = DW_BLOCK (attr)->data;
13547 dlbaton->size = DW_BLOCK (attr)->size;
a50264ba 13548 dlbaton->per_objfile = per_objfile;
96408a79
SA
13549 dlbaton->per_cu = cu->per_cu;
13550
13551 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13552 }
cd6c91b4 13553 else if (attr->form_is_ref ())
96408a79 13554 {
96408a79
SA
13555 struct dwarf2_cu *target_cu = cu;
13556 struct die_info *target_die;
13557
ac9ec31b 13558 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13559 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13560 if (die_is_declaration (target_die, target_cu))
13561 {
7d45c7c3 13562 const char *target_physname;
9112db09
JK
13563
13564 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13565 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13566 if (target_physname == NULL)
9112db09 13567 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13568 if (target_physname == NULL)
b98664d3 13569 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13570 "physname, for referencing DIE %s [in module %s]"),
13571 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13572 else
7d455152 13573 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13574 }
13575 else
13576 {
13577 CORE_ADDR lowpc;
13578
13579 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13580 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13581 <= PC_BOUNDS_INVALID)
b98664d3 13582 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13583 "low pc, for referencing DIE %s [in module %s]"),
13584 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13585 else
3e29f34a
MR
13586 {
13587 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13588 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13589 }
96408a79
SA
13590 }
13591 }
13592 else
b98664d3 13593 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13594 "block nor reference, for DIE %s [in module %s]"),
13595 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13596
13597 call_site->per_cu = cu->per_cu;
9f47c707 13598 call_site->per_objfile = per_objfile;
96408a79
SA
13599
13600 for (child_die = die->child;
13601 child_die && child_die->tag;
436c571c 13602 child_die = child_die->sibling)
96408a79 13603 {
96408a79 13604 struct call_site_parameter *parameter;
1788b2d3 13605 struct attribute *loc, *origin;
96408a79 13606
216f72a1
JK
13607 if (child_die->tag != DW_TAG_call_site_parameter
13608 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13609 {
13610 /* Already printed the complaint above. */
13611 continue;
13612 }
13613
13614 gdb_assert (call_site->parameter_count < nparams);
13615 parameter = &call_site->parameter[call_site->parameter_count];
13616
1788b2d3
JK
13617 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13618 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13619 register is contained in DW_AT_call_value. */
96408a79 13620
24c5c679 13621 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13622 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13623 if (origin == NULL)
13624 {
13625 /* This was a pre-DWARF-5 GNU extension alias
13626 for DW_AT_call_parameter. */
13627 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13628 }
cd6c91b4 13629 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13630 {
1788b2d3 13631 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13632
0826b30a 13633 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13634 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13635 {
13636 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13637 binding can be done only inside one CU. Such referenced DIE
13638 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13639 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13640 "DW_TAG_call_site child DIE %s [in module %s]"),
13641 sect_offset_str (child_die->sect_off),
9c541725 13642 objfile_name (objfile));
d76b7dbc
JK
13643 continue;
13644 }
9c541725
PA
13645 parameter->u.param_cu_off
13646 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13647 }
4fc6c0d5 13648 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13649 {
b98664d3 13650 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13651 "DW_TAG_call_site child DIE %s [in module %s]"),
13652 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13653 continue;
13654 }
24c5c679 13655 else
96408a79 13656 {
24c5c679
JK
13657 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13658 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13659 if (parameter->u.dwarf_reg != -1)
13660 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13661 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13662 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13663 &parameter->u.fb_offset))
13664 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13665 else
13666 {
b98664d3 13667 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13668 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13669 "DW_TAG_call_site child DIE %s "
24c5c679 13670 "[in module %s]"),
9d8780f0 13671 sect_offset_str (child_die->sect_off),
9c541725 13672 objfile_name (objfile));
24c5c679
JK
13673 continue;
13674 }
96408a79
SA
13675 }
13676
216f72a1
JK
13677 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13678 if (attr == NULL)
13679 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13680 if (attr == NULL || !attr->form_is_block ())
96408a79 13681 {
b98664d3 13682 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13683 "DW_TAG_call_site child DIE %s [in module %s]"),
13684 sect_offset_str (child_die->sect_off),
9c541725 13685 objfile_name (objfile));
96408a79
SA
13686 continue;
13687 }
13688 parameter->value = DW_BLOCK (attr)->data;
13689 parameter->value_size = DW_BLOCK (attr)->size;
13690
13691 /* Parameters are not pre-cleared by memset above. */
13692 parameter->data_value = NULL;
13693 parameter->data_value_size = 0;
13694 call_site->parameter_count++;
13695
216f72a1
JK
13696 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13697 if (attr == NULL)
13698 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13699 if (attr != nullptr)
96408a79 13700 {
4fc6c0d5 13701 if (!attr->form_is_block ())
b98664d3 13702 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13703 "DW_TAG_call_site child DIE %s [in module %s]"),
13704 sect_offset_str (child_die->sect_off),
9c541725 13705 objfile_name (objfile));
96408a79
SA
13706 else
13707 {
13708 parameter->data_value = DW_BLOCK (attr)->data;
13709 parameter->data_value_size = DW_BLOCK (attr)->size;
13710 }
13711 }
13712 }
13713}
13714
71a3c369
TT
13715/* Helper function for read_variable. If DIE represents a virtual
13716 table, then return the type of the concrete object that is
13717 associated with the virtual table. Otherwise, return NULL. */
13718
13719static struct type *
13720rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13721{
13722 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13723 if (attr == NULL)
13724 return NULL;
13725
13726 /* Find the type DIE. */
13727 struct die_info *type_die = NULL;
13728 struct dwarf2_cu *type_cu = cu;
13729
cd6c91b4 13730 if (attr->form_is_ref ())
71a3c369
TT
13731 type_die = follow_die_ref (die, attr, &type_cu);
13732 if (type_die == NULL)
13733 return NULL;
13734
13735 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13736 return NULL;
13737 return die_containing_type (type_die, type_cu);
13738}
13739
13740/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13741
13742static void
13743read_variable (struct die_info *die, struct dwarf2_cu *cu)
13744{
13745 struct rust_vtable_symbol *storage = NULL;
13746
13747 if (cu->language == language_rust)
13748 {
13749 struct type *containing_type = rust_containing_type (die, cu);
13750
13751 if (containing_type != NULL)
13752 {
5e22e966 13753 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 13754
8c14c3a3 13755 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 13756 storage->concrete_type = containing_type;
cf724bc9 13757 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13758 }
13759 }
13760
e4a62c65
TV
13761 struct symbol *res = new_symbol (die, NULL, cu, storage);
13762 struct attribute *abstract_origin
13763 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13764 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13765 if (res == NULL && loc && abstract_origin)
13766 {
13767 /* We have a variable without a name, but with a location and an abstract
13768 origin. This may be a concrete instance of an abstract variable
13769 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13770 later. */
13771 struct dwarf2_cu *origin_cu = cu;
13772 struct die_info *origin_die
13773 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
13774 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13775 per_objfile->per_bfd->abstract_to_concrete
13776 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13777 }
71a3c369
TT
13778}
13779
43988095
JK
13780/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13781 reading .debug_rnglists.
13782 Callback's type should be:
13783 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13784 Return true if the attributes are present and valid, otherwise,
13785 return false. */
13786
13787template <typename Callback>
13788static bool
13789dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13790 Callback &&callback)
13791{
976ca316
SM
13792 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13793 struct objfile *objfile = per_objfile->objfile;
43988095 13794 bfd *obfd = objfile->obfd;
43988095 13795 /* Base address selection entry. */
2b24b6e4 13796 gdb::optional<CORE_ADDR> base;
43988095 13797 const gdb_byte *buffer;
43988095
JK
13798 CORE_ADDR baseaddr;
13799 bool overflow = false;
13800
43988095
JK
13801 base = cu->base_address;
13802
976ca316
SM
13803 per_objfile->per_bfd->rnglists.read (objfile);
13804 if (offset >= per_objfile->per_bfd->rnglists.size)
43988095 13805 {
b98664d3 13806 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13807 offset);
13808 return false;
13809 }
976ca316 13810 buffer = per_objfile->per_bfd->rnglists.buffer + offset;
43988095 13811
b3b3bada 13812 baseaddr = objfile->text_section_offset ();
43988095
JK
13813
13814 while (1)
13815 {
7814882a
JK
13816 /* Initialize it due to a false compiler warning. */
13817 CORE_ADDR range_beginning = 0, range_end = 0;
976ca316
SM
13818 const gdb_byte *buf_end = (per_objfile->per_bfd->rnglists.buffer
13819 + per_objfile->per_bfd->rnglists.size);
43988095
JK
13820 unsigned int bytes_read;
13821
13822 if (buffer == buf_end)
13823 {
13824 overflow = true;
13825 break;
13826 }
13827 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13828 switch (rlet)
13829 {
13830 case DW_RLE_end_of_list:
13831 break;
13832 case DW_RLE_base_address:
13833 if (buffer + cu->header.addr_size > buf_end)
13834 {
13835 overflow = true;
13836 break;
13837 }
c8a7a66f 13838 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13839 buffer += bytes_read;
13840 break;
13841 case DW_RLE_start_length:
13842 if (buffer + cu->header.addr_size > buf_end)
13843 {
13844 overflow = true;
13845 break;
13846 }
c8a7a66f
TT
13847 range_beginning = cu->header.read_address (obfd, buffer,
13848 &bytes_read);
43988095
JK
13849 buffer += bytes_read;
13850 range_end = (range_beginning
13851 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13852 buffer += bytes_read;
13853 if (buffer > buf_end)
13854 {
13855 overflow = true;
13856 break;
13857 }
13858 break;
13859 case DW_RLE_offset_pair:
13860 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13861 buffer += bytes_read;
13862 if (buffer > buf_end)
13863 {
13864 overflow = true;
13865 break;
13866 }
13867 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13868 buffer += bytes_read;
13869 if (buffer > buf_end)
13870 {
13871 overflow = true;
13872 break;
13873 }
13874 break;
13875 case DW_RLE_start_end:
13876 if (buffer + 2 * cu->header.addr_size > buf_end)
13877 {
13878 overflow = true;
13879 break;
13880 }
c8a7a66f
TT
13881 range_beginning = cu->header.read_address (obfd, buffer,
13882 &bytes_read);
43988095 13883 buffer += bytes_read;
c8a7a66f 13884 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13885 buffer += bytes_read;
13886 break;
13887 default:
b98664d3 13888 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13889 return false;
13890 }
13891 if (rlet == DW_RLE_end_of_list || overflow)
13892 break;
13893 if (rlet == DW_RLE_base_address)
13894 continue;
13895
2b24b6e4 13896 if (!base.has_value ())
43988095
JK
13897 {
13898 /* We have no valid base address for the ranges
13899 data. */
b98664d3 13900 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13901 return false;
13902 }
13903
13904 if (range_beginning > range_end)
13905 {
13906 /* Inverted range entries are invalid. */
b98664d3 13907 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13908 return false;
13909 }
13910
13911 /* Empty range entries have no effect. */
13912 if (range_beginning == range_end)
13913 continue;
13914
2b24b6e4
TT
13915 range_beginning += *base;
13916 range_end += *base;
43988095
JK
13917
13918 /* A not-uncommon case of bad debug info.
13919 Don't pollute the addrmap with bad data. */
13920 if (range_beginning + baseaddr == 0
976ca316 13921 && !per_objfile->per_bfd->has_section_at_zero)
43988095 13922 {
b98664d3 13923 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13924 " [in module %s]"), objfile_name (objfile));
13925 continue;
13926 }
13927
13928 callback (range_beginning, range_end);
13929 }
13930
13931 if (overflow)
13932 {
b98664d3 13933 complaint (_("Offset %d is not terminated "
43988095
JK
13934 "for DW_AT_ranges attribute"),
13935 offset);
13936 return false;
13937 }
13938
13939 return true;
13940}
13941
13942/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13943 Callback's type should be:
13944 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13945 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13946
43988095 13947template <typename Callback>
43039443 13948static int
5f46c5a5 13949dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13950 Callback &&callback)
43039443 13951{
5e22e966
SM
13952 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13953 struct objfile *objfile = per_objfile->objfile;
43039443
JK
13954 struct comp_unit_head *cu_header = &cu->header;
13955 bfd *obfd = objfile->obfd;
13956 unsigned int addr_size = cu_header->addr_size;
13957 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13958 /* Base address selection entry. */
2b24b6e4 13959 gdb::optional<CORE_ADDR> base;
43039443 13960 unsigned int dummy;
d521ce57 13961 const gdb_byte *buffer;
ff013f42 13962 CORE_ADDR baseaddr;
43039443 13963
43988095
JK
13964 if (cu_header->version >= 5)
13965 return dwarf2_rnglists_process (offset, cu, callback);
13966
d00adf39 13967 base = cu->base_address;
43039443 13968
5e22e966
SM
13969 per_objfile->per_bfd->ranges.read (objfile);
13970 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 13971 {
b98664d3 13972 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13973 offset);
13974 return 0;
13975 }
5e22e966 13976 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 13977
b3b3bada 13978 baseaddr = objfile->text_section_offset ();
ff013f42 13979
43039443
JK
13980 while (1)
13981 {
13982 CORE_ADDR range_beginning, range_end;
13983
c8a7a66f 13984 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13985 buffer += addr_size;
c8a7a66f 13986 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13987 buffer += addr_size;
13988 offset += 2 * addr_size;
13989
13990 /* An end of list marker is a pair of zero addresses. */
13991 if (range_beginning == 0 && range_end == 0)
13992 /* Found the end of list entry. */
13993 break;
13994
13995 /* Each base address selection entry is a pair of 2 values.
13996 The first is the largest possible address, the second is
13997 the base address. Check for a base address here. */
13998 if ((range_beginning & mask) == mask)
13999 {
28d2bfb9
AB
14000 /* If we found the largest possible address, then we already
14001 have the base address in range_end. */
14002 base = range_end;
43039443
JK
14003 continue;
14004 }
14005
2b24b6e4 14006 if (!base.has_value ())
43039443
JK
14007 {
14008 /* We have no valid base address for the ranges
14009 data. */
b98664d3 14010 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14011 return 0;
14012 }
14013
9277c30c
UW
14014 if (range_beginning > range_end)
14015 {
14016 /* Inverted range entries are invalid. */
b98664d3 14017 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14018 return 0;
14019 }
14020
14021 /* Empty range entries have no effect. */
14022 if (range_beginning == range_end)
14023 continue;
14024
2b24b6e4
TT
14025 range_beginning += *base;
14026 range_end += *base;
43039443 14027
01093045
DE
14028 /* A not-uncommon case of bad debug info.
14029 Don't pollute the addrmap with bad data. */
14030 if (range_beginning + baseaddr == 0
5e22e966 14031 && !per_objfile->per_bfd->has_section_at_zero)
01093045 14032 {
b98664d3 14033 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14034 " [in module %s]"), objfile_name (objfile));
01093045
DE
14035 continue;
14036 }
14037
5f46c5a5
JK
14038 callback (range_beginning, range_end);
14039 }
14040
14041 return 1;
14042}
14043
14044/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14045 Return 1 if the attributes are present and valid, otherwise, return 0.
14046 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14047
14048static int
14049dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14050 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 14051 dwarf2_psymtab *ranges_pst)
5f46c5a5 14052{
5e22e966 14053 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14054 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 14055 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
14056 int low_set = 0;
14057 CORE_ADDR low = 0;
14058 CORE_ADDR high = 0;
14059 int retval;
14060
14061 retval = dwarf2_ranges_process (offset, cu,
14062 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14063 {
9277c30c 14064 if (ranges_pst != NULL)
3e29f34a
MR
14065 {
14066 CORE_ADDR lowpc;
14067 CORE_ADDR highpc;
14068
79748972
TT
14069 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14070 range_beginning + baseaddr)
14071 - baseaddr);
14072 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14073 range_end + baseaddr)
14074 - baseaddr);
d320c2b5
TT
14075 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14076 lowpc, highpc - 1, ranges_pst);
3e29f34a 14077 }
ff013f42 14078
43039443
JK
14079 /* FIXME: This is recording everything as a low-high
14080 segment of consecutive addresses. We should have a
14081 data structure for discontiguous block ranges
14082 instead. */
14083 if (! low_set)
14084 {
14085 low = range_beginning;
14086 high = range_end;
14087 low_set = 1;
14088 }
14089 else
14090 {
14091 if (range_beginning < low)
14092 low = range_beginning;
14093 if (range_end > high)
14094 high = range_end;
14095 }
5f46c5a5
JK
14096 });
14097 if (!retval)
14098 return 0;
43039443
JK
14099
14100 if (! low_set)
14101 /* If the first entry is an end-of-list marker, the range
14102 describes an empty scope, i.e. no instructions. */
14103 return 0;
14104
14105 if (low_return)
14106 *low_return = low;
14107 if (high_return)
14108 *high_return = high;
14109 return 1;
14110}
14111
3a2b436a
JK
14112/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14113 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14114 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14115
3a2b436a 14116static enum pc_bounds_kind
af34e669 14117dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14118 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14119 dwarf2_psymtab *pst)
c906108c 14120{
976ca316 14121 dwarf2_per_objfile *per_objfile = cu->per_objfile;
c906108c 14122 struct attribute *attr;
91da1414 14123 struct attribute *attr_high;
af34e669
DJ
14124 CORE_ADDR low = 0;
14125 CORE_ADDR high = 0;
e385593e 14126 enum pc_bounds_kind ret;
c906108c 14127
91da1414
MW
14128 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14129 if (attr_high)
af34e669 14130 {
e142c38c 14131 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14132 if (attr != nullptr)
91da1414 14133 {
cd6c91b4
TT
14134 low = attr->value_as_address ();
14135 high = attr_high->value_as_address ();
14136 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14137 high += low;
91da1414 14138 }
af34e669
DJ
14139 else
14140 /* Found high w/o low attribute. */
e385593e 14141 return PC_BOUNDS_INVALID;
af34e669
DJ
14142
14143 /* Found consecutive range of addresses. */
3a2b436a 14144 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14145 }
c906108c 14146 else
af34e669 14147 {
e142c38c 14148 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14149 if (attr != NULL)
14150 {
18a8505e 14151 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14152 We take advantage of the fact that DW_AT_ranges does not appear
14153 in DW_TAG_compile_unit of DWO files. */
14154 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14155 unsigned int ranges_offset = (DW_UNSND (attr)
14156 + (need_ranges_base
14157 ? cu->ranges_base
14158 : 0));
2e3cf129 14159
af34e669 14160 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14161 .debug_ranges section. */
2e3cf129 14162 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14163 return PC_BOUNDS_INVALID;
43039443 14164 /* Found discontinuous range of addresses. */
3a2b436a 14165 ret = PC_BOUNDS_RANGES;
af34e669 14166 }
e385593e
JK
14167 else
14168 return PC_BOUNDS_NOT_PRESENT;
af34e669 14169 }
c906108c 14170
48fbe735 14171 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14172 if (high <= low)
e385593e 14173 return PC_BOUNDS_INVALID;
c906108c
SS
14174
14175 /* When using the GNU linker, .gnu.linkonce. sections are used to
14176 eliminate duplicate copies of functions and vtables and such.
14177 The linker will arbitrarily choose one and discard the others.
14178 The AT_*_pc values for such functions refer to local labels in
14179 these sections. If the section from that file was discarded, the
14180 labels are not in the output, so the relocs get a value of 0.
14181 If this is a discarded function, mark the pc bounds as invalid,
14182 so that GDB will ignore it. */
976ca316 14183 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
e385593e 14184 return PC_BOUNDS_INVALID;
c906108c
SS
14185
14186 *lowpc = low;
96408a79
SA
14187 if (highpc)
14188 *highpc = high;
af34e669 14189 return ret;
c906108c
SS
14190}
14191
b084d499
JB
14192/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14193 its low and high PC addresses. Do nothing if these addresses could not
14194 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14195 and HIGHPC to the high address if greater than HIGHPC. */
14196
14197static void
14198dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14199 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14200 struct dwarf2_cu *cu)
14201{
14202 CORE_ADDR low, high;
14203 struct die_info *child = die->child;
14204
e385593e 14205 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14206 {
325fac50
PA
14207 *lowpc = std::min (*lowpc, low);
14208 *highpc = std::max (*highpc, high);
b084d499
JB
14209 }
14210
14211 /* If the language does not allow nested subprograms (either inside
14212 subprograms or lexical blocks), we're done. */
14213 if (cu->language != language_ada)
14214 return;
6e70227d 14215
b084d499
JB
14216 /* Check all the children of the given DIE. If it contains nested
14217 subprograms, then check their pc bounds. Likewise, we need to
14218 check lexical blocks as well, as they may also contain subprogram
14219 definitions. */
14220 while (child && child->tag)
14221 {
14222 if (child->tag == DW_TAG_subprogram
14223 || child->tag == DW_TAG_lexical_block)
14224 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14225 child = child->sibling;
b084d499
JB
14226 }
14227}
14228
fae299cd
DC
14229/* Get the low and high pc's represented by the scope DIE, and store
14230 them in *LOWPC and *HIGHPC. If the correct values can't be
14231 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14232
14233static void
14234get_scope_pc_bounds (struct die_info *die,
14235 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14236 struct dwarf2_cu *cu)
14237{
14238 CORE_ADDR best_low = (CORE_ADDR) -1;
14239 CORE_ADDR best_high = (CORE_ADDR) 0;
14240 CORE_ADDR current_low, current_high;
14241
3a2b436a 14242 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14243 >= PC_BOUNDS_RANGES)
fae299cd
DC
14244 {
14245 best_low = current_low;
14246 best_high = current_high;
14247 }
14248 else
14249 {
14250 struct die_info *child = die->child;
14251
14252 while (child && child->tag)
14253 {
14254 switch (child->tag) {
14255 case DW_TAG_subprogram:
b084d499 14256 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14257 break;
14258 case DW_TAG_namespace:
f55ee35c 14259 case DW_TAG_module:
fae299cd
DC
14260 /* FIXME: carlton/2004-01-16: Should we do this for
14261 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14262 that current GCC's always emit the DIEs corresponding
14263 to definitions of methods of classes as children of a
14264 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14265 the DIEs giving the declarations, which could be
14266 anywhere). But I don't see any reason why the
14267 standards says that they have to be there. */
14268 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14269
14270 if (current_low != ((CORE_ADDR) -1))
14271 {
325fac50
PA
14272 best_low = std::min (best_low, current_low);
14273 best_high = std::max (best_high, current_high);
fae299cd
DC
14274 }
14275 break;
14276 default:
0963b4bd 14277 /* Ignore. */
fae299cd
DC
14278 break;
14279 }
14280
436c571c 14281 child = child->sibling;
fae299cd
DC
14282 }
14283 }
14284
14285 *lowpc = best_low;
14286 *highpc = best_high;
14287}
14288
801e3a5b
JB
14289/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14290 in DIE. */
380bca97 14291
801e3a5b
JB
14292static void
14293dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14294 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14295{
5e22e966 14296 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14297 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14298 struct attribute *attr;
91da1414 14299 struct attribute *attr_high;
801e3a5b 14300
91da1414
MW
14301 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14302 if (attr_high)
801e3a5b 14303 {
801e3a5b 14304 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14305 if (attr != nullptr)
801e3a5b 14306 {
cd6c91b4
TT
14307 CORE_ADDR low = attr->value_as_address ();
14308 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 14309
cd6c91b4 14310 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14311 high += low;
9a619af0 14312
3e29f34a
MR
14313 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14314 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14315 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14316 }
14317 }
14318
14319 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14320 if (attr != nullptr)
801e3a5b 14321 {
18a8505e 14322 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14323 We take advantage of the fact that DW_AT_ranges does not appear
14324 in DW_TAG_compile_unit of DWO files. */
14325 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14326
14327 /* The value of the DW_AT_ranges attribute is the offset of the
14328 address range list in the .debug_ranges section. */
ab435259
DE
14329 unsigned long offset = (DW_UNSND (attr)
14330 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14331
2d5f09ec 14332 std::vector<blockrange> blockvec;
5f46c5a5
JK
14333 dwarf2_ranges_process (offset, cu,
14334 [&] (CORE_ADDR start, CORE_ADDR end)
14335 {
58fdfd2c
JK
14336 start += baseaddr;
14337 end += baseaddr;
5f46c5a5
JK
14338 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14339 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14340 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14341 blockvec.emplace_back (start, end);
5f46c5a5 14342 });
2d5f09ec
KB
14343
14344 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14345 }
14346}
14347
685b1105
JK
14348/* Check whether the producer field indicates either of GCC < 4.6, or the
14349 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14350
685b1105
JK
14351static void
14352check_producer (struct dwarf2_cu *cu)
60d5a603 14353{
38360086 14354 int major, minor;
60d5a603
JK
14355
14356 if (cu->producer == NULL)
14357 {
14358 /* For unknown compilers expect their behavior is DWARF version
14359 compliant.
14360
14361 GCC started to support .debug_types sections by -gdwarf-4 since
14362 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14363 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14364 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14365 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14366 }
b1ffba5a 14367 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14368 {
38360086
MW
14369 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14370 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14371 }
5230b05a 14372 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14373 {
14374 cu->producer_is_icc = true;
14375 cu->producer_is_icc_lt_14 = major < 14;
14376 }
c258c396
JD
14377 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14378 cu->producer_is_codewarrior = true;
685b1105
JK
14379 else
14380 {
14381 /* For other non-GCC compilers, expect their behavior is DWARF version
14382 compliant. */
60d5a603
JK
14383 }
14384
9068261f 14385 cu->checked_producer = true;
685b1105 14386}
ba919b58 14387
685b1105
JK
14388/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14389 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14390 during 4.6.0 experimental. */
14391
9068261f 14392static bool
685b1105
JK
14393producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14394{
14395 if (!cu->checked_producer)
14396 check_producer (cu);
14397
14398 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14399}
14400
c258c396
JD
14401
14402/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14403 with incorrect is_stmt attributes. */
14404
14405static bool
14406producer_is_codewarrior (struct dwarf2_cu *cu)
14407{
14408 if (!cu->checked_producer)
14409 check_producer (cu);
14410
14411 return cu->producer_is_codewarrior;
14412}
14413
405feb71 14414/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14415 DW_AT_accessibility. */
14416
14417static enum dwarf_access_attribute
14418dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14419{
14420 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14421 {
14422 /* The default DWARF 2 accessibility for members is public, the default
14423 accessibility for inheritance is private. */
14424
14425 if (die->tag != DW_TAG_inheritance)
14426 return DW_ACCESS_public;
14427 else
14428 return DW_ACCESS_private;
14429 }
14430 else
14431 {
14432 /* DWARF 3+ defines the default accessibility a different way. The same
14433 rules apply now for DW_TAG_inheritance as for the members and it only
14434 depends on the container kind. */
14435
14436 if (die->parent->tag == DW_TAG_class_type)
14437 return DW_ACCESS_private;
14438 else
14439 return DW_ACCESS_public;
14440 }
14441}
14442
74ac6d43
TT
14443/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14444 offset. If the attribute was not found return 0, otherwise return
14445 1. If it was found but could not properly be handled, set *OFFSET
14446 to 0. */
14447
14448static int
14449handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14450 LONGEST *offset)
14451{
14452 struct attribute *attr;
14453
14454 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14455 if (attr != NULL)
14456 {
14457 *offset = 0;
14458
14459 /* Note that we do not check for a section offset first here.
14460 This is because DW_AT_data_member_location is new in DWARF 4,
14461 so if we see it, we can assume that a constant form is really
14462 a constant and not a section offset. */
cd6c91b4 14463 if (attr->form_is_constant ())
0826b30a 14464 *offset = attr->constant_value (0);
cd6c91b4 14465 else if (attr->form_is_section_offset ())
74ac6d43 14466 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14467 else if (attr->form_is_block ())
74ac6d43
TT
14468 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14469 else
14470 dwarf2_complex_location_expr_complaint ();
14471
14472 return 1;
14473 }
14474
14475 return 0;
14476}
14477
7d79de9a
TT
14478/* Look for DW_AT_data_member_location and store the results in FIELD. */
14479
14480static void
14481handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14482 struct field *field)
14483{
14484 struct attribute *attr;
14485
14486 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14487 if (attr != NULL)
14488 {
14489 if (attr->form_is_constant ())
14490 {
14491 LONGEST offset = attr->constant_value (0);
14492 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14493 }
14494 else if (attr->form_is_section_offset ())
14495 dwarf2_complex_location_expr_complaint ();
14496 else if (attr->form_is_block ())
14497 {
14498 bool handled;
14499 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14500 if (handled)
14501 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14502 else
14503 {
5e22e966
SM
14504 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14505 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14506 struct dwarf2_locexpr_baton *dlbaton
14507 = XOBNEW (&objfile->objfile_obstack,
14508 struct dwarf2_locexpr_baton);
14509 dlbaton->data = DW_BLOCK (attr)->data;
14510 dlbaton->size = DW_BLOCK (attr)->size;
14511 /* When using this baton, we want to compute the address
14512 of the field, not the value. This is why
14513 is_reference is set to false here. */
14514 dlbaton->is_reference = false;
5e22e966 14515 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14516 dlbaton->per_cu = cu->per_cu;
14517
14518 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14519 }
14520 }
14521 else
14522 dwarf2_complex_location_expr_complaint ();
14523 }
14524}
14525
c906108c
SS
14526/* Add an aggregate field to the field list. */
14527
14528static void
107d2387 14529dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14530 struct dwarf2_cu *cu)
6e70227d 14531{
5e22e966 14532 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14533 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14534 struct nextfield *new_field;
14535 struct attribute *attr;
14536 struct field *fp;
15d034d0 14537 const char *fieldname = "";
c906108c 14538
7d0ccb61
DJ
14539 if (die->tag == DW_TAG_inheritance)
14540 {
be2daae6
TT
14541 fip->baseclasses.emplace_back ();
14542 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14543 }
14544 else
14545 {
be2daae6
TT
14546 fip->fields.emplace_back ();
14547 new_field = &fip->fields.back ();
7d0ccb61 14548 }
be2daae6 14549
9c6a1327
TT
14550 new_field->offset = die->sect_off;
14551
e142c38c 14552 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14553 if (attr != nullptr)
c906108c 14554 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14555 else
14556 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14557 if (new_field->accessibility != DW_ACCESS_public)
14558 fip->non_public_fields = 1;
60d5a603 14559
e142c38c 14560 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14561 if (attr != nullptr)
c906108c 14562 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14563 else
14564 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14565
14566 fp = &new_field->field;
a9a9bd0f 14567
e142c38c 14568 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14569 {
a9a9bd0f 14570 /* Data member other than a C++ static data member. */
6e70227d 14571
c906108c 14572 /* Get type of field. */
5d14b6e5 14573 fp->set_type (die_type (die, cu));
c906108c 14574
d6a843b5 14575 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14576
c906108c 14577 /* Get bit size of field (zero if none). */
e142c38c 14578 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14579 if (attr != nullptr)
c906108c
SS
14580 {
14581 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14582 }
14583 else
14584 {
14585 FIELD_BITSIZE (*fp) = 0;
14586 }
14587
14588 /* Get bit offset of field. */
7d79de9a 14589 handle_data_member_location (die, cu, fp);
e142c38c 14590 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14591 if (attr != nullptr)
c906108c 14592 {
d5a22e77 14593 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14594 {
14595 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14596 additional bit offset from the MSB of the containing
14597 anonymous object to the MSB of the field. We don't
14598 have to do anything special since we don't need to
14599 know the size of the anonymous object. */
f41f5e61 14600 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14601 }
14602 else
14603 {
14604 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14605 MSB of the anonymous object, subtract off the number of
14606 bits from the MSB of the field to the MSB of the
14607 object, and then subtract off the number of bits of
14608 the field itself. The result is the bit offset of
14609 the LSB of the field. */
c906108c
SS
14610 int anonymous_size;
14611 int bit_offset = DW_UNSND (attr);
14612
e142c38c 14613 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14614 if (attr != nullptr)
c906108c
SS
14615 {
14616 /* The size of the anonymous object containing
14617 the bit field is explicit, so use the
14618 indicated size (in bytes). */
14619 anonymous_size = DW_UNSND (attr);
14620 }
14621 else
14622 {
14623 /* The size of the anonymous object containing
14624 the bit field must be inferred from the type
14625 attribute of the data member containing the
14626 bit field. */
5d14b6e5 14627 anonymous_size = TYPE_LENGTH (fp->type ());
c906108c 14628 }
f41f5e61
PA
14629 SET_FIELD_BITPOS (*fp,
14630 (FIELD_BITPOS (*fp)
14631 + anonymous_size * bits_per_byte
14632 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14633 }
14634 }
da5b30da
AA
14635 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14636 if (attr != NULL)
14637 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14638 + attr->constant_value (0)));
c906108c
SS
14639
14640 /* Get name of field. */
39cbfefa
DJ
14641 fieldname = dwarf2_name (die, cu);
14642 if (fieldname == NULL)
14643 fieldname = "";
d8151005
DJ
14644
14645 /* The name is already allocated along with this objfile, so we don't
14646 need to duplicate it for the type. */
14647 fp->name = fieldname;
c906108c
SS
14648
14649 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14650 pointer or virtual base class pointer) to private. */
e142c38c 14651 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14652 {
d48cc9dd 14653 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14654 new_field->accessibility = DW_ACCESS_private;
14655 fip->non_public_fields = 1;
14656 }
14657 }
a9a9bd0f 14658 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14659 {
a9a9bd0f
DC
14660 /* C++ static member. */
14661
14662 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14663 is a declaration, but all versions of G++ as of this writing
14664 (so through at least 3.2.1) incorrectly generate
14665 DW_TAG_variable tags. */
6e70227d 14666
ff355380 14667 const char *physname;
c906108c 14668
a9a9bd0f 14669 /* Get name of field. */
39cbfefa
DJ
14670 fieldname = dwarf2_name (die, cu);
14671 if (fieldname == NULL)
c906108c
SS
14672 return;
14673
254e6b9e 14674 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14675 if (attr
14676 /* Only create a symbol if this is an external value.
14677 new_symbol checks this and puts the value in the global symbol
14678 table, which we want. If it is not external, new_symbol
14679 will try to put the value in cu->list_in_scope which is wrong. */
14680 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14681 {
14682 /* A static const member, not much different than an enum as far as
14683 we're concerned, except that we can support more types. */
14684 new_symbol (die, NULL, cu);
14685 }
14686
2df3850c 14687 /* Get physical name. */
ff355380 14688 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14689
d8151005
DJ
14690 /* The name is already allocated along with this objfile, so we don't
14691 need to duplicate it for the type. */
14692 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
5d14b6e5 14693 fp->set_type (die_type (die, cu));
d8151005 14694 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14695 }
14696 else if (die->tag == DW_TAG_inheritance)
14697 {
74ac6d43 14698 /* C++ base class field. */
7d79de9a 14699 handle_data_member_location (die, cu, fp);
c906108c 14700 FIELD_BITSIZE (*fp) = 0;
5d14b6e5
SM
14701 fp->set_type (die_type (die, cu));
14702 FIELD_NAME (*fp) = fp->type ()->name ();
c906108c 14703 }
2ddeaf8a
TT
14704 else
14705 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14706}
14707
883fd55a
KS
14708/* Can the type given by DIE define another type? */
14709
14710static bool
14711type_can_define_types (const struct die_info *die)
14712{
14713 switch (die->tag)
14714 {
14715 case DW_TAG_typedef:
14716 case DW_TAG_class_type:
14717 case DW_TAG_structure_type:
14718 case DW_TAG_union_type:
14719 case DW_TAG_enumeration_type:
14720 return true;
14721
14722 default:
14723 return false;
14724 }
14725}
14726
14727/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14728
14729static void
883fd55a
KS
14730dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14731 struct dwarf2_cu *cu)
6e70227d 14732{
be2daae6
TT
14733 struct decl_field fp;
14734 memset (&fp, 0, sizeof (fp));
98751a41 14735
883fd55a 14736 gdb_assert (type_can_define_types (die));
98751a41 14737
883fd55a 14738 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14739 fp.name = dwarf2_name (die, cu);
14740 fp.type = read_type_die (die, cu);
98751a41 14741
c191a687
KS
14742 /* Save accessibility. */
14743 enum dwarf_access_attribute accessibility;
14744 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14745 if (attr != NULL)
14746 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14747 else
14748 accessibility = dwarf2_default_access_attribute (die, cu);
14749 switch (accessibility)
14750 {
14751 case DW_ACCESS_public:
14752 /* The assumed value if neither private nor protected. */
14753 break;
14754 case DW_ACCESS_private:
be2daae6 14755 fp.is_private = 1;
c191a687
KS
14756 break;
14757 case DW_ACCESS_protected:
be2daae6 14758 fp.is_protected = 1;
c191a687
KS
14759 break;
14760 default:
b98664d3 14761 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14762 }
14763
883fd55a 14764 if (die->tag == DW_TAG_typedef)
be2daae6 14765 fip->typedef_field_list.push_back (fp);
883fd55a 14766 else
be2daae6 14767 fip->nested_types_list.push_back (fp);
98751a41
JK
14768}
14769
9c6a1327
TT
14770/* A convenience typedef that's used when finding the discriminant
14771 field for a variant part. */
1b95cdb7
SM
14772typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14773 offset_map_type;
9c6a1327
TT
14774
14775/* Compute the discriminant range for a given variant. OBSTACK is
14776 where the results will be stored. VARIANT is the variant to
14777 process. IS_UNSIGNED indicates whether the discriminant is signed
14778 or unsigned. */
14779
14780static const gdb::array_view<discriminant_range>
14781convert_variant_range (struct obstack *obstack, const variant_field &variant,
14782 bool is_unsigned)
14783{
14784 std::vector<discriminant_range> ranges;
14785
14786 if (variant.default_branch)
14787 return {};
14788
14789 if (variant.discr_list_data == nullptr)
14790 {
14791 discriminant_range r
14792 = {variant.discriminant_value, variant.discriminant_value};
14793 ranges.push_back (r);
14794 }
14795 else
14796 {
14797 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14798 variant.discr_list_data->size);
14799 while (!data.empty ())
14800 {
14801 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14802 {
14803 complaint (_("invalid discriminant marker: %d"), data[0]);
14804 break;
14805 }
14806 bool is_range = data[0] == DW_DSC_range;
14807 data = data.slice (1);
14808
14809 ULONGEST low, high;
14810 unsigned int bytes_read;
14811
14812 if (data.empty ())
14813 {
14814 complaint (_("DW_AT_discr_list missing low value"));
14815 break;
14816 }
14817 if (is_unsigned)
14818 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14819 else
14820 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14821 &bytes_read);
14822 data = data.slice (bytes_read);
14823
14824 if (is_range)
14825 {
14826 if (data.empty ())
14827 {
14828 complaint (_("DW_AT_discr_list missing high value"));
14829 break;
14830 }
14831 if (is_unsigned)
14832 high = read_unsigned_leb128 (nullptr, data.data (),
14833 &bytes_read);
14834 else
14835 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14836 &bytes_read);
14837 data = data.slice (bytes_read);
14838 }
14839 else
14840 high = low;
14841
14842 ranges.push_back ({ low, high });
14843 }
14844 }
14845
14846 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14847 ranges.size ());
14848 std::copy (ranges.begin (), ranges.end (), result);
14849 return gdb::array_view<discriminant_range> (result, ranges.size ());
14850}
14851
14852static const gdb::array_view<variant_part> create_variant_parts
14853 (struct obstack *obstack,
14854 const offset_map_type &offset_map,
14855 struct field_info *fi,
14856 const std::vector<variant_part_builder> &variant_parts);
14857
14858/* Fill in a "struct variant" for a given variant field. RESULT is
14859 the variant to fill in. OBSTACK is where any needed allocations
14860 will be done. OFFSET_MAP holds the mapping from section offsets to
14861 fields for the type. FI describes the fields of the type we're
14862 processing. FIELD is the variant field we're converting. */
14863
14864static void
14865create_one_variant (variant &result, struct obstack *obstack,
14866 const offset_map_type &offset_map,
14867 struct field_info *fi, const variant_field &field)
14868{
14869 result.discriminants = convert_variant_range (obstack, field, false);
14870 result.first_field = field.first_field + fi->baseclasses.size ();
14871 result.last_field = field.last_field + fi->baseclasses.size ();
14872 result.parts = create_variant_parts (obstack, offset_map, fi,
14873 field.variant_parts);
14874}
14875
14876/* Fill in a "struct variant_part" for a given variant part. RESULT
14877 is the variant part to fill in. OBSTACK is where any needed
14878 allocations will be done. OFFSET_MAP holds the mapping from
14879 section offsets to fields for the type. FI describes the fields of
14880 the type we're processing. BUILDER is the variant part to be
14881 converted. */
14882
14883static void
14884create_one_variant_part (variant_part &result,
14885 struct obstack *obstack,
14886 const offset_map_type &offset_map,
14887 struct field_info *fi,
14888 const variant_part_builder &builder)
14889{
14890 auto iter = offset_map.find (builder.discriminant_offset);
14891 if (iter == offset_map.end ())
14892 {
14893 result.discriminant_index = -1;
14894 /* Doesn't matter. */
14895 result.is_unsigned = false;
14896 }
14897 else
14898 {
14899 result.discriminant_index = iter->second;
14900 result.is_unsigned
b6cdac4b 14901 = TYPE_UNSIGNED (fi->fields[result.discriminant_index].field.type ());
9c6a1327
TT
14902 }
14903
14904 size_t n = builder.variants.size ();
14905 variant *output = new (obstack) variant[n];
14906 for (size_t i = 0; i < n; ++i)
14907 create_one_variant (output[i], obstack, offset_map, fi,
14908 builder.variants[i]);
14909
14910 result.variants = gdb::array_view<variant> (output, n);
14911}
14912
14913/* Create a vector of variant parts that can be attached to a type.
14914 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14915 holds the mapping from section offsets to fields for the type. FI
14916 describes the fields of the type we're processing. VARIANT_PARTS
14917 is the vector to convert. */
14918
14919static const gdb::array_view<variant_part>
14920create_variant_parts (struct obstack *obstack,
14921 const offset_map_type &offset_map,
14922 struct field_info *fi,
14923 const std::vector<variant_part_builder> &variant_parts)
14924{
14925 if (variant_parts.empty ())
14926 return {};
14927
14928 size_t n = variant_parts.size ();
14929 variant_part *result = new (obstack) variant_part[n];
14930 for (size_t i = 0; i < n; ++i)
14931 create_one_variant_part (result[i], obstack, offset_map, fi,
14932 variant_parts[i]);
14933
14934 return gdb::array_view<variant_part> (result, n);
14935}
14936
14937/* Compute the variant part vector for FIP, attaching it to TYPE when
14938 done. */
14939
14940static void
14941add_variant_property (struct field_info *fip, struct type *type,
14942 struct dwarf2_cu *cu)
14943{
14944 /* Map section offsets of fields to their field index. Note the
14945 field index here does not take the number of baseclasses into
14946 account. */
14947 offset_map_type offset_map;
14948 for (int i = 0; i < fip->fields.size (); ++i)
14949 offset_map[fip->fields[i].offset] = i;
14950
5e22e966 14951 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
14952 gdb::array_view<variant_part> parts
14953 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14954 fip->variant_parts);
14955
14956 struct dynamic_prop prop;
14957 prop.kind = PROP_VARIANT_PARTS;
14958 prop.data.variant_parts
14959 = ((gdb::array_view<variant_part> *)
14960 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14961
5c54719c 14962 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
14963}
14964
c906108c
SS
14965/* Create the vector of fields, and attach it to the type. */
14966
14967static void
fba45db2 14968dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14969 struct dwarf2_cu *cu)
c906108c 14970{
317f7127 14971 int nfields = fip->nfields ();
c906108c
SS
14972
14973 /* Record the field count, allocate space for the array of fields,
14974 and create blank accessibility bitfields if necessary. */
5e33d5f4 14975 type->set_num_fields (nfields);
3cabb6b0
SM
14976 type->set_fields
14977 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 14978
b4ba55a1 14979 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14980 {
14981 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14982
14983 TYPE_FIELD_PRIVATE_BITS (type) =
14984 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14985 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14986
14987 TYPE_FIELD_PROTECTED_BITS (type) =
14988 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14989 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14990
774b6a14
TT
14991 TYPE_FIELD_IGNORE_BITS (type) =
14992 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14993 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14994 }
14995
14996 /* If the type has baseclasses, allocate and clear a bit vector for
14997 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14998 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14999 {
be2daae6 15000 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15001 unsigned char *pointer;
c906108c
SS
15002
15003 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15004 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15005 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15006 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15007 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15008 }
15009
9c6a1327
TT
15010 if (!fip->variant_parts.empty ())
15011 add_variant_property (fip, type, cu);
2ddeaf8a 15012
be2daae6
TT
15013 /* Copy the saved-up fields into the field vector. */
15014 for (int i = 0; i < nfields; ++i)
c906108c 15015 {
be2daae6
TT
15016 struct nextfield &field
15017 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15018 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15019
ceacbf6e 15020 type->field (i) = field.field;
be2daae6 15021 switch (field.accessibility)
c906108c 15022 {
c5aa993b 15023 case DW_ACCESS_private:
b4ba55a1 15024 if (cu->language != language_ada)
be2daae6 15025 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15026 break;
c906108c 15027
c5aa993b 15028 case DW_ACCESS_protected:
b4ba55a1 15029 if (cu->language != language_ada)
be2daae6 15030 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15031 break;
c906108c 15032
c5aa993b
JM
15033 case DW_ACCESS_public:
15034 break;
c906108c 15035
c5aa993b
JM
15036 default:
15037 /* Unknown accessibility. Complain and treat it as public. */
15038 {
b98664d3 15039 complaint (_("unsupported accessibility %d"),
be2daae6 15040 field.accessibility);
c5aa993b
JM
15041 }
15042 break;
c906108c 15043 }
be2daae6 15044 if (i < fip->baseclasses.size ())
c906108c 15045 {
be2daae6 15046 switch (field.virtuality)
c906108c 15047 {
c5aa993b
JM
15048 case DW_VIRTUALITY_virtual:
15049 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15050 if (cu->language == language_ada)
a73c6dcd 15051 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15052 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15053 break;
c906108c
SS
15054 }
15055 }
c906108c
SS
15056 }
15057}
15058
7d27a96d
TT
15059/* Return true if this member function is a constructor, false
15060 otherwise. */
15061
15062static int
15063dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15064{
15065 const char *fieldname;
fe978cb0 15066 const char *type_name;
7d27a96d
TT
15067 int len;
15068
15069 if (die->parent == NULL)
15070 return 0;
15071
15072 if (die->parent->tag != DW_TAG_structure_type
15073 && die->parent->tag != DW_TAG_union_type
15074 && die->parent->tag != DW_TAG_class_type)
15075 return 0;
15076
15077 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15078 type_name = dwarf2_name (die->parent, cu);
15079 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15080 return 0;
15081
15082 len = strlen (fieldname);
fe978cb0
PA
15083 return (strncmp (fieldname, type_name, len) == 0
15084 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15085}
15086
e35000a7
TBA
15087/* Check if the given VALUE is a recognized enum
15088 dwarf_defaulted_attribute constant according to DWARF5 spec,
15089 Table 7.24. */
15090
15091static bool
15092is_valid_DW_AT_defaulted (ULONGEST value)
15093{
15094 switch (value)
15095 {
15096 case DW_DEFAULTED_no:
15097 case DW_DEFAULTED_in_class:
15098 case DW_DEFAULTED_out_of_class:
15099 return true;
15100 }
15101
3142e908 15102 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
15103 return false;
15104}
15105
c906108c
SS
15106/* Add a member function to the proper fieldlist. */
15107
15108static void
107d2387 15109dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15110 struct type *type, struct dwarf2_cu *cu)
c906108c 15111{
5e22e966 15112 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15113 struct attribute *attr;
c906108c 15114 int i;
be2daae6 15115 struct fnfieldlist *flp = nullptr;
c906108c 15116 struct fn_field *fnp;
15d034d0 15117 const char *fieldname;
f792889a 15118 struct type *this_type;
60d5a603 15119 enum dwarf_access_attribute accessibility;
c906108c 15120
b4ba55a1 15121 if (cu->language == language_ada)
a73c6dcd 15122 error (_("unexpected member function in Ada type"));
b4ba55a1 15123
2df3850c 15124 /* Get name of member function. */
39cbfefa
DJ
15125 fieldname = dwarf2_name (die, cu);
15126 if (fieldname == NULL)
2df3850c 15127 return;
c906108c 15128
c906108c 15129 /* Look up member function name in fieldlist. */
be2daae6 15130 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15131 {
27bfe10e 15132 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15133 {
15134 flp = &fip->fnfieldlists[i];
15135 break;
15136 }
c906108c
SS
15137 }
15138
be2daae6
TT
15139 /* Create a new fnfieldlist if necessary. */
15140 if (flp == nullptr)
c906108c 15141 {
be2daae6
TT
15142 fip->fnfieldlists.emplace_back ();
15143 flp = &fip->fnfieldlists.back ();
c906108c 15144 flp->name = fieldname;
be2daae6 15145 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15146 }
15147
be2daae6
TT
15148 /* Create a new member function field and add it to the vector of
15149 fnfieldlists. */
15150 flp->fnfields.emplace_back ();
15151 fnp = &flp->fnfields.back ();
3da10d80
KS
15152
15153 /* Delay processing of the physname until later. */
9c37b5ae 15154 if (cu->language == language_cplus)
be2daae6
TT
15155 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15156 die, cu);
3da10d80
KS
15157 else
15158 {
1d06ead6 15159 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15160 fnp->physname = physname ? physname : "";
15161 }
15162
c906108c 15163 fnp->type = alloc_type (objfile);
f792889a 15164 this_type = read_type_die (die, cu);
78134374 15165 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15166 {
1f704f76 15167 int nparams = this_type->num_fields ();
c906108c 15168
f792889a 15169 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15170 of the method itself (TYPE_CODE_METHOD). */
15171 smash_to_method_type (fnp->type, type,
f792889a 15172 TYPE_TARGET_TYPE (this_type),
80fc5e77 15173 this_type->fields (),
1f704f76 15174 this_type->num_fields (),
f792889a 15175 TYPE_VARARGS (this_type));
c906108c
SS
15176
15177 /* Handle static member functions.
c5aa993b 15178 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15179 member functions. G++ helps GDB by marking the first
15180 parameter for non-static member functions (which is the this
15181 pointer) as artificial. We obtain this information from
15182 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15183 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15184 fnp->voffset = VOFFSET_STATIC;
15185 }
15186 else
b98664d3 15187 complaint (_("member function type missing for '%s'"),
3da10d80 15188 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15189
15190 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15191 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15192 fnp->fcontext = die_containing_type (die, cu);
c906108c 15193
3e43a32a
MS
15194 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15195 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15196
15197 /* Get accessibility. */
e142c38c 15198 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15199 if (attr != nullptr)
aead7601 15200 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15201 else
15202 accessibility = dwarf2_default_access_attribute (die, cu);
15203 switch (accessibility)
c906108c 15204 {
60d5a603
JK
15205 case DW_ACCESS_private:
15206 fnp->is_private = 1;
15207 break;
15208 case DW_ACCESS_protected:
15209 fnp->is_protected = 1;
15210 break;
c906108c
SS
15211 }
15212
b02dede2 15213 /* Check for artificial methods. */
e142c38c 15214 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15215 if (attr && DW_UNSND (attr) != 0)
15216 fnp->is_artificial = 1;
15217
e35000a7
TBA
15218 /* Check for defaulted methods. */
15219 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15220 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15221 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15222
15223 /* Check for deleted methods. */
15224 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15225 if (attr != nullptr && DW_UNSND (attr) != 0)
15226 fnp->is_deleted = 1;
15227
7d27a96d
TT
15228 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15229
0d564a31 15230 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15231 function. For older versions of GCC, this is an offset in the
15232 appropriate virtual table, as specified by DW_AT_containing_type.
15233 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15234 to the object address. */
15235
e142c38c 15236 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15237 if (attr != nullptr)
8e19ed76 15238 {
4fc6c0d5 15239 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 15240 {
aec5aa8b
TT
15241 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15242 {
15243 /* Old-style GCC. */
15244 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15245 }
15246 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15247 || (DW_BLOCK (attr)->size > 1
15248 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15249 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15250 {
aec5aa8b
TT
15251 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15252 if ((fnp->voffset % cu->header.addr_size) != 0)
15253 dwarf2_complex_location_expr_complaint ();
15254 else
15255 fnp->voffset /= cu->header.addr_size;
15256 fnp->voffset += 2;
15257 }
15258 else
15259 dwarf2_complex_location_expr_complaint ();
15260
15261 if (!fnp->fcontext)
7e993ebf
KS
15262 {
15263 /* If there is no `this' field and no DW_AT_containing_type,
15264 we cannot actually find a base class context for the
15265 vtable! */
1f704f76 15266 if (this_type->num_fields () == 0
7e993ebf
KS
15267 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15268 {
b98664d3 15269 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15270 "function \"%s\" (offset %s)"),
15271 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15272 }
15273 else
15274 {
15275 fnp->fcontext
940da03e 15276 = TYPE_TARGET_TYPE (this_type->field (0).type ());
7e993ebf
KS
15277 }
15278 }
aec5aa8b 15279 }
cd6c91b4 15280 else if (attr->form_is_section_offset ())
8e19ed76 15281 {
4d3c2250 15282 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15283 }
15284 else
15285 {
4d3c2250
KB
15286 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15287 fieldname);
8e19ed76 15288 }
0d564a31 15289 }
d48cc9dd
DJ
15290 else
15291 {
15292 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15293 if (attr && DW_UNSND (attr))
15294 {
15295 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15296 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15297 "but the vtable offset is not specified"),
9d8780f0 15298 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15299 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15300 TYPE_CPLUS_DYNAMIC (type) = 1;
15301 }
15302 }
c906108c
SS
15303}
15304
15305/* Create the vector of member function fields, and attach it to the type. */
15306
15307static void
fba45db2 15308dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15309 struct dwarf2_cu *cu)
c906108c 15310{
b4ba55a1 15311 if (cu->language == language_ada)
a73c6dcd 15312 error (_("unexpected member functions in Ada type"));
b4ba55a1 15313
c906108c
SS
15314 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15315 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15316 TYPE_ALLOC (type,
15317 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15318
be2daae6 15319 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15320 {
be2daae6 15321 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15322 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15323
be2daae6
TT
15324 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15325 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15326 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15327 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15328
15329 for (int k = 0; k < nf.fnfields.size (); ++k)
15330 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15331 }
15332
be2daae6 15333 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15334}
15335
1168df01
JB
15336/* Returns non-zero if NAME is the name of a vtable member in CU's
15337 language, zero otherwise. */
15338static int
15339is_vtable_name (const char *name, struct dwarf2_cu *cu)
15340{
15341 static const char vptr[] = "_vptr";
15342
9c37b5ae
TT
15343 /* Look for the C++ form of the vtable. */
15344 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15345 return 1;
15346
15347 return 0;
15348}
15349
c0dd20ea 15350/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15351 functions, with the ABI-specified layout. If TYPE describes
15352 such a structure, smash it into a member function type.
61049d3b
DJ
15353
15354 GCC shouldn't do this; it should just output pointer to member DIEs.
15355 This is GCC PR debug/28767. */
c0dd20ea 15356
0b92b5bb
TT
15357static void
15358quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15359{
09e2d7c7 15360 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15361
15362 /* Check for a structure with no name and two children. */
1f704f76 15363 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15364 return;
c0dd20ea
DJ
15365
15366 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15367 if (TYPE_FIELD_NAME (type, 0) == NULL
15368 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15369 || TYPE_FIELD_NAME (type, 1) == NULL
15370 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15371 return;
c0dd20ea
DJ
15372
15373 /* Find the type of the method. */
940da03e 15374 pfn_type = type->field (0).type ();
c0dd20ea 15375 if (pfn_type == NULL
78134374
SM
15376 || pfn_type->code () != TYPE_CODE_PTR
15377 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15378 return;
c0dd20ea
DJ
15379
15380 /* Look for the "this" argument. */
15381 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15382 if (pfn_type->num_fields () == 0
940da03e
SM
15383 /* || pfn_type->field (0).type () == NULL */
15384 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
0b92b5bb 15385 return;
c0dd20ea 15386
940da03e 15387 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
0b92b5bb 15388 new_type = alloc_type (objfile);
09e2d7c7 15389 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15390 pfn_type->fields (), pfn_type->num_fields (),
c0dd20ea 15391 TYPE_VARARGS (pfn_type));
0b92b5bb 15392 smash_to_methodptr_type (type, new_type);
c0dd20ea 15393}
1168df01 15394
2b4424c3
TT
15395/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15396 appropriate error checking and issuing complaints if there is a
15397 problem. */
15398
15399static ULONGEST
15400get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15401{
15402 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15403
15404 if (attr == nullptr)
15405 return 0;
15406
cd6c91b4 15407 if (!attr->form_is_constant ())
2b4424c3 15408 {
b98664d3 15409 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15410 " - DIE at %s [in module %s]"),
15411 sect_offset_str (die->sect_off),
5e22e966 15412 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15413 return 0;
15414 }
15415
15416 ULONGEST align;
15417 if (attr->form == DW_FORM_sdata)
15418 {
15419 LONGEST val = DW_SND (attr);
15420 if (val < 0)
15421 {
b98664d3 15422 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15423 " - DIE at %s [in module %s]"),
15424 sect_offset_str (die->sect_off),
5e22e966 15425 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15426 return 0;
15427 }
15428 align = val;
15429 }
15430 else
15431 align = DW_UNSND (attr);
15432
15433 if (align == 0)
15434 {
b98664d3 15435 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15436 " - DIE at %s [in module %s]"),
15437 sect_offset_str (die->sect_off),
5e22e966 15438 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15439 return 0;
15440 }
15441 if ((align & (align - 1)) != 0)
15442 {
b98664d3 15443 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15444 " - DIE at %s [in module %s]"),
15445 sect_offset_str (die->sect_off),
5e22e966 15446 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15447 return 0;
15448 }
15449
15450 return align;
15451}
15452
15453/* If the DIE has a DW_AT_alignment attribute, use its value to set
15454 the alignment for TYPE. */
15455
15456static void
15457maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15458 struct type *type)
15459{
15460 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15461 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15462 " - DIE at %s [in module %s]"),
15463 sect_offset_str (die->sect_off),
5e22e966 15464 objfile_name (cu->per_objfile->objfile));
2b4424c3 15465}
685b1105 15466
e35000a7
TBA
15467/* Check if the given VALUE is a valid enum dwarf_calling_convention
15468 constant for a type, according to DWARF5 spec, Table 5.5. */
15469
15470static bool
15471is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15472{
15473 switch (value)
15474 {
15475 case DW_CC_normal:
15476 case DW_CC_pass_by_reference:
15477 case DW_CC_pass_by_value:
15478 return true;
15479
15480 default:
15481 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15482 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15483 return false;
15484 }
15485}
15486
d0922fcf
TBA
15487/* Check if the given VALUE is a valid enum dwarf_calling_convention
15488 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15489 also according to GNU-specific values (see include/dwarf2.h). */
15490
15491static bool
15492is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15493{
15494 switch (value)
15495 {
15496 case DW_CC_normal:
15497 case DW_CC_program:
15498 case DW_CC_nocall:
15499 return true;
15500
15501 case DW_CC_GNU_renesas_sh:
15502 case DW_CC_GNU_borland_fastcall_i386:
15503 case DW_CC_GDB_IBM_OpenCL:
15504 return true;
15505
15506 default:
15507 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15508 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15509 return false;
15510 }
15511}
15512
c906108c 15513/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15514 (definition) to create a type for the structure or union. Fill in
15515 the type's name and general properties; the members will not be
83655187
DE
15516 processed until process_structure_scope. A symbol table entry for
15517 the type will also not be done until process_structure_scope (assuming
15518 the type has a name).
c906108c 15519
c767944b
DJ
15520 NOTE: we need to call these functions regardless of whether or not the
15521 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15522 structure or union. This gets the type entered into our set of
83655187 15523 user defined types. */
c906108c 15524
f792889a 15525static struct type *
134d01f1 15526read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15527{
5e22e966 15528 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
15529 struct type *type;
15530 struct attribute *attr;
15d034d0 15531 const char *name;
c906108c 15532
348e048f
DE
15533 /* If the definition of this type lives in .debug_types, read that type.
15534 Don't follow DW_AT_specification though, that will take us back up
15535 the chain and we want to go down. */
052c8bb8 15536 attr = die->attr (DW_AT_signature);
435d3d88 15537 if (attr != nullptr)
348e048f 15538 {
ac9ec31b 15539 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15540
ac9ec31b 15541 /* The type's CU may not be the same as CU.
02142a6c 15542 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15543 return set_die_type (die, type, cu);
15544 }
15545
c0dd20ea 15546 type = alloc_type (objfile);
c906108c 15547 INIT_CPLUS_SPECIFIC (type);
93311388 15548
39cbfefa
DJ
15549 name = dwarf2_name (die, cu);
15550 if (name != NULL)
c906108c 15551 {
987504bb 15552 if (cu->language == language_cplus
c44af4eb
TT
15553 || cu->language == language_d
15554 || cu->language == language_rust)
63d06c5c 15555 {
15d034d0 15556 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15557
15558 /* dwarf2_full_name might have already finished building the DIE's
15559 type. If so, there is no need to continue. */
15560 if (get_die_type (die, cu) != NULL)
15561 return get_die_type (die, cu);
15562
d0e39ea2 15563 type->set_name (full_name);
63d06c5c
DC
15564 }
15565 else
15566 {
d8151005
DJ
15567 /* The name is already allocated along with this objfile, so
15568 we don't need to duplicate it for the type. */
d0e39ea2 15569 type->set_name (name);
63d06c5c 15570 }
c906108c
SS
15571 }
15572
15573 if (die->tag == DW_TAG_structure_type)
15574 {
67607e24 15575 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15576 }
15577 else if (die->tag == DW_TAG_union_type)
15578 {
67607e24 15579 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15580 }
15581 else
15582 {
67607e24 15583 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15584 }
15585
0cc2414c
TT
15586 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15587 TYPE_DECLARED_CLASS (type) = 1;
15588
e35000a7
TBA
15589 /* Store the calling convention in the type if it's available in
15590 the die. Otherwise the calling convention remains set to
15591 the default value DW_CC_normal. */
15592 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15593 if (attr != nullptr
15594 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15595 {
15596 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15597 TYPE_CPLUS_CALLING_CONVENTION (type)
15598 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15599 }
15600
e142c38c 15601 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15602 if (attr != nullptr)
c906108c 15603 {
cd6c91b4 15604 if (attr->form_is_constant ())
155bfbd3
JB
15605 TYPE_LENGTH (type) = DW_UNSND (attr);
15606 else
15607 {
f8e89861 15608 struct dynamic_prop prop;
293e7e51 15609 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 15610 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
155bfbd3
JB
15611 TYPE_LENGTH (type) = 0;
15612 }
c906108c
SS
15613 }
15614 else
15615 {
15616 TYPE_LENGTH (type) = 0;
15617 }
15618
2b4424c3
TT
15619 maybe_set_alignment (cu, die, type);
15620
5230b05a 15621 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15622 {
5230b05a
WT
15623 /* ICC<14 does not output the required DW_AT_declaration on
15624 incomplete types, but gives them a size of zero. */
422b1cb0 15625 TYPE_STUB (type) = 1;
685b1105
JK
15626 }
15627 else
15628 TYPE_STUB_SUPPORTED (type) = 1;
15629
dc718098 15630 if (die_is_declaration (die, cu))
876cecd0 15631 TYPE_STUB (type) = 1;
a6c727b2
DJ
15632 else if (attr == NULL && die->child == NULL
15633 && producer_is_realview (cu->producer))
15634 /* RealView does not output the required DW_AT_declaration
15635 on incomplete types. */
15636 TYPE_STUB (type) = 1;
dc718098 15637
c906108c
SS
15638 /* We need to add the type field to the die immediately so we don't
15639 infinitely recurse when dealing with pointers to the structure
0963b4bd 15640 type within the structure itself. */
1c379e20 15641 set_die_type (die, type, cu);
c906108c 15642
7e314c57
JK
15643 /* set_die_type should be already done. */
15644 set_descriptive_type (type, die, cu);
15645
c767944b
DJ
15646 return type;
15647}
15648
9c6a1327
TT
15649static void handle_struct_member_die
15650 (struct die_info *child_die,
15651 struct type *type,
15652 struct field_info *fi,
15653 std::vector<struct symbol *> *template_args,
15654 struct dwarf2_cu *cu);
15655
15656/* A helper for handle_struct_member_die that handles
15657 DW_TAG_variant_part. */
15658
15659static void
15660handle_variant_part (struct die_info *die, struct type *type,
15661 struct field_info *fi,
15662 std::vector<struct symbol *> *template_args,
15663 struct dwarf2_cu *cu)
15664{
15665 variant_part_builder *new_part;
15666 if (fi->current_variant_part == nullptr)
15667 {
15668 fi->variant_parts.emplace_back ();
15669 new_part = &fi->variant_parts.back ();
15670 }
15671 else if (!fi->current_variant_part->processing_variant)
15672 {
15673 complaint (_("nested DW_TAG_variant_part seen "
15674 "- DIE at %s [in module %s]"),
15675 sect_offset_str (die->sect_off),
5e22e966 15676 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15677 return;
15678 }
15679 else
15680 {
15681 variant_field &current = fi->current_variant_part->variants.back ();
15682 current.variant_parts.emplace_back ();
15683 new_part = &current.variant_parts.back ();
15684 }
15685
15686 /* When we recurse, we want callees to add to this new variant
15687 part. */
15688 scoped_restore save_current_variant_part
15689 = make_scoped_restore (&fi->current_variant_part, new_part);
15690
15691 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15692 if (discr == NULL)
15693 {
15694 /* It's a univariant form, an extension we support. */
15695 }
15696 else if (discr->form_is_ref ())
15697 {
15698 struct dwarf2_cu *target_cu = cu;
15699 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15700
15701 new_part->discriminant_offset = target_die->sect_off;
15702 }
15703 else
15704 {
15705 complaint (_("DW_AT_discr does not have DIE reference form"
15706 " - DIE at %s [in module %s]"),
15707 sect_offset_str (die->sect_off),
5e22e966 15708 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15709 }
15710
15711 for (die_info *child_die = die->child;
15712 child_die != NULL;
15713 child_die = child_die->sibling)
15714 handle_struct_member_die (child_die, type, fi, template_args, cu);
15715}
15716
15717/* A helper for handle_struct_member_die that handles
15718 DW_TAG_variant. */
15719
15720static void
15721handle_variant (struct die_info *die, struct type *type,
15722 struct field_info *fi,
15723 std::vector<struct symbol *> *template_args,
15724 struct dwarf2_cu *cu)
15725{
15726 if (fi->current_variant_part == nullptr)
15727 {
15728 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15729 "- DIE at %s [in module %s]"),
15730 sect_offset_str (die->sect_off),
5e22e966 15731 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15732 return;
15733 }
15734 if (fi->current_variant_part->processing_variant)
15735 {
15736 complaint (_("nested DW_TAG_variant seen "
15737 "- DIE at %s [in module %s]"),
15738 sect_offset_str (die->sect_off),
5e22e966 15739 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15740 return;
15741 }
15742
15743 scoped_restore save_processing_variant
15744 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15745 true);
15746
15747 fi->current_variant_part->variants.emplace_back ();
15748 variant_field &variant = fi->current_variant_part->variants.back ();
15749 variant.first_field = fi->fields.size ();
15750
15751 /* In a variant we want to get the discriminant and also add a
15752 field for our sole member child. */
15753 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15754 if (discr == nullptr)
15755 {
15756 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15757 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15758 variant.default_branch = true;
15759 else
15760 variant.discr_list_data = DW_BLOCK (discr);
15761 }
15762 else
15763 variant.discriminant_value = DW_UNSND (discr);
15764
15765 for (die_info *variant_child = die->child;
15766 variant_child != NULL;
15767 variant_child = variant_child->sibling)
15768 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15769
15770 variant.last_field = fi->fields.size ();
15771}
15772
2ddeaf8a
TT
15773/* A helper for process_structure_scope that handles a single member
15774 DIE. */
15775
15776static void
15777handle_struct_member_die (struct die_info *child_die, struct type *type,
15778 struct field_info *fi,
15779 std::vector<struct symbol *> *template_args,
15780 struct dwarf2_cu *cu)
15781{
15782 if (child_die->tag == DW_TAG_member
9c6a1327 15783 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
15784 {
15785 /* NOTE: carlton/2002-11-05: A C++ static data member
15786 should be a DW_TAG_member that is a declaration, but
15787 all versions of G++ as of this writing (so through at
15788 least 3.2.1) incorrectly generate DW_TAG_variable
15789 tags for them instead. */
15790 dwarf2_add_field (fi, child_die, cu);
15791 }
15792 else if (child_die->tag == DW_TAG_subprogram)
15793 {
15794 /* Rust doesn't have member functions in the C++ sense.
15795 However, it does emit ordinary functions as children
15796 of a struct DIE. */
15797 if (cu->language == language_rust)
15798 read_func_scope (child_die, cu);
15799 else
15800 {
15801 /* C++ member function. */
15802 dwarf2_add_member_fn (fi, child_die, type, cu);
15803 }
15804 }
15805 else if (child_die->tag == DW_TAG_inheritance)
15806 {
15807 /* C++ base class field. */
15808 dwarf2_add_field (fi, child_die, cu);
15809 }
15810 else if (type_can_define_types (child_die))
15811 dwarf2_add_type_defn (fi, child_die, cu);
15812 else if (child_die->tag == DW_TAG_template_type_param
15813 || child_die->tag == DW_TAG_template_value_param)
15814 {
15815 struct symbol *arg = new_symbol (child_die, NULL, cu);
15816
15817 if (arg != NULL)
15818 template_args->push_back (arg);
15819 }
9c6a1327
TT
15820 else if (child_die->tag == DW_TAG_variant_part)
15821 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 15822 else if (child_die->tag == DW_TAG_variant)
9c6a1327 15823 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
15824}
15825
c767944b
DJ
15826/* Finish creating a structure or union type, including filling in
15827 its members and creating a symbol for it. */
15828
15829static void
15830process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15831{
5e22e966 15832 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 15833 struct die_info *child_die;
c767944b
DJ
15834 struct type *type;
15835
15836 type = get_die_type (die, cu);
15837 if (type == NULL)
15838 type = read_structure_type (die, cu);
15839
3e1d3d8c 15840 bool has_template_parameters = false;
e142c38c 15841 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15842 {
15843 struct field_info fi;
2f4732b0 15844 std::vector<struct symbol *> template_args;
c906108c 15845
639d11d3 15846 child_die = die->child;
c906108c
SS
15847
15848 while (child_die && child_die->tag)
15849 {
2ddeaf8a 15850 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 15851 child_die = child_die->sibling;
c906108c
SS
15852 }
15853
34eaf542 15854 /* Attach template arguments to type. */
2f4732b0 15855 if (!template_args.empty ())
34eaf542 15856 {
3e1d3d8c 15857 has_template_parameters = true;
34eaf542 15858 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15859 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15860 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15861 = XOBNEWVEC (&objfile->objfile_obstack,
15862 struct symbol *,
15863 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15864 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15865 template_args.data (),
34eaf542
TT
15866 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15867 * sizeof (struct symbol *)));
34eaf542
TT
15868 }
15869
c906108c 15870 /* Attach fields and member functions to the type. */
317f7127 15871 if (fi.nfields () > 0)
e7c27a73 15872 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15873 if (!fi.fnfieldlists.empty ())
c906108c 15874 {
e7c27a73 15875 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15876
c5aa993b 15877 /* Get the type which refers to the base class (possibly this
c906108c 15878 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15879 class from the DW_AT_containing_type attribute. This use of
15880 DW_AT_containing_type is a GNU extension. */
c906108c 15881
e142c38c 15882 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15883 {
e7c27a73 15884 struct type *t = die_containing_type (die, cu);
c906108c 15885
ae6ae975 15886 set_type_vptr_basetype (type, t);
c906108c
SS
15887 if (type == t)
15888 {
c906108c
SS
15889 int i;
15890
15891 /* Our own class provides vtbl ptr. */
1f704f76 15892 for (i = t->num_fields () - 1;
c906108c
SS
15893 i >= TYPE_N_BASECLASSES (t);
15894 --i)
15895 {
0d5cff50 15896 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15897
1168df01 15898 if (is_vtable_name (fieldname, cu))
c906108c 15899 {
ae6ae975 15900 set_type_vptr_fieldno (type, i);
c906108c
SS
15901 break;
15902 }
15903 }
15904
15905 /* Complain if virtual function table field not found. */
15906 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15907 complaint (_("virtual function table pointer "
3e43a32a 15908 "not found when defining class '%s'"),
7d93a1e0 15909 type->name () ? type->name () : "");
c906108c
SS
15910 }
15911 else
15912 {
ae6ae975 15913 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15914 }
15915 }
f6235d4c 15916 else if (cu->producer
61012eef 15917 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15918 {
15919 /* The IBM XLC compiler does not provide direct indication
15920 of the containing type, but the vtable pointer is
15921 always named __vfp. */
15922
15923 int i;
15924
1f704f76 15925 for (i = type->num_fields () - 1;
f6235d4c
EZ
15926 i >= TYPE_N_BASECLASSES (type);
15927 --i)
15928 {
15929 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15930 {
ae6ae975
DE
15931 set_type_vptr_fieldno (type, i);
15932 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15933 break;
15934 }
15935 }
15936 }
c906108c 15937 }
98751a41
JK
15938
15939 /* Copy fi.typedef_field_list linked list elements content into the
15940 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15941 if (!fi.typedef_field_list.empty ())
98751a41 15942 {
be2daae6 15943 int count = fi.typedef_field_list.size ();
98751a41 15944
a0d7a4ff 15945 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15946 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15947 = ((struct decl_field *)
be2daae6
TT
15948 TYPE_ALLOC (type,
15949 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15950 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15951
be2daae6
TT
15952 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15953 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15954 }
c767944b 15955
883fd55a
KS
15956 /* Copy fi.nested_types_list linked list elements content into the
15957 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15958 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15959 {
be2daae6 15960 int count = fi.nested_types_list.size ();
883fd55a
KS
15961
15962 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15963 TYPE_NESTED_TYPES_ARRAY (type)
15964 = ((struct decl_field *)
be2daae6
TT
15965 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15966 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15967
be2daae6
TT
15968 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15969 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15970 }
c906108c 15971 }
63d06c5c 15972
bb5ed363 15973 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15974 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15975 cu->rust_unions.push_back (type);
0b92b5bb 15976
90aeadfc
DC
15977 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15978 snapshots) has been known to create a die giving a declaration
15979 for a class that has, as a child, a die giving a definition for a
15980 nested class. So we have to process our children even if the
15981 current die is a declaration. Normally, of course, a declaration
15982 won't have any children at all. */
134d01f1 15983
ca040673
DE
15984 child_die = die->child;
15985
90aeadfc
DC
15986 while (child_die != NULL && child_die->tag)
15987 {
15988 if (child_die->tag == DW_TAG_member
15989 || child_die->tag == DW_TAG_variable
34eaf542
TT
15990 || child_die->tag == DW_TAG_inheritance
15991 || child_die->tag == DW_TAG_template_value_param
15992 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15993 {
90aeadfc 15994 /* Do nothing. */
134d01f1 15995 }
90aeadfc
DC
15996 else
15997 process_die (child_die, cu);
134d01f1 15998
436c571c 15999 child_die = child_die->sibling;
134d01f1
DJ
16000 }
16001
fa4028e9
JB
16002 /* Do not consider external references. According to the DWARF standard,
16003 these DIEs are identified by the fact that they have no byte_size
16004 attribute, and a declaration attribute. */
16005 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
16006 || !die_is_declaration (die, cu)
16007 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
16008 {
16009 struct symbol *sym = new_symbol (die, type, cu);
16010
16011 if (has_template_parameters)
16012 {
a776957c
TT
16013 struct symtab *symtab;
16014 if (sym != nullptr)
16015 symtab = symbol_symtab (sym);
16016 else if (cu->line_header != nullptr)
16017 {
16018 /* Any related symtab will do. */
16019 symtab
7ba99d21 16020 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16021 }
16022 else
16023 {
16024 symtab = nullptr;
16025 complaint (_("could not find suitable "
16026 "symtab for template parameter"
16027 " - DIE at %s [in module %s]"),
16028 sect_offset_str (die->sect_off),
16029 objfile_name (objfile));
16030 }
16031
16032 if (symtab != nullptr)
16033 {
16034 /* Make sure that the symtab is set on the new symbols.
16035 Even though they don't appear in this symtab directly,
16036 other parts of gdb assume that symbols do, and this is
16037 reasonably true. */
16038 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16039 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16040 }
3e1d3d8c
TT
16041 }
16042 }
134d01f1
DJ
16043}
16044
ed6acedd
TT
16045/* Assuming DIE is an enumeration type, and TYPE is its associated
16046 type, update TYPE using some information only available in DIE's
16047 children. In particular, the fields are computed. */
55426c9d
JB
16048
16049static void
16050update_enumeration_type_from_children (struct die_info *die,
16051 struct type *type,
16052 struct dwarf2_cu *cu)
16053{
60f7655a 16054 struct die_info *child_die;
55426c9d
JB
16055 int unsigned_enum = 1;
16056 int flag_enum = 1;
55426c9d 16057
8268c778 16058 auto_obstack obstack;
ed6acedd 16059 std::vector<struct field> fields;
55426c9d 16060
60f7655a
DE
16061 for (child_die = die->child;
16062 child_die != NULL && child_die->tag;
436c571c 16063 child_die = child_die->sibling)
55426c9d
JB
16064 {
16065 struct attribute *attr;
16066 LONGEST value;
16067 const gdb_byte *bytes;
16068 struct dwarf2_locexpr_baton *baton;
16069 const char *name;
60f7655a 16070
55426c9d
JB
16071 if (child_die->tag != DW_TAG_enumerator)
16072 continue;
16073
16074 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16075 if (attr == NULL)
16076 continue;
16077
16078 name = dwarf2_name (child_die, cu);
16079 if (name == NULL)
16080 name = "<anonymous enumerator>";
16081
16082 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16083 &value, &bytes, &baton);
16084 if (value < 0)
16085 {
16086 unsigned_enum = 0;
16087 flag_enum = 0;
16088 }
55426c9d 16089 else
edd45eb0
SM
16090 {
16091 if (count_one_bits_ll (value) >= 2)
16092 flag_enum = 0;
edd45eb0 16093 }
55426c9d 16094
ed6acedd
TT
16095 fields.emplace_back ();
16096 struct field &field = fields.back ();
16097 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16098 SET_FIELD_ENUMVAL (field, value);
16099 }
16100
16101 if (!fields.empty ())
16102 {
5e33d5f4 16103 type->set_num_fields (fields.size ());
3cabb6b0
SM
16104 type->set_fields
16105 ((struct field *)
16106 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16107 memcpy (type->fields (), fields.data (),
ed6acedd 16108 sizeof (struct field) * fields.size ());
55426c9d
JB
16109 }
16110
16111 if (unsigned_enum)
16112 TYPE_UNSIGNED (type) = 1;
16113 if (flag_enum)
16114 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16115}
16116
134d01f1
DJ
16117/* Given a DW_AT_enumeration_type die, set its type. We do not
16118 complete the type's fields yet, or create any symbols. */
c906108c 16119
f792889a 16120static struct type *
134d01f1 16121read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16122{
5e22e966 16123 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16124 struct type *type;
c906108c 16125 struct attribute *attr;
0114d602 16126 const char *name;
134d01f1 16127
348e048f
DE
16128 /* If the definition of this type lives in .debug_types, read that type.
16129 Don't follow DW_AT_specification though, that will take us back up
16130 the chain and we want to go down. */
052c8bb8 16131 attr = die->attr (DW_AT_signature);
435d3d88 16132 if (attr != nullptr)
348e048f 16133 {
ac9ec31b 16134 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16135
ac9ec31b 16136 /* The type's CU may not be the same as CU.
02142a6c 16137 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16138 return set_die_type (die, type, cu);
16139 }
16140
c906108c
SS
16141 type = alloc_type (objfile);
16142
67607e24 16143 type->set_code (TYPE_CODE_ENUM);
94af9270 16144 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16145 if (name != NULL)
d0e39ea2 16146 type->set_name (name);
c906108c 16147
0626fc76
TT
16148 attr = dwarf2_attr (die, DW_AT_type, cu);
16149 if (attr != NULL)
16150 {
16151 struct type *underlying_type = die_type (die, cu);
16152
16153 TYPE_TARGET_TYPE (type) = underlying_type;
16154 }
16155
e142c38c 16156 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16157 if (attr != nullptr)
c906108c
SS
16158 {
16159 TYPE_LENGTH (type) = DW_UNSND (attr);
16160 }
16161 else
16162 {
16163 TYPE_LENGTH (type) = 0;
16164 }
16165
2b4424c3
TT
16166 maybe_set_alignment (cu, die, type);
16167
137033e9
JB
16168 /* The enumeration DIE can be incomplete. In Ada, any type can be
16169 declared as private in the package spec, and then defined only
16170 inside the package body. Such types are known as Taft Amendment
16171 Types. When another package uses such a type, an incomplete DIE
16172 may be generated by the compiler. */
02eb380e 16173 if (die_is_declaration (die, cu))
876cecd0 16174 TYPE_STUB (type) = 1;
02eb380e 16175
0626fc76
TT
16176 /* If this type has an underlying type that is not a stub, then we
16177 may use its attributes. We always use the "unsigned" attribute
16178 in this situation, because ordinarily we guess whether the type
16179 is unsigned -- but the guess can be wrong and the underlying type
16180 can tell us the reality. However, we defer to a local size
16181 attribute if one exists, because this lets the compiler override
16182 the underlying type if needed. */
16183 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16184 {
9e7c9a03
HD
16185 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16186 underlying_type = check_typedef (underlying_type);
16187 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
0626fc76 16188 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16189 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
2b4424c3 16190 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16191 && TYPE_RAW_ALIGN (underlying_type) != 0)
16192 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16193 }
16194
3d567982
TT
16195 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16196
ed6acedd
TT
16197 set_die_type (die, type, cu);
16198
16199 /* Finish the creation of this type by using the enum's children.
16200 Note that, as usual, this must come after set_die_type to avoid
16201 infinite recursion when trying to compute the names of the
16202 enumerators. */
16203 update_enumeration_type_from_children (die, type, cu);
16204
16205 return type;
134d01f1
DJ
16206}
16207
16208/* Given a pointer to a die which begins an enumeration, process all
16209 the dies that define the members of the enumeration, and create the
16210 symbol for the enumeration type.
16211
16212 NOTE: We reverse the order of the element list. */
16213
16214static void
16215process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16216{
f792889a 16217 struct type *this_type;
134d01f1 16218
f792889a
DJ
16219 this_type = get_die_type (die, cu);
16220 if (this_type == NULL)
16221 this_type = read_enumeration_type (die, cu);
9dc481d3 16222
639d11d3 16223 if (die->child != NULL)
c906108c 16224 {
9dc481d3 16225 struct die_info *child_die;
15d034d0 16226 const char *name;
9dc481d3 16227
639d11d3 16228 child_die = die->child;
c906108c
SS
16229 while (child_die && child_die->tag)
16230 {
16231 if (child_die->tag != DW_TAG_enumerator)
16232 {
e7c27a73 16233 process_die (child_die, cu);
c906108c
SS
16234 }
16235 else
16236 {
39cbfefa
DJ
16237 name = dwarf2_name (child_die, cu);
16238 if (name)
ed6acedd 16239 new_symbol (child_die, this_type, cu);
c906108c
SS
16240 }
16241
436c571c 16242 child_die = child_die->sibling;
c906108c 16243 }
c906108c 16244 }
134d01f1 16245
6c83ed52
TT
16246 /* If we are reading an enum from a .debug_types unit, and the enum
16247 is a declaration, and the enum is not the signatured type in the
16248 unit, then we do not want to add a symbol for it. Adding a
16249 symbol would in some cases obscure the true definition of the
16250 enum, giving users an incomplete type when the definition is
16251 actually available. Note that we do not want to do this for all
16252 enums which are just declarations, because C++0x allows forward
16253 enum declarations. */
3019eac3 16254 if (cu->per_cu->is_debug_types
6c83ed52
TT
16255 && die_is_declaration (die, cu))
16256 {
52dc124a 16257 struct signatured_type *sig_type;
6c83ed52 16258
c0f78cd4 16259 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16260 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16261 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16262 return;
16263 }
16264
f792889a 16265 new_symbol (die, this_type, cu);
c906108c
SS
16266}
16267
16268/* Extract all information from a DW_TAG_array_type DIE and put it in
16269 the DIE's type field. For now, this only handles one dimensional
16270 arrays. */
16271
f792889a 16272static struct type *
e7c27a73 16273read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16274{
5e22e966 16275 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16276 struct die_info *child_die;
7e314c57 16277 struct type *type;
c906108c 16278 struct type *element_type, *range_type, *index_type;
c906108c 16279 struct attribute *attr;
15d034d0 16280 const char *name;
a405673c 16281 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16282 unsigned int bit_stride = 0;
c906108c 16283
e7c27a73 16284 element_type = die_type (die, cu);
c906108c 16285
7e314c57
JK
16286 /* The die_type call above may have already set the type for this DIE. */
16287 type = get_die_type (die, cu);
16288 if (type)
16289 return type;
16290
dc53a7ad
JB
16291 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16292 if (attr != NULL)
a405673c
JB
16293 {
16294 int stride_ok;
293e7e51 16295 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
16296
16297 byte_stride_prop
16298 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16299 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16300 prop_type);
a405673c
JB
16301 if (!stride_ok)
16302 {
b98664d3 16303 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16304 " - DIE at %s [in module %s]"),
16305 sect_offset_str (die->sect_off),
5e22e966 16306 objfile_name (cu->per_objfile->objfile));
a405673c
JB
16307 /* Ignore this attribute. We will likely not be able to print
16308 arrays of this type correctly, but there is little we can do
16309 to help if we cannot read the attribute's value. */
16310 byte_stride_prop = NULL;
16311 }
16312 }
dc53a7ad
JB
16313
16314 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16315 if (attr != NULL)
16316 bit_stride = DW_UNSND (attr);
16317
c906108c
SS
16318 /* Irix 6.2 native cc creates array types without children for
16319 arrays with unspecified length. */
639d11d3 16320 if (die->child == NULL)
c906108c 16321 {
46bf5051 16322 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16323 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16324 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16325 byte_stride_prop, bit_stride);
f792889a 16326 return set_die_type (die, type, cu);
c906108c
SS
16327 }
16328
791afaa2 16329 std::vector<struct type *> range_types;
639d11d3 16330 child_die = die->child;
c906108c
SS
16331 while (child_die && child_die->tag)
16332 {
16333 if (child_die->tag == DW_TAG_subrange_type)
16334 {
f792889a 16335 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16336
f792889a 16337 if (child_type != NULL)
a02abb62 16338 {
0963b4bd
MS
16339 /* The range type was succesfully read. Save it for the
16340 array type creation. */
791afaa2 16341 range_types.push_back (child_type);
a02abb62 16342 }
c906108c 16343 }
436c571c 16344 child_die = child_die->sibling;
c906108c
SS
16345 }
16346
16347 /* Dwarf2 dimensions are output from left to right, create the
16348 necessary array types in backwards order. */
7ca2d3a3 16349
c906108c 16350 type = element_type;
7ca2d3a3
DL
16351
16352 if (read_array_order (die, cu) == DW_ORD_col_major)
16353 {
16354 int i = 0;
9a619af0 16355
791afaa2 16356 while (i < range_types.size ())
dc53a7ad 16357 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16358 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16359 }
16360 else
16361 {
791afaa2 16362 size_t ndim = range_types.size ();
7ca2d3a3 16363 while (ndim-- > 0)
dc53a7ad 16364 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16365 byte_stride_prop, bit_stride);
7ca2d3a3 16366 }
c906108c 16367
f5f8a009
EZ
16368 /* Understand Dwarf2 support for vector types (like they occur on
16369 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16370 array type. This is not part of the Dwarf2/3 standard yet, but a
16371 custom vendor extension. The main difference between a regular
16372 array and the vector variant is that vectors are passed by value
16373 to functions. */
e142c38c 16374 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16375 if (attr != nullptr)
ea37ba09 16376 make_vector_type (type);
f5f8a009 16377
dbc98a8b
KW
16378 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16379 implementation may choose to implement triple vectors using this
16380 attribute. */
16381 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16382 if (attr != nullptr)
dbc98a8b
KW
16383 {
16384 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16385 TYPE_LENGTH (type) = DW_UNSND (attr);
16386 else
b98664d3 16387 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16388 "than the total size of elements"));
dbc98a8b
KW
16389 }
16390
39cbfefa
DJ
16391 name = dwarf2_name (die, cu);
16392 if (name)
d0e39ea2 16393 type->set_name (name);
6e70227d 16394
2b4424c3
TT
16395 maybe_set_alignment (cu, die, type);
16396
0963b4bd 16397 /* Install the type in the die. */
7e314c57
JK
16398 set_die_type (die, type, cu);
16399
16400 /* set_die_type should be already done. */
b4ba55a1
JB
16401 set_descriptive_type (type, die, cu);
16402
7e314c57 16403 return type;
c906108c
SS
16404}
16405
7ca2d3a3 16406static enum dwarf_array_dim_ordering
6e70227d 16407read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16408{
16409 struct attribute *attr;
16410
16411 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16412
435d3d88 16413 if (attr != nullptr)
aead7601 16414 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16415
0963b4bd
MS
16416 /* GNU F77 is a special case, as at 08/2004 array type info is the
16417 opposite order to the dwarf2 specification, but data is still
16418 laid out as per normal fortran.
7ca2d3a3 16419
0963b4bd
MS
16420 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16421 version checking. */
7ca2d3a3 16422
905e0470
PM
16423 if (cu->language == language_fortran
16424 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16425 {
16426 return DW_ORD_row_major;
16427 }
16428
6e70227d 16429 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16430 {
16431 case array_column_major:
16432 return DW_ORD_col_major;
16433 case array_row_major:
16434 default:
16435 return DW_ORD_row_major;
16436 };
16437}
16438
72019c9c 16439/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16440 the DIE's type field. */
72019c9c 16441
f792889a 16442static struct type *
72019c9c
GM
16443read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16444{
7e314c57
JK
16445 struct type *domain_type, *set_type;
16446 struct attribute *attr;
f792889a 16447
7e314c57
JK
16448 domain_type = die_type (die, cu);
16449
16450 /* The die_type call above may have already set the type for this DIE. */
16451 set_type = get_die_type (die, cu);
16452 if (set_type)
16453 return set_type;
16454
16455 set_type = create_set_type (NULL, domain_type);
16456
16457 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16458 if (attr != nullptr)
d09039dd 16459 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16460
2b4424c3
TT
16461 maybe_set_alignment (cu, die, set_type);
16462
f792889a 16463 return set_die_type (die, set_type, cu);
72019c9c 16464}
7ca2d3a3 16465
0971de02
TT
16466/* A helper for read_common_block that creates a locexpr baton.
16467 SYM is the symbol which we are marking as computed.
16468 COMMON_DIE is the DIE for the common block.
16469 COMMON_LOC is the location expression attribute for the common
16470 block itself.
16471 MEMBER_LOC is the location expression attribute for the particular
16472 member of the common block that we are processing.
16473 CU is the CU from which the above come. */
16474
16475static void
16476mark_common_block_symbol_computed (struct symbol *sym,
16477 struct die_info *common_die,
16478 struct attribute *common_loc,
16479 struct attribute *member_loc,
16480 struct dwarf2_cu *cu)
16481{
5e22e966 16482 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 16483 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
16484 struct dwarf2_locexpr_baton *baton;
16485 gdb_byte *ptr;
16486 unsigned int cu_off;
08feed99 16487 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
16488 LONGEST offset = 0;
16489
16490 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16491 gdb_assert (common_loc->form_is_block ());
16492 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16493 || member_loc->form_is_constant ());
0971de02 16494
8d749320 16495 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 16496 baton->per_objfile = per_objfile;
0971de02
TT
16497 baton->per_cu = cu->per_cu;
16498 gdb_assert (baton->per_cu);
16499
16500 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16501
cd6c91b4 16502 if (member_loc->form_is_constant ())
0971de02 16503 {
0826b30a 16504 offset = member_loc->constant_value (0);
0971de02
TT
16505 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16506 }
16507 else
16508 baton->size += DW_BLOCK (member_loc)->size;
16509
224c3ddb 16510 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16511 baton->data = ptr;
16512
16513 *ptr++ = DW_OP_call4;
9c541725 16514 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16515 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16516 ptr += 4;
16517
cd6c91b4 16518 if (member_loc->form_is_constant ())
0971de02
TT
16519 {
16520 *ptr++ = DW_OP_addr;
16521 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16522 ptr += cu->header.addr_size;
16523 }
16524 else
16525 {
16526 /* We have to copy the data here, because DW_OP_call4 will only
16527 use a DW_AT_location attribute. */
16528 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16529 ptr += DW_BLOCK (member_loc)->size;
16530 }
16531
16532 *ptr++ = DW_OP_plus;
16533 gdb_assert (ptr - baton->data == baton->size);
16534
0971de02 16535 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16536 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16537}
16538
4357ac6c
TT
16539/* Create appropriate locally-scoped variables for all the
16540 DW_TAG_common_block entries. Also create a struct common_block
16541 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16542 is used to separate the common blocks name namespace from regular
4357ac6c 16543 variable names. */
c906108c
SS
16544
16545static void
e7c27a73 16546read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16547{
0971de02
TT
16548 struct attribute *attr;
16549
16550 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16551 if (attr != nullptr)
0971de02
TT
16552 {
16553 /* Support the .debug_loc offsets. */
4fc6c0d5 16554 if (attr->form_is_block ())
0971de02
TT
16555 {
16556 /* Ok. */
16557 }
cd6c91b4 16558 else if (attr->form_is_section_offset ())
0971de02
TT
16559 {
16560 dwarf2_complex_location_expr_complaint ();
16561 attr = NULL;
16562 }
16563 else
16564 {
16565 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16566 "common block member");
16567 attr = NULL;
16568 }
16569 }
16570
639d11d3 16571 if (die->child != NULL)
c906108c 16572 {
5e22e966 16573 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
16574 struct die_info *child_die;
16575 size_t n_entries = 0, size;
16576 struct common_block *common_block;
16577 struct symbol *sym;
74ac6d43 16578
4357ac6c
TT
16579 for (child_die = die->child;
16580 child_die && child_die->tag;
436c571c 16581 child_die = child_die->sibling)
4357ac6c
TT
16582 ++n_entries;
16583
16584 size = (sizeof (struct common_block)
16585 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16586 common_block
16587 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16588 size);
4357ac6c
TT
16589 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16590 common_block->n_entries = 0;
16591
16592 for (child_die = die->child;
16593 child_die && child_die->tag;
436c571c 16594 child_die = child_die->sibling)
4357ac6c
TT
16595 {
16596 /* Create the symbol in the DW_TAG_common_block block in the current
16597 symbol scope. */
e7c27a73 16598 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16599 if (sym != NULL)
16600 {
16601 struct attribute *member_loc;
16602
16603 common_block->contents[common_block->n_entries++] = sym;
16604
16605 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16606 cu);
16607 if (member_loc)
16608 {
16609 /* GDB has handled this for a long time, but it is
16610 not specified by DWARF. It seems to have been
16611 emitted by gfortran at least as recently as:
16612 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16613 complaint (_("Variable in common block has "
0971de02 16614 "DW_AT_data_member_location "
9d8780f0
SM
16615 "- DIE at %s [in module %s]"),
16616 sect_offset_str (child_die->sect_off),
518817b3 16617 objfile_name (objfile));
0971de02 16618
cd6c91b4 16619 if (member_loc->form_is_section_offset ())
0971de02 16620 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16621 else if (member_loc->form_is_constant ()
4fc6c0d5 16622 || member_loc->form_is_block ())
0971de02 16623 {
435d3d88 16624 if (attr != nullptr)
0971de02
TT
16625 mark_common_block_symbol_computed (sym, die, attr,
16626 member_loc, cu);
16627 }
16628 else
16629 dwarf2_complex_location_expr_complaint ();
16630 }
16631 }
c906108c 16632 }
4357ac6c
TT
16633
16634 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16635 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16636 }
16637}
16638
0114d602 16639/* Create a type for a C++ namespace. */
d9fa45fe 16640
0114d602
DJ
16641static struct type *
16642read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16643{
5e22e966 16644 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16645 const char *previous_prefix, *name;
9219021c 16646 int is_anonymous;
0114d602
DJ
16647 struct type *type;
16648
16649 /* For extensions, reuse the type of the original namespace. */
16650 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16651 {
16652 struct die_info *ext_die;
16653 struct dwarf2_cu *ext_cu = cu;
9a619af0 16654
0114d602
DJ
16655 ext_die = dwarf2_extension (die, &ext_cu);
16656 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16657
16658 /* EXT_CU may not be the same as CU.
02142a6c 16659 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16660 return set_die_type (die, type, cu);
16661 }
9219021c 16662
e142c38c 16663 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16664
16665 /* Now build the name of the current namespace. */
16666
0114d602
DJ
16667 previous_prefix = determine_prefix (die, cu);
16668 if (previous_prefix[0] != '\0')
16669 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16670 previous_prefix, name, 0, cu);
0114d602
DJ
16671
16672 /* Create the type. */
19f392bc 16673 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16674
60531b24 16675 return set_die_type (die, type, cu);
0114d602
DJ
16676}
16677
22cee43f 16678/* Read a namespace scope. */
0114d602
DJ
16679
16680static void
16681read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16682{
5e22e966 16683 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16684 int is_anonymous;
9219021c 16685
5c4e30ca
DC
16686 /* Add a symbol associated to this if we haven't seen the namespace
16687 before. Also, add a using directive if it's an anonymous
16688 namespace. */
9219021c 16689
f2f0e013 16690 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16691 {
16692 struct type *type;
16693
0114d602 16694 type = read_type_die (die, cu);
e7c27a73 16695 new_symbol (die, type, cu);
5c4e30ca 16696
e8e80198 16697 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16698 if (is_anonymous)
0114d602
DJ
16699 {
16700 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16701
eb1e02fd 16702 std::vector<const char *> excludes;
804d2729 16703 add_using_directive (using_directives (cu),
7d93a1e0 16704 previous_prefix, type->name (), NULL,
eb1e02fd 16705 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16706 }
5c4e30ca 16707 }
9219021c 16708
639d11d3 16709 if (die->child != NULL)
d9fa45fe 16710 {
639d11d3 16711 struct die_info *child_die = die->child;
6e70227d 16712
d9fa45fe
DC
16713 while (child_die && child_die->tag)
16714 {
e7c27a73 16715 process_die (child_die, cu);
436c571c 16716 child_die = child_die->sibling;
d9fa45fe
DC
16717 }
16718 }
38d518c9
EZ
16719}
16720
f55ee35c
JK
16721/* Read a Fortran module as type. This DIE can be only a declaration used for
16722 imported module. Still we need that type as local Fortran "use ... only"
16723 declaration imports depend on the created type in determine_prefix. */
16724
16725static struct type *
16726read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16727{
5e22e966 16728 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 16729 const char *module_name;
f55ee35c
JK
16730 struct type *type;
16731
16732 module_name = dwarf2_name (die, cu);
19f392bc 16733 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16734
f55ee35c
JK
16735 return set_die_type (die, type, cu);
16736}
16737
5d7cb8df
JK
16738/* Read a Fortran module. */
16739
16740static void
16741read_module (struct die_info *die, struct dwarf2_cu *cu)
16742{
16743 struct die_info *child_die = die->child;
530e8392
KB
16744 struct type *type;
16745
16746 type = read_type_die (die, cu);
16747 new_symbol (die, type, cu);
5d7cb8df 16748
5d7cb8df
JK
16749 while (child_die && child_die->tag)
16750 {
16751 process_die (child_die, cu);
436c571c 16752 child_die = child_die->sibling;
5d7cb8df
JK
16753 }
16754}
16755
38d518c9
EZ
16756/* Return the name of the namespace represented by DIE. Set
16757 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16758 namespace. */
16759
16760static const char *
e142c38c 16761namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16762{
16763 struct die_info *current_die;
16764 const char *name = NULL;
16765
16766 /* Loop through the extensions until we find a name. */
16767
16768 for (current_die = die;
16769 current_die != NULL;
f2f0e013 16770 current_die = dwarf2_extension (die, &cu))
38d518c9 16771 {
96553a0c
DE
16772 /* We don't use dwarf2_name here so that we can detect the absence
16773 of a name -> anonymous namespace. */
7d45c7c3 16774 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16775
38d518c9
EZ
16776 if (name != NULL)
16777 break;
16778 }
16779
16780 /* Is it an anonymous namespace? */
16781
16782 *is_anonymous = (name == NULL);
16783 if (*is_anonymous)
2b1dbab0 16784 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16785
16786 return name;
d9fa45fe
DC
16787}
16788
c906108c
SS
16789/* Extract all information from a DW_TAG_pointer_type DIE and add to
16790 the user defined type vector. */
16791
f792889a 16792static struct type *
e7c27a73 16793read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16794{
5e22e966 16795 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 16796 struct comp_unit_head *cu_header = &cu->header;
c906108c 16797 struct type *type;
8b2dbe47
KB
16798 struct attribute *attr_byte_size;
16799 struct attribute *attr_address_class;
16800 int byte_size, addr_class;
7e314c57
JK
16801 struct type *target_type;
16802
16803 target_type = die_type (die, cu);
c906108c 16804
7e314c57
JK
16805 /* The die_type call above may have already set the type for this DIE. */
16806 type = get_die_type (die, cu);
16807 if (type)
16808 return type;
16809
16810 type = lookup_pointer_type (target_type);
8b2dbe47 16811
e142c38c 16812 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16813 if (attr_byte_size)
16814 byte_size = DW_UNSND (attr_byte_size);
c906108c 16815 else
8b2dbe47
KB
16816 byte_size = cu_header->addr_size;
16817
e142c38c 16818 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16819 if (attr_address_class)
16820 addr_class = DW_UNSND (attr_address_class);
16821 else
16822 addr_class = DW_ADDR_none;
16823
2b4424c3
TT
16824 ULONGEST alignment = get_alignment (cu, die);
16825
16826 /* If the pointer size, alignment, or address class is different
16827 than the default, create a type variant marked as such and set
16828 the length accordingly. */
16829 if (TYPE_LENGTH (type) != byte_size
16830 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16831 && alignment != TYPE_RAW_ALIGN (type))
16832 || addr_class != DW_ADDR_none)
c906108c 16833 {
5e2b427d 16834 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16835 {
16836 int type_flags;
16837
849957d9 16838 type_flags = gdbarch_address_class_type_flags
5e2b427d 16839 (gdbarch, byte_size, addr_class);
876cecd0
TT
16840 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16841 == 0);
8b2dbe47
KB
16842 type = make_type_with_address_space (type, type_flags);
16843 }
16844 else if (TYPE_LENGTH (type) != byte_size)
16845 {
b98664d3 16846 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16847 }
2b4424c3
TT
16848 else if (TYPE_RAW_ALIGN (type) != alignment)
16849 {
b98664d3 16850 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16851 " - DIE at %s [in module %s]"),
16852 sect_offset_str (die->sect_off),
5e22e966 16853 objfile_name (cu->per_objfile->objfile));
2b4424c3 16854 }
6e70227d 16855 else
9a619af0
MS
16856 {
16857 /* Should we also complain about unhandled address classes? */
16858 }
c906108c 16859 }
8b2dbe47
KB
16860
16861 TYPE_LENGTH (type) = byte_size;
2b4424c3 16862 set_type_align (type, alignment);
f792889a 16863 return set_die_type (die, type, cu);
c906108c
SS
16864}
16865
16866/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16867 the user defined type vector. */
16868
f792889a 16869static struct type *
e7c27a73 16870read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16871{
16872 struct type *type;
16873 struct type *to_type;
16874 struct type *domain;
16875
e7c27a73
DJ
16876 to_type = die_type (die, cu);
16877 domain = die_containing_type (die, cu);
0d5de010 16878
7e314c57
JK
16879 /* The calls above may have already set the type for this DIE. */
16880 type = get_die_type (die, cu);
16881 if (type)
16882 return type;
16883
78134374 16884 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 16885 type = lookup_methodptr_type (to_type);
78134374 16886 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 16887 {
5e22e966 16888 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
16889
16890 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 16891 to_type->fields (), to_type->num_fields (),
7078baeb
TT
16892 TYPE_VARARGS (to_type));
16893 type = lookup_methodptr_type (new_type);
16894 }
0d5de010
DJ
16895 else
16896 type = lookup_memberptr_type (to_type, domain);
c906108c 16897
f792889a 16898 return set_die_type (die, type, cu);
c906108c
SS
16899}
16900
4297a3f0 16901/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16902 the user defined type vector. */
16903
f792889a 16904static struct type *
4297a3f0
AV
16905read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16906 enum type_code refcode)
c906108c 16907{
e7c27a73 16908 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16909 struct type *type, *target_type;
c906108c
SS
16910 struct attribute *attr;
16911
4297a3f0
AV
16912 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16913
7e314c57
JK
16914 target_type = die_type (die, cu);
16915
16916 /* The die_type call above may have already set the type for this DIE. */
16917 type = get_die_type (die, cu);
16918 if (type)
16919 return type;
16920
4297a3f0 16921 type = lookup_reference_type (target_type, refcode);
e142c38c 16922 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16923 if (attr != nullptr)
c906108c
SS
16924 {
16925 TYPE_LENGTH (type) = DW_UNSND (attr);
16926 }
16927 else
16928 {
107d2387 16929 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16930 }
2b4424c3 16931 maybe_set_alignment (cu, die, type);
f792889a 16932 return set_die_type (die, type, cu);
c906108c
SS
16933}
16934
cf363f18
MW
16935/* Add the given cv-qualifiers to the element type of the array. GCC
16936 outputs DWARF type qualifiers that apply to an array, not the
16937 element type. But GDB relies on the array element type to carry
16938 the cv-qualifiers. This mimics section 6.7.3 of the C99
16939 specification. */
16940
16941static struct type *
16942add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16943 struct type *base_type, int cnst, int voltl)
16944{
16945 struct type *el_type, *inner_array;
16946
16947 base_type = copy_type (base_type);
16948 inner_array = base_type;
16949
78134374 16950 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16951 {
16952 TYPE_TARGET_TYPE (inner_array) =
16953 copy_type (TYPE_TARGET_TYPE (inner_array));
16954 inner_array = TYPE_TARGET_TYPE (inner_array);
16955 }
16956
16957 el_type = TYPE_TARGET_TYPE (inner_array);
16958 cnst |= TYPE_CONST (el_type);
16959 voltl |= TYPE_VOLATILE (el_type);
16960 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16961
16962 return set_die_type (die, base_type, cu);
16963}
16964
f792889a 16965static struct type *
e7c27a73 16966read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16967{
f792889a 16968 struct type *base_type, *cv_type;
c906108c 16969
e7c27a73 16970 base_type = die_type (die, cu);
7e314c57
JK
16971
16972 /* The die_type call above may have already set the type for this DIE. */
16973 cv_type = get_die_type (die, cu);
16974 if (cv_type)
16975 return cv_type;
16976
2f608a3a
KW
16977 /* In case the const qualifier is applied to an array type, the element type
16978 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 16979 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 16980 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16981
f792889a
DJ
16982 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16983 return set_die_type (die, cv_type, cu);
c906108c
SS
16984}
16985
f792889a 16986static struct type *
e7c27a73 16987read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16988{
f792889a 16989 struct type *base_type, *cv_type;
c906108c 16990
e7c27a73 16991 base_type = die_type (die, cu);
7e314c57
JK
16992
16993 /* The die_type call above may have already set the type for this DIE. */
16994 cv_type = get_die_type (die, cu);
16995 if (cv_type)
16996 return cv_type;
16997
cf363f18
MW
16998 /* In case the volatile qualifier is applied to an array type, the
16999 element type is so qualified, not the array type (section 6.7.3
17000 of C99). */
78134374 17001 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17002 return add_array_cv_type (die, cu, base_type, 0, 1);
17003
f792889a
DJ
17004 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17005 return set_die_type (die, cv_type, cu);
c906108c
SS
17006}
17007
06d66ee9
TT
17008/* Handle DW_TAG_restrict_type. */
17009
17010static struct type *
17011read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17012{
17013 struct type *base_type, *cv_type;
17014
17015 base_type = die_type (die, cu);
17016
17017 /* The die_type call above may have already set the type for this DIE. */
17018 cv_type = get_die_type (die, cu);
17019 if (cv_type)
17020 return cv_type;
17021
17022 cv_type = make_restrict_type (base_type);
17023 return set_die_type (die, cv_type, cu);
17024}
17025
a2c2acaf
MW
17026/* Handle DW_TAG_atomic_type. */
17027
17028static struct type *
17029read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17030{
17031 struct type *base_type, *cv_type;
17032
17033 base_type = die_type (die, cu);
17034
17035 /* The die_type call above may have already set the type for this DIE. */
17036 cv_type = get_die_type (die, cu);
17037 if (cv_type)
17038 return cv_type;
17039
17040 cv_type = make_atomic_type (base_type);
17041 return set_die_type (die, cv_type, cu);
17042}
17043
c906108c
SS
17044/* Extract all information from a DW_TAG_string_type DIE and add to
17045 the user defined type vector. It isn't really a user defined type,
17046 but it behaves like one, with other DIE's using an AT_user_def_type
17047 attribute to reference it. */
17048
f792889a 17049static struct type *
e7c27a73 17050read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17051{
5e22e966 17052 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 17053 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
17054 struct type *type, *range_type, *index_type, *char_type;
17055 struct attribute *attr;
216a7e6b
AB
17056 struct dynamic_prop prop;
17057 bool length_is_constant = true;
17058 LONGEST length;
17059
17060 /* There are a couple of places where bit sizes might be made use of
17061 when parsing a DW_TAG_string_type, however, no producer that we know
17062 of make use of these. Handling bit sizes that are a multiple of the
17063 byte size is easy enough, but what about other bit sizes? Lets deal
17064 with that problem when we have to. Warn about these attributes being
17065 unsupported, then parse the type and ignore them like we always
17066 have. */
17067 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17068 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17069 {
17070 static bool warning_printed = false;
17071 if (!warning_printed)
17072 {
17073 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17074 "currently supported on DW_TAG_string_type."));
17075 warning_printed = true;
17076 }
17077 }
c906108c 17078
e142c38c 17079 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 17080 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
17081 {
17082 /* The string length describes the location at which the length of
17083 the string can be found. The size of the length field can be
17084 specified with one of the attributes below. */
17085 struct type *prop_type;
17086 struct attribute *len
17087 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17088 if (len == nullptr)
17089 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17090 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17091 {
17092 /* Pass 0 as the default as we know this attribute is constant
17093 and the default value will not be returned. */
0826b30a 17094 LONGEST sz = len->constant_value (0);
293e7e51 17095 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17096 }
17097 else
17098 {
17099 /* If the size is not specified then we assume it is the size of
17100 an address on this target. */
293e7e51 17101 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17102 }
17103
17104 /* Convert the attribute into a dynamic property. */
17105 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17106 length = 1;
17107 else
17108 length_is_constant = false;
17109 }
17110 else if (attr != nullptr)
17111 {
17112 /* This DW_AT_string_length just contains the length with no
17113 indirection. There's no need to create a dynamic property in this
17114 case. Pass 0 for the default value as we know it will not be
17115 returned in this case. */
0826b30a 17116 length = attr->constant_value (0);
216a7e6b
AB
17117 }
17118 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17119 {
216a7e6b 17120 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17121 length = attr->constant_value (1);
c906108c
SS
17122 }
17123 else
17124 {
216a7e6b
AB
17125 /* Use 1 as a fallback length if we have nothing else. */
17126 length = 1;
c906108c 17127 }
6ccb9162 17128
46bf5051 17129 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17130 if (length_is_constant)
17131 range_type = create_static_range_type (NULL, index_type, 1, length);
17132 else
17133 {
17134 struct dynamic_prop low_bound;
17135
17136 low_bound.kind = PROP_CONST;
17137 low_bound.data.const_val = 1;
17138 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17139 }
3b7538c0
UW
17140 char_type = language_string_char_type (cu->language_defn, gdbarch);
17141 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17142
f792889a 17143 return set_die_type (die, type, cu);
c906108c
SS
17144}
17145
4d804846
JB
17146/* Assuming that DIE corresponds to a function, returns nonzero
17147 if the function is prototyped. */
17148
17149static int
17150prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17151{
17152 struct attribute *attr;
17153
17154 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17155 if (attr && (DW_UNSND (attr) != 0))
17156 return 1;
17157
17158 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17159 is only meaningful for C, but the concept also extends to other
4d804846
JB
17160 languages that allow unprototyped functions (Eg: Objective C).
17161 For all other languages, assume that functions are always
17162 prototyped. */
17163 if (cu->language != language_c
17164 && cu->language != language_objc
17165 && cu->language != language_opencl)
17166 return 1;
17167
17168 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17169 prototyped and unprototyped functions; default to prototyped,
17170 since that is more common in modern code (and RealView warns
17171 about unprototyped functions). */
17172 if (producer_is_realview (cu->producer))
17173 return 1;
17174
17175 return 0;
17176}
17177
c906108c
SS
17178/* Handle DIES due to C code like:
17179
17180 struct foo
c5aa993b
JM
17181 {
17182 int (*funcp)(int a, long l);
17183 int b;
17184 };
c906108c 17185
0963b4bd 17186 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17187
f792889a 17188static struct type *
e7c27a73 17189read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17190{
5e22e966 17191 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
17192 struct type *type; /* Type that this function returns. */
17193 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17194 struct attribute *attr;
17195
e7c27a73 17196 type = die_type (die, cu);
7e314c57
JK
17197
17198 /* The die_type call above may have already set the type for this DIE. */
17199 ftype = get_die_type (die, cu);
17200 if (ftype)
17201 return ftype;
17202
0c8b41f1 17203 ftype = lookup_function_type (type);
c906108c 17204
4d804846 17205 if (prototyped_function_p (die, cu))
a6c727b2 17206 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17207
c055b101
CV
17208 /* Store the calling convention in the type if it's available in
17209 the subroutine die. Otherwise set the calling convention to
17210 the default value DW_CC_normal. */
17211 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17212 if (attr != nullptr
17213 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17214 TYPE_CALLING_CONVENTION (ftype)
17215 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17216 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17217 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17218 else
17219 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17220
743649fd
MW
17221 /* Record whether the function returns normally to its caller or not
17222 if the DWARF producer set that information. */
17223 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17224 if (attr && (DW_UNSND (attr) != 0))
17225 TYPE_NO_RETURN (ftype) = 1;
17226
76c10ea2
GM
17227 /* We need to add the subroutine type to the die immediately so
17228 we don't infinitely recurse when dealing with parameters
0963b4bd 17229 declared as the same subroutine type. */
76c10ea2 17230 set_die_type (die, ftype, cu);
6e70227d 17231
639d11d3 17232 if (die->child != NULL)
c906108c 17233 {
bb5ed363 17234 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17235 struct die_info *child_die;
8072405b 17236 int nparams, iparams;
c906108c
SS
17237
17238 /* Count the number of parameters.
17239 FIXME: GDB currently ignores vararg functions, but knows about
17240 vararg member functions. */
8072405b 17241 nparams = 0;
639d11d3 17242 child_die = die->child;
c906108c
SS
17243 while (child_die && child_die->tag)
17244 {
17245 if (child_die->tag == DW_TAG_formal_parameter)
17246 nparams++;
17247 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17248 TYPE_VARARGS (ftype) = 1;
436c571c 17249 child_die = child_die->sibling;
c906108c
SS
17250 }
17251
17252 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17253 ftype->set_num_fields (nparams);
3cabb6b0
SM
17254 ftype->set_fields
17255 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17256
8072405b
JK
17257 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17258 even if we error out during the parameters reading below. */
17259 for (iparams = 0; iparams < nparams; iparams++)
5d14b6e5 17260 ftype->field (iparams).set_type (void_type);
8072405b
JK
17261
17262 iparams = 0;
639d11d3 17263 child_die = die->child;
c906108c
SS
17264 while (child_die && child_die->tag)
17265 {
17266 if (child_die->tag == DW_TAG_formal_parameter)
17267 {
3ce3b1ba
PA
17268 struct type *arg_type;
17269
17270 /* DWARF version 2 has no clean way to discern C++
17271 static and non-static member functions. G++ helps
17272 GDB by marking the first parameter for non-static
17273 member functions (which is the this pointer) as
17274 artificial. We pass this information to
17275 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17276
17277 DWARF version 3 added DW_AT_object_pointer, which GCC
17278 4.5 does not yet generate. */
e142c38c 17279 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17280 if (attr != nullptr)
c906108c
SS
17281 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17282 else
9c37b5ae 17283 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17284 arg_type = die_type (child_die, cu);
17285
17286 /* RealView does not mark THIS as const, which the testsuite
17287 expects. GCC marks THIS as const in method definitions,
17288 but not in the class specifications (GCC PR 43053). */
17289 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17290 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17291 {
17292 int is_this = 0;
17293 struct dwarf2_cu *arg_cu = cu;
17294 const char *name = dwarf2_name (child_die, cu);
17295
17296 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17297 if (attr != nullptr)
3ce3b1ba
PA
17298 {
17299 /* If the compiler emits this, use it. */
17300 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17301 is_this = 1;
17302 }
17303 else if (name && strcmp (name, "this") == 0)
17304 /* Function definitions will have the argument names. */
17305 is_this = 1;
17306 else if (name == NULL && iparams == 0)
17307 /* Declarations may not have the names, so like
17308 elsewhere in GDB, assume an artificial first
17309 argument is "this". */
17310 is_this = 1;
17311
17312 if (is_this)
17313 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17314 arg_type, 0);
17315 }
17316
5d14b6e5 17317 ftype->field (iparams).set_type (arg_type);
c906108c
SS
17318 iparams++;
17319 }
436c571c 17320 child_die = child_die->sibling;
c906108c
SS
17321 }
17322 }
17323
76c10ea2 17324 return ftype;
c906108c
SS
17325}
17326
f792889a 17327static struct type *
e7c27a73 17328read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17329{
5e22e966 17330 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17331 const char *name = NULL;
3c8e0968 17332 struct type *this_type, *target_type;
c906108c 17333
94af9270 17334 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17335 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17336 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17337 set_die_type (die, this_type, cu);
3c8e0968
DE
17338 target_type = die_type (die, cu);
17339 if (target_type != this_type)
17340 TYPE_TARGET_TYPE (this_type) = target_type;
17341 else
17342 {
17343 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17344 spec and cause infinite loops in GDB. */
b98664d3 17345 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17346 "- DIE at %s [in module %s]"),
17347 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17348 TYPE_TARGET_TYPE (this_type) = NULL;
17349 }
e4003a34
TV
17350 if (name == NULL)
17351 {
17352 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17353 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17354 Handle these by just returning the target type, rather than
17355 constructing an anonymous typedef type and trying to handle this
17356 elsewhere. */
17357 set_die_type (die, target_type, cu);
17358 return target_type;
17359 }
f792889a 17360 return this_type;
c906108c
SS
17361}
17362
9b790ce7
UW
17363/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17364 (which may be different from NAME) to the architecture back-end to allow
17365 it to guess the correct format if necessary. */
17366
17367static struct type *
17368dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17369 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 17370{
08feed99 17371 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
17372 const struct floatformat **format;
17373 struct type *type;
17374
17375 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17376 if (format)
103a685e 17377 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17378 else
77b7c781 17379 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17380
17381 return type;
17382}
17383
eb77c9df
AB
17384/* Allocate an integer type of size BITS and name NAME. */
17385
17386static struct type *
17387dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17388 int bits, int unsigned_p, const char *name)
17389{
17390 struct type *type;
17391
17392 /* Versions of Intel's C Compiler generate an integer type called "void"
17393 instead of using DW_TAG_unspecified_type. This has been seen on
17394 at least versions 14, 17, and 18. */
35ee2dc2
AB
17395 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17396 && strcmp (name, "void") == 0)
eb77c9df
AB
17397 type = objfile_type (objfile)->builtin_void;
17398 else
17399 type = init_integer_type (objfile, bits, unsigned_p, name);
17400
17401 return type;
17402}
17403
8bdc1658
AB
17404/* Initialise and return a floating point type of size BITS suitable for
17405 use as a component of a complex number. The NAME_HINT is passed through
17406 when initialising the floating point type and is the name of the complex
17407 type.
17408
17409 As DWARF doesn't currently provide an explicit name for the components
17410 of a complex number, but it can be helpful to have these components
17411 named, we try to select a suitable name based on the size of the
17412 component. */
17413static struct type *
17414dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17415 struct objfile *objfile,
103a685e
TT
17416 int bits, const char *name_hint,
17417 enum bfd_endian byte_order)
8bdc1658 17418{
08feed99 17419 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
17420 struct type *tt = nullptr;
17421
35add35e
AB
17422 /* Try to find a suitable floating point builtin type of size BITS.
17423 We're going to use the name of this type as the name for the complex
17424 target type that we are about to create. */
1db455a7 17425 switch (cu->language)
8bdc1658 17426 {
1db455a7
AB
17427 case language_fortran:
17428 switch (bits)
17429 {
17430 case 32:
17431 tt = builtin_f_type (gdbarch)->builtin_real;
17432 break;
17433 case 64:
17434 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17435 break;
17436 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17437 case 128:
17438 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17439 break;
17440 }
8bdc1658 17441 break;
1db455a7
AB
17442 default:
17443 switch (bits)
17444 {
17445 case 32:
17446 tt = builtin_type (gdbarch)->builtin_float;
17447 break;
17448 case 64:
17449 tt = builtin_type (gdbarch)->builtin_double;
17450 break;
17451 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17452 case 128:
17453 tt = builtin_type (gdbarch)->builtin_long_double;
17454 break;
17455 }
8bdc1658
AB
17456 break;
17457 }
17458
35add35e
AB
17459 /* If the type we found doesn't match the size we were looking for, then
17460 pretend we didn't find a type at all, the complex target type we
17461 create will then be nameless. */
a12e5744 17462 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17463 tt = nullptr;
17464
7d93a1e0 17465 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 17466 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17467}
17468
c906108c
SS
17469/* Find a representation of a given base type and install
17470 it in the TYPE field of the die. */
17471
f792889a 17472static struct type *
e7c27a73 17473read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17474{
5e22e966 17475 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
17476 struct type *type;
17477 struct attribute *attr;
19f392bc 17478 int encoding = 0, bits = 0;
15d034d0 17479 const char *name;
34877895 17480 gdbarch *arch;
c906108c 17481
e142c38c 17482 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17483 if (attr != nullptr)
34877895 17484 encoding = DW_UNSND (attr);
e142c38c 17485 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17486 if (attr != nullptr)
34877895 17487 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17488 name = dwarf2_name (die, cu);
6ccb9162 17489 if (!name)
34877895 17490 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 17491
08feed99 17492 arch = objfile->arch ();
103a685e
TT
17493 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17494
34877895
PJ
17495 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17496 if (attr)
103a685e
TT
17497 {
17498 int endianity = DW_UNSND (attr);
17499
17500 switch (endianity)
17501 {
17502 case DW_END_big:
17503 byte_order = BFD_ENDIAN_BIG;
17504 break;
17505 case DW_END_little:
17506 byte_order = BFD_ENDIAN_LITTLE;
17507 break;
17508 default:
17509 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17510 break;
17511 }
17512 }
6ccb9162
UW
17513
17514 switch (encoding)
c906108c 17515 {
6ccb9162
UW
17516 case DW_ATE_address:
17517 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17518 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17519 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17520 break;
17521 case DW_ATE_boolean:
19f392bc 17522 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17523 break;
17524 case DW_ATE_complex_float:
103a685e
TT
17525 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17526 byte_order);
78134374 17527 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
17528 {
17529 if (name == nullptr)
17530 {
17531 struct obstack *obstack
5e22e966 17532 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 17533 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
17534 nullptr);
17535 }
17536 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17537 }
17538 else
17539 type = init_complex_type (name, type);
6ccb9162
UW
17540 break;
17541 case DW_ATE_decimal_float:
19f392bc 17542 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17543 break;
17544 case DW_ATE_float:
103a685e 17545 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17546 break;
17547 case DW_ATE_signed:
eb77c9df 17548 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17549 break;
17550 case DW_ATE_unsigned:
3b2b8fea
TT
17551 if (cu->language == language_fortran
17552 && name
61012eef 17553 && startswith (name, "character("))
19f392bc
UW
17554 type = init_character_type (objfile, bits, 1, name);
17555 else
eb77c9df 17556 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17557 break;
17558 case DW_ATE_signed_char:
6e70227d 17559 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17560 || cu->language == language_pascal
17561 || cu->language == language_fortran)
19f392bc
UW
17562 type = init_character_type (objfile, bits, 0, name);
17563 else
eb77c9df 17564 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17565 break;
17566 case DW_ATE_unsigned_char:
868a0084 17567 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17568 || cu->language == language_pascal
c44af4eb
TT
17569 || cu->language == language_fortran
17570 || cu->language == language_rust)
19f392bc
UW
17571 type = init_character_type (objfile, bits, 1, name);
17572 else
eb77c9df 17573 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17574 break;
75079b2b 17575 case DW_ATE_UTF:
53e710ac 17576 {
53e710ac
PA
17577 if (bits == 16)
17578 type = builtin_type (arch)->builtin_char16;
17579 else if (bits == 32)
17580 type = builtin_type (arch)->builtin_char32;
17581 else
17582 {
b98664d3 17583 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17584 bits);
eb77c9df 17585 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17586 }
17587 return set_die_type (die, type, cu);
17588 }
75079b2b
TT
17589 break;
17590
6ccb9162 17591 default:
b98664d3 17592 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17593 dwarf_type_encoding_name (encoding));
77b7c781 17594 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17595 break;
c906108c 17596 }
6ccb9162 17597
0114d602 17598 if (name && strcmp (name, "char") == 0)
876cecd0 17599 TYPE_NOSIGN (type) = 1;
0114d602 17600
2b4424c3
TT
17601 maybe_set_alignment (cu, die, type);
17602
103a685e 17603 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17604
f792889a 17605 return set_die_type (die, type, cu);
c906108c
SS
17606}
17607
80180f79
SA
17608/* Parse dwarf attribute if it's a block, reference or constant and put the
17609 resulting value of the attribute into struct bound_prop.
17610 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17611
17612static int
17613attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17614 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17615 struct type *default_type)
80180f79
SA
17616{
17617 struct dwarf2_property_baton *baton;
5e22e966 17618 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
17619 struct objfile *objfile = per_objfile->objfile;
17620 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 17621
9a49df9d
AB
17622 gdb_assert (default_type != NULL);
17623
80180f79
SA
17624 if (attr == NULL || prop == NULL)
17625 return 0;
17626
4fc6c0d5 17627 if (attr->form_is_block ())
80180f79 17628 {
8d749320 17629 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17630 baton->property_type = default_type;
80180f79 17631 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17632 baton->locexpr.per_objfile = per_objfile;
80180f79
SA
17633 baton->locexpr.size = DW_BLOCK (attr)->size;
17634 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17635 switch (attr->name)
17636 {
17637 case DW_AT_string_length:
17638 baton->locexpr.is_reference = true;
17639 break;
17640 default:
17641 baton->locexpr.is_reference = false;
17642 break;
17643 }
80180f79
SA
17644 prop->data.baton = baton;
17645 prop->kind = PROP_LOCEXPR;
17646 gdb_assert (prop->data.baton != NULL);
17647 }
cd6c91b4 17648 else if (attr->form_is_ref ())
80180f79
SA
17649 {
17650 struct dwarf2_cu *target_cu = cu;
17651 struct die_info *target_die;
17652 struct attribute *target_attr;
17653
17654 target_die = follow_die_ref (die, attr, &target_cu);
17655 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17656 if (target_attr == NULL)
17657 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17658 target_cu);
80180f79
SA
17659 if (target_attr == NULL)
17660 return 0;
17661
df25ebbd 17662 switch (target_attr->name)
80180f79 17663 {
df25ebbd 17664 case DW_AT_location:
cd6c91b4 17665 if (target_attr->form_is_section_offset ())
df25ebbd 17666 {
8d749320 17667 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17668 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17669 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17670 prop->data.baton = baton;
17671 prop->kind = PROP_LOCLIST;
17672 gdb_assert (prop->data.baton != NULL);
17673 }
4fc6c0d5 17674 else if (target_attr->form_is_block ())
df25ebbd 17675 {
8d749320 17676 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17677 baton->property_type = die_type (target_die, target_cu);
df25ebbd 17678 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17679 baton->locexpr.per_objfile = per_objfile;
df25ebbd
JB
17680 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17681 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17682 baton->locexpr.is_reference = true;
df25ebbd
JB
17683 prop->data.baton = baton;
17684 prop->kind = PROP_LOCEXPR;
17685 gdb_assert (prop->data.baton != NULL);
17686 }
17687 else
17688 {
17689 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17690 "dynamic property");
17691 return 0;
17692 }
17693 break;
17694 case DW_AT_data_member_location:
17695 {
17696 LONGEST offset;
17697
17698 if (!handle_data_member_location (target_die, target_cu,
17699 &offset))
17700 return 0;
17701
8d749320 17702 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17703 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17704 target_cu);
df25ebbd
JB
17705 baton->offset_info.offset = offset;
17706 baton->offset_info.type = die_type (target_die, target_cu);
17707 prop->data.baton = baton;
17708 prop->kind = PROP_ADDR_OFFSET;
17709 break;
17710 }
80180f79
SA
17711 }
17712 }
cd6c91b4 17713 else if (attr->form_is_constant ())
80180f79 17714 {
0826b30a 17715 prop->data.const_val = attr->constant_value (0);
80180f79
SA
17716 prop->kind = PROP_CONST;
17717 }
17718 else
17719 {
17720 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17721 dwarf2_name (die, cu));
17722 return 0;
17723 }
17724
17725 return 1;
17726}
17727
09ba997f 17728/* See read.h. */
9a49df9d 17729
09ba997f 17730struct type *
293e7e51 17731dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17732{
9a49df9d
AB
17733 struct type *int_type;
17734
17735 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17736#define TRY_TYPE(F) \
17737 int_type = (unsigned_p \
17738 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17739 : objfile_type (objfile)->builtin_ ## F); \
17740 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17741 return int_type
17742
17743 TRY_TYPE (char);
17744 TRY_TYPE (short);
17745 TRY_TYPE (int);
17746 TRY_TYPE (long);
17747 TRY_TYPE (long_long);
17748
17749#undef TRY_TYPE
17750
17751 gdb_assert_not_reached ("unable to find suitable integer type");
17752}
17753
09ba997f 17754/* See read.h. */
11a8b164 17755
09ba997f 17756struct type *
293e7e51 17757dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
11a8b164 17758{
293e7e51
SM
17759 int addr_size = this->per_cu->addr_size ();
17760 return this->per_objfile->int_type (addr_size, unsigned_p);
11a8b164
AB
17761}
17762
b86352cf
AB
17763/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17764 present (which is valid) then compute the default type based on the
17765 compilation units address size. */
17766
17767static struct type *
17768read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17769{
17770 struct type *index_type = die_type (die, cu);
17771
17772 /* Dwarf-2 specifications explicitly allows to create subrange types
17773 without specifying a base type.
17774 In that case, the base type must be set to the type of
17775 the lower bound, upper bound or count, in that order, if any of these
17776 three attributes references an object that has a type.
17777 If no base type is found, the Dwarf-2 specifications say that
17778 a signed integer type of size equal to the size of an address should
17779 be used.
17780 For the following C code: `extern char gdb_int [];'
17781 GCC produces an empty range DIE.
17782 FIXME: muller/2010-05-28: Possible references to object for low bound,
17783 high bound or count are not yet handled by this code. */
78134374 17784 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 17785 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
17786
17787 return index_type;
17788}
17789
a02abb62
JB
17790/* Read the given DW_AT_subrange DIE. */
17791
f792889a 17792static struct type *
a02abb62
JB
17793read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17794{
4c9ad8c2 17795 struct type *base_type, *orig_base_type;
a02abb62
JB
17796 struct type *range_type;
17797 struct attribute *attr;
729efb13 17798 struct dynamic_prop low, high;
4fae6e18 17799 int low_default_is_valid;
c451ebe5 17800 int high_bound_is_count = 0;
15d034d0 17801 const char *name;
d359392f 17802 ULONGEST negative_mask;
e77813c8 17803
b86352cf
AB
17804 orig_base_type = read_subrange_index_type (die, cu);
17805
4c9ad8c2
TT
17806 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17807 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17808 creating the range type, but we use the result of check_typedef
17809 when examining properties of the type. */
17810 base_type = check_typedef (orig_base_type);
a02abb62 17811
7e314c57
JK
17812 /* The die_type call above may have already set the type for this DIE. */
17813 range_type = get_die_type (die, cu);
17814 if (range_type)
17815 return range_type;
17816
729efb13
SA
17817 low.kind = PROP_CONST;
17818 high.kind = PROP_CONST;
17819 high.data.const_val = 0;
17820
4fae6e18
JK
17821 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17822 omitting DW_AT_lower_bound. */
17823 switch (cu->language)
6e70227d 17824 {
4fae6e18
JK
17825 case language_c:
17826 case language_cplus:
729efb13 17827 low.data.const_val = 0;
4fae6e18
JK
17828 low_default_is_valid = 1;
17829 break;
17830 case language_fortran:
729efb13 17831 low.data.const_val = 1;
4fae6e18
JK
17832 low_default_is_valid = 1;
17833 break;
17834 case language_d:
4fae6e18 17835 case language_objc:
c44af4eb 17836 case language_rust:
729efb13 17837 low.data.const_val = 0;
4fae6e18
JK
17838 low_default_is_valid = (cu->header.version >= 4);
17839 break;
17840 case language_ada:
17841 case language_m2:
17842 case language_pascal:
729efb13 17843 low.data.const_val = 1;
4fae6e18
JK
17844 low_default_is_valid = (cu->header.version >= 4);
17845 break;
17846 default:
729efb13 17847 low.data.const_val = 0;
4fae6e18
JK
17848 low_default_is_valid = 0;
17849 break;
a02abb62
JB
17850 }
17851
e142c38c 17852 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17853 if (attr != nullptr)
9a49df9d 17854 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17855 else if (!low_default_is_valid)
b98664d3 17856 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17857 "- DIE at %s [in module %s]"),
17858 sect_offset_str (die->sect_off),
5e22e966 17859 objfile_name (cu->per_objfile->objfile));
a02abb62 17860
506f5c41
TV
17861 struct attribute *attr_ub, *attr_count;
17862 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17863 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17864 {
506f5c41 17865 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17866 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17867 {
c451ebe5
SA
17868 /* If bounds are constant do the final calculation here. */
17869 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17870 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17871 else
17872 high_bound_is_count = 1;
c2ff108b 17873 }
506f5c41
TV
17874 else
17875 {
17876 if (attr_ub != NULL)
17877 complaint (_("Unresolved DW_AT_upper_bound "
17878 "- DIE at %s [in module %s]"),
17879 sect_offset_str (die->sect_off),
5e22e966 17880 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
17881 if (attr_count != NULL)
17882 complaint (_("Unresolved DW_AT_count "
17883 "- DIE at %s [in module %s]"),
17884 sect_offset_str (die->sect_off),
5e22e966 17885 objfile_name (cu->per_objfile->objfile));
506f5c41 17886 }
e77813c8 17887 }
a02abb62 17888
4e962e74
TT
17889 LONGEST bias = 0;
17890 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17891 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 17892 bias = bias_attr->constant_value (0);
4e962e74 17893
dbb9c2b1
JB
17894 /* Normally, the DWARF producers are expected to use a signed
17895 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17896 But this is unfortunately not always the case, as witnessed
17897 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17898 is used instead. To work around that ambiguity, we treat
17899 the bounds as signed, and thus sign-extend their values, when
17900 the base type is signed. */
6e70227d 17901 negative_mask =
d359392f 17902 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17903 if (low.kind == PROP_CONST
17904 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17905 low.data.const_val |= negative_mask;
17906 if (high.kind == PROP_CONST
17907 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17908 high.data.const_val |= negative_mask;
43bbcdc2 17909
5bbd8269
AB
17910 /* Check for bit and byte strides. */
17911 struct dynamic_prop byte_stride_prop;
17912 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17913 if (attr_byte_stride != nullptr)
17914 {
293e7e51 17915 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
17916 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17917 prop_type);
17918 }
17919
17920 struct dynamic_prop bit_stride_prop;
17921 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17922 if (attr_bit_stride != nullptr)
17923 {
17924 /* It only makes sense to have either a bit or byte stride. */
17925 if (attr_byte_stride != nullptr)
17926 {
17927 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17928 "- DIE at %s [in module %s]"),
17929 sect_offset_str (die->sect_off),
5e22e966 17930 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
17931 attr_bit_stride = nullptr;
17932 }
17933 else
17934 {
293e7e51 17935 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
17936 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17937 prop_type);
17938 }
17939 }
17940
17941 if (attr_byte_stride != nullptr
17942 || attr_bit_stride != nullptr)
17943 {
17944 bool byte_stride_p = (attr_byte_stride != nullptr);
17945 struct dynamic_prop *stride
17946 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17947
17948 range_type
17949 = create_range_type_with_stride (NULL, orig_base_type, &low,
17950 &high, bias, stride, byte_stride_p);
17951 }
17952 else
17953 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17954
c451ebe5
SA
17955 if (high_bound_is_count)
17956 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17957
c2ff108b
JK
17958 /* Ada expects an empty array on no boundary attributes. */
17959 if (attr == NULL && cu->language != language_ada)
729efb13 17960 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17961
39cbfefa
DJ
17962 name = dwarf2_name (die, cu);
17963 if (name)
d0e39ea2 17964 range_type->set_name (name);
6e70227d 17965
e142c38c 17966 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17967 if (attr != nullptr)
a02abb62
JB
17968 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17969
2b4424c3
TT
17970 maybe_set_alignment (cu, die, range_type);
17971
7e314c57
JK
17972 set_die_type (die, range_type, cu);
17973
17974 /* set_die_type should be already done. */
b4ba55a1
JB
17975 set_descriptive_type (range_type, die, cu);
17976
7e314c57 17977 return range_type;
a02abb62 17978}
6e70227d 17979
f792889a 17980static struct type *
81a17f79
JB
17981read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17982{
17983 struct type *type;
81a17f79 17984
5e22e966 17985 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 17986 type->set_name (dwarf2_name (die, cu));
81a17f79 17987
74a2f8ff 17988 /* In Ada, an unspecified type is typically used when the description
85102364 17989 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17990 such a type, we treat it as a stub, and try to resolve it later on,
17991 when needed. */
17992 if (cu->language == language_ada)
17993 TYPE_STUB (type) = 1;
17994
f792889a 17995 return set_die_type (die, type, cu);
81a17f79 17996}
a02abb62 17997
639d11d3
DC
17998/* Read a single die and all its descendents. Set the die's sibling
17999 field to NULL; set other fields in the die correctly, and set all
18000 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18001 location of the info_ptr after reading all of those dies. PARENT
18002 is the parent of the die in question. */
18003
18004static struct die_info *
dee91e82 18005read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18006 const gdb_byte *info_ptr,
18007 const gdb_byte **new_info_ptr,
dee91e82 18008 struct die_info *parent)
639d11d3
DC
18009{
18010 struct die_info *die;
d521ce57 18011 const gdb_byte *cur_ptr;
639d11d3 18012
3e225074 18013 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
18014 if (die == NULL)
18015 {
18016 *new_info_ptr = cur_ptr;
18017 return NULL;
18018 }
93311388 18019 store_in_ref_table (die, reader->cu);
639d11d3 18020
3e225074 18021 if (die->has_children)
bf6af496 18022 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18023 else
18024 {
18025 die->child = NULL;
18026 *new_info_ptr = cur_ptr;
18027 }
18028
18029 die->sibling = NULL;
18030 die->parent = parent;
18031 return die;
18032}
18033
18034/* Read a die, all of its descendents, and all of its siblings; set
18035 all of the fields of all of the dies correctly. Arguments are as
18036 in read_die_and_children. */
18037
18038static struct die_info *
bf6af496 18039read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18040 const gdb_byte *info_ptr,
18041 const gdb_byte **new_info_ptr,
bf6af496 18042 struct die_info *parent)
639d11d3
DC
18043{
18044 struct die_info *first_die, *last_sibling;
d521ce57 18045 const gdb_byte *cur_ptr;
639d11d3 18046
c906108c 18047 cur_ptr = info_ptr;
639d11d3
DC
18048 first_die = last_sibling = NULL;
18049
18050 while (1)
c906108c 18051 {
639d11d3 18052 struct die_info *die
dee91e82 18053 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18054
1d325ec1 18055 if (die == NULL)
c906108c 18056 {
639d11d3
DC
18057 *new_info_ptr = cur_ptr;
18058 return first_die;
c906108c 18059 }
1d325ec1
DJ
18060
18061 if (!first_die)
18062 first_die = die;
c906108c 18063 else
1d325ec1
DJ
18064 last_sibling->sibling = die;
18065
18066 last_sibling = die;
c906108c 18067 }
c906108c
SS
18068}
18069
bf6af496
DE
18070/* Read a die, all of its descendents, and all of its siblings; set
18071 all of the fields of all of the dies correctly. Arguments are as
18072 in read_die_and_children.
18073 This the main entry point for reading a DIE and all its children. */
18074
18075static struct die_info *
18076read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18077 const gdb_byte *info_ptr,
18078 const gdb_byte **new_info_ptr,
bf6af496
DE
18079 struct die_info *parent)
18080{
18081 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18082 new_info_ptr, parent);
18083
b4f54984 18084 if (dwarf_die_debug)
bf6af496
DE
18085 {
18086 fprintf_unfiltered (gdb_stdlog,
18087 "Read die from %s@0x%x of %s:\n",
96b79293 18088 reader->die_section->get_name (),
bf6af496
DE
18089 (unsigned) (info_ptr - reader->die_section->buffer),
18090 bfd_get_filename (reader->abfd));
b4f54984 18091 dump_die (die, dwarf_die_debug);
bf6af496
DE
18092 }
18093
18094 return die;
18095}
18096
3019eac3
DE
18097/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18098 attributes.
18099 The caller is responsible for filling in the extra attributes
18100 and updating (*DIEP)->num_attrs.
18101 Set DIEP to point to a newly allocated die with its information,
3e225074 18102 except for its child, sibling, and parent fields. */
93311388 18103
d521ce57 18104static const gdb_byte *
3019eac3 18105read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18106 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 18107 int num_extra_attrs)
93311388 18108{
b64f50a1 18109 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18110 struct abbrev_info *abbrev;
18111 struct die_info *die;
18112 struct dwarf2_cu *cu = reader->cu;
18113 bfd *abfd = reader->abfd;
18114
9c541725 18115 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18116 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18117 info_ptr += bytes_read;
18118 if (!abbrev_number)
18119 {
18120 *diep = NULL;
93311388
DE
18121 return info_ptr;
18122 }
18123
685af9cd 18124 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18125 if (!abbrev)
348e048f
DE
18126 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18127 abbrev_number,
18128 bfd_get_filename (abfd));
18129
3019eac3 18130 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18131 die->sect_off = sect_off;
93311388
DE
18132 die->tag = abbrev->tag;
18133 die->abbrev = abbrev_number;
3e225074 18134 die->has_children = abbrev->has_children;
93311388 18135
3019eac3
DE
18136 /* Make the result usable.
18137 The caller needs to update num_attrs after adding the extra
18138 attributes. */
93311388
DE
18139 die->num_attrs = abbrev->num_attrs;
18140
18a8505e 18141 std::vector<int> indexes_that_need_reprocess;
93311388 18142 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
18143 {
18144 bool need_reprocess;
18145 info_ptr =
18146 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18147 info_ptr, &need_reprocess);
18148 if (need_reprocess)
18149 indexes_that_need_reprocess.push_back (i);
18150 }
18151
052c8bb8 18152 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18a8505e
AT
18153 if (attr != nullptr)
18154 cu->str_offsets_base = DW_UNSND (attr);
93311388 18155
41144253 18156 attr = die->attr (DW_AT_loclists_base);
18157 if (attr != nullptr)
18158 cu->loclist_base = DW_UNSND (attr);
18159
a39fdb41 18160 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
18161 if (maybe_addr_base.has_value ())
18162 cu->addr_base = *maybe_addr_base;
18163 for (int index : indexes_that_need_reprocess)
18164 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 18165 *diep = die;
93311388
DE
18166 return info_ptr;
18167}
18168
3019eac3
DE
18169/* Read a die and all its attributes.
18170 Set DIEP to point to a newly allocated die with its information,
3e225074 18171 except for its child, sibling, and parent fields. */
3019eac3 18172
d521ce57 18173static const gdb_byte *
3019eac3 18174read_full_die (const struct die_reader_specs *reader,
3e225074 18175 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 18176{
d521ce57 18177 const gdb_byte *result;
bf6af496 18178
3e225074 18179 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 18180
b4f54984 18181 if (dwarf_die_debug)
bf6af496
DE
18182 {
18183 fprintf_unfiltered (gdb_stdlog,
18184 "Read die from %s@0x%x of %s:\n",
96b79293 18185 reader->die_section->get_name (),
bf6af496
DE
18186 (unsigned) (info_ptr - reader->die_section->buffer),
18187 bfd_get_filename (reader->abfd));
b4f54984 18188 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18189 }
18190
18191 return result;
3019eac3 18192}
433df2d4 18193\f
c906108c 18194
72bf9492
DJ
18195/* Returns nonzero if TAG represents a type that we might generate a partial
18196 symbol for. */
18197
18198static int
18199is_type_tag_for_partial (int tag)
18200{
18201 switch (tag)
18202 {
18203#if 0
18204 /* Some types that would be reasonable to generate partial symbols for,
18205 that we don't at present. */
18206 case DW_TAG_array_type:
18207 case DW_TAG_file_type:
18208 case DW_TAG_ptr_to_member_type:
18209 case DW_TAG_set_type:
18210 case DW_TAG_string_type:
18211 case DW_TAG_subroutine_type:
18212#endif
18213 case DW_TAG_base_type:
18214 case DW_TAG_class_type:
680b30c7 18215 case DW_TAG_interface_type:
72bf9492
DJ
18216 case DW_TAG_enumeration_type:
18217 case DW_TAG_structure_type:
18218 case DW_TAG_subrange_type:
18219 case DW_TAG_typedef:
18220 case DW_TAG_union_type:
18221 return 1;
18222 default:
18223 return 0;
18224 }
18225}
18226
18227/* Load all DIEs that are interesting for partial symbols into memory. */
18228
18229static struct partial_die_info *
dee91e82 18230load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18231 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18232{
dee91e82 18233 struct dwarf2_cu *cu = reader->cu;
5e22e966 18234 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 18235 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18236 unsigned int bytes_read;
5afb4e99 18237 unsigned int load_all = 0;
72bf9492
DJ
18238 int nesting_level = 1;
18239
18240 parent_die = NULL;
18241 last_die = NULL;
18242
7adf1e79
DE
18243 gdb_assert (cu->per_cu != NULL);
18244 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18245 load_all = 1;
18246
72bf9492
DJ
18247 cu->partial_dies
18248 = htab_create_alloc_ex (cu->header.length / 12,
18249 partial_die_hash,
18250 partial_die_eq,
18251 NULL,
18252 &cu->comp_unit_obstack,
18253 hashtab_obstack_allocate,
18254 dummy_obstack_deallocate);
18255
72bf9492
DJ
18256 while (1)
18257 {
685af9cd 18258 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18259
18260 /* A NULL abbrev means the end of a series of children. */
18261 if (abbrev == NULL)
18262 {
18263 if (--nesting_level == 0)
cd9983dd
YQ
18264 return first_die;
18265
72bf9492
DJ
18266 info_ptr += bytes_read;
18267 last_die = parent_die;
18268 parent_die = parent_die->die_parent;
18269 continue;
18270 }
18271
98bfdba5
PA
18272 /* Check for template arguments. We never save these; if
18273 they're seen, we just mark the parent, and go on our way. */
18274 if (parent_die != NULL
18275 && cu->language == language_cplus
18276 && (abbrev->tag == DW_TAG_template_type_param
18277 || abbrev->tag == DW_TAG_template_value_param))
18278 {
18279 parent_die->has_template_arguments = 1;
18280
18281 if (!load_all)
18282 {
18283 /* We don't need a partial DIE for the template argument. */
dee91e82 18284 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18285 continue;
18286 }
18287 }
18288
0d99eb77 18289 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18290 Skip their other children. */
18291 if (!load_all
18292 && cu->language == language_cplus
18293 && parent_die != NULL
f9b5d5ea
TV
18294 && parent_die->tag == DW_TAG_subprogram
18295 && abbrev->tag != DW_TAG_inlined_subroutine)
98bfdba5 18296 {
dee91e82 18297 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18298 continue;
18299 }
18300
5afb4e99
DJ
18301 /* Check whether this DIE is interesting enough to save. Normally
18302 we would not be interested in members here, but there may be
18303 later variables referencing them via DW_AT_specification (for
18304 static members). */
18305 if (!load_all
18306 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18307 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18308 && abbrev->tag != DW_TAG_enumerator
18309 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18310 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18311 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18312 && abbrev->tag != DW_TAG_variable
5afb4e99 18313 && abbrev->tag != DW_TAG_namespace
f55ee35c 18314 && abbrev->tag != DW_TAG_module
95554aad 18315 && abbrev->tag != DW_TAG_member
74921315
KS
18316 && abbrev->tag != DW_TAG_imported_unit
18317 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18318 {
18319 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18320 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18321 continue;
18322 }
18323
6f06d47b
YQ
18324 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18325 abbrev);
cd9983dd 18326
48fbe735 18327 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18328
18329 /* This two-pass algorithm for processing partial symbols has a
18330 high cost in cache pressure. Thus, handle some simple cases
18331 here which cover the majority of C partial symbols. DIEs
18332 which neither have specification tags in them, nor could have
18333 specification tags elsewhere pointing at them, can simply be
18334 processed and discarded.
18335
18336 This segment is also optional; scan_partial_symbols and
18337 add_partial_symbol will handle these DIEs if we chain
18338 them in normally. When compilers which do not emit large
18339 quantities of duplicate debug information are more common,
18340 this code can probably be removed. */
18341
18342 /* Any complete simple types at the top level (pretty much all
18343 of them, for a language without namespaces), can be processed
18344 directly. */
18345 if (parent_die == NULL
cd9983dd
YQ
18346 && pdi.has_specification == 0
18347 && pdi.is_declaration == 0
18348 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18349 || pdi.tag == DW_TAG_base_type
18350 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18351 {
7d00ffec 18352 if (building_psymtab && pdi.raw_name != NULL)
f0fbb768
TT
18353 add_partial_symbol (&pdi, cu);
18354
cd9983dd 18355 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18356 continue;
18357 }
18358
d8228535
JK
18359 /* The exception for DW_TAG_typedef with has_children above is
18360 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18361 type_name_or_error will error on such types later.
d8228535
JK
18362
18363 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18364 it could not find the child DIEs referenced later, this is checked
18365 above. In correct DWARF DW_TAG_typedef should have no children. */
18366
cd9983dd 18367 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18368 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18369 "- DIE at %s [in module %s]"),
cd9983dd 18370 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18371
72bf9492
DJ
18372 /* If we're at the second level, and we're an enumerator, and
18373 our parent has no specification (meaning possibly lives in a
18374 namespace elsewhere), then we can add the partial symbol now
18375 instead of queueing it. */
cd9983dd 18376 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18377 && parent_die != NULL
18378 && parent_die->die_parent == NULL
18379 && parent_die->tag == DW_TAG_enumeration_type
18380 && parent_die->has_specification == 0)
18381 {
7d00ffec 18382 if (pdi.raw_name == NULL)
b98664d3 18383 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18384 else if (building_psymtab)
f0fbb768 18385 add_partial_symbol (&pdi, cu);
72bf9492 18386
cd9983dd 18387 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18388 continue;
18389 }
18390
cd9983dd 18391 struct partial_die_info *part_die
6f06d47b 18392 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18393
72bf9492
DJ
18394 /* We'll save this DIE so link it in. */
18395 part_die->die_parent = parent_die;
18396 part_die->die_sibling = NULL;
18397 part_die->die_child = NULL;
18398
18399 if (last_die && last_die == parent_die)
18400 last_die->die_child = part_die;
18401 else if (last_die)
18402 last_die->die_sibling = part_die;
18403
18404 last_die = part_die;
18405
18406 if (first_die == NULL)
18407 first_die = part_die;
18408
18409 /* Maybe add the DIE to the hash table. Not all DIEs that we
18410 find interesting need to be in the hash table, because we
18411 also have the parent/sibling/child chains; only those that we
18412 might refer to by offset later during partial symbol reading.
18413
18414 For now this means things that might have be the target of a
18415 DW_AT_specification, DW_AT_abstract_origin, or
18416 DW_AT_extension. DW_AT_extension will refer only to
18417 namespaces; DW_AT_abstract_origin refers to functions (and
18418 many things under the function DIE, but we do not recurse
18419 into function DIEs during partial symbol reading) and
18420 possibly variables as well; DW_AT_specification refers to
18421 declarations. Declarations ought to have the DW_AT_declaration
18422 flag. It happens that GCC forgets to put it in sometimes, but
18423 only for functions, not for types.
18424
18425 Adding more things than necessary to the hash table is harmless
18426 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18427 wasted time in find_partial_die, when we reread the compilation
18428 unit with load_all_dies set. */
72bf9492 18429
5afb4e99 18430 if (load_all
72929c62 18431 || abbrev->tag == DW_TAG_constant
5afb4e99 18432 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18433 || abbrev->tag == DW_TAG_variable
18434 || abbrev->tag == DW_TAG_namespace
18435 || part_die->is_declaration)
18436 {
18437 void **slot;
18438
18439 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18440 to_underlying (part_die->sect_off),
18441 INSERT);
72bf9492
DJ
18442 *slot = part_die;
18443 }
18444
72bf9492 18445 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18446 we have no reason to follow the children of structures; for other
98bfdba5
PA
18447 languages we have to, so that we can get at method physnames
18448 to infer fully qualified class names, for DW_AT_specification,
18449 and for C++ template arguments. For C++, we also look one level
18450 inside functions to find template arguments (if the name of the
18451 function does not already contain the template arguments).
bc30ff58 18452
0a4b0913
AB
18453 For Ada and Fortran, we need to scan the children of subprograms
18454 and lexical blocks as well because these languages allow the
18455 definition of nested entities that could be interesting for the
18456 debugger, such as nested subprograms for instance. */
72bf9492 18457 if (last_die->has_children
5afb4e99
DJ
18458 && (load_all
18459 || last_die->tag == DW_TAG_namespace
f55ee35c 18460 || last_die->tag == DW_TAG_module
72bf9492 18461 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18462 || (cu->language == language_cplus
18463 && last_die->tag == DW_TAG_subprogram
7d00ffec
TT
18464 && (last_die->raw_name == NULL
18465 || strchr (last_die->raw_name, '<') == NULL))
72bf9492
DJ
18466 || (cu->language != language_c
18467 && (last_die->tag == DW_TAG_class_type
680b30c7 18468 || last_die->tag == DW_TAG_interface_type
72bf9492 18469 || last_die->tag == DW_TAG_structure_type
bc30ff58 18470 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18471 || ((cu->language == language_ada
18472 || cu->language == language_fortran)
bc30ff58
JB
18473 && (last_die->tag == DW_TAG_subprogram
18474 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18475 {
18476 nesting_level++;
18477 parent_die = last_die;
18478 continue;
18479 }
18480
18481 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18482 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18483
18484 /* Back to the top, do it again. */
18485 }
18486}
18487
6f06d47b
YQ
18488partial_die_info::partial_die_info (sect_offset sect_off_,
18489 struct abbrev_info *abbrev)
18490 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18491{
18492}
18493
7d00ffec
TT
18494/* See class definition. */
18495
18496const char *
18497partial_die_info::name (dwarf2_cu *cu)
18498{
18499 if (!canonical_name && raw_name != nullptr)
18500 {
18501 struct objfile *objfile = cu->per_objfile->objfile;
18502 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
18503 canonical_name = 1;
18504 }
18505
18506 return raw_name;
18507}
18508
35cc7ed7
YQ
18509/* Read a minimal amount of information into the minimal die structure.
18510 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18511
48fbe735
YQ
18512const gdb_byte *
18513partial_die_info::read (const struct die_reader_specs *reader,
18514 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18515{
dee91e82 18516 struct dwarf2_cu *cu = reader->cu;
976ca316 18517 dwarf2_per_objfile *per_objfile = cu->per_objfile;
fa238c03 18518 unsigned int i;
c5aa993b 18519 int has_low_pc_attr = 0;
c906108c 18520 int has_high_pc_attr = 0;
91da1414 18521 int high_pc_relative = 0;
c906108c 18522
fd0a254f 18523 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18524 {
e7da7f8f 18525 attribute attr;
18a8505e 18526 bool need_reprocess;
e7da7f8f 18527 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18a8505e
AT
18528 info_ptr, &need_reprocess);
18529 /* String and address offsets that need to do the reprocessing have
18530 already been read at this point, so there is no need to wait until
18531 the loop terminates to do the reprocessing. */
18532 if (need_reprocess)
e7da7f8f 18533 read_attribute_reprocess (reader, &attr);
c906108c 18534 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18535 partial symbol table. */
c906108c
SS
18536 switch (attr.name)
18537 {
18538 case DW_AT_name:
48fbe735 18539 switch (tag)
71c25dea
TT
18540 {
18541 case DW_TAG_compile_unit:
95554aad 18542 case DW_TAG_partial_unit:
348e048f 18543 case DW_TAG_type_unit:
71c25dea
TT
18544 /* Compilation units have a DW_AT_name that is a filename, not
18545 a source language identifier. */
18546 case DW_TAG_enumeration_type:
18547 case DW_TAG_enumerator:
18548 /* These tags always have simple identifiers already; no need
18549 to canonicalize them. */
7d00ffec
TT
18550 canonical_name = 1;
18551 raw_name = DW_STRING (&attr);
71c25dea
TT
18552 break;
18553 default:
7d00ffec
TT
18554 canonical_name = 0;
18555 raw_name = DW_STRING (&attr);
71c25dea
TT
18556 break;
18557 }
c906108c 18558 break;
31ef98ae 18559 case DW_AT_linkage_name:
c906108c 18560 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18561 /* Note that both forms of linkage name might appear. We
18562 assume they will be the same, and we only store the last
18563 one we see. */
e61108c9 18564 linkage_name = attr.value_as_string ();
787de330
TT
18565 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18566 See https://github.com/rust-lang/rust/issues/32925. */
18567 if (cu->language == language_rust && linkage_name != NULL
18568 && strchr (linkage_name, '{') != NULL)
18569 linkage_name = NULL;
c906108c
SS
18570 break;
18571 case DW_AT_low_pc:
18572 has_low_pc_attr = 1;
cd6c91b4 18573 lowpc = attr.value_as_address ();
c906108c
SS
18574 break;
18575 case DW_AT_high_pc:
18576 has_high_pc_attr = 1;
cd6c91b4
TT
18577 highpc = attr.value_as_address ();
18578 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 18579 high_pc_relative = 1;
c906108c
SS
18580 break;
18581 case DW_AT_location:
0963b4bd 18582 /* Support the .debug_loc offsets. */
4fc6c0d5 18583 if (attr.form_is_block ())
8e19ed76 18584 {
48fbe735 18585 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18586 }
cd6c91b4 18587 else if (attr.form_is_section_offset ())
8e19ed76 18588 {
4d3c2250 18589 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18590 }
18591 else
18592 {
4d3c2250
KB
18593 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18594 "partial symbol information");
8e19ed76 18595 }
c906108c 18596 break;
c906108c 18597 case DW_AT_external:
48fbe735 18598 is_external = DW_UNSND (&attr);
c906108c
SS
18599 break;
18600 case DW_AT_declaration:
48fbe735 18601 is_declaration = DW_UNSND (&attr);
c906108c
SS
18602 break;
18603 case DW_AT_type:
48fbe735 18604 has_type = 1;
c906108c
SS
18605 break;
18606 case DW_AT_abstract_origin:
18607 case DW_AT_specification:
72bf9492 18608 case DW_AT_extension:
48fbe735 18609 has_specification = 1;
0826b30a 18610 spec_offset = attr.get_ref_die_offset ();
48fbe735 18611 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18612 || cu->per_cu->is_dwz);
c906108c
SS
18613 break;
18614 case DW_AT_sibling:
18615 /* Ignore absolute siblings, they might point outside of
18616 the current compile unit. */
18617 if (attr.form == DW_FORM_ref_addr)
b98664d3 18618 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18619 else
b9502d3f 18620 {
48fbe735 18621 const gdb_byte *buffer = reader->buffer;
0826b30a 18622 sect_offset off = attr.get_ref_die_offset ();
9c541725 18623 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18624
18625 if (sibling_ptr < info_ptr)
b98664d3 18626 complaint (_("DW_AT_sibling points backwards"));
22869d73 18627 else if (sibling_ptr > reader->buffer_end)
a0194fa8 18628 reader->die_section->overflow_complaint ();
b9502d3f 18629 else
48fbe735 18630 sibling = sibling_ptr;
b9502d3f 18631 }
c906108c 18632 break;
fa4028e9 18633 case DW_AT_byte_size:
48fbe735 18634 has_byte_size = 1;
fa4028e9 18635 break;
ff908ebf 18636 case DW_AT_const_value:
48fbe735 18637 has_const_value = 1;
ff908ebf 18638 break;
68511cec
CES
18639 case DW_AT_calling_convention:
18640 /* DWARF doesn't provide a way to identify a program's source-level
18641 entry point. DW_AT_calling_convention attributes are only meant
18642 to describe functions' calling conventions.
18643
18644 However, because it's a necessary piece of information in
0c1b455e
TT
18645 Fortran, and before DWARF 4 DW_CC_program was the only
18646 piece of debugging information whose definition refers to
18647 a 'main program' at all, several compilers marked Fortran
18648 main programs with DW_CC_program --- even when those
18649 functions use the standard calling conventions.
18650
18651 Although DWARF now specifies a way to provide this
18652 information, we support this practice for backward
18653 compatibility. */
68511cec 18654 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18655 && cu->language == language_fortran)
48fbe735 18656 main_subprogram = 1;
68511cec 18657 break;
481860b3
GB
18658 case DW_AT_inline:
18659 if (DW_UNSND (&attr) == DW_INL_inlined
18660 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18661 may_be_inlined = 1;
481860b3 18662 break;
95554aad
TT
18663
18664 case DW_AT_import:
48fbe735 18665 if (tag == DW_TAG_imported_unit)
36586728 18666 {
0826b30a 18667 d.sect_off = attr.get_ref_die_offset ();
48fbe735 18668 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18669 || cu->per_cu->is_dwz);
18670 }
95554aad
TT
18671 break;
18672
0c1b455e 18673 case DW_AT_main_subprogram:
48fbe735 18674 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18675 break;
18676
05caa1d2
TT
18677 case DW_AT_ranges:
18678 {
18679 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18680 but that requires a full DIE, so instead we just
18681 reimplement it. */
18682 int need_ranges_base = tag != DW_TAG_compile_unit;
18683 unsigned int ranges_offset = (DW_UNSND (&attr)
18684 + (need_ranges_base
18685 ? cu->ranges_base
18686 : 0));
18687
18688 /* Value of the DW_AT_ranges attribute is the offset in the
18689 .debug_ranges section. */
18690 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18691 nullptr))
18692 has_pc_info = 1;
18693 }
18694 break;
18695
c906108c
SS
18696 default:
18697 break;
18698 }
18699 }
18700
10d06d82
TT
18701 /* For Ada, if both the name and the linkage name appear, we prefer
18702 the latter. This lets "catch exception" work better, regardless
18703 of the order in which the name and linkage name were emitted.
18704 Really, though, this is just a workaround for the fact that gdb
18705 doesn't store both the name and the linkage name. */
18706 if (cu->language == language_ada && linkage_name != nullptr)
7d00ffec 18707 raw_name = linkage_name;
10d06d82 18708
91da1414 18709 if (high_pc_relative)
48fbe735 18710 highpc += lowpc;
91da1414 18711
9373cf26
JK
18712 if (has_low_pc_attr && has_high_pc_attr)
18713 {
18714 /* When using the GNU linker, .gnu.linkonce. sections are used to
18715 eliminate duplicate copies of functions and vtables and such.
18716 The linker will arbitrarily choose one and discard the others.
18717 The AT_*_pc values for such functions refer to local labels in
18718 these sections. If the section from that file was discarded, the
18719 labels are not in the output, so the relocs get a value of 0.
18720 If this is a discarded function, mark the pc bounds as invalid,
18721 so that GDB will ignore it. */
976ca316 18722 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
9373cf26 18723 {
976ca316 18724 struct objfile *objfile = per_objfile->objfile;
08feed99 18725 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18726
b98664d3 18727 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18728 "for DIE at %s [in module %s]"),
48fbe735
YQ
18729 paddress (gdbarch, lowpc),
18730 sect_offset_str (sect_off),
9d8780f0 18731 objfile_name (objfile));
9373cf26
JK
18732 }
18733 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18734 else if (lowpc >= highpc)
9373cf26 18735 {
976ca316 18736 struct objfile *objfile = per_objfile->objfile;
08feed99 18737 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18738
b98664d3 18739 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18740 "for DIE at %s [in module %s]"),
48fbe735
YQ
18741 paddress (gdbarch, lowpc),
18742 paddress (gdbarch, highpc),
18743 sect_offset_str (sect_off),
9c541725 18744 objfile_name (objfile));
9373cf26
JK
18745 }
18746 else
48fbe735 18747 has_pc_info = 1;
9373cf26 18748 }
85cbf3d3 18749
c906108c
SS
18750 return info_ptr;
18751}
18752
72bf9492
DJ
18753/* Find a cached partial DIE at OFFSET in CU. */
18754
d590ff25
YQ
18755struct partial_die_info *
18756dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18757{
18758 struct partial_die_info *lookup_die = NULL;
6f06d47b 18759 struct partial_die_info part_die (sect_off);
72bf9492 18760
9a3c8263 18761 lookup_die = ((struct partial_die_info *)
d590ff25 18762 htab_find_with_hash (partial_dies, &part_die,
9c541725 18763 to_underlying (sect_off)));
72bf9492 18764
72bf9492
DJ
18765 return lookup_die;
18766}
18767
348e048f
DE
18768/* Find a partial DIE at OFFSET, which may or may not be in CU,
18769 except in the case of .debug_types DIEs which do not reference
18770 outside their CU (they do however referencing other types via
55f1336d 18771 DW_FORM_ref_sig8). */
72bf9492 18772
122cf0f2 18773static const struct cu_partial_die_info
9c541725 18774find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18775{
976ca316
SM
18776 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18777 struct objfile *objfile = per_objfile->objfile;
5afb4e99 18778 struct partial_die_info *pd = NULL;
72bf9492 18779
36586728 18780 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18781 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18782 {
d590ff25 18783 pd = cu->find_partial_die (sect_off);
5afb4e99 18784 if (pd != NULL)
fb816e8b 18785 return { cu, pd };
0d99eb77
DE
18786 /* We missed recording what we needed.
18787 Load all dies and try again. */
5afb4e99 18788 }
0d99eb77
DE
18789 else
18790 {
18791 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18792 if (cu->per_cu->is_debug_types)
0d99eb77 18793 {
9d8780f0
SM
18794 error (_("Dwarf Error: Type Unit at offset %s contains"
18795 " external reference to offset %s [in module %s].\n"),
18796 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18797 bfd_get_filename (objfile->obfd));
18798 }
7188ed02
SM
18799 dwarf2_per_cu_data *per_cu
18800 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 18801 per_objfile);
72bf9492 18802
976ca316 18803 cu = per_objfile->get_cu (per_cu);
7188ed02 18804 if (cu == NULL || cu->partial_dies == NULL)
976ca316 18805 load_partial_comp_unit (per_cu, per_objfile, nullptr);
ae038cb0 18806
976ca316 18807 cu = per_objfile->get_cu (per_cu);
7188ed02
SM
18808
18809 cu->last_used = 0;
18810 pd = cu->find_partial_die (sect_off);
0d99eb77 18811 }
5afb4e99 18812
dee91e82
DE
18813 /* If we didn't find it, and not all dies have been loaded,
18814 load them all and try again. */
18815
7188ed02 18816 if (pd == NULL && cu->per_cu->load_all_dies == 0)
5afb4e99 18817 {
7188ed02 18818 cu->per_cu->load_all_dies = 1;
fd820528
DE
18819
18820 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18821 THIS_CU->cu may already be in use. So we can't just free it and
18822 replace its DIEs with the ones we read in. Instead, we leave those
18823 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18824 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18825 set. */
976ca316 18826 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
5afb4e99 18827
7188ed02 18828 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
18829 }
18830
18831 if (pd == NULL)
18832 internal_error (__FILE__, __LINE__,
9d8780f0 18833 _("could not find partial DIE %s "
3e43a32a 18834 "in cache [from module %s]\n"),
9d8780f0 18835 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
7188ed02 18836 return { cu, pd };
72bf9492
DJ
18837}
18838
abc72ce4
DE
18839/* See if we can figure out if the class lives in a namespace. We do
18840 this by looking for a member function; its demangled name will
18841 contain namespace info, if there is any. */
18842
18843static void
18844guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18845 struct dwarf2_cu *cu)
18846{
18847 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18848 what template types look like, because the demangler
18849 frequently doesn't give the same name as the debug info. We
18850 could fix this by only using the demangled name to get the
18851 prefix (but see comment in read_structure_type). */
18852
18853 struct partial_die_info *real_pdi;
18854 struct partial_die_info *child_pdi;
18855
18856 /* If this DIE (this DIE's specification, if any) has a parent, then
18857 we should not do this. We'll prepend the parent's fully qualified
18858 name when we create the partial symbol. */
18859
18860 real_pdi = struct_pdi;
18861 while (real_pdi->has_specification)
fb816e8b 18862 {
122cf0f2
AB
18863 auto res = find_partial_die (real_pdi->spec_offset,
18864 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18865 real_pdi = res.pdi;
18866 cu = res.cu;
18867 }
abc72ce4
DE
18868
18869 if (real_pdi->die_parent != NULL)
18870 return;
18871
18872 for (child_pdi = struct_pdi->die_child;
18873 child_pdi != NULL;
18874 child_pdi = child_pdi->die_sibling)
18875 {
18876 if (child_pdi->tag == DW_TAG_subprogram
18877 && child_pdi->linkage_name != NULL)
18878 {
43816ebc
TT
18879 gdb::unique_xmalloc_ptr<char> actual_class_name
18880 (language_class_name_from_physname (cu->language_defn,
18881 child_pdi->linkage_name));
abc72ce4
DE
18882 if (actual_class_name != NULL)
18883 {
5e22e966 18884 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
18885 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
18886 struct_pdi->canonical_name = 1;
abc72ce4
DE
18887 }
18888 break;
18889 }
18890 }
18891}
18892
25c11aca
TV
18893/* Return true if a DIE with TAG may have the DW_AT_const_value
18894 attribute. */
18895
18896static bool
18897can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18898{
18899 switch (tag)
18900 {
18901 case DW_TAG_constant:
18902 case DW_TAG_enumerator:
18903 case DW_TAG_formal_parameter:
18904 case DW_TAG_template_value_param:
18905 case DW_TAG_variable:
18906 return true;
18907 }
18908
18909 return false;
18910}
18911
52356b79
YQ
18912void
18913partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18914{
abc72ce4
DE
18915 /* Once we've fixed up a die, there's no point in doing so again.
18916 This also avoids a memory leak if we were to call
18917 guess_partial_die_structure_name multiple times. */
52356b79 18918 if (fixup_called)
abc72ce4
DE
18919 return;
18920
72bf9492
DJ
18921 /* If we found a reference attribute and the DIE has no name, try
18922 to find a name in the referred to DIE. */
18923
7d00ffec 18924 if (raw_name == NULL && has_specification)
72bf9492
DJ
18925 {
18926 struct partial_die_info *spec_die;
72bf9492 18927
122cf0f2 18928 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18929 spec_die = res.pdi;
18930 cu = res.cu;
72bf9492 18931
52356b79 18932 spec_die->fixup (cu);
72bf9492 18933
7d00ffec 18934 if (spec_die->raw_name)
72bf9492 18935 {
7d00ffec
TT
18936 raw_name = spec_die->raw_name;
18937 canonical_name = spec_die->canonical_name;
72bf9492
DJ
18938
18939 /* Copy DW_AT_external attribute if it is set. */
18940 if (spec_die->is_external)
52356b79 18941 is_external = spec_die->is_external;
72bf9492
DJ
18942 }
18943 }
18944
25c11aca
TV
18945 if (!has_const_value && has_specification
18946 && can_have_DW_AT_const_value_p (tag))
18947 {
18948 struct partial_die_info *spec_die;
18949
18950 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18951 spec_die = res.pdi;
18952 cu = res.cu;
18953
18954 spec_die->fixup (cu);
18955
18956 if (spec_die->has_const_value)
18957 {
18958 /* Copy DW_AT_const_value attribute if it is set. */
18959 has_const_value = spec_die->has_const_value;
18960 }
18961 }
18962
72bf9492 18963 /* Set default names for some unnamed DIEs. */
72bf9492 18964
7d00ffec
TT
18965 if (raw_name == NULL && tag == DW_TAG_namespace)
18966 {
18967 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
18968 canonical_name = 1;
18969 }
72bf9492 18970
abc72ce4
DE
18971 /* If there is no parent die to provide a namespace, and there are
18972 children, see if we can determine the namespace from their linkage
122d1940 18973 name. */
abc72ce4 18974 if (cu->language == language_cplus
5e22e966 18975 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
18976 && die_parent == NULL
18977 && has_children
18978 && (tag == DW_TAG_class_type
18979 || tag == DW_TAG_structure_type
18980 || tag == DW_TAG_union_type))
18981 guess_partial_die_structure_name (this, cu);
abc72ce4 18982
53832f31
TT
18983 /* GCC might emit a nameless struct or union that has a linkage
18984 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
7d00ffec 18985 if (raw_name == NULL
52356b79
YQ
18986 && (tag == DW_TAG_class_type
18987 || tag == DW_TAG_interface_type
18988 || tag == DW_TAG_structure_type
18989 || tag == DW_TAG_union_type)
18990 && linkage_name != NULL)
53832f31 18991 {
43816ebc
TT
18992 gdb::unique_xmalloc_ptr<char> demangled
18993 (gdb_demangle (linkage_name, DMGL_TYPES));
18994 if (demangled != nullptr)
53832f31 18995 {
96408a79
SA
18996 const char *base;
18997
18998 /* Strip any leading namespaces/classes, keep only the base name.
18999 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
19000 base = strrchr (demangled.get (), ':');
19001 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
19002 base++;
19003 else
43816ebc 19004 base = demangled.get ();
96408a79 19005
5e22e966 19006 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
19007 raw_name = objfile->intern (base);
19008 canonical_name = 1;
53832f31
TT
19009 }
19010 }
19011
52356b79 19012 fixup_called = 1;
72bf9492
DJ
19013}
19014
41144253 19015/* Read the .debug_loclists header contents from the given SECTION in the
19016 HEADER. */
19017static void
19018read_loclist_header (struct loclist_header *header,
19019 struct dwarf2_section_info *section)
19020{
19021 unsigned int bytes_read;
19022 bfd *abfd = section->get_bfd_owner ();
19023 const gdb_byte *info_ptr = section->buffer;
19024 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19025 info_ptr += bytes_read;
19026 header->version = read_2_bytes (abfd, info_ptr);
19027 info_ptr += 2;
19028 header->addr_size = read_1_byte (abfd, info_ptr);
19029 info_ptr += 1;
19030 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19031 info_ptr += 1;
19032 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19033}
19034
19035/* Return the DW_AT_loclists_base value for the CU. */
19036static ULONGEST
19037lookup_loclist_base (struct dwarf2_cu *cu)
19038{
19039 /* For the .dwo unit, the loclist_base points to the first offset following
19040 the header. The header consists of the following entities-
19041 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19042 bit format)
19043 2. version (2 bytes)
19044 3. address size (1 byte)
19045 4. segment selector size (1 byte)
19046 5. offset entry count (4 bytes)
19047 These sizes are derived as per the DWARFv5 standard. */
19048 if (cu->dwo_unit != nullptr)
19049 {
19050 if (cu->header.initial_length_size == 4)
19051 return LOCLIST_HEADER_SIZE32;
19052 return LOCLIST_HEADER_SIZE64;
19053 }
19054 return cu->loclist_base;
19055}
19056
19057/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19058 array of offsets in the .debug_loclists section. */
19059static CORE_ADDR
19060read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19061{
976ca316
SM
19062 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19063 struct objfile *objfile = per_objfile->objfile;
41144253 19064 bfd *abfd = objfile->obfd;
19065 ULONGEST loclist_base = lookup_loclist_base (cu);
19066 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19067
19068 section->read (objfile);
19069 if (section->buffer == NULL)
19070 complaint (_("DW_FORM_loclistx used without .debug_loclists "
19071 "section [in module %s]"), objfile_name (objfile));
19072 struct loclist_header header;
19073 read_loclist_header (&header, section);
19074 if (loclist_index >= header.offset_entry_count)
19075 complaint (_("DW_FORM_loclistx pointing outside of "
19076 ".debug_loclists offset array [in module %s]"),
19077 objfile_name (objfile));
19078 if (loclist_base + loclist_index * cu->header.offset_size
19079 >= section->size)
19080 complaint (_("DW_FORM_loclistx pointing outside of "
19081 ".debug_loclists section [in module %s]"),
19082 objfile_name (objfile));
19083 const gdb_byte *info_ptr
19084 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
19085
19086 if (cu->header.offset_size == 4)
19087 return bfd_get_32 (abfd, info_ptr) + loclist_base;
19088 else
19089 return bfd_get_64 (abfd, info_ptr) + loclist_base;
19090}
19091
18a8505e
AT
19092/* Process the attributes that had to be skipped in the first round. These
19093 attributes are the ones that need str_offsets_base or addr_base attributes.
19094 They could not have been processed in the first round, because at the time
19095 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
19096static void
19097read_attribute_reprocess (const struct die_reader_specs *reader,
19098 struct attribute *attr)
18a8505e
AT
19099{
19100 struct dwarf2_cu *cu = reader->cu;
19101 switch (attr->form)
19102 {
19103 case DW_FORM_addrx:
19104 case DW_FORM_GNU_addr_index:
19105 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19106 break;
41144253 19107 case DW_FORM_loclistx:
19108 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
19109 break;
18a8505e
AT
19110 case DW_FORM_strx:
19111 case DW_FORM_strx1:
19112 case DW_FORM_strx2:
19113 case DW_FORM_strx3:
19114 case DW_FORM_strx4:
19115 case DW_FORM_GNU_str_index:
19116 {
19117 unsigned int str_index = DW_UNSND (attr);
19118 if (reader->dwo_file != NULL)
19119 {
19120 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19121 DW_STRING_IS_CANONICAL (attr) = 0;
19122 }
19123 else
19124 {
19125 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19126 DW_STRING_IS_CANONICAL (attr) = 0;
19127 }
19128 break;
19129 }
19130 default:
19131 gdb_assert_not_reached (_("Unexpected DWARF form."));
19132 }
19133}
19134
a8329558 19135/* Read an attribute value described by an attribute form. */
c906108c 19136
d521ce57 19137static const gdb_byte *
dee91e82
DE
19138read_attribute_value (const struct die_reader_specs *reader,
19139 struct attribute *attr, unsigned form,
18a8505e
AT
19140 LONGEST implicit_const, const gdb_byte *info_ptr,
19141 bool *need_reprocess)
c906108c 19142{
dee91e82 19143 struct dwarf2_cu *cu = reader->cu;
976ca316
SM
19144 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19145 struct objfile *objfile = per_objfile->objfile;
dee91e82 19146 bfd *abfd = reader->abfd;
e7c27a73 19147 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19148 unsigned int bytes_read;
19149 struct dwarf_block *blk;
18a8505e 19150 *need_reprocess = false;
c906108c 19151
aead7601 19152 attr->form = (enum dwarf_form) form;
a8329558 19153 switch (form)
c906108c 19154 {
c906108c 19155 case DW_FORM_ref_addr:
ae411497 19156 if (cu->header.version == 2)
c8a7a66f
TT
19157 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19158 &bytes_read);
ae411497 19159 else
8266302d
TT
19160 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19161 &bytes_read);
ae411497
TT
19162 info_ptr += bytes_read;
19163 break;
36586728 19164 case DW_FORM_GNU_ref_alt:
8266302d 19165 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
19166 info_ptr += bytes_read;
19167 break;
ae411497 19168 case DW_FORM_addr:
08feed99
TT
19169 {
19170 struct gdbarch *gdbarch = objfile->arch ();
19171 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19172 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19173 info_ptr += bytes_read;
19174 }
c906108c
SS
19175 break;
19176 case DW_FORM_block2:
7b5a2f43 19177 blk = dwarf_alloc_block (cu);
c906108c
SS
19178 blk->size = read_2_bytes (abfd, info_ptr);
19179 info_ptr += 2;
19180 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19181 info_ptr += blk->size;
19182 DW_BLOCK (attr) = blk;
19183 break;
19184 case DW_FORM_block4:
7b5a2f43 19185 blk = dwarf_alloc_block (cu);
c906108c
SS
19186 blk->size = read_4_bytes (abfd, info_ptr);
19187 info_ptr += 4;
19188 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19189 info_ptr += blk->size;
19190 DW_BLOCK (attr) = blk;
19191 break;
19192 case DW_FORM_data2:
19193 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19194 info_ptr += 2;
19195 break;
19196 case DW_FORM_data4:
19197 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19198 info_ptr += 4;
19199 break;
19200 case DW_FORM_data8:
19201 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19202 info_ptr += 8;
19203 break;
0224619f
JK
19204 case DW_FORM_data16:
19205 blk = dwarf_alloc_block (cu);
19206 blk->size = 16;
19207 blk->data = read_n_bytes (abfd, info_ptr, 16);
19208 info_ptr += 16;
19209 DW_BLOCK (attr) = blk;
19210 break;
2dc7f7b3 19211 case DW_FORM_sec_offset:
8266302d 19212 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
19213 info_ptr += bytes_read;
19214 break;
41144253 19215 case DW_FORM_loclistx:
19216 {
19217 *need_reprocess = true;
19218 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19219 info_ptr += bytes_read;
19220 }
19221 break;
c906108c 19222 case DW_FORM_string:
9b1c24c8 19223 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19224 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19225 info_ptr += bytes_read;
19226 break;
4bdf3d34 19227 case DW_FORM_strp:
36586728
TT
19228 if (!cu->per_cu->is_dwz)
19229 {
976ca316 19230 DW_STRING (attr) = read_indirect_string (per_objfile,
ed2dc618 19231 abfd, info_ptr, cu_header,
36586728
TT
19232 &bytes_read);
19233 DW_STRING_IS_CANONICAL (attr) = 0;
19234 info_ptr += bytes_read;
19235 break;
19236 }
19237 /* FALLTHROUGH */
43988095
JK
19238 case DW_FORM_line_strp:
19239 if (!cu->per_cu->is_dwz)
19240 {
976ca316
SM
19241 DW_STRING (attr) = per_objfile->read_line_string (info_ptr, cu_header,
19242 &bytes_read);
43988095
JK
19243 DW_STRING_IS_CANONICAL (attr) = 0;
19244 info_ptr += bytes_read;
19245 break;
19246 }
19247 /* FALLTHROUGH */
36586728
TT
19248 case DW_FORM_GNU_strp_alt:
19249 {
976ca316 19250 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8266302d
TT
19251 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19252 &bytes_read);
36586728 19253
0314b390 19254 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
36586728
TT
19255 DW_STRING_IS_CANONICAL (attr) = 0;
19256 info_ptr += bytes_read;
19257 }
4bdf3d34 19258 break;
2dc7f7b3 19259 case DW_FORM_exprloc:
c906108c 19260 case DW_FORM_block:
7b5a2f43 19261 blk = dwarf_alloc_block (cu);
c906108c
SS
19262 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19263 info_ptr += bytes_read;
19264 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19265 info_ptr += blk->size;
19266 DW_BLOCK (attr) = blk;
19267 break;
19268 case DW_FORM_block1:
7b5a2f43 19269 blk = dwarf_alloc_block (cu);
c906108c
SS
19270 blk->size = read_1_byte (abfd, info_ptr);
19271 info_ptr += 1;
19272 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19273 info_ptr += blk->size;
19274 DW_BLOCK (attr) = blk;
19275 break;
19276 case DW_FORM_data1:
19277 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19278 info_ptr += 1;
19279 break;
19280 case DW_FORM_flag:
19281 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19282 info_ptr += 1;
19283 break;
2dc7f7b3
TT
19284 case DW_FORM_flag_present:
19285 DW_UNSND (attr) = 1;
19286 break;
c906108c
SS
19287 case DW_FORM_sdata:
19288 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19289 info_ptr += bytes_read;
19290 break;
19291 case DW_FORM_udata:
18a8505e 19292 case DW_FORM_rnglistx:
c906108c
SS
19293 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19294 info_ptr += bytes_read;
19295 break;
19296 case DW_FORM_ref1:
9c541725 19297 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19298 + read_1_byte (abfd, info_ptr));
c906108c
SS
19299 info_ptr += 1;
19300 break;
19301 case DW_FORM_ref2:
9c541725 19302 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19303 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19304 info_ptr += 2;
19305 break;
19306 case DW_FORM_ref4:
9c541725 19307 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19308 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19309 info_ptr += 4;
19310 break;
613e1657 19311 case DW_FORM_ref8:
9c541725 19312 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19313 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19314 info_ptr += 8;
19315 break;
55f1336d 19316 case DW_FORM_ref_sig8:
ac9ec31b 19317 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19318 info_ptr += 8;
19319 break;
c906108c 19320 case DW_FORM_ref_udata:
9c541725 19321 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19322 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19323 info_ptr += bytes_read;
19324 break;
c906108c 19325 case DW_FORM_indirect:
a8329558
KW
19326 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19327 info_ptr += bytes_read;
43988095
JK
19328 if (form == DW_FORM_implicit_const)
19329 {
19330 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19331 info_ptr += bytes_read;
19332 }
19333 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 19334 info_ptr, need_reprocess);
43988095
JK
19335 break;
19336 case DW_FORM_implicit_const:
19337 DW_SND (attr) = implicit_const;
a8329558 19338 break;
336d760d 19339 case DW_FORM_addrx:
3019eac3 19340 case DW_FORM_GNU_addr_index:
18a8505e
AT
19341 *need_reprocess = true;
19342 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
19343 info_ptr += bytes_read;
19344 break;
cf532bd1 19345 case DW_FORM_strx:
15f18d14
AT
19346 case DW_FORM_strx1:
19347 case DW_FORM_strx2:
19348 case DW_FORM_strx3:
19349 case DW_FORM_strx4:
3019eac3 19350 case DW_FORM_GNU_str_index:
3019eac3 19351 {
15f18d14
AT
19352 ULONGEST str_index;
19353 if (form == DW_FORM_strx1)
19354 {
19355 str_index = read_1_byte (abfd, info_ptr);
19356 info_ptr += 1;
19357 }
19358 else if (form == DW_FORM_strx2)
19359 {
19360 str_index = read_2_bytes (abfd, info_ptr);
19361 info_ptr += 2;
19362 }
19363 else if (form == DW_FORM_strx3)
19364 {
19365 str_index = read_3_bytes (abfd, info_ptr);
19366 info_ptr += 3;
19367 }
19368 else if (form == DW_FORM_strx4)
19369 {
19370 str_index = read_4_bytes (abfd, info_ptr);
19371 info_ptr += 4;
19372 }
19373 else
19374 {
19375 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19376 info_ptr += bytes_read;
19377 }
18a8505e
AT
19378 *need_reprocess = true;
19379 DW_UNSND (attr) = str_index;
19380 }
3019eac3 19381 break;
c906108c 19382 default:
8a3fe4f8 19383 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19384 dwarf_form_name (form),
19385 bfd_get_filename (abfd));
c906108c 19386 }
28e94949 19387
36586728 19388 /* Super hack. */
cd6c91b4 19389 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
19390 attr->form = DW_FORM_GNU_ref_alt;
19391
28e94949
JB
19392 /* We have seen instances where the compiler tried to emit a byte
19393 size attribute of -1 which ended up being encoded as an unsigned
19394 0xffffffff. Although 0xffffffff is technically a valid size value,
19395 an object of this size seems pretty unlikely so we can relatively
19396 safely treat these cases as if the size attribute was invalid and
19397 treat them as zero by default. */
19398 if (attr->name == DW_AT_byte_size
19399 && form == DW_FORM_data4
19400 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19401 {
19402 complaint
b98664d3 19403 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19404 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19405 DW_UNSND (attr) = 0;
19406 }
28e94949 19407
c906108c
SS
19408 return info_ptr;
19409}
19410
a8329558
KW
19411/* Read an attribute described by an abbreviated attribute. */
19412
d521ce57 19413static const gdb_byte *
dee91e82
DE
19414read_attribute (const struct die_reader_specs *reader,
19415 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 19416 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
19417{
19418 attr->name = abbrev->name;
43988095 19419 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
19420 abbrev->implicit_const, info_ptr,
19421 need_reprocess);
a8329558
KW
19422}
19423
43988095
JK
19424/* Return pointer to string at .debug_str offset STR_OFFSET. */
19425
19426static const char *
976ca316 19427read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
4f44ae6c 19428 LONGEST str_offset)
43988095 19429{
976ca316
SM
19430 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
19431 str_offset, "DW_FORM_strp");
c906108c
SS
19432}
19433
43988095
JK
19434/* Return pointer to string at .debug_str offset as read from BUF.
19435 BUF is assumed to be in a compilation unit described by CU_HEADER.
19436 Return *BYTES_READ_PTR count of bytes read from BUF. */
19437
d521ce57 19438static const char *
976ca316 19439read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
ed2dc618 19440 const gdb_byte *buf,
cf2c3c16
TT
19441 const struct comp_unit_head *cu_header,
19442 unsigned int *bytes_read_ptr)
19443{
8266302d 19444 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 19445
976ca316 19446 return read_indirect_string_at_offset (per_objfile, str_offset);
cf2c3c16
TT
19447}
19448
86c0bb4c 19449/* See read.h. */
43988095 19450
86c0bb4c
TT
19451const char *
19452dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
19453 const struct comp_unit_head *cu_header,
19454 unsigned int *bytes_read_ptr)
43988095 19455{
86c0bb4c 19456 bfd *abfd = objfile->obfd;
8266302d 19457 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 19458
5989a64e 19459 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
19460}
19461
3019eac3 19462/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 19463 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
19464 ADDR_SIZE is the size of addresses from the CU header. */
19465
19466static CORE_ADDR
976ca316
SM
19467read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
19468 gdb::optional<ULONGEST> addr_base, int addr_size)
3019eac3 19469{
976ca316 19470 struct objfile *objfile = per_objfile->objfile;
3019eac3
DE
19471 bfd *abfd = objfile->obfd;
19472 const gdb_byte *info_ptr;
18a8505e 19473 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 19474
976ca316
SM
19475 per_objfile->per_bfd->addr.read (objfile);
19476 if (per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 19477 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19478 objfile_name (objfile));
18a8505e 19479 if (addr_base_or_zero + addr_index * addr_size
976ca316 19480 >= per_objfile->per_bfd->addr.size)
3019eac3
DE
19481 error (_("DW_FORM_addr_index pointing outside of "
19482 ".debug_addr section [in module %s]"),
4262abfb 19483 objfile_name (objfile));
976ca316
SM
19484 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
19485 + addr_index * addr_size);
3019eac3
DE
19486 if (addr_size == 4)
19487 return bfd_get_32 (abfd, info_ptr);
19488 else
19489 return bfd_get_64 (abfd, info_ptr);
19490}
19491
19492/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19493
19494static CORE_ADDR
19495read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19496{
5e22e966 19497 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 19498 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19499}
19500
19501/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19502
19503static CORE_ADDR
d521ce57 19504read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19505 unsigned int *bytes_read)
19506{
5e22e966 19507 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
19508 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19509
19510 return read_addr_index (cu, addr_index);
19511}
19512
450a1bfc 19513/* See read.h. */
3019eac3
DE
19514
19515CORE_ADDR
82ca3f51 19516dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
976ca316 19517 dwarf2_per_objfile *per_objfile,
82ca3f51 19518 unsigned int addr_index)
3019eac3 19519{
976ca316 19520 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
18a8505e 19521 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
19522 int addr_size;
19523
3019eac3
DE
19524 /* We need addr_base and addr_size.
19525 If we don't have PER_CU->cu, we have to get it.
19526 Nasty, but the alternative is storing the needed info in PER_CU,
19527 which at this point doesn't seem justified: it's not clear how frequently
19528 it would get used and it would increase the size of every PER_CU.
19529 Entry points like dwarf2_per_cu_addr_size do a similar thing
19530 so we're not in uncharted territory here.
19531 Alas we need to be a bit more complicated as addr_base is contained
19532 in the DIE.
19533
19534 We don't need to read the entire CU(/TU).
19535 We just need the header and top level die.
a1b64ce1 19536
3019eac3 19537 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19538 For now we skip this optimization. */
3019eac3
DE
19539
19540 if (cu != NULL)
19541 {
19542 addr_base = cu->addr_base;
19543 addr_size = cu->header.addr_size;
19544 }
19545 else
19546 {
976ca316 19547 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
19548 addr_base = reader.cu->addr_base;
19549 addr_size = reader.cu->header.addr_size;
3019eac3
DE
19550 }
19551
976ca316 19552 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
3019eac3
DE
19553}
19554
18a8505e
AT
19555/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19556 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19557 DWO file. */
3019eac3 19558
d521ce57 19559static const char *
18a8505e
AT
19560read_str_index (struct dwarf2_cu *cu,
19561 struct dwarf2_section_info *str_section,
19562 struct dwarf2_section_info *str_offsets_section,
19563 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 19564{
976ca316
SM
19565 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19566 struct objfile *objfile = per_objfile->objfile;
c5164cbc 19567 const char *objf_name = objfile_name (objfile);
3019eac3 19568 bfd *abfd = objfile->obfd;
d521ce57 19569 const gdb_byte *info_ptr;
3019eac3 19570 ULONGEST str_offset;
cf532bd1 19571 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 19572
96b79293
TT
19573 str_section->read (objfile);
19574 str_offsets_section->read (objfile);
73869dc2 19575 if (str_section->buffer == NULL)
18a8505e 19576 error (_("%s used without %s section"
9d8780f0 19577 " in CU at offset %s [in module %s]"),
96b79293 19578 form_name, str_section->get_name (),
18a8505e 19579 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19580 if (str_offsets_section->buffer == NULL)
18a8505e 19581 error (_("%s used without %s section"
9d8780f0 19582 " in CU at offset %s [in module %s]"),
96b79293 19583 form_name, str_section->get_name (),
18a8505e 19584 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19585 info_ptr = (str_offsets_section->buffer
18a8505e 19586 + str_offsets_base
3019eac3
DE
19587 + str_index * cu->header.offset_size);
19588 if (cu->header.offset_size == 4)
19589 str_offset = bfd_get_32 (abfd, info_ptr);
19590 else
19591 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19592 if (str_offset >= str_section->size)
57d63ce2 19593 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19594 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19595 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19596 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19597}
19598
18a8505e
AT
19599/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19600
19601static const char *
19602read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19603{
19604 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19605 ? reader->cu->header.addr_size : 0;
19606 return read_str_index (reader->cu,
19607 &reader->dwo_file->sections.str,
19608 &reader->dwo_file->sections.str_offsets,
19609 str_offsets_base, str_index);
19610}
19611
19612/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19613
19614static const char *
19615read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19616{
5e22e966 19617 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
19618 const char *objf_name = objfile_name (objfile);
19619 static const char form_name[] = "DW_FORM_GNU_str_index";
19620 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19621
19622 if (!cu->str_offsets_base.has_value ())
19623 error (_("%s used in Fission stub without %s"
19624 " in CU at offset 0x%lx [in module %s]"),
19625 form_name, str_offsets_attr_name,
19626 (long) cu->header.offset_size, objf_name);
19627
19628 return read_str_index (cu,
5e22e966
SM
19629 &cu->per_objfile->per_bfd->str,
19630 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
19631 *cu->str_offsets_base, str_index);
19632}
19633
3019eac3
DE
19634/* Return the length of an LEB128 number in BUF. */
19635
19636static int
19637leb128_size (const gdb_byte *buf)
19638{
19639 const gdb_byte *begin = buf;
19640 gdb_byte byte;
19641
19642 while (1)
19643 {
19644 byte = *buf++;
19645 if ((byte & 128) == 0)
19646 return buf - begin;
19647 }
19648}
19649
c906108c 19650static void
e142c38c 19651set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19652{
19653 switch (lang)
19654 {
19655 case DW_LANG_C89:
76bee0cc 19656 case DW_LANG_C99:
0cfd832f 19657 case DW_LANG_C11:
c906108c 19658 case DW_LANG_C:
d1be3247 19659 case DW_LANG_UPC:
e142c38c 19660 cu->language = language_c;
c906108c 19661 break;
9c37b5ae 19662 case DW_LANG_Java:
c906108c 19663 case DW_LANG_C_plus_plus:
0cfd832f
MW
19664 case DW_LANG_C_plus_plus_11:
19665 case DW_LANG_C_plus_plus_14:
e142c38c 19666 cu->language = language_cplus;
c906108c 19667 break;
6aecb9c2
JB
19668 case DW_LANG_D:
19669 cu->language = language_d;
19670 break;
c906108c
SS
19671 case DW_LANG_Fortran77:
19672 case DW_LANG_Fortran90:
b21b22e0 19673 case DW_LANG_Fortran95:
f7de9aab
MW
19674 case DW_LANG_Fortran03:
19675 case DW_LANG_Fortran08:
e142c38c 19676 cu->language = language_fortran;
c906108c 19677 break;
a766d390
DE
19678 case DW_LANG_Go:
19679 cu->language = language_go;
19680 break;
c906108c 19681 case DW_LANG_Mips_Assembler:
e142c38c 19682 cu->language = language_asm;
c906108c
SS
19683 break;
19684 case DW_LANG_Ada83:
8aaf0b47 19685 case DW_LANG_Ada95:
bc5f45f8
JB
19686 cu->language = language_ada;
19687 break;
72019c9c
GM
19688 case DW_LANG_Modula2:
19689 cu->language = language_m2;
19690 break;
fe8e67fd
PM
19691 case DW_LANG_Pascal83:
19692 cu->language = language_pascal;
19693 break;
22566fbd
DJ
19694 case DW_LANG_ObjC:
19695 cu->language = language_objc;
19696 break;
c44af4eb
TT
19697 case DW_LANG_Rust:
19698 case DW_LANG_Rust_old:
19699 cu->language = language_rust;
19700 break;
c906108c
SS
19701 case DW_LANG_Cobol74:
19702 case DW_LANG_Cobol85:
c906108c 19703 default:
e142c38c 19704 cu->language = language_minimal;
c906108c
SS
19705 break;
19706 }
e142c38c 19707 cu->language_defn = language_def (cu->language);
c906108c
SS
19708}
19709
19710/* Return the named attribute or NULL if not there. */
19711
19712static struct attribute *
e142c38c 19713dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19714{
a48e046c 19715 for (;;)
c906108c 19716 {
a48e046c
TT
19717 unsigned int i;
19718 struct attribute *spec = NULL;
19719
19720 for (i = 0; i < die->num_attrs; ++i)
19721 {
19722 if (die->attrs[i].name == name)
19723 return &die->attrs[i];
19724 if (die->attrs[i].name == DW_AT_specification
19725 || die->attrs[i].name == DW_AT_abstract_origin)
19726 spec = &die->attrs[i];
19727 }
19728
19729 if (!spec)
19730 break;
c906108c 19731
f2f0e013 19732 die = follow_die_ref (die, spec, &cu);
f2f0e013 19733 }
c5aa993b 19734
c906108c
SS
19735 return NULL;
19736}
19737
7d45c7c3
KB
19738/* Return the string associated with a string-typed attribute, or NULL if it
19739 is either not found or is of an incorrect type. */
19740
19741static const char *
19742dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19743{
19744 struct attribute *attr;
19745 const char *str = NULL;
19746
19747 attr = dwarf2_attr (die, name, cu);
19748
19749 if (attr != NULL)
19750 {
e61108c9
TT
19751 str = attr->value_as_string ();
19752 if (str == nullptr)
b98664d3 19753 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19754 "DIE at %s in module %s"),
19755 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 19756 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
19757 }
19758
19759 return str;
19760}
19761
a084a2a6 19762/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19763 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19764static const char *
19765dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19766{
19767 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19768 if (dwo_name == nullptr)
19769 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19770 return dwo_name;
19771}
19772
05cf31d1
JB
19773/* Return non-zero iff the attribute NAME is defined for the given DIE,
19774 and holds a non-zero value. This function should only be used for
2dc7f7b3 19775 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19776
19777static int
19778dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19779{
19780 struct attribute *attr = dwarf2_attr (die, name, cu);
19781
19782 return (attr && DW_UNSND (attr));
19783}
19784
3ca72b44 19785static int
e142c38c 19786die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19787{
05cf31d1
JB
19788 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19789 which value is non-zero. However, we have to be careful with
19790 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19791 (via dwarf2_flag_true_p) follows this attribute. So we may
19792 end up accidently finding a declaration attribute that belongs
19793 to a different DIE referenced by the specification attribute,
19794 even though the given DIE does not have a declaration attribute. */
19795 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19796 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19797}
19798
63d06c5c 19799/* Return the die giving the specification for DIE, if there is
f2f0e013 19800 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19801 containing the return value on output. If there is no
19802 specification, but there is an abstract origin, that is
19803 returned. */
63d06c5c
DC
19804
19805static struct die_info *
f2f0e013 19806die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19807{
f2f0e013
DJ
19808 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19809 *spec_cu);
63d06c5c 19810
edb3359d
DJ
19811 if (spec_attr == NULL)
19812 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19813
63d06c5c
DC
19814 if (spec_attr == NULL)
19815 return NULL;
19816 else
f2f0e013 19817 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19818}
c906108c 19819
527f3840
JK
19820/* Stub for free_line_header to match void * callback types. */
19821
19822static void
19823free_line_header_voidp (void *arg)
19824{
9a3c8263 19825 struct line_header *lh = (struct line_header *) arg;
527f3840 19826
fff8551c 19827 delete lh;
527f3840
JK
19828}
19829
83769d0b 19830/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19831
19832static struct dwarf2_section_info *
19833get_debug_line_section (struct dwarf2_cu *cu)
19834{
19835 struct dwarf2_section_info *section;
976ca316 19836 dwarf2_per_objfile *per_objfile = cu->per_objfile;
36586728
TT
19837
19838 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19839 DWO file. */
19840 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19841 section = &cu->dwo_unit->dwo_file->sections.line;
19842 else if (cu->per_cu->is_dwz)
19843 {
976ca316 19844 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
36586728
TT
19845
19846 section = &dwz->line;
19847 }
19848 else
976ca316 19849 section = &per_objfile->per_bfd->line;
36586728
TT
19850
19851 return section;
19852}
19853
debd256d 19854/* Read the statement program header starting at OFFSET in
3019eac3 19855 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19856 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19857 Returns NULL if there is a problem reading the header, e.g., if it
19858 has a version we don't understand.
debd256d
JB
19859
19860 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19861 the returned object point into the dwarf line section buffer,
19862 and must not be freed. */
ae2de4f8 19863
fff8551c 19864static line_header_up
9c541725 19865dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19866{
3019eac3 19867 struct dwarf2_section_info *section;
976ca316 19868 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 19869
36586728 19870 section = get_debug_line_section (cu);
976ca316 19871 section->read (per_objfile->objfile);
3019eac3 19872 if (section->buffer == NULL)
debd256d 19873 {
3019eac3 19874 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19875 complaint (_("missing .debug_line.dwo section"));
3019eac3 19876 else
b98664d3 19877 complaint (_("missing .debug_line section"));
debd256d
JB
19878 return 0;
19879 }
19880
0df7ad3a 19881 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
976ca316 19882 per_objfile, section, &cu->header);
debd256d 19883}
c906108c 19884
c6da4cef 19885/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19886 Return the file name of the psymtab for the given file_entry.
c6da4cef 19887 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19888 If space for the result is malloc'd, *NAME_HOLDER will be set.
19889 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19890
d521ce57 19891static const char *
7ba99d21 19892psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19893 const dwarf2_psymtab *pst,
c89b44cd
TT
19894 const char *comp_dir,
19895 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19896{
d521ce57
TT
19897 const char *include_name = fe.name;
19898 const char *include_name_to_compare = include_name;
72b9f47f 19899 const char *pst_filename;
c6da4cef
DE
19900 int file_is_pst;
19901
8c43009f 19902 const char *dir_name = fe.include_dir (lh);
c6da4cef 19903
c89b44cd 19904 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19905 if (!IS_ABSOLUTE_PATH (include_name)
19906 && (dir_name != NULL || comp_dir != NULL))
19907 {
19908 /* Avoid creating a duplicate psymtab for PST.
19909 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19910 Before we do the comparison, however, we need to account
19911 for DIR_NAME and COMP_DIR.
19912 First prepend dir_name (if non-NULL). If we still don't
19913 have an absolute path prepend comp_dir (if non-NULL).
19914 However, the directory we record in the include-file's
19915 psymtab does not contain COMP_DIR (to match the
19916 corresponding symtab(s)).
19917
19918 Example:
19919
19920 bash$ cd /tmp
19921 bash$ gcc -g ./hello.c
19922 include_name = "hello.c"
19923 dir_name = "."
19924 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19925 DW_AT_name = "./hello.c"
19926
19927 */
c6da4cef
DE
19928
19929 if (dir_name != NULL)
19930 {
c89b44cd
TT
19931 name_holder->reset (concat (dir_name, SLASH_STRING,
19932 include_name, (char *) NULL));
19933 include_name = name_holder->get ();
c6da4cef 19934 include_name_to_compare = include_name;
c6da4cef
DE
19935 }
19936 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19937 {
c89b44cd
TT
19938 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19939 include_name, (char *) NULL));
19940 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19941 }
19942 }
19943
19944 pst_filename = pst->filename;
c89b44cd 19945 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19946 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19947 {
c89b44cd
TT
19948 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19949 pst_filename, (char *) NULL));
19950 pst_filename = copied_name.get ();
c6da4cef
DE
19951 }
19952
1e3fad37 19953 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19954
c6da4cef
DE
19955 if (file_is_pst)
19956 return NULL;
19957 return include_name;
19958}
19959
d9b3de22
DE
19960/* State machine to track the state of the line number program. */
19961
6f77053d 19962class lnp_state_machine
d9b3de22 19963{
6f77053d
PA
19964public:
19965 /* Initialize a machine state for the start of a line number
19966 program. */
804d2729
TT
19967 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19968 bool record_lines_p);
6f77053d 19969
8c43009f
PA
19970 file_entry *current_file ()
19971 {
19972 /* lh->file_names is 0-based, but the file name numbers in the
19973 statement program are 1-based. */
6f77053d
PA
19974 return m_line_header->file_name_at (m_file);
19975 }
19976
19977 /* Record the line in the state machine. END_SEQUENCE is true if
19978 we're processing the end of a sequence. */
19979 void record_line (bool end_sequence);
19980
7ab6656f
OJ
19981 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19982 nop-out rest of the lines in this sequence. */
6f77053d
PA
19983 void check_line_address (struct dwarf2_cu *cu,
19984 const gdb_byte *line_ptr,
7ab6656f 19985 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19986
19987 void handle_set_discriminator (unsigned int discriminator)
19988 {
19989 m_discriminator = discriminator;
19990 m_line_has_non_zero_discriminator |= discriminator != 0;
19991 }
19992
19993 /* Handle DW_LNE_set_address. */
19994 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19995 {
19996 m_op_index = 0;
19997 address += baseaddr;
19998 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19999 }
20000
20001 /* Handle DW_LNS_advance_pc. */
20002 void handle_advance_pc (CORE_ADDR adjust);
20003
20004 /* Handle a special opcode. */
20005 void handle_special_opcode (unsigned char op_code);
20006
20007 /* Handle DW_LNS_advance_line. */
20008 void handle_advance_line (int line_delta)
20009 {
20010 advance_line (line_delta);
20011 }
20012
20013 /* Handle DW_LNS_set_file. */
20014 void handle_set_file (file_name_index file);
20015
20016 /* Handle DW_LNS_negate_stmt. */
20017 void handle_negate_stmt ()
20018 {
20019 m_is_stmt = !m_is_stmt;
20020 }
20021
20022 /* Handle DW_LNS_const_add_pc. */
20023 void handle_const_add_pc ();
20024
20025 /* Handle DW_LNS_fixed_advance_pc. */
20026 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20027 {
20028 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20029 m_op_index = 0;
20030 }
20031
20032 /* Handle DW_LNS_copy. */
20033 void handle_copy ()
20034 {
20035 record_line (false);
20036 m_discriminator = 0;
20037 }
20038
20039 /* Handle DW_LNE_end_sequence. */
20040 void handle_end_sequence ()
20041 {
804d2729 20042 m_currently_recording_lines = true;
6f77053d
PA
20043 }
20044
20045private:
20046 /* Advance the line by LINE_DELTA. */
20047 void advance_line (int line_delta)
20048 {
20049 m_line += line_delta;
20050
20051 if (line_delta != 0)
20052 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20053 }
20054
804d2729
TT
20055 struct dwarf2_cu *m_cu;
20056
6f77053d
PA
20057 gdbarch *m_gdbarch;
20058
20059 /* True if we're recording lines.
20060 Otherwise we're building partial symtabs and are just interested in
20061 finding include files mentioned by the line number program. */
20062 bool m_record_lines_p;
20063
8c43009f 20064 /* The line number header. */
6f77053d 20065 line_header *m_line_header;
8c43009f 20066
6f77053d
PA
20067 /* These are part of the standard DWARF line number state machine,
20068 and initialized according to the DWARF spec. */
d9b3de22 20069
6f77053d 20070 unsigned char m_op_index = 0;
7ba99d21
AT
20071 /* The line table index of the current file. */
20072 file_name_index m_file = 1;
6f77053d
PA
20073 unsigned int m_line = 1;
20074
20075 /* These are initialized in the constructor. */
20076
20077 CORE_ADDR m_address;
20078 bool m_is_stmt;
20079 unsigned int m_discriminator;
d9b3de22
DE
20080
20081 /* Additional bits of state we need to track. */
20082
20083 /* The last file that we called dwarf2_start_subfile for.
20084 This is only used for TLLs. */
6f77053d 20085 unsigned int m_last_file = 0;
d9b3de22 20086 /* The last file a line number was recorded for. */
6f77053d 20087 struct subfile *m_last_subfile = NULL;
d9b3de22 20088
1313c56e
AB
20089 /* The address of the last line entry. */
20090 CORE_ADDR m_last_address;
20091
20092 /* Set to true when a previous line at the same address (using
20093 m_last_address) had m_is_stmt true. This is reset to false when a
20094 line entry at a new address (m_address different to m_last_address) is
20095 processed. */
20096 bool m_stmt_at_address = false;
20097
804d2729
TT
20098 /* When true, record the lines we decode. */
20099 bool m_currently_recording_lines = false;
d9b3de22
DE
20100
20101 /* The last line number that was recorded, used to coalesce
20102 consecutive entries for the same line. This can happen, for
20103 example, when discriminators are present. PR 17276. */
6f77053d
PA
20104 unsigned int m_last_line = 0;
20105 bool m_line_has_non_zero_discriminator = false;
8c43009f 20106};
d9b3de22 20107
6f77053d
PA
20108void
20109lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20110{
20111 CORE_ADDR addr_adj = (((m_op_index + adjust)
20112 / m_line_header->maximum_ops_per_instruction)
20113 * m_line_header->minimum_instruction_length);
20114 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20115 m_op_index = ((m_op_index + adjust)
20116 % m_line_header->maximum_ops_per_instruction);
20117}
d9b3de22 20118
6f77053d
PA
20119void
20120lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20121{
6f77053d 20122 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
20123 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20124 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20125 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
20126 / m_line_header->maximum_ops_per_instruction)
20127 * m_line_header->minimum_instruction_length);
20128 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 20129 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 20130 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20131
258bf0ee 20132 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
20133 advance_line (line_delta);
20134 record_line (false);
20135 m_discriminator = 0;
20136}
d9b3de22 20137
6f77053d
PA
20138void
20139lnp_state_machine::handle_set_file (file_name_index file)
20140{
20141 m_file = file;
20142
20143 const file_entry *fe = current_file ();
20144 if (fe == NULL)
20145 dwarf2_debug_line_missing_file_complaint ();
20146 else if (m_record_lines_p)
20147 {
20148 const char *dir = fe->include_dir (m_line_header);
20149
c24bdb02 20150 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20151 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20152 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20153 }
20154}
20155
20156void
20157lnp_state_machine::handle_const_add_pc ()
20158{
20159 CORE_ADDR adjust
20160 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20161
20162 CORE_ADDR addr_adj
20163 = (((m_op_index + adjust)
20164 / m_line_header->maximum_ops_per_instruction)
20165 * m_line_header->minimum_instruction_length);
20166
20167 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20168 m_op_index = ((m_op_index + adjust)
20169 % m_line_header->maximum_ops_per_instruction);
20170}
d9b3de22 20171
a05a36a5
DE
20172/* Return non-zero if we should add LINE to the line number table.
20173 LINE is the line to add, LAST_LINE is the last line that was added,
20174 LAST_SUBFILE is the subfile for LAST_LINE.
20175 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20176 had a non-zero discriminator.
20177
20178 We have to be careful in the presence of discriminators.
20179 E.g., for this line:
20180
20181 for (i = 0; i < 100000; i++);
20182
20183 clang can emit four line number entries for that one line,
20184 each with a different discriminator.
20185 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20186
20187 However, we want gdb to coalesce all four entries into one.
20188 Otherwise the user could stepi into the middle of the line and
20189 gdb would get confused about whether the pc really was in the
20190 middle of the line.
20191
20192 Things are further complicated by the fact that two consecutive
20193 line number entries for the same line is a heuristic used by gcc
20194 to denote the end of the prologue. So we can't just discard duplicate
20195 entries, we have to be selective about it. The heuristic we use is
20196 that we only collapse consecutive entries for the same line if at least
20197 one of those entries has a non-zero discriminator. PR 17276.
20198
20199 Note: Addresses in the line number state machine can never go backwards
20200 within one sequence, thus this coalescing is ok. */
20201
20202static int
804d2729
TT
20203dwarf_record_line_p (struct dwarf2_cu *cu,
20204 unsigned int line, unsigned int last_line,
a05a36a5
DE
20205 int line_has_non_zero_discriminator,
20206 struct subfile *last_subfile)
20207{
c24bdb02 20208 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20209 return 1;
20210 if (line != last_line)
20211 return 1;
20212 /* Same line for the same file that we've seen already.
20213 As a last check, for pr 17276, only record the line if the line
20214 has never had a non-zero discriminator. */
20215 if (!line_has_non_zero_discriminator)
20216 return 1;
20217 return 0;
20218}
20219
804d2729
TT
20220/* Use the CU's builder to record line number LINE beginning at
20221 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20222
20223static void
d9b3de22 20224dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 20225 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 20226 struct dwarf2_cu *cu)
252a6764
DE
20227{
20228 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20229
27e0867f
DE
20230 if (dwarf_line_debug)
20231 {
20232 fprintf_unfiltered (gdb_stdlog,
20233 "Recording line %u, file %s, address %s\n",
20234 line, lbasename (subfile->name),
20235 paddress (gdbarch, address));
20236 }
20237
804d2729 20238 if (cu != nullptr)
8c95582d 20239 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
20240}
20241
20242/* Subroutine of dwarf_decode_lines_1 to simplify it.
20243 Mark the end of a set of line number records.
d9b3de22 20244 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20245 If SUBFILE is NULL the request is ignored. */
20246
20247static void
20248dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20249 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20250{
27e0867f
DE
20251 if (subfile == NULL)
20252 return;
20253
20254 if (dwarf_line_debug)
20255 {
20256 fprintf_unfiltered (gdb_stdlog,
20257 "Finishing current line, file %s, address %s\n",
20258 lbasename (subfile->name),
20259 paddress (gdbarch, address));
20260 }
20261
8c95582d 20262 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
20263}
20264
6f77053d
PA
20265void
20266lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20267{
d9b3de22
DE
20268 if (dwarf_line_debug)
20269 {
20270 fprintf_unfiltered (gdb_stdlog,
20271 "Processing actual line %u: file %u,"
94a72be7 20272 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 20273 m_line, m_file,
6f77053d 20274 paddress (m_gdbarch, m_address),
94a72be7
AB
20275 m_is_stmt, m_discriminator,
20276 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
20277 }
20278
6f77053d 20279 file_entry *fe = current_file ();
8c43009f
PA
20280
20281 if (fe == NULL)
d9b3de22
DE
20282 dwarf2_debug_line_missing_file_complaint ();
20283 /* For now we ignore lines not starting on an instruction boundary.
20284 But not when processing end_sequence for compatibility with the
20285 previous version of the code. */
6f77053d 20286 else if (m_op_index == 0 || end_sequence)
d9b3de22 20287 {
8c43009f 20288 fe->included_p = 1;
8c95582d 20289 if (m_record_lines_p)
d9b3de22 20290 {
1313c56e
AB
20291 /* When we switch files we insert an end maker in the first file,
20292 switch to the second file and add a new line entry. The
20293 problem is that the end marker inserted in the first file will
20294 discard any previous line entries at the same address. If the
20295 line entries in the first file are marked as is-stmt, while
20296 the new line in the second file is non-stmt, then this means
20297 the end marker will discard is-stmt lines so we can have a
20298 non-stmt line. This means that there are less addresses at
20299 which the user can insert a breakpoint.
20300
20301 To improve this we track the last address in m_last_address,
20302 and whether we have seen an is-stmt at this address. Then
20303 when switching files, if we have seen a stmt at the current
20304 address, and we are switching to create a non-stmt line, then
20305 discard the new line. */
20306 bool file_changed
20307 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
20308 bool ignore_this_line
20309 = (file_changed && !end_sequence && m_last_address == m_address
20310 && !m_is_stmt && m_stmt_at_address);
20311
20312 if ((file_changed && !ignore_this_line) || end_sequence)
d9b3de22 20313 {
804d2729
TT
20314 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20315 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20316 }
20317
1313c56e 20318 if (!end_sequence && !ignore_this_line)
d9b3de22 20319 {
8c95582d
AB
20320 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20321
804d2729 20322 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20323 m_line_has_non_zero_discriminator,
20324 m_last_subfile))
d9b3de22 20325 {
c24bdb02 20326 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20327 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20328 builder->get_current_subfile (),
8c95582d 20329 m_line, m_address, is_stmt,
804d2729 20330 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20331 }
c24bdb02 20332 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20333 m_last_line = m_line;
d9b3de22
DE
20334 }
20335 }
20336 }
1313c56e
AB
20337
20338 /* Track whether we have seen any m_is_stmt true at m_address in case we
20339 have multiple line table entries all at m_address. */
20340 if (m_last_address != m_address)
20341 {
20342 m_stmt_at_address = false;
20343 m_last_address = m_address;
20344 }
20345 m_stmt_at_address |= m_is_stmt;
d9b3de22
DE
20346}
20347
804d2729
TT
20348lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20349 line_header *lh, bool record_lines_p)
d9b3de22 20350{
804d2729 20351 m_cu = cu;
6f77053d
PA
20352 m_gdbarch = arch;
20353 m_record_lines_p = record_lines_p;
20354 m_line_header = lh;
d9b3de22 20355
804d2729 20356 m_currently_recording_lines = true;
d9b3de22 20357
d9b3de22
DE
20358 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20359 was a line entry for it so that the backend has a chance to adjust it
20360 and also record it in case it needs it. This is currently used by MIPS
20361 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20362 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20363 m_is_stmt = lh->default_is_stmt;
20364 m_discriminator = 0;
1313c56e
AB
20365
20366 m_last_address = m_address;
20367 m_stmt_at_address = false;
252a6764
DE
20368}
20369
6f77053d
PA
20370void
20371lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20372 const gdb_byte *line_ptr,
7ab6656f 20373 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20374{
7ab6656f
OJ
20375 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20376 the pc range of the CU. However, we restrict the test to only ADDRESS
20377 values of zero to preserve GDB's previous behaviour which is to handle
20378 the specific case of a function being GC'd by the linker. */
924c2928 20379
7ab6656f 20380 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20381 {
20382 /* This line table is for a function which has been
20383 GCd by the linker. Ignore it. PR gdb/12528 */
20384
5e22e966 20385 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
20386 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20387
b98664d3 20388 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20389 line_offset, objfile_name (objfile));
804d2729
TT
20390 m_currently_recording_lines = false;
20391 /* Note: m_currently_recording_lines is left as false until we see
20392 DW_LNE_end_sequence. */
924c2928
DE
20393 }
20394}
20395
f3f5162e 20396/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20397 Process the line number information in LH.
20398 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20399 program in order to set included_p for every referenced header. */
debd256d 20400
c906108c 20401static void
43f3e411
DE
20402dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20403 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20404{
d521ce57
TT
20405 const gdb_byte *line_ptr, *extended_end;
20406 const gdb_byte *line_end;
a8c50c1f 20407 unsigned int bytes_read, extended_len;
699ca60a 20408 unsigned char op_code, extended_op;
e142c38c 20409 CORE_ADDR baseaddr;
5e22e966 20410 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20411 bfd *abfd = objfile->obfd;
08feed99 20412 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
20413 /* True if we're recording line info (as opposed to building partial
20414 symtabs and just interested in finding include files mentioned by
20415 the line number program). */
20416 bool record_lines_p = !decode_for_pst_p;
e142c38c 20417
b3b3bada 20418 baseaddr = objfile->text_section_offset ();
c906108c 20419
debd256d
JB
20420 line_ptr = lh->statement_program_start;
20421 line_end = lh->statement_program_end;
c906108c
SS
20422
20423 /* Read the statement sequences until there's nothing left. */
20424 while (line_ptr < line_end)
20425 {
6f77053d
PA
20426 /* The DWARF line number program state machine. Reset the state
20427 machine at the start of each sequence. */
804d2729 20428 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20429 bool end_sequence = false;
d9b3de22 20430
8c43009f 20431 if (record_lines_p)
c906108c 20432 {
8c43009f
PA
20433 /* Start a subfile for the current file of the state
20434 machine. */
20435 const file_entry *fe = state_machine.current_file ();
20436
20437 if (fe != NULL)
804d2729 20438 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20439 }
20440
a738430d 20441 /* Decode the table. */
d9b3de22 20442 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20443 {
20444 op_code = read_1_byte (abfd, line_ptr);
20445 line_ptr += 1;
9aa1fe7e 20446
debd256d 20447 if (op_code >= lh->opcode_base)
6e70227d 20448 {
8e07a239 20449 /* Special opcode. */
6f77053d 20450 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20451 }
20452 else switch (op_code)
c906108c
SS
20453 {
20454 case DW_LNS_extended_op:
3e43a32a
MS
20455 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20456 &bytes_read);
473b7be6 20457 line_ptr += bytes_read;
a8c50c1f 20458 extended_end = line_ptr + extended_len;
c906108c
SS
20459 extended_op = read_1_byte (abfd, line_ptr);
20460 line_ptr += 1;
20461 switch (extended_op)
20462 {
20463 case DW_LNE_end_sequence:
6f77053d
PA
20464 state_machine.handle_end_sequence ();
20465 end_sequence = true;
c906108c
SS
20466 break;
20467 case DW_LNE_set_address:
d9b3de22
DE
20468 {
20469 CORE_ADDR address
c8a7a66f 20470 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 20471 line_ptr += bytes_read;
6f77053d
PA
20472
20473 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20474 lowpc - baseaddr, address);
6f77053d 20475 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20476 }
c906108c
SS
20477 break;
20478 case DW_LNE_define_file:
debd256d 20479 {
d521ce57 20480 const char *cur_file;
ecfb656c
PA
20481 unsigned int mod_time, length;
20482 dir_index dindex;
6e70227d 20483
3e43a32a
MS
20484 cur_file = read_direct_string (abfd, line_ptr,
20485 &bytes_read);
debd256d 20486 line_ptr += bytes_read;
ecfb656c 20487 dindex = (dir_index)
debd256d
JB
20488 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20489 line_ptr += bytes_read;
20490 mod_time =
20491 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20492 line_ptr += bytes_read;
20493 length =
20494 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20495 line_ptr += bytes_read;
ecfb656c 20496 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20497 }
c906108c 20498 break;
d0c6ba3d 20499 case DW_LNE_set_discriminator:
6f77053d
PA
20500 {
20501 /* The discriminator is not interesting to the
20502 debugger; just ignore it. We still need to
20503 check its value though:
20504 if there are consecutive entries for the same
20505 (non-prologue) line we want to coalesce them.
20506 PR 17276. */
20507 unsigned int discr
20508 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20509 line_ptr += bytes_read;
20510
20511 state_machine.handle_set_discriminator (discr);
20512 }
d0c6ba3d 20513 break;
c906108c 20514 default:
b98664d3 20515 complaint (_("mangled .debug_line section"));
debd256d 20516 return;
c906108c 20517 }
a8c50c1f
DJ
20518 /* Make sure that we parsed the extended op correctly. If e.g.
20519 we expected a different address size than the producer used,
20520 we may have read the wrong number of bytes. */
20521 if (line_ptr != extended_end)
20522 {
b98664d3 20523 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20524 return;
20525 }
c906108c
SS
20526 break;
20527 case DW_LNS_copy:
6f77053d 20528 state_machine.handle_copy ();
c906108c
SS
20529 break;
20530 case DW_LNS_advance_pc:
2dc7f7b3
TT
20531 {
20532 CORE_ADDR adjust
20533 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20534 line_ptr += bytes_read;
6f77053d
PA
20535
20536 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20537 }
c906108c
SS
20538 break;
20539 case DW_LNS_advance_line:
a05a36a5
DE
20540 {
20541 int line_delta
20542 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20543 line_ptr += bytes_read;
6f77053d
PA
20544
20545 state_machine.handle_advance_line (line_delta);
a05a36a5 20546 }
c906108c
SS
20547 break;
20548 case DW_LNS_set_file:
d9b3de22 20549 {
6f77053d 20550 file_name_index file
ecfb656c
PA
20551 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20552 &bytes_read);
d9b3de22 20553 line_ptr += bytes_read;
8c43009f 20554
6f77053d 20555 state_machine.handle_set_file (file);
d9b3de22 20556 }
c906108c
SS
20557 break;
20558 case DW_LNS_set_column:
0ad93d4f 20559 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20560 line_ptr += bytes_read;
20561 break;
20562 case DW_LNS_negate_stmt:
6f77053d 20563 state_machine.handle_negate_stmt ();
c906108c
SS
20564 break;
20565 case DW_LNS_set_basic_block:
c906108c 20566 break;
c2c6d25f
JM
20567 /* Add to the address register of the state machine the
20568 address increment value corresponding to special opcode
a738430d
MK
20569 255. I.e., this value is scaled by the minimum
20570 instruction length since special opcode 255 would have
b021a221 20571 scaled the increment. */
c906108c 20572 case DW_LNS_const_add_pc:
6f77053d 20573 state_machine.handle_const_add_pc ();
c906108c
SS
20574 break;
20575 case DW_LNS_fixed_advance_pc:
3e29f34a 20576 {
6f77053d 20577 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20578 line_ptr += 2;
6f77053d
PA
20579
20580 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20581 }
c906108c 20582 break;
9aa1fe7e 20583 default:
a738430d
MK
20584 {
20585 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20586 int i;
a738430d 20587
debd256d 20588 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20589 {
20590 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20591 line_ptr += bytes_read;
20592 }
20593 }
c906108c
SS
20594 }
20595 }
d9b3de22
DE
20596
20597 if (!end_sequence)
20598 dwarf2_debug_line_missing_end_sequence_complaint ();
20599
20600 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20601 in which case we still finish recording the last line). */
6f77053d 20602 state_machine.record_line (true);
c906108c 20603 }
f3f5162e
DE
20604}
20605
20606/* Decode the Line Number Program (LNP) for the given line_header
20607 structure and CU. The actual information extracted and the type
20608 of structures created from the LNP depends on the value of PST.
20609
20610 1. If PST is NULL, then this procedure uses the data from the program
20611 to create all necessary symbol tables, and their linetables.
20612
20613 2. If PST is not NULL, this procedure reads the program to determine
20614 the list of files included by the unit represented by PST, and
20615 builds all the associated partial symbol tables.
20616
20617 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20618 It is used for relative paths in the line table.
20619 NOTE: When processing partial symtabs (pst != NULL),
20620 comp_dir == pst->dirname.
20621
20622 NOTE: It is important that psymtabs have the same file name (via strcmp)
20623 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20624 symtab we don't use it in the name of the psymtabs we create.
20625 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20626 A good testcase for this is mb-inline.exp.
20627
527f3840
JK
20628 LOWPC is the lowest address in CU (or 0 if not known).
20629
20630 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20631 for its PC<->lines mapping information. Otherwise only the filename
20632 table is read in. */
f3f5162e
DE
20633
20634static void
20635dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20636 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20637 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20638{
5e22e966 20639 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20640 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20641
527f3840
JK
20642 if (decode_mapping)
20643 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20644
20645 if (decode_for_pst_p)
20646 {
aaa75496
JB
20647 /* Now that we're done scanning the Line Header Program, we can
20648 create the psymtab of each included file. */
7ba99d21
AT
20649 for (auto &file_entry : lh->file_names ())
20650 if (file_entry.included_p == 1)
aaa75496 20651 {
c89b44cd 20652 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20653 const char *include_name =
7ba99d21
AT
20654 psymtab_include_file_name (lh, file_entry, pst,
20655 comp_dir, &name_holder);
c6da4cef 20656 if (include_name != NULL)
aaa75496
JB
20657 dwarf2_create_include_psymtab (include_name, pst, objfile);
20658 }
20659 }
cb1df416
DJ
20660 else
20661 {
20662 /* Make sure a symtab is created for every file, even files
20663 which contain only variables (i.e. no code with associated
20664 line numbers). */
c24bdb02
KS
20665 buildsym_compunit *builder = cu->get_builder ();
20666 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20667
7ba99d21 20668 for (auto &fe : lh->file_names ())
cb1df416 20669 {
804d2729 20670 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20671 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20672 {
c24bdb02 20673 builder->get_current_subfile ()->symtab
804d2729 20674 = allocate_symtab (cust,
c24bdb02 20675 builder->get_current_subfile ()->name);
43f3e411 20676 }
c24bdb02 20677 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20678 }
20679 }
c906108c
SS
20680}
20681
20682/* Start a subfile for DWARF. FILENAME is the name of the file and
20683 DIRNAME the name of the source directory which contains FILENAME
4d663531 20684 or NULL if not known.
c906108c
SS
20685 This routine tries to keep line numbers from identical absolute and
20686 relative file names in a common subfile.
20687
20688 Using the `list' example from the GDB testsuite, which resides in
20689 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20690 of /srcdir/list0.c yields the following debugging information for list0.c:
20691
c5aa993b 20692 DW_AT_name: /srcdir/list0.c
4d663531 20693 DW_AT_comp_dir: /compdir
357e46e7 20694 files.files[0].name: list0.h
c5aa993b 20695 files.files[0].dir: /srcdir
357e46e7 20696 files.files[1].name: list0.c
c5aa993b 20697 files.files[1].dir: /srcdir
c906108c
SS
20698
20699 The line number information for list0.c has to end up in a single
4f1520fb
FR
20700 subfile, so that `break /srcdir/list0.c:1' works as expected.
20701 start_subfile will ensure that this happens provided that we pass the
20702 concatenation of files.files[1].dir and files.files[1].name as the
20703 subfile's name. */
c906108c
SS
20704
20705static void
804d2729
TT
20706dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20707 const char *dirname)
c906108c 20708{
43816ebc 20709 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20710
4d663531 20711 /* In order not to lose the line information directory,
4f1520fb
FR
20712 we concatenate it to the filename when it makes sense.
20713 Note that the Dwarf3 standard says (speaking of filenames in line
20714 information): ``The directory index is ignored for file names
20715 that represent full path names''. Thus ignoring dirname in the
20716 `else' branch below isn't an issue. */
c906108c 20717
d5166ae1 20718 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20719 {
43816ebc
TT
20720 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20721 filename = copy.get ();
d521ce57 20722 }
c906108c 20723
c24bdb02 20724 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20725}
20726
804d2729
TT
20727/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20728 buildsym_compunit constructor. */
f4dc4d17 20729
c24bdb02
KS
20730struct compunit_symtab *
20731dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20732 CORE_ADDR low_pc)
f4dc4d17 20733{
c24bdb02 20734 gdb_assert (m_builder == nullptr);
43f3e411 20735
c24bdb02 20736 m_builder.reset (new struct buildsym_compunit
f6e649dd 20737 (this->per_objfile->objfile,
c24bdb02 20738 name, comp_dir, language, low_pc));
93b8bea4 20739
c24bdb02 20740 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20741
c24bdb02
KS
20742 get_builder ()->record_debugformat ("DWARF 2");
20743 get_builder ()->record_producer (producer);
f4dc4d17 20744
c24bdb02 20745 processing_has_namespace_info = false;
43f3e411 20746
c24bdb02 20747 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20748}
20749
4c2df51b
DJ
20750static void
20751var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20752 struct dwarf2_cu *cu)
4c2df51b 20753{
5e22e966 20754 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
20755 struct comp_unit_head *cu_header = &cu->header;
20756
4c2df51b
DJ
20757 /* NOTE drow/2003-01-30: There used to be a comment and some special
20758 code here to turn a symbol with DW_AT_external and a
20759 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20760 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20761 with some versions of binutils) where shared libraries could have
20762 relocations against symbols in their debug information - the
20763 minimal symbol would have the right address, but the debug info
20764 would not. It's no longer necessary, because we will explicitly
20765 apply relocations when we read in the debug information now. */
20766
20767 /* A DW_AT_location attribute with no contents indicates that a
20768 variable has been optimized away. */
4fc6c0d5 20769 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20770 {
f1e6e072 20771 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20772 return;
20773 }
20774
20775 /* Handle one degenerate form of location expression specially, to
20776 preserve GDB's previous behavior when section offsets are
336d760d
AT
20777 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20778 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20779
4fc6c0d5 20780 if (attr->form_is_block ()
3019eac3
DE
20781 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20782 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20783 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20784 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20785 && (DW_BLOCK (attr)->size
20786 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20787 {
891d2f0b 20788 unsigned int dummy;
4c2df51b 20789
3019eac3 20790 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20791 SET_SYMBOL_VALUE_ADDRESS
20792 (sym, cu->header.read_address (objfile->obfd,
20793 DW_BLOCK (attr)->data + 1,
20794 &dummy));
3019eac3 20795 else
38583298
TT
20796 SET_SYMBOL_VALUE_ADDRESS
20797 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20798 &dummy));
f1e6e072 20799 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20800 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20801 SET_SYMBOL_VALUE_ADDRESS
20802 (sym,
20803 SYMBOL_VALUE_ADDRESS (sym)
20804 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20805 return;
20806 }
20807
20808 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20809 expression evaluator, and use LOC_COMPUTED only when necessary
20810 (i.e. when the value of a register or memory location is
20811 referenced, or a thread-local block, etc.). Then again, it might
20812 not be worthwhile. I'm assuming that it isn't unless performance
20813 or memory numbers show me otherwise. */
20814
f1e6e072 20815 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20816
f1e6e072 20817 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20818 cu->has_loclist = true;
4c2df51b
DJ
20819}
20820
c906108c
SS
20821/* Given a pointer to a DWARF information entry, figure out if we need
20822 to make a symbol table entry for it, and if so, create a new entry
20823 and return a pointer to it.
20824 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20825 used the passed type.
20826 If SPACE is not NULL, use it to hold the new symbol. If it is
20827 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20828
20829static struct symbol *
5e2db402
TT
20830new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20831 struct symbol *space)
c906108c 20832{
976ca316
SM
20833 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20834 struct objfile *objfile = per_objfile->objfile;
08feed99 20835 struct gdbarch *gdbarch = objfile->arch ();
c906108c 20836 struct symbol *sym = NULL;
15d034d0 20837 const char *name;
c906108c
SS
20838 struct attribute *attr = NULL;
20839 struct attribute *attr2 = NULL;
e142c38c 20840 CORE_ADDR baseaddr;
e37fd15a
SW
20841 struct pending **list_to_add = NULL;
20842
edb3359d 20843 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20844
b3b3bada 20845 baseaddr = objfile->text_section_offset ();
c906108c 20846
94af9270 20847 name = dwarf2_name (die, cu);
c906108c
SS
20848 if (name)
20849 {
34eaf542 20850 int suppress_add = 0;
94af9270 20851
34eaf542
TT
20852 if (space)
20853 sym = space;
20854 else
8c14c3a3 20855 sym = new (&objfile->objfile_obstack) symbol;
c906108c 20856 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20857
20858 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20859 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
20860 /* Fortran does not have mangling standard and the mangling does differ
20861 between gfortran, iFort etc. */
bcfe6157
TT
20862 const char *physname
20863 = (cu->language == language_fortran
20864 ? dwarf2_full_name (name, die, cu)
20865 : dwarf2_physname (name, die, cu));
20866 const char *linkagename = dw2_linkage_name (die, cu);
20867
20868 if (linkagename == nullptr || cu->language == language_ada)
20869 sym->set_linkage_name (physname);
20870 else
20871 {
20872 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20873 sym->set_linkage_name (linkagename);
20874 }
f55ee35c 20875
c906108c 20876 /* Default assumptions.
c5aa993b 20877 Use the passed type or decode it from the die. */
176620f1 20878 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20879 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20880 if (type != NULL)
20881 SYMBOL_TYPE (sym) = type;
20882 else
e7c27a73 20883 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20884 attr = dwarf2_attr (die,
20885 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20886 cu);
435d3d88 20887 if (attr != nullptr)
c906108c
SS
20888 {
20889 SYMBOL_LINE (sym) = DW_UNSND (attr);
20890 }
cb1df416 20891
edb3359d
DJ
20892 attr = dwarf2_attr (die,
20893 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20894 cu);
435d3d88 20895 if (attr != nullptr)
cb1df416 20896 {
ecfb656c 20897 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20898 struct file_entry *fe;
9a619af0 20899
ecfb656c
PA
20900 if (cu->line_header != NULL)
20901 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20902 else
20903 fe = NULL;
20904
20905 if (fe == NULL)
b98664d3 20906 complaint (_("file index out of range"));
8c43009f
PA
20907 else
20908 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20909 }
20910
c906108c
SS
20911 switch (die->tag)
20912 {
20913 case DW_TAG_label:
e142c38c 20914 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20915 if (attr != nullptr)
3e29f34a
MR
20916 {
20917 CORE_ADDR addr;
20918
cd6c91b4 20919 addr = attr->value_as_address ();
3e29f34a 20920 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20921 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20922 }
0f5238ed
TT
20923 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20924 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20925 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20926 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20927 break;
20928 case DW_TAG_subprogram:
20929 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20930 finish_block. */
f1e6e072 20931 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20932 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20933 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20934 || cu->language == language_ada
20935 || cu->language == language_fortran)
c906108c 20936 {
2cfa0c8d 20937 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20938 Ada and Fortran subprograms, whether marked external or
20939 not, are always stored as a global symbol, because we want
20940 to be able to access them globally. For instance, we want
20941 to be able to break on a nested subprogram without having
20942 to specify the context. */
c24bdb02 20943 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20944 }
20945 else
20946 {
e37fd15a 20947 list_to_add = cu->list_in_scope;
c906108c
SS
20948 }
20949 break;
edb3359d
DJ
20950 case DW_TAG_inlined_subroutine:
20951 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20952 finish_block. */
f1e6e072 20953 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20954 SYMBOL_INLINED (sym) = 1;
481860b3 20955 list_to_add = cu->list_in_scope;
edb3359d 20956 break;
34eaf542
TT
20957 case DW_TAG_template_value_param:
20958 suppress_add = 1;
20959 /* Fall through. */
72929c62 20960 case DW_TAG_constant:
c906108c 20961 case DW_TAG_variable:
254e6b9e 20962 case DW_TAG_member:
0963b4bd
MS
20963 /* Compilation with minimal debug info may result in
20964 variables with missing type entries. Change the
20965 misleading `void' type to something sensible. */
78134374 20966 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 20967 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20968
e142c38c 20969 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20970 /* In the case of DW_TAG_member, we should only be called for
20971 static const members. */
20972 if (die->tag == DW_TAG_member)
20973 {
3863f96c
DE
20974 /* dwarf2_add_field uses die_is_declaration,
20975 so we do the same. */
254e6b9e
DE
20976 gdb_assert (die_is_declaration (die, cu));
20977 gdb_assert (attr);
20978 }
435d3d88 20979 if (attr != nullptr)
c906108c 20980 {
e7c27a73 20981 dwarf2_const_value (attr, sym, cu);
e142c38c 20982 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20983 if (!suppress_add)
34eaf542
TT
20984 {
20985 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20986 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20987 else
e37fd15a 20988 list_to_add = cu->list_in_scope;
34eaf542 20989 }
c906108c
SS
20990 break;
20991 }
e142c38c 20992 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20993 if (attr != nullptr)
c906108c 20994 {
e7c27a73 20995 var_decode_location (attr, sym, cu);
e142c38c 20996 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20997
20998 /* Fortran explicitly imports any global symbols to the local
20999 scope by DW_TAG_common_block. */
21000 if (cu->language == language_fortran && die->parent
21001 && die->parent->tag == DW_TAG_common_block)
21002 attr2 = NULL;
21003
caac4577
JG
21004 if (SYMBOL_CLASS (sym) == LOC_STATIC
21005 && SYMBOL_VALUE_ADDRESS (sym) == 0
976ca316 21006 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
21007 {
21008 /* When a static variable is eliminated by the linker,
21009 the corresponding debug information is not stripped
21010 out, but the variable address is set to null;
21011 do not add such variables into symbol table. */
21012 }
21013 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21014 {
4b610737
TT
21015 if (SYMBOL_CLASS (sym) == LOC_STATIC
21016 && (objfile->flags & OBJF_MAINLINE) == 0
976ca316 21017 && per_objfile->per_bfd->can_copy)
4b610737
TT
21018 {
21019 /* A global static variable might be subject to
21020 copy relocation. We first check for a local
21021 minsym, though, because maybe the symbol was
21022 marked hidden, in which case this would not
21023 apply. */
21024 bound_minimal_symbol found
21025 = (lookup_minimal_symbol_linkage
987012b8 21026 (sym->linkage_name (), objfile));
4b610737
TT
21027 if (found.minsym != nullptr)
21028 sym->maybe_copied = 1;
21029 }
f55ee35c 21030
1c809c68
TT
21031 /* A variable with DW_AT_external is never static,
21032 but it may be block-scoped. */
804d2729 21033 list_to_add
c24bdb02
KS
21034 = ((cu->list_in_scope
21035 == cu->get_builder ()->get_file_symbols ())
21036 ? cu->get_builder ()->get_global_symbols ()
804d2729 21037 : cu->list_in_scope);
1c809c68 21038 }
c906108c 21039 else
e37fd15a 21040 list_to_add = cu->list_in_scope;
c906108c
SS
21041 }
21042 else
21043 {
21044 /* We do not know the address of this symbol.
c5aa993b
JM
21045 If it is an external symbol and we have type information
21046 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21047 The address of the variable will then be determined from
21048 the minimal symbol table whenever the variable is
21049 referenced. */
e142c38c 21050 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21051
21052 /* Fortran explicitly imports any global symbols to the local
21053 scope by DW_TAG_common_block. */
21054 if (cu->language == language_fortran && die->parent
21055 && die->parent->tag == DW_TAG_common_block)
21056 {
21057 /* SYMBOL_CLASS doesn't matter here because
21058 read_common_block is going to reset it. */
21059 if (!suppress_add)
21060 list_to_add = cu->list_in_scope;
21061 }
21062 else if (attr2 && (DW_UNSND (attr2) != 0)
21063 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21064 {
0fe7935b
DJ
21065 /* A variable with DW_AT_external is never static, but it
21066 may be block-scoped. */
804d2729 21067 list_to_add
c24bdb02
KS
21068 = ((cu->list_in_scope
21069 == cu->get_builder ()->get_file_symbols ())
21070 ? cu->get_builder ()->get_global_symbols ()
804d2729 21071 : cu->list_in_scope);
0fe7935b 21072
f1e6e072 21073 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21074 }
442ddf59
JK
21075 else if (!die_is_declaration (die, cu))
21076 {
21077 /* Use the default LOC_OPTIMIZED_OUT class. */
21078 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21079 if (!suppress_add)
21080 list_to_add = cu->list_in_scope;
442ddf59 21081 }
c906108c
SS
21082 }
21083 break;
21084 case DW_TAG_formal_parameter:
a60f3166
TT
21085 {
21086 /* If we are inside a function, mark this as an argument. If
21087 not, we might be looking at an argument to an inlined function
21088 when we do not have enough information to show inlined frames;
21089 pretend it's a local variable in that case so that the user can
21090 still see it. */
804d2729 21091 struct context_stack *curr
c24bdb02 21092 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
21093 if (curr != nullptr && curr->name != nullptr)
21094 SYMBOL_IS_ARGUMENT (sym) = 1;
21095 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21096 if (attr != nullptr)
a60f3166
TT
21097 {
21098 var_decode_location (attr, sym, cu);
21099 }
21100 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21101 if (attr != nullptr)
a60f3166
TT
21102 {
21103 dwarf2_const_value (attr, sym, cu);
21104 }
f346a30d 21105
a60f3166
TT
21106 list_to_add = cu->list_in_scope;
21107 }
c906108c
SS
21108 break;
21109 case DW_TAG_unspecified_parameters:
21110 /* From varargs functions; gdb doesn't seem to have any
21111 interest in this information, so just ignore it for now.
21112 (FIXME?) */
21113 break;
34eaf542
TT
21114 case DW_TAG_template_type_param:
21115 suppress_add = 1;
21116 /* Fall through. */
c906108c 21117 case DW_TAG_class_type:
680b30c7 21118 case DW_TAG_interface_type:
c906108c
SS
21119 case DW_TAG_structure_type:
21120 case DW_TAG_union_type:
72019c9c 21121 case DW_TAG_set_type:
c906108c 21122 case DW_TAG_enumeration_type:
f1e6e072 21123 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21124 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21125
63d06c5c 21126 {
9c37b5ae 21127 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21128 really ever be static objects: otherwise, if you try
21129 to, say, break of a class's method and you're in a file
21130 which doesn't mention that class, it won't work unless
21131 the check for all static symbols in lookup_symbol_aux
21132 saves you. See the OtherFileClass tests in
21133 gdb.c++/namespace.exp. */
21134
e37fd15a 21135 if (!suppress_add)
34eaf542 21136 {
c24bdb02 21137 buildsym_compunit *builder = cu->get_builder ();
804d2729 21138 list_to_add
c24bdb02 21139 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 21140 && cu->language == language_cplus
c24bdb02 21141 ? builder->get_global_symbols ()
804d2729 21142 : cu->list_in_scope);
63d06c5c 21143
64382290 21144 /* The semantics of C++ state that "struct foo {
9c37b5ae 21145 ... }" also defines a typedef for "foo". */
64382290 21146 if (cu->language == language_cplus
45280282 21147 || cu->language == language_ada
c44af4eb
TT
21148 || cu->language == language_d
21149 || cu->language == language_rust)
64382290
TT
21150 {
21151 /* The symbol's name is already allocated along
21152 with this objfile, so we don't need to
21153 duplicate it for the type. */
7d93a1e0 21154 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 21155 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 21156 }
63d06c5c
DC
21157 }
21158 }
c906108c
SS
21159 break;
21160 case DW_TAG_typedef:
f1e6e072 21161 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21162 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21163 list_to_add = cu->list_in_scope;
63d06c5c 21164 break;
c906108c 21165 case DW_TAG_base_type:
a02abb62 21166 case DW_TAG_subrange_type:
f1e6e072 21167 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21168 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21169 list_to_add = cu->list_in_scope;
c906108c
SS
21170 break;
21171 case DW_TAG_enumerator:
e142c38c 21172 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21173 if (attr != nullptr)
c906108c 21174 {
e7c27a73 21175 dwarf2_const_value (attr, sym, cu);
c906108c 21176 }
63d06c5c
DC
21177 {
21178 /* NOTE: carlton/2003-11-10: See comment above in the
21179 DW_TAG_class_type, etc. block. */
21180
804d2729 21181 list_to_add
c24bdb02 21182 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21183 && cu->language == language_cplus
c24bdb02 21184 ? cu->get_builder ()->get_global_symbols ()
804d2729 21185 : cu->list_in_scope);
63d06c5c 21186 }
c906108c 21187 break;
74921315 21188 case DW_TAG_imported_declaration:
5c4e30ca 21189 case DW_TAG_namespace:
f1e6e072 21190 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21191 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21192 break;
530e8392
KB
21193 case DW_TAG_module:
21194 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21195 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21196 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21197 break;
4357ac6c 21198 case DW_TAG_common_block:
f1e6e072 21199 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21200 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21201 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21202 break;
c906108c
SS
21203 default:
21204 /* Not a tag we recognize. Hopefully we aren't processing
21205 trash data, but since we must specifically ignore things
21206 we don't recognize, there is nothing else we should do at
0963b4bd 21207 this point. */
b98664d3 21208 complaint (_("unsupported tag: '%s'"),
4d3c2250 21209 dwarf_tag_name (die->tag));
c906108c
SS
21210 break;
21211 }
df8a16a1 21212
e37fd15a
SW
21213 if (suppress_add)
21214 {
21215 sym->hash_next = objfile->template_symbols;
21216 objfile->template_symbols = sym;
21217 list_to_add = NULL;
21218 }
21219
21220 if (list_to_add != NULL)
d3cb6808 21221 add_symbol_to_list (sym, list_to_add);
e37fd15a 21222
df8a16a1
DJ
21223 /* For the benefit of old versions of GCC, check for anonymous
21224 namespaces based on the demangled name. */
4d4ec4e5 21225 if (!cu->processing_has_namespace_info
94af9270 21226 && cu->language == language_cplus)
c24bdb02 21227 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21228 }
21229 return (sym);
21230}
21231
98bfdba5
PA
21232/* Given an attr with a DW_FORM_dataN value in host byte order,
21233 zero-extend it as appropriate for the symbol's type. The DWARF
21234 standard (v4) is not entirely clear about the meaning of using
21235 DW_FORM_dataN for a constant with a signed type, where the type is
21236 wider than the data. The conclusion of a discussion on the DWARF
21237 list was that this is unspecified. We choose to always zero-extend
21238 because that is the interpretation long in use by GCC. */
c906108c 21239
98bfdba5 21240static gdb_byte *
ff39bb5e 21241dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21242 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21243{
5e22e966 21244 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
21245 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21246 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21247 LONGEST l = DW_UNSND (attr);
21248
21249 if (bits < sizeof (*value) * 8)
21250 {
21251 l &= ((LONGEST) 1 << bits) - 1;
21252 *value = l;
21253 }
21254 else if (bits == sizeof (*value) * 8)
21255 *value = l;
21256 else
21257 {
224c3ddb 21258 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21259 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21260 return bytes;
21261 }
21262
21263 return NULL;
21264}
21265
21266/* Read a constant value from an attribute. Either set *VALUE, or if
21267 the value does not fit in *VALUE, set *BYTES - either already
21268 allocated on the objfile obstack, or newly allocated on OBSTACK,
21269 or, set *BATON, if we translated the constant to a location
21270 expression. */
21271
21272static void
ff39bb5e 21273dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21274 const char *name, struct obstack *obstack,
21275 struct dwarf2_cu *cu,
d521ce57 21276 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21277 struct dwarf2_locexpr_baton **baton)
21278{
5e22e966 21279 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 21280 struct objfile *objfile = per_objfile->objfile;
98bfdba5 21281 struct comp_unit_head *cu_header = &cu->header;
c906108c 21282 struct dwarf_block *blk;
98bfdba5
PA
21283 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21284 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21285
21286 *value = 0;
21287 *bytes = NULL;
21288 *baton = NULL;
c906108c
SS
21289
21290 switch (attr->form)
21291 {
21292 case DW_FORM_addr:
336d760d 21293 case DW_FORM_addrx:
3019eac3 21294 case DW_FORM_GNU_addr_index:
ac56253d 21295 {
ac56253d
TT
21296 gdb_byte *data;
21297
98bfdba5
PA
21298 if (TYPE_LENGTH (type) != cu_header->addr_size)
21299 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21300 cu_header->addr_size,
98bfdba5 21301 TYPE_LENGTH (type));
ac56253d
TT
21302 /* Symbols of this form are reasonably rare, so we just
21303 piggyback on the existing location code rather than writing
21304 a new implementation of symbol_computed_ops. */
8d749320 21305 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 21306 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
21307 (*baton)->per_cu = cu->per_cu;
21308 gdb_assert ((*baton)->per_cu);
ac56253d 21309
98bfdba5 21310 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21311 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21312 (*baton)->data = data;
ac56253d
TT
21313
21314 data[0] = DW_OP_addr;
21315 store_unsigned_integer (&data[1], cu_header->addr_size,
21316 byte_order, DW_ADDR (attr));
21317 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21318 }
c906108c 21319 break;
4ac36638 21320 case DW_FORM_string:
93b5768b 21321 case DW_FORM_strp:
cf532bd1 21322 case DW_FORM_strx:
3019eac3 21323 case DW_FORM_GNU_str_index:
36586728 21324 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21325 /* DW_STRING is already allocated on the objfile obstack, point
21326 directly to it. */
d521ce57 21327 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21328 break;
c906108c
SS
21329 case DW_FORM_block1:
21330 case DW_FORM_block2:
21331 case DW_FORM_block4:
21332 case DW_FORM_block:
2dc7f7b3 21333 case DW_FORM_exprloc:
0224619f 21334 case DW_FORM_data16:
c906108c 21335 blk = DW_BLOCK (attr);
98bfdba5
PA
21336 if (TYPE_LENGTH (type) != blk->size)
21337 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21338 TYPE_LENGTH (type));
21339 *bytes = blk->data;
c906108c 21340 break;
2df3850c
JM
21341
21342 /* The DW_AT_const_value attributes are supposed to carry the
21343 symbol's value "represented as it would be on the target
21344 architecture." By the time we get here, it's already been
21345 converted to host endianness, so we just need to sign- or
21346 zero-extend it as appropriate. */
21347 case DW_FORM_data1:
3aef2284 21348 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21349 break;
c906108c 21350 case DW_FORM_data2:
3aef2284 21351 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21352 break;
c906108c 21353 case DW_FORM_data4:
3aef2284 21354 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21355 break;
c906108c 21356 case DW_FORM_data8:
3aef2284 21357 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21358 break;
21359
c906108c 21360 case DW_FORM_sdata:
663c44ac 21361 case DW_FORM_implicit_const:
98bfdba5 21362 *value = DW_SND (attr);
2df3850c
JM
21363 break;
21364
c906108c 21365 case DW_FORM_udata:
98bfdba5 21366 *value = DW_UNSND (attr);
c906108c 21367 break;
2df3850c 21368
c906108c 21369 default:
b98664d3 21370 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21371 dwarf_form_name (attr->form));
98bfdba5 21372 *value = 0;
c906108c
SS
21373 break;
21374 }
21375}
21376
2df3850c 21377
98bfdba5
PA
21378/* Copy constant value from an attribute to a symbol. */
21379
2df3850c 21380static void
ff39bb5e 21381dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21382 struct dwarf2_cu *cu)
2df3850c 21383{
5e22e966 21384 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 21385 LONGEST value;
d521ce57 21386 const gdb_byte *bytes;
98bfdba5 21387 struct dwarf2_locexpr_baton *baton;
2df3850c 21388
98bfdba5 21389 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21390 sym->print_name (),
98bfdba5
PA
21391 &objfile->objfile_obstack, cu,
21392 &value, &bytes, &baton);
2df3850c 21393
98bfdba5
PA
21394 if (baton != NULL)
21395 {
98bfdba5 21396 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21397 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21398 }
21399 else if (bytes != NULL)
21400 {
21401 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21402 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21403 }
21404 else
21405 {
21406 SYMBOL_VALUE (sym) = value;
f1e6e072 21407 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21408 }
2df3850c
JM
21409}
21410
c906108c
SS
21411/* Return the type of the die in question using its DW_AT_type attribute. */
21412
21413static struct type *
e7c27a73 21414die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21415{
c906108c 21416 struct attribute *type_attr;
c906108c 21417
e142c38c 21418 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21419 if (!type_attr)
21420 {
5e22e966 21421 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21422 /* A missing DW_AT_type represents a void type. */
518817b3 21423 return objfile_type (objfile)->builtin_void;
c906108c 21424 }
348e048f 21425
673bfd45 21426 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21427}
21428
b4ba55a1
JB
21429/* True iff CU's producer generates GNAT Ada auxiliary information
21430 that allows to find parallel types through that information instead
21431 of having to do expensive parallel lookups by type name. */
21432
21433static int
21434need_gnat_info (struct dwarf2_cu *cu)
21435{
de4cb04a
JB
21436 /* Assume that the Ada compiler was GNAT, which always produces
21437 the auxiliary information. */
21438 return (cu->language == language_ada);
b4ba55a1
JB
21439}
21440
b4ba55a1
JB
21441/* Return the auxiliary type of the die in question using its
21442 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21443 attribute is not present. */
21444
21445static struct type *
21446die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21447{
b4ba55a1 21448 struct attribute *type_attr;
b4ba55a1
JB
21449
21450 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21451 if (!type_attr)
21452 return NULL;
21453
673bfd45 21454 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21455}
21456
21457/* If DIE has a descriptive_type attribute, then set the TYPE's
21458 descriptive type accordingly. */
21459
21460static void
21461set_descriptive_type (struct type *type, struct die_info *die,
21462 struct dwarf2_cu *cu)
21463{
21464 struct type *descriptive_type = die_descriptive_type (die, cu);
21465
21466 if (descriptive_type)
21467 {
21468 ALLOCATE_GNAT_AUX_TYPE (type);
21469 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21470 }
21471}
21472
c906108c
SS
21473/* Return the containing type of the die in question using its
21474 DW_AT_containing_type attribute. */
21475
21476static struct type *
e7c27a73 21477die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21478{
c906108c 21479 struct attribute *type_attr;
5e22e966 21480 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21481
e142c38c 21482 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21483 if (!type_attr)
21484 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21485 "[in module %s]"), objfile_name (objfile));
33ac96f0 21486
673bfd45 21487 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21488}
21489
ac9ec31b
DE
21490/* Return an error marker type to use for the ill formed type in DIE/CU. */
21491
21492static struct type *
21493build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21494{
976ca316
SM
21495 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21496 struct objfile *objfile = per_objfile->objfile;
528e1572 21497 char *saved;
ac9ec31b 21498
528e1572
SM
21499 std::string message
21500 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21501 objfile_name (objfile),
21502 sect_offset_str (cu->header.sect_off),
21503 sect_offset_str (die->sect_off));
efba19b0 21504 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21505
19f392bc 21506 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21507}
21508
673bfd45 21509/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21510 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21511 DW_AT_containing_type.
673bfd45
DE
21512 If there is no type substitute an error marker. */
21513
c906108c 21514static struct type *
ff39bb5e 21515lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21516 struct dwarf2_cu *cu)
c906108c 21517{
976ca316
SM
21518 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21519 struct objfile *objfile = per_objfile->objfile;
f792889a
DJ
21520 struct type *this_type;
21521
ac9ec31b
DE
21522 gdb_assert (attr->name == DW_AT_type
21523 || attr->name == DW_AT_GNAT_descriptive_type
21524 || attr->name == DW_AT_containing_type);
21525
673bfd45
DE
21526 /* First see if we have it cached. */
21527
36586728
TT
21528 if (attr->form == DW_FORM_GNU_ref_alt)
21529 {
21530 struct dwarf2_per_cu_data *per_cu;
0826b30a 21531 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 21532
976ca316
SM
21533 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
21534 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
36586728 21535 }
cd6c91b4 21536 else if (attr->form_is_ref ())
673bfd45 21537 {
0826b30a 21538 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 21539
976ca316 21540 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
673bfd45 21541 }
55f1336d 21542 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21543 {
ac9ec31b 21544 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21545
ac9ec31b 21546 return get_signatured_type (die, signature, cu);
673bfd45
DE
21547 }
21548 else
21549 {
b98664d3 21550 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21551 " at %s [in module %s]"),
21552 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21553 objfile_name (objfile));
ac9ec31b 21554 return build_error_marker_type (cu, die);
673bfd45
DE
21555 }
21556
21557 /* If not cached we need to read it in. */
21558
21559 if (this_type == NULL)
21560 {
ac9ec31b 21561 struct die_info *type_die = NULL;
673bfd45
DE
21562 struct dwarf2_cu *type_cu = cu;
21563
cd6c91b4 21564 if (attr->form_is_ref ())
ac9ec31b
DE
21565 type_die = follow_die_ref (die, attr, &type_cu);
21566 if (type_die == NULL)
21567 return build_error_marker_type (cu, die);
21568 /* If we find the type now, it's probably because the type came
3019eac3
DE
21569 from an inter-CU reference and the type's CU got expanded before
21570 ours. */
ac9ec31b 21571 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21572 }
21573
21574 /* If we still don't have a type use an error marker. */
21575
21576 if (this_type == NULL)
ac9ec31b 21577 return build_error_marker_type (cu, die);
673bfd45 21578
f792889a 21579 return this_type;
c906108c
SS
21580}
21581
673bfd45
DE
21582/* Return the type in DIE, CU.
21583 Returns NULL for invalid types.
21584
02142a6c 21585 This first does a lookup in die_type_hash,
673bfd45
DE
21586 and only reads the die in if necessary.
21587
21588 NOTE: This can be called when reading in partial or full symbols. */
21589
f792889a 21590static struct type *
e7c27a73 21591read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21592{
f792889a
DJ
21593 struct type *this_type;
21594
21595 this_type = get_die_type (die, cu);
21596 if (this_type)
21597 return this_type;
21598
673bfd45
DE
21599 return read_type_die_1 (die, cu);
21600}
21601
21602/* Read the type in DIE, CU.
21603 Returns NULL for invalid types. */
21604
21605static struct type *
21606read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21607{
21608 struct type *this_type = NULL;
21609
c906108c
SS
21610 switch (die->tag)
21611 {
21612 case DW_TAG_class_type:
680b30c7 21613 case DW_TAG_interface_type:
c906108c
SS
21614 case DW_TAG_structure_type:
21615 case DW_TAG_union_type:
f792889a 21616 this_type = read_structure_type (die, cu);
c906108c
SS
21617 break;
21618 case DW_TAG_enumeration_type:
f792889a 21619 this_type = read_enumeration_type (die, cu);
c906108c
SS
21620 break;
21621 case DW_TAG_subprogram:
21622 case DW_TAG_subroutine_type:
edb3359d 21623 case DW_TAG_inlined_subroutine:
f792889a 21624 this_type = read_subroutine_type (die, cu);
c906108c
SS
21625 break;
21626 case DW_TAG_array_type:
f792889a 21627 this_type = read_array_type (die, cu);
c906108c 21628 break;
72019c9c 21629 case DW_TAG_set_type:
f792889a 21630 this_type = read_set_type (die, cu);
72019c9c 21631 break;
c906108c 21632 case DW_TAG_pointer_type:
f792889a 21633 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21634 break;
21635 case DW_TAG_ptr_to_member_type:
f792889a 21636 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21637 break;
21638 case DW_TAG_reference_type:
4297a3f0
AV
21639 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21640 break;
21641 case DW_TAG_rvalue_reference_type:
21642 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21643 break;
21644 case DW_TAG_const_type:
f792889a 21645 this_type = read_tag_const_type (die, cu);
c906108c
SS
21646 break;
21647 case DW_TAG_volatile_type:
f792889a 21648 this_type = read_tag_volatile_type (die, cu);
c906108c 21649 break;
06d66ee9
TT
21650 case DW_TAG_restrict_type:
21651 this_type = read_tag_restrict_type (die, cu);
21652 break;
c906108c 21653 case DW_TAG_string_type:
f792889a 21654 this_type = read_tag_string_type (die, cu);
c906108c
SS
21655 break;
21656 case DW_TAG_typedef:
f792889a 21657 this_type = read_typedef (die, cu);
c906108c 21658 break;
a02abb62 21659 case DW_TAG_subrange_type:
f792889a 21660 this_type = read_subrange_type (die, cu);
a02abb62 21661 break;
c906108c 21662 case DW_TAG_base_type:
f792889a 21663 this_type = read_base_type (die, cu);
c906108c 21664 break;
81a17f79 21665 case DW_TAG_unspecified_type:
f792889a 21666 this_type = read_unspecified_type (die, cu);
81a17f79 21667 break;
0114d602
DJ
21668 case DW_TAG_namespace:
21669 this_type = read_namespace_type (die, cu);
21670 break;
f55ee35c
JK
21671 case DW_TAG_module:
21672 this_type = read_module_type (die, cu);
21673 break;
a2c2acaf
MW
21674 case DW_TAG_atomic_type:
21675 this_type = read_tag_atomic_type (die, cu);
21676 break;
c906108c 21677 default:
b98664d3 21678 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21679 dwarf_tag_name (die->tag));
c906108c
SS
21680 break;
21681 }
63d06c5c 21682
f792889a 21683 return this_type;
63d06c5c
DC
21684}
21685
abc72ce4
DE
21686/* See if we can figure out if the class lives in a namespace. We do
21687 this by looking for a member function; its demangled name will
21688 contain namespace info, if there is any.
21689 Return the computed name or NULL.
21690 Space for the result is allocated on the objfile's obstack.
21691 This is the full-die version of guess_partial_die_structure_name.
21692 In this case we know DIE has no useful parent. */
21693
43816ebc 21694static const char *
abc72ce4
DE
21695guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21696{
21697 struct die_info *spec_die;
21698 struct dwarf2_cu *spec_cu;
21699 struct die_info *child;
5e22e966 21700 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
21701
21702 spec_cu = cu;
21703 spec_die = die_specification (die, &spec_cu);
21704 if (spec_die != NULL)
21705 {
21706 die = spec_die;
21707 cu = spec_cu;
21708 }
21709
21710 for (child = die->child;
21711 child != NULL;
21712 child = child->sibling)
21713 {
21714 if (child->tag == DW_TAG_subprogram)
21715 {
73b9be8b 21716 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21717
7d45c7c3 21718 if (linkage_name != NULL)
abc72ce4 21719 {
43816ebc
TT
21720 gdb::unique_xmalloc_ptr<char> actual_name
21721 (language_class_name_from_physname (cu->language_defn,
21722 linkage_name));
21723 const char *name = NULL;
abc72ce4
DE
21724
21725 if (actual_name != NULL)
21726 {
15d034d0 21727 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21728
21729 if (die_name != NULL
43816ebc 21730 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21731 {
21732 /* Strip off the class name from the full name.
21733 We want the prefix. */
21734 int die_name_len = strlen (die_name);
43816ebc
TT
21735 int actual_name_len = strlen (actual_name.get ());
21736 const char *ptr = actual_name.get ();
abc72ce4
DE
21737
21738 /* Test for '::' as a sanity check. */
21739 if (actual_name_len > die_name_len + 2
43816ebc 21740 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21741 name = obstack_strndup (
e3b94546 21742 &objfile->per_bfd->storage_obstack,
43816ebc 21743 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21744 }
21745 }
abc72ce4
DE
21746 return name;
21747 }
21748 }
21749 }
21750
21751 return NULL;
21752}
21753
96408a79
SA
21754/* GCC might emit a nameless typedef that has a linkage name. Determine the
21755 prefix part in such case. See
21756 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21757
a121b7c1 21758static const char *
96408a79
SA
21759anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21760{
21761 struct attribute *attr;
e6a959d6 21762 const char *base;
96408a79
SA
21763
21764 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21765 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21766 return NULL;
21767
7d45c7c3 21768 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21769 return NULL;
21770
73b9be8b 21771 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21772 if (attr == NULL || DW_STRING (attr) == NULL)
21773 return NULL;
21774
21775 /* dwarf2_name had to be already called. */
21776 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21777
21778 /* Strip the base name, keep any leading namespaces/classes. */
21779 base = strrchr (DW_STRING (attr), ':');
21780 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21781 return "";
21782
5e22e966 21783 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9
TT
21784 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21785 DW_STRING (attr),
21786 &base[-1] - DW_STRING (attr));
96408a79
SA
21787}
21788
fdde2d81 21789/* Return the name of the namespace/class that DIE is defined within,
0114d602 21790 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21791
0114d602
DJ
21792 For example, if we're within the method foo() in the following
21793 code:
21794
21795 namespace N {
21796 class C {
21797 void foo () {
21798 }
21799 };
21800 }
21801
21802 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21803
0d5cff50 21804static const char *
e142c38c 21805determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21806{
976ca316 21807 dwarf2_per_objfile *per_objfile = cu->per_objfile;
0114d602
DJ
21808 struct die_info *parent, *spec_die;
21809 struct dwarf2_cu *spec_cu;
21810 struct type *parent_type;
a121b7c1 21811 const char *retval;
63d06c5c 21812
9c37b5ae 21813 if (cu->language != language_cplus
c44af4eb
TT
21814 && cu->language != language_fortran && cu->language != language_d
21815 && cu->language != language_rust)
0114d602
DJ
21816 return "";
21817
96408a79
SA
21818 retval = anonymous_struct_prefix (die, cu);
21819 if (retval)
21820 return retval;
21821
0114d602
DJ
21822 /* We have to be careful in the presence of DW_AT_specification.
21823 For example, with GCC 3.4, given the code
21824
21825 namespace N {
21826 void foo() {
21827 // Definition of N::foo.
21828 }
21829 }
21830
21831 then we'll have a tree of DIEs like this:
21832
21833 1: DW_TAG_compile_unit
21834 2: DW_TAG_namespace // N
21835 3: DW_TAG_subprogram // declaration of N::foo
21836 4: DW_TAG_subprogram // definition of N::foo
21837 DW_AT_specification // refers to die #3
21838
21839 Thus, when processing die #4, we have to pretend that we're in
21840 the context of its DW_AT_specification, namely the contex of die
21841 #3. */
21842 spec_cu = cu;
21843 spec_die = die_specification (die, &spec_cu);
21844 if (spec_die == NULL)
21845 parent = die->parent;
21846 else
63d06c5c 21847 {
0114d602
DJ
21848 parent = spec_die->parent;
21849 cu = spec_cu;
63d06c5c 21850 }
0114d602
DJ
21851
21852 if (parent == NULL)
21853 return "";
98bfdba5
PA
21854 else if (parent->building_fullname)
21855 {
21856 const char *name;
21857 const char *parent_name;
21858
21859 /* It has been seen on RealView 2.2 built binaries,
21860 DW_TAG_template_type_param types actually _defined_ as
21861 children of the parent class:
21862
21863 enum E {};
21864 template class <class Enum> Class{};
21865 Class<enum E> class_e;
21866
21867 1: DW_TAG_class_type (Class)
21868 2: DW_TAG_enumeration_type (E)
21869 3: DW_TAG_enumerator (enum1:0)
21870 3: DW_TAG_enumerator (enum2:1)
21871 ...
21872 2: DW_TAG_template_type_param
21873 DW_AT_type DW_FORM_ref_udata (E)
21874
21875 Besides being broken debug info, it can put GDB into an
21876 infinite loop. Consider:
21877
21878 When we're building the full name for Class<E>, we'll start
21879 at Class, and go look over its template type parameters,
21880 finding E. We'll then try to build the full name of E, and
21881 reach here. We're now trying to build the full name of E,
21882 and look over the parent DIE for containing scope. In the
21883 broken case, if we followed the parent DIE of E, we'd again
21884 find Class, and once again go look at its template type
21885 arguments, etc., etc. Simply don't consider such parent die
21886 as source-level parent of this die (it can't be, the language
21887 doesn't allow it), and break the loop here. */
21888 name = dwarf2_name (die, cu);
21889 parent_name = dwarf2_name (parent, cu);
b98664d3 21890 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21891 name ? name : "<unknown>",
21892 parent_name ? parent_name : "<unknown>");
21893 return "";
21894 }
63d06c5c 21895 else
0114d602
DJ
21896 switch (parent->tag)
21897 {
63d06c5c 21898 case DW_TAG_namespace:
0114d602 21899 parent_type = read_type_die (parent, cu);
acebe513
UW
21900 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21901 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21902 Work around this problem here. */
21903 if (cu->language == language_cplus
7d93a1e0 21904 && strcmp (parent_type->name (), "::") == 0)
acebe513 21905 return "";
0114d602 21906 /* We give a name to even anonymous namespaces. */
7d93a1e0 21907 return parent_type->name ();
63d06c5c 21908 case DW_TAG_class_type:
680b30c7 21909 case DW_TAG_interface_type:
63d06c5c 21910 case DW_TAG_structure_type:
0114d602 21911 case DW_TAG_union_type:
f55ee35c 21912 case DW_TAG_module:
0114d602 21913 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
21914 if (parent_type->name () != NULL)
21915 return parent_type->name ();
0114d602
DJ
21916 else
21917 /* An anonymous structure is only allowed non-static data
21918 members; no typedefs, no member functions, et cetera.
21919 So it does not need a prefix. */
21920 return "";
abc72ce4 21921 case DW_TAG_compile_unit:
95554aad 21922 case DW_TAG_partial_unit:
abc72ce4
DE
21923 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21924 if (cu->language == language_cplus
976ca316 21925 && !per_objfile->per_bfd->types.empty ()
abc72ce4
DE
21926 && die->child != NULL
21927 && (die->tag == DW_TAG_class_type
21928 || die->tag == DW_TAG_structure_type
21929 || die->tag == DW_TAG_union_type))
21930 {
43816ebc 21931 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21932 if (name != NULL)
21933 return name;
21934 }
21935 return "";
0a4b0913
AB
21936 case DW_TAG_subprogram:
21937 /* Nested subroutines in Fortran get a prefix with the name
21938 of the parent's subroutine. */
21939 if (cu->language == language_fortran)
21940 {
21941 if ((die->tag == DW_TAG_subprogram)
21942 && (dwarf2_name (parent, cu) != NULL))
21943 return dwarf2_name (parent, cu);
21944 }
21945 return determine_prefix (parent, cu);
3d567982
TT
21946 case DW_TAG_enumeration_type:
21947 parent_type = read_type_die (parent, cu);
21948 if (TYPE_DECLARED_CLASS (parent_type))
21949 {
7d93a1e0
SM
21950 if (parent_type->name () != NULL)
21951 return parent_type->name ();
3d567982
TT
21952 return "";
21953 }
21954 /* Fall through. */
63d06c5c 21955 default:
8176b9b8 21956 return determine_prefix (parent, cu);
63d06c5c 21957 }
63d06c5c
DC
21958}
21959
3e43a32a
MS
21960/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21961 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21962 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21963 an obconcat, otherwise allocate storage for the result. The CU argument is
21964 used to determine the language and hence, the appropriate separator. */
987504bb 21965
f55ee35c 21966#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21967
21968static char *
f55ee35c
JK
21969typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21970 int physname, struct dwarf2_cu *cu)
63d06c5c 21971{
f55ee35c 21972 const char *lead = "";
5c315b68 21973 const char *sep;
63d06c5c 21974
3e43a32a
MS
21975 if (suffix == NULL || suffix[0] == '\0'
21976 || prefix == NULL || prefix[0] == '\0')
987504bb 21977 sep = "";
45280282
IB
21978 else if (cu->language == language_d)
21979 {
21980 /* For D, the 'main' function could be defined in any module, but it
21981 should never be prefixed. */
21982 if (strcmp (suffix, "D main") == 0)
21983 {
21984 prefix = "";
21985 sep = "";
21986 }
21987 else
21988 sep = ".";
21989 }
f55ee35c
JK
21990 else if (cu->language == language_fortran && physname)
21991 {
21992 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21993 DW_AT_MIPS_linkage_name is preferred and used instead. */
21994
21995 lead = "__";
21996 sep = "_MOD_";
21997 }
987504bb
JJ
21998 else
21999 sep = "::";
63d06c5c 22000
6dd47d34
DE
22001 if (prefix == NULL)
22002 prefix = "";
22003 if (suffix == NULL)
22004 suffix = "";
22005
987504bb
JJ
22006 if (obs == NULL)
22007 {
3e43a32a 22008 char *retval
224c3ddb
SM
22009 = ((char *)
22010 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22011
f55ee35c
JK
22012 strcpy (retval, lead);
22013 strcat (retval, prefix);
6dd47d34
DE
22014 strcat (retval, sep);
22015 strcat (retval, suffix);
63d06c5c
DC
22016 return retval;
22017 }
987504bb
JJ
22018 else
22019 {
22020 /* We have an obstack. */
f55ee35c 22021 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22022 }
63d06c5c
DC
22023}
22024
71c25dea
TT
22025/* Get name of a die, return NULL if not found. */
22026
15d034d0
TT
22027static const char *
22028dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 22029 struct objfile *objfile)
71c25dea
TT
22030{
22031 if (name && cu->language == language_cplus)
22032 {
596dc4ad
TT
22033 gdb::unique_xmalloc_ptr<char> canon_name
22034 = cp_canonicalize_string (name);
71c25dea 22035
596dc4ad
TT
22036 if (canon_name != nullptr)
22037 name = objfile->intern (canon_name.get ());
71c25dea
TT
22038 }
22039
22040 return name;
c906108c
SS
22041}
22042
96553a0c
DE
22043/* Get name of a die, return NULL if not found.
22044 Anonymous namespaces are converted to their magic string. */
9219021c 22045
15d034d0 22046static const char *
e142c38c 22047dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22048{
22049 struct attribute *attr;
5e22e966 22050 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 22051
e142c38c 22052 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22053 if ((!attr || !DW_STRING (attr))
96553a0c 22054 && die->tag != DW_TAG_namespace
53832f31
TT
22055 && die->tag != DW_TAG_class_type
22056 && die->tag != DW_TAG_interface_type
22057 && die->tag != DW_TAG_structure_type
22058 && die->tag != DW_TAG_union_type)
71c25dea
TT
22059 return NULL;
22060
22061 switch (die->tag)
22062 {
22063 case DW_TAG_compile_unit:
95554aad 22064 case DW_TAG_partial_unit:
71c25dea
TT
22065 /* Compilation units have a DW_AT_name that is a filename, not
22066 a source language identifier. */
22067 case DW_TAG_enumeration_type:
22068 case DW_TAG_enumerator:
22069 /* These tags always have simple identifiers already; no need
22070 to canonicalize them. */
22071 return DW_STRING (attr);
907af001 22072
96553a0c
DE
22073 case DW_TAG_namespace:
22074 if (attr != NULL && DW_STRING (attr) != NULL)
22075 return DW_STRING (attr);
22076 return CP_ANONYMOUS_NAMESPACE_STR;
22077
907af001
UW
22078 case DW_TAG_class_type:
22079 case DW_TAG_interface_type:
22080 case DW_TAG_structure_type:
22081 case DW_TAG_union_type:
22082 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22083 structures or unions. These were of the form "._%d" in GCC 4.1,
22084 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22085 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22086 if (attr && DW_STRING (attr)
61012eef
GB
22087 && (startswith (DW_STRING (attr), "._")
22088 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22089 return NULL;
53832f31
TT
22090
22091 /* GCC might emit a nameless typedef that has a linkage name. See
22092 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22093 if (!attr || DW_STRING (attr) == NULL)
22094 {
73b9be8b 22095 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22096 if (attr == NULL || DW_STRING (attr) == NULL)
22097 return NULL;
22098
df5c6c50
JK
22099 /* Avoid demangling DW_STRING (attr) the second time on a second
22100 call for the same DIE. */
22101 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 22102 {
43816ebc
TT
22103 gdb::unique_xmalloc_ptr<char> demangled
22104 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
22105 if (demangled == nullptr)
22106 return nullptr;
43816ebc 22107
be1e3d3e 22108 DW_STRING (attr) = objfile->intern (demangled.get ());
53832f31 22109 DW_STRING_IS_CANONICAL (attr) = 1;
53832f31 22110 }
67430cd0
TT
22111
22112 /* Strip any leading namespaces/classes, keep only the base name.
22113 DW_AT_name for named DIEs does not contain the prefixes. */
22114 const char *base = strrchr (DW_STRING (attr), ':');
22115 if (base && base > DW_STRING (attr) && base[-1] == ':')
22116 return &base[1];
22117 else
22118 return DW_STRING (attr);
53832f31 22119 }
907af001
UW
22120 break;
22121
71c25dea 22122 default:
907af001
UW
22123 break;
22124 }
22125
22126 if (!DW_STRING_IS_CANONICAL (attr))
22127 {
be1e3d3e
TT
22128 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22129 objfile);
907af001 22130 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22131 }
907af001 22132 return DW_STRING (attr);
9219021c
DC
22133}
22134
22135/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22136 is none. *EXT_CU is the CU containing DIE on input, and the CU
22137 containing the return value on output. */
9219021c
DC
22138
22139static struct die_info *
f2f0e013 22140dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22141{
22142 struct attribute *attr;
9219021c 22143
f2f0e013 22144 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22145 if (attr == NULL)
22146 return NULL;
22147
f2f0e013 22148 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22149}
22150
f9aca02d 22151static void
d97bc12b 22152dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22153{
22154 unsigned int i;
22155
d97bc12b 22156 print_spaces (indent, f);
9d8780f0 22157 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22158 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22159 sect_offset_str (die->sect_off));
d97bc12b
DE
22160
22161 if (die->parent != NULL)
22162 {
22163 print_spaces (indent, f);
9d8780f0
SM
22164 fprintf_unfiltered (f, " parent at offset: %s\n",
22165 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22166 }
22167
22168 print_spaces (indent, f);
22169 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22170 dwarf_bool_name (die->child != NULL));
c906108c 22171
d97bc12b
DE
22172 print_spaces (indent, f);
22173 fprintf_unfiltered (f, " attributes:\n");
22174
c906108c
SS
22175 for (i = 0; i < die->num_attrs; ++i)
22176 {
d97bc12b
DE
22177 print_spaces (indent, f);
22178 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22179 dwarf_attr_name (die->attrs[i].name),
22180 dwarf_form_name (die->attrs[i].form));
d97bc12b 22181
c906108c
SS
22182 switch (die->attrs[i].form)
22183 {
c906108c 22184 case DW_FORM_addr:
336d760d 22185 case DW_FORM_addrx:
3019eac3 22186 case DW_FORM_GNU_addr_index:
d97bc12b 22187 fprintf_unfiltered (f, "address: ");
5af949e3 22188 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22189 break;
22190 case DW_FORM_block2:
22191 case DW_FORM_block4:
22192 case DW_FORM_block:
22193 case DW_FORM_block1:
56eb65bd
SP
22194 fprintf_unfiltered (f, "block: size %s",
22195 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22196 break;
2dc7f7b3 22197 case DW_FORM_exprloc:
56eb65bd
SP
22198 fprintf_unfiltered (f, "expression: size %s",
22199 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22200 break;
0224619f
JK
22201 case DW_FORM_data16:
22202 fprintf_unfiltered (f, "constant of 16 bytes");
22203 break;
4568ecf9
DE
22204 case DW_FORM_ref_addr:
22205 fprintf_unfiltered (f, "ref address: ");
22206 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22207 break;
36586728
TT
22208 case DW_FORM_GNU_ref_alt:
22209 fprintf_unfiltered (f, "alt ref address: ");
22210 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22211 break;
10b3939b
DJ
22212 case DW_FORM_ref1:
22213 case DW_FORM_ref2:
22214 case DW_FORM_ref4:
4568ecf9
DE
22215 case DW_FORM_ref8:
22216 case DW_FORM_ref_udata:
d97bc12b 22217 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22218 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22219 break;
c906108c
SS
22220 case DW_FORM_data1:
22221 case DW_FORM_data2:
22222 case DW_FORM_data4:
ce5d95e1 22223 case DW_FORM_data8:
c906108c
SS
22224 case DW_FORM_udata:
22225 case DW_FORM_sdata:
43bbcdc2
PH
22226 fprintf_unfiltered (f, "constant: %s",
22227 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22228 break;
2dc7f7b3
TT
22229 case DW_FORM_sec_offset:
22230 fprintf_unfiltered (f, "section offset: %s",
22231 pulongest (DW_UNSND (&die->attrs[i])));
22232 break;
55f1336d 22233 case DW_FORM_ref_sig8:
ac9ec31b
DE
22234 fprintf_unfiltered (f, "signature: %s",
22235 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22236 break;
c906108c 22237 case DW_FORM_string:
4bdf3d34 22238 case DW_FORM_strp:
43988095 22239 case DW_FORM_line_strp:
cf532bd1 22240 case DW_FORM_strx:
3019eac3 22241 case DW_FORM_GNU_str_index:
36586728 22242 case DW_FORM_GNU_strp_alt:
8285870a 22243 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22244 DW_STRING (&die->attrs[i])
8285870a
JK
22245 ? DW_STRING (&die->attrs[i]) : "",
22246 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22247 break;
22248 case DW_FORM_flag:
22249 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22250 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22251 else
d97bc12b 22252 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22253 break;
2dc7f7b3
TT
22254 case DW_FORM_flag_present:
22255 fprintf_unfiltered (f, "flag: TRUE");
22256 break;
a8329558 22257 case DW_FORM_indirect:
0963b4bd
MS
22258 /* The reader will have reduced the indirect form to
22259 the "base form" so this form should not occur. */
5f48f8f3 22260 fprintf_unfiltered (f,
3e43a32a 22261 "unexpected attribute form: DW_FORM_indirect");
a8329558 22262 break;
663c44ac
JK
22263 case DW_FORM_implicit_const:
22264 fprintf_unfiltered (f, "constant: %s",
22265 plongest (DW_SND (&die->attrs[i])));
22266 break;
c906108c 22267 default:
d97bc12b 22268 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22269 die->attrs[i].form);
d97bc12b 22270 break;
c906108c 22271 }
d97bc12b 22272 fprintf_unfiltered (f, "\n");
c906108c
SS
22273 }
22274}
22275
f9aca02d 22276static void
d97bc12b 22277dump_die_for_error (struct die_info *die)
c906108c 22278{
d97bc12b
DE
22279 dump_die_shallow (gdb_stderr, 0, die);
22280}
22281
22282static void
22283dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22284{
22285 int indent = level * 4;
22286
22287 gdb_assert (die != NULL);
22288
22289 if (level >= max_level)
22290 return;
22291
22292 dump_die_shallow (f, indent, die);
22293
22294 if (die->child != NULL)
c906108c 22295 {
d97bc12b
DE
22296 print_spaces (indent, f);
22297 fprintf_unfiltered (f, " Children:");
22298 if (level + 1 < max_level)
22299 {
22300 fprintf_unfiltered (f, "\n");
22301 dump_die_1 (f, level + 1, max_level, die->child);
22302 }
22303 else
22304 {
3e43a32a
MS
22305 fprintf_unfiltered (f,
22306 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22307 }
22308 }
22309
22310 if (die->sibling != NULL && level > 0)
22311 {
22312 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22313 }
22314}
22315
d97bc12b
DE
22316/* This is called from the pdie macro in gdbinit.in.
22317 It's not static so gcc will keep a copy callable from gdb. */
22318
22319void
22320dump_die (struct die_info *die, int max_level)
22321{
22322 dump_die_1 (gdb_stdlog, 0, max_level, die);
22323}
22324
f9aca02d 22325static void
51545339 22326store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22327{
51545339 22328 void **slot;
c906108c 22329
9c541725
PA
22330 slot = htab_find_slot_with_hash (cu->die_hash, die,
22331 to_underlying (die->sect_off),
b64f50a1 22332 INSERT);
51545339
DJ
22333
22334 *slot = die;
c906108c
SS
22335}
22336
348e048f
DE
22337/* Follow reference or signature attribute ATTR of SRC_DIE.
22338 On entry *REF_CU is the CU of SRC_DIE.
22339 On exit *REF_CU is the CU of the result. */
22340
22341static struct die_info *
ff39bb5e 22342follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22343 struct dwarf2_cu **ref_cu)
22344{
22345 struct die_info *die;
22346
cd6c91b4 22347 if (attr->form_is_ref ())
348e048f 22348 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22349 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22350 die = follow_die_sig (src_die, attr, ref_cu);
22351 else
22352 {
22353 dump_die_for_error (src_die);
22354 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 22355 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
22356 }
22357
22358 return die;
03dd20cc
DJ
22359}
22360
5c631832 22361/* Follow reference OFFSET.
673bfd45
DE
22362 On entry *REF_CU is the CU of the source die referencing OFFSET.
22363 On exit *REF_CU is the CU of the result.
22364 Returns NULL if OFFSET is invalid. */
f504f079 22365
f9aca02d 22366static struct die_info *
9c541725 22367follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22368 struct dwarf2_cu **ref_cu)
c906108c 22369{
10b3939b 22370 struct die_info temp_die;
f2f0e013 22371 struct dwarf2_cu *target_cu, *cu = *ref_cu;
976ca316 22372 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10b3939b 22373
348e048f
DE
22374 gdb_assert (cu->per_cu != NULL);
22375
98bfdba5
PA
22376 target_cu = cu;
22377
3019eac3 22378 if (cu->per_cu->is_debug_types)
348e048f
DE
22379 {
22380 /* .debug_types CUs cannot reference anything outside their CU.
22381 If they need to, they have to reference a signatured type via
55f1336d 22382 DW_FORM_ref_sig8. */
4057dfde 22383 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22384 return NULL;
348e048f 22385 }
36586728 22386 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22387 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22388 {
22389 struct dwarf2_per_cu_data *per_cu;
9a619af0 22390
9c541725 22391 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 22392 per_objfile);
03dd20cc
DJ
22393
22394 /* If necessary, add it to the queue and load its DIEs. */
976ca316
SM
22395 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
22396 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
03dd20cc 22397
976ca316 22398 target_cu = per_objfile->get_cu (per_cu);
10b3939b 22399 }
98bfdba5
PA
22400 else if (cu->dies == NULL)
22401 {
22402 /* We're loading full DIEs during partial symbol reading. */
976ca316
SM
22403 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
22404 load_full_comp_unit (cu->per_cu, per_objfile, false, language_minimal);
98bfdba5 22405 }
c906108c 22406
f2f0e013 22407 *ref_cu = target_cu;
9c541725 22408 temp_die.sect_off = sect_off;
c24bdb02
KS
22409
22410 if (target_cu != cu)
22411 target_cu->ancestor = cu;
22412
9a3c8263 22413 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22414 &temp_die,
22415 to_underlying (sect_off));
5c631832 22416}
10b3939b 22417
5c631832
JK
22418/* Follow reference attribute ATTR of SRC_DIE.
22419 On entry *REF_CU is the CU of SRC_DIE.
22420 On exit *REF_CU is the CU of the result. */
22421
22422static struct die_info *
ff39bb5e 22423follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22424 struct dwarf2_cu **ref_cu)
22425{
0826b30a 22426 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
22427 struct dwarf2_cu *cu = *ref_cu;
22428 struct die_info *die;
22429
9c541725 22430 die = follow_die_offset (sect_off,
36586728
TT
22431 (attr->form == DW_FORM_GNU_ref_alt
22432 || cu->per_cu->is_dwz),
22433 ref_cu);
5c631832 22434 if (!die)
9d8780f0
SM
22435 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22436 "at %s [in module %s]"),
22437 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 22438 objfile_name (cu->per_objfile->objfile));
348e048f 22439
5c631832
JK
22440 return die;
22441}
22442
d4c9a4f8 22443/* See read.h. */
5c631832
JK
22444
22445struct dwarf2_locexpr_baton
9c541725 22446dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 22447 dwarf2_per_cu_data *per_cu,
976ca316 22448 dwarf2_per_objfile *per_objfile,
8b9737bf 22449 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22450 void *baton, bool resolve_abstract_p)
5c631832 22451{
5c631832
JK
22452 struct die_info *die;
22453 struct attribute *attr;
22454 struct dwarf2_locexpr_baton retval;
976ca316 22455 struct objfile *objfile = per_objfile->objfile;
8cf6f0b1 22456
976ca316 22457 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17 22458 if (cu == nullptr)
976ca316 22459 cu = load_cu (per_cu, per_objfile, false);
1b555f17
SM
22460
22461 if (cu == nullptr)
cc12ce38
DE
22462 {
22463 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22464 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22465 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22466 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22467 }
918dd910 22468
9c541725 22469 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22470 if (!die)
9d8780f0
SM
22471 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22472 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22473
22474 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22475 if (!attr && resolve_abstract_p
976ca316
SM
22476 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22477 != per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65
TV
22478 {
22479 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22480 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 22481 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 22482
3360b6e7 22483 for (const auto &cand_off
976ca316 22484 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 22485 {
3360b6e7
TV
22486 struct dwarf2_cu *cand_cu = cu;
22487 struct die_info *cand
22488 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22489 if (!cand
22490 || !cand->parent
e4a62c65
TV
22491 || cand->parent->tag != DW_TAG_subprogram)
22492 continue;
22493
22494 CORE_ADDR pc_low, pc_high;
22495 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22496 if (pc_low == ((CORE_ADDR) -1))
22497 continue;
22498 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22499 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22500 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22501 continue;
22502
22503 die = cand;
22504 attr = dwarf2_attr (die, DW_AT_location, cu);
22505 break;
22506 }
22507 }
22508
5c631832
JK
22509 if (!attr)
22510 {
e103e986
JK
22511 /* DWARF: "If there is no such attribute, then there is no effect.".
22512 DATA is ignored if SIZE is 0. */
5c631832 22513
e103e986 22514 retval.data = NULL;
5c631832
JK
22515 retval.size = 0;
22516 }
cd6c91b4 22517 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22518 {
22519 struct dwarf2_loclist_baton loclist_baton;
22520 CORE_ADDR pc = (*get_frame_pc) (baton);
22521 size_t size;
22522
22523 fill_in_loclist_baton (cu, &loclist_baton, attr);
22524
22525 retval.data = dwarf2_find_location_expression (&loclist_baton,
22526 &size, pc);
22527 retval.size = size;
22528 }
5c631832
JK
22529 else
22530 {
4fc6c0d5 22531 if (!attr->form_is_block ())
9d8780f0 22532 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22533 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22534 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22535
22536 retval.data = DW_BLOCK (attr)->data;
22537 retval.size = DW_BLOCK (attr)->size;
22538 }
976ca316 22539 retval.per_objfile = per_objfile;
5c631832 22540 retval.per_cu = cu->per_cu;
918dd910 22541
976ca316 22542 per_objfile->age_comp_units ();
918dd910 22543
5c631832 22544 return retval;
348e048f
DE
22545}
22546
d4c9a4f8 22547/* See read.h. */
8b9737bf
TT
22548
22549struct dwarf2_locexpr_baton
22550dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 22551 dwarf2_per_cu_data *per_cu,
14095eb3 22552 dwarf2_per_objfile *per_objfile,
8b9737bf
TT
22553 CORE_ADDR (*get_frame_pc) (void *baton),
22554 void *baton)
22555{
9c541725 22556 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22557
14095eb3
SM
22558 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22559 get_frame_pc, baton);
8b9737bf
TT
22560}
22561
b6807d98
TT
22562/* Write a constant of a given type as target-ordered bytes into
22563 OBSTACK. */
22564
22565static const gdb_byte *
22566write_constant_as_bytes (struct obstack *obstack,
22567 enum bfd_endian byte_order,
22568 struct type *type,
22569 ULONGEST value,
22570 LONGEST *len)
22571{
22572 gdb_byte *result;
22573
22574 *len = TYPE_LENGTH (type);
224c3ddb 22575 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22576 store_unsigned_integer (result, *len, byte_order, value);
22577
22578 return result;
22579}
22580
d4c9a4f8 22581/* See read.h. */
b6807d98
TT
22582
22583const gdb_byte *
9c541725 22584dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 22585 dwarf2_per_cu_data *per_cu,
14095eb3 22586 dwarf2_per_objfile *per_objfile,
d4c9a4f8 22587 obstack *obstack,
b6807d98
TT
22588 LONGEST *len)
22589{
b6807d98
TT
22590 struct die_info *die;
22591 struct attribute *attr;
22592 const gdb_byte *result = NULL;
22593 struct type *type;
22594 LONGEST value;
22595 enum bfd_endian byte_order;
14095eb3 22596 struct objfile *objfile = per_objfile->objfile;
b6807d98 22597
7188ed02 22598 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
22599 if (cu == nullptr)
22600 cu = load_cu (per_cu, per_objfile, false);
22601
22602 if (cu == nullptr)
cc12ce38
DE
22603 {
22604 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22605 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22606 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22607 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22608 }
b6807d98 22609
9c541725 22610 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22611 if (!die)
9d8780f0
SM
22612 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22613 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22614
22615 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22616 if (attr == NULL)
22617 return NULL;
22618
e3b94546 22619 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22620 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22621
22622 switch (attr->form)
22623 {
22624 case DW_FORM_addr:
336d760d 22625 case DW_FORM_addrx:
b6807d98
TT
22626 case DW_FORM_GNU_addr_index:
22627 {
22628 gdb_byte *tem;
22629
22630 *len = cu->header.addr_size;
224c3ddb 22631 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22632 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22633 result = tem;
22634 }
22635 break;
22636 case DW_FORM_string:
22637 case DW_FORM_strp:
cf532bd1 22638 case DW_FORM_strx:
b6807d98
TT
22639 case DW_FORM_GNU_str_index:
22640 case DW_FORM_GNU_strp_alt:
22641 /* DW_STRING is already allocated on the objfile obstack, point
22642 directly to it. */
22643 result = (const gdb_byte *) DW_STRING (attr);
22644 *len = strlen (DW_STRING (attr));
22645 break;
22646 case DW_FORM_block1:
22647 case DW_FORM_block2:
22648 case DW_FORM_block4:
22649 case DW_FORM_block:
22650 case DW_FORM_exprloc:
0224619f 22651 case DW_FORM_data16:
b6807d98
TT
22652 result = DW_BLOCK (attr)->data;
22653 *len = DW_BLOCK (attr)->size;
22654 break;
22655
22656 /* The DW_AT_const_value attributes are supposed to carry the
22657 symbol's value "represented as it would be on the target
22658 architecture." By the time we get here, it's already been
22659 converted to host endianness, so we just need to sign- or
22660 zero-extend it as appropriate. */
22661 case DW_FORM_data1:
22662 type = die_type (die, cu);
22663 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22664 if (result == NULL)
22665 result = write_constant_as_bytes (obstack, byte_order,
22666 type, value, len);
22667 break;
22668 case DW_FORM_data2:
22669 type = die_type (die, cu);
22670 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22671 if (result == NULL)
22672 result = write_constant_as_bytes (obstack, byte_order,
22673 type, value, len);
22674 break;
22675 case DW_FORM_data4:
22676 type = die_type (die, cu);
22677 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22678 if (result == NULL)
22679 result = write_constant_as_bytes (obstack, byte_order,
22680 type, value, len);
22681 break;
22682 case DW_FORM_data8:
22683 type = die_type (die, cu);
22684 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22685 if (result == NULL)
22686 result = write_constant_as_bytes (obstack, byte_order,
22687 type, value, len);
22688 break;
22689
22690 case DW_FORM_sdata:
663c44ac 22691 case DW_FORM_implicit_const:
b6807d98
TT
22692 type = die_type (die, cu);
22693 result = write_constant_as_bytes (obstack, byte_order,
22694 type, DW_SND (attr), len);
22695 break;
22696
22697 case DW_FORM_udata:
22698 type = die_type (die, cu);
22699 result = write_constant_as_bytes (obstack, byte_order,
22700 type, DW_UNSND (attr), len);
22701 break;
22702
22703 default:
b98664d3 22704 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22705 dwarf_form_name (attr->form));
22706 break;
22707 }
22708
22709 return result;
22710}
22711
d4c9a4f8 22712/* See read.h. */
7942e96e
AA
22713
22714struct type *
9c541725 22715dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
22716 dwarf2_per_cu_data *per_cu,
22717 dwarf2_per_objfile *per_objfile)
7942e96e 22718{
7942e96e
AA
22719 struct die_info *die;
22720
7188ed02 22721 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
22722 if (cu == nullptr)
22723 cu = load_cu (per_cu, per_objfile, false);
22724
22725 if (cu == nullptr)
22726 return nullptr;
7942e96e 22727
9c541725 22728 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22729 if (!die)
22730 return NULL;
22731
22732 return die_type (die, cu);
22733}
22734
8cb5117c 22735/* See read.h. */
8a9b8146
TT
22736
22737struct type *
b64f50a1 22738dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
22739 dwarf2_per_cu_data *per_cu,
22740 dwarf2_per_objfile *per_objfile)
8a9b8146 22741{
9c541725 22742 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 22743 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
22744}
22745
ac9ec31b 22746/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22747 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22748 On exit *REF_CU is the CU of the result.
22749 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22750
22751static struct die_info *
ac9ec31b
DE
22752follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22753 struct dwarf2_cu **ref_cu)
348e048f 22754{
348e048f 22755 struct die_info temp_die;
c24bdb02 22756 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f 22757 struct die_info *die;
976ca316 22758 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
120ce1b5 22759
348e048f 22760
ac9ec31b
DE
22761 /* While it might be nice to assert sig_type->type == NULL here,
22762 we can get here for DW_AT_imported_declaration where we need
22763 the DIE not the type. */
348e048f
DE
22764
22765 /* If necessary, add it to the queue and load its DIEs. */
22766
976ca316 22767 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
120ce1b5 22768 language_minimal))
976ca316 22769 read_signatured_type (sig_type, per_objfile);
348e048f 22770
976ca316 22771 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
69d751e3 22772 gdb_assert (sig_cu != NULL);
9c541725
PA
22773 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22774 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22775 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22776 to_underlying (temp_die.sect_off));
348e048f
DE
22777 if (die)
22778 {
796a7ff8
DE
22779 /* For .gdb_index version 7 keep track of included TUs.
22780 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
976ca316
SM
22781 if (per_objfile->per_bfd->index_table != NULL
22782 && per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 22783 {
ae640021 22784 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22785 }
22786
348e048f 22787 *ref_cu = sig_cu;
c24bdb02
KS
22788 if (sig_cu != cu)
22789 sig_cu->ancestor = cu;
22790
348e048f
DE
22791 return die;
22792 }
22793
ac9ec31b
DE
22794 return NULL;
22795}
22796
22797/* Follow signatured type referenced by ATTR in SRC_DIE.
22798 On entry *REF_CU is the CU of SRC_DIE.
22799 On exit *REF_CU is the CU of the result.
22800 The result is the DIE of the type.
22801 If the referenced type cannot be found an error is thrown. */
22802
22803static struct die_info *
ff39bb5e 22804follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22805 struct dwarf2_cu **ref_cu)
22806{
22807 ULONGEST signature = DW_SIGNATURE (attr);
22808 struct signatured_type *sig_type;
22809 struct die_info *die;
22810
22811 gdb_assert (attr->form == DW_FORM_ref_sig8);
22812
a2ce51a0 22813 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22814 /* sig_type will be NULL if the signatured type is missing from
22815 the debug info. */
22816 if (sig_type == NULL)
22817 {
22818 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22819 " from DIE at %s [in module %s]"),
22820 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22821 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22822 }
22823
22824 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22825 if (die == NULL)
22826 {
22827 dump_die_for_error (src_die);
22828 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22829 " from DIE at %s [in module %s]"),
22830 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22831 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22832 }
22833
22834 return die;
22835}
22836
22837/* Get the type specified by SIGNATURE referenced in DIE/CU,
22838 reading in and processing the type unit if necessary. */
22839
22840static struct type *
22841get_signatured_type (struct die_info *die, ULONGEST signature,
22842 struct dwarf2_cu *cu)
22843{
976ca316 22844 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ac9ec31b
DE
22845 struct signatured_type *sig_type;
22846 struct dwarf2_cu *type_cu;
22847 struct die_info *type_die;
22848 struct type *type;
22849
a2ce51a0 22850 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22851 /* sig_type will be NULL if the signatured type is missing from
22852 the debug info. */
22853 if (sig_type == NULL)
22854 {
b98664d3 22855 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22856 " from DIE at %s [in module %s]"),
22857 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 22858 objfile_name (per_objfile->objfile));
ac9ec31b
DE
22859 return build_error_marker_type (cu, die);
22860 }
22861
22862 /* If we already know the type we're done. */
976ca316 22863 type = per_objfile->get_type_for_signatured_type (sig_type);
e286671b
TT
22864 if (type != nullptr)
22865 return type;
ac9ec31b
DE
22866
22867 type_cu = cu;
22868 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22869 if (type_die != NULL)
22870 {
22871 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22872 is created. This is important, for example, because for c++ classes
22873 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22874 type = read_type_die (type_die, type_cu);
22875 if (type == NULL)
22876 {
b98664d3 22877 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22878 " referenced from DIE at %s [in module %s]"),
22879 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 22880 objfile_name (per_objfile->objfile));
ac9ec31b
DE
22881 type = build_error_marker_type (cu, die);
22882 }
22883 }
22884 else
22885 {
b98664d3 22886 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22887 " from DIE at %s [in module %s]"),
22888 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 22889 objfile_name (per_objfile->objfile));
ac9ec31b
DE
22890 type = build_error_marker_type (cu, die);
22891 }
e286671b 22892
976ca316 22893 per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
22894
22895 return type;
22896}
22897
22898/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22899 reading in and processing the type unit if necessary. */
22900
22901static struct type *
ff39bb5e 22902get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22903 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22904{
22905 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22906 if (attr->form_is_ref ())
ac9ec31b
DE
22907 {
22908 struct dwarf2_cu *type_cu = cu;
22909 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22910
22911 return read_type_die (type_die, type_cu);
22912 }
22913 else if (attr->form == DW_FORM_ref_sig8)
22914 {
22915 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22916 }
22917 else
22918 {
976ca316 22919 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 22920
b98664d3 22921 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22922 " at %s [in module %s]"),
22923 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
976ca316 22924 objfile_name (per_objfile->objfile));
ac9ec31b
DE
22925 return build_error_marker_type (cu, die);
22926 }
348e048f
DE
22927}
22928
e5fe5e75 22929/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22930
22931static void
ab432490
SM
22932load_full_type_unit (dwarf2_per_cu_data *per_cu,
22933 dwarf2_per_objfile *per_objfile)
348e048f 22934{
52dc124a 22935 struct signatured_type *sig_type;
348e048f 22936
f4dc4d17 22937 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22938 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22939
6721b2ec
DE
22940 /* We have the per_cu, but we need the signatured_type.
22941 Fortunately this is an easy translation. */
22942 gdb_assert (per_cu->is_debug_types);
22943 sig_type = (struct signatured_type *) per_cu;
348e048f 22944
7188ed02 22945 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 22946
ab432490 22947 read_signatured_type (sig_type, per_objfile);
348e048f 22948
7188ed02 22949 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
348e048f
DE
22950}
22951
3019eac3
DE
22952/* Read in a signatured type and build its CU and DIEs.
22953 If the type is a stub for the real type in a DWO file,
22954 read in the real type from the DWO file as well. */
dee91e82
DE
22955
22956static void
ab432490
SM
22957read_signatured_type (signatured_type *sig_type,
22958 dwarf2_per_objfile *per_objfile)
dee91e82
DE
22959{
22960 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22961
3019eac3 22962 gdb_assert (per_cu->is_debug_types);
7188ed02 22963 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 22964
2e671100 22965 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
22966
22967 if (!reader.dummy_p)
22968 {
22969 struct dwarf2_cu *cu = reader.cu;
22970 const gdb_byte *info_ptr = reader.info_ptr;
22971
22972 gdb_assert (cu->die_hash == NULL);
22973 cu->die_hash =
22974 htab_create_alloc_ex (cu->header.length / 12,
22975 die_hash,
22976 die_eq,
22977 NULL,
22978 &cu->comp_unit_obstack,
22979 hashtab_obstack_allocate,
22980 dummy_obstack_deallocate);
22981
3e225074 22982 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22983 reader.comp_unit_die->child
22984 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22985 reader.comp_unit_die);
22986 cu->dies = reader.comp_unit_die;
22987 /* comp_unit_die is not stored in die_hash, no need. */
22988
22989 /* We try not to read any attributes in this function, because
22990 not all CUs needed for references have been loaded yet, and
22991 symbol table processing isn't initialized. But we have to
22992 set the CU language, or we won't be able to build types
22993 correctly. Similarly, if we do not read the producer, we can
22994 not apply producer-specific interpretation. */
22995 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22996
22997 reader.keep ();
c0ab21c2
TT
22998 }
22999
7ee85ab1 23000 sig_type->per_cu.tu_read = 1;
c906108c
SS
23001}
23002
c906108c
SS
23003/* Decode simple location descriptions.
23004 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
23005 the location and return the value. If COMPUTED is non-null, it is
23006 set to true to indicate that decoding was successful, and false
23007 otherwise. If COMPUTED is null, then this function may emit a
23008 complaint. */
c906108c
SS
23009
23010static CORE_ADDR
7d79de9a 23011decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 23012{
5e22e966 23013 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
23014 size_t i;
23015 size_t size = blk->size;
d521ce57 23016 const gdb_byte *data = blk->data;
21ae7a4d
JK
23017 CORE_ADDR stack[64];
23018 int stacki;
23019 unsigned int bytes_read, unsnd;
23020 gdb_byte op;
c906108c 23021
7d79de9a
TT
23022 if (computed != nullptr)
23023 *computed = false;
23024
21ae7a4d
JK
23025 i = 0;
23026 stacki = 0;
23027 stack[stacki] = 0;
23028 stack[++stacki] = 0;
23029
23030 while (i < size)
23031 {
23032 op = data[i++];
23033 switch (op)
23034 {
23035 case DW_OP_lit0:
23036 case DW_OP_lit1:
23037 case DW_OP_lit2:
23038 case DW_OP_lit3:
23039 case DW_OP_lit4:
23040 case DW_OP_lit5:
23041 case DW_OP_lit6:
23042 case DW_OP_lit7:
23043 case DW_OP_lit8:
23044 case DW_OP_lit9:
23045 case DW_OP_lit10:
23046 case DW_OP_lit11:
23047 case DW_OP_lit12:
23048 case DW_OP_lit13:
23049 case DW_OP_lit14:
23050 case DW_OP_lit15:
23051 case DW_OP_lit16:
23052 case DW_OP_lit17:
23053 case DW_OP_lit18:
23054 case DW_OP_lit19:
23055 case DW_OP_lit20:
23056 case DW_OP_lit21:
23057 case DW_OP_lit22:
23058 case DW_OP_lit23:
23059 case DW_OP_lit24:
23060 case DW_OP_lit25:
23061 case DW_OP_lit26:
23062 case DW_OP_lit27:
23063 case DW_OP_lit28:
23064 case DW_OP_lit29:
23065 case DW_OP_lit30:
23066 case DW_OP_lit31:
23067 stack[++stacki] = op - DW_OP_lit0;
23068 break;
f1bea926 23069
21ae7a4d
JK
23070 case DW_OP_reg0:
23071 case DW_OP_reg1:
23072 case DW_OP_reg2:
23073 case DW_OP_reg3:
23074 case DW_OP_reg4:
23075 case DW_OP_reg5:
23076 case DW_OP_reg6:
23077 case DW_OP_reg7:
23078 case DW_OP_reg8:
23079 case DW_OP_reg9:
23080 case DW_OP_reg10:
23081 case DW_OP_reg11:
23082 case DW_OP_reg12:
23083 case DW_OP_reg13:
23084 case DW_OP_reg14:
23085 case DW_OP_reg15:
23086 case DW_OP_reg16:
23087 case DW_OP_reg17:
23088 case DW_OP_reg18:
23089 case DW_OP_reg19:
23090 case DW_OP_reg20:
23091 case DW_OP_reg21:
23092 case DW_OP_reg22:
23093 case DW_OP_reg23:
23094 case DW_OP_reg24:
23095 case DW_OP_reg25:
23096 case DW_OP_reg26:
23097 case DW_OP_reg27:
23098 case DW_OP_reg28:
23099 case DW_OP_reg29:
23100 case DW_OP_reg30:
23101 case DW_OP_reg31:
23102 stack[++stacki] = op - DW_OP_reg0;
23103 if (i < size)
7d79de9a
TT
23104 {
23105 if (computed == nullptr)
23106 dwarf2_complex_location_expr_complaint ();
23107 else
23108 return 0;
23109 }
21ae7a4d 23110 break;
c906108c 23111
21ae7a4d
JK
23112 case DW_OP_regx:
23113 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23114 i += bytes_read;
23115 stack[++stacki] = unsnd;
23116 if (i < size)
7d79de9a
TT
23117 {
23118 if (computed == nullptr)
23119 dwarf2_complex_location_expr_complaint ();
23120 else
23121 return 0;
23122 }
21ae7a4d 23123 break;
c906108c 23124
21ae7a4d 23125 case DW_OP_addr:
c8a7a66f
TT
23126 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23127 &bytes_read);
21ae7a4d
JK
23128 i += bytes_read;
23129 break;
d53d4ac5 23130
21ae7a4d
JK
23131 case DW_OP_const1u:
23132 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23133 i += 1;
23134 break;
23135
23136 case DW_OP_const1s:
23137 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23138 i += 1;
23139 break;
23140
23141 case DW_OP_const2u:
23142 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23143 i += 2;
23144 break;
23145
23146 case DW_OP_const2s:
23147 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23148 i += 2;
23149 break;
d53d4ac5 23150
21ae7a4d
JK
23151 case DW_OP_const4u:
23152 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23153 i += 4;
23154 break;
23155
23156 case DW_OP_const4s:
23157 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23158 i += 4;
23159 break;
23160
585861ea
JK
23161 case DW_OP_const8u:
23162 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23163 i += 8;
23164 break;
23165
21ae7a4d
JK
23166 case DW_OP_constu:
23167 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23168 &bytes_read);
23169 i += bytes_read;
23170 break;
23171
23172 case DW_OP_consts:
23173 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23174 i += bytes_read;
23175 break;
23176
23177 case DW_OP_dup:
23178 stack[stacki + 1] = stack[stacki];
23179 stacki++;
23180 break;
23181
23182 case DW_OP_plus:
23183 stack[stacki - 1] += stack[stacki];
23184 stacki--;
23185 break;
23186
23187 case DW_OP_plus_uconst:
23188 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23189 &bytes_read);
23190 i += bytes_read;
23191 break;
23192
23193 case DW_OP_minus:
23194 stack[stacki - 1] -= stack[stacki];
23195 stacki--;
23196 break;
23197
23198 case DW_OP_deref:
23199 /* If we're not the last op, then we definitely can't encode
23200 this using GDB's address_class enum. This is valid for partial
23201 global symbols, although the variable's address will be bogus
23202 in the psymtab. */
23203 if (i < size)
7d79de9a
TT
23204 {
23205 if (computed == nullptr)
23206 dwarf2_complex_location_expr_complaint ();
23207 else
23208 return 0;
23209 }
21ae7a4d
JK
23210 break;
23211
23212 case DW_OP_GNU_push_tls_address:
4aa4e28b 23213 case DW_OP_form_tls_address:
21ae7a4d
JK
23214 /* The top of the stack has the offset from the beginning
23215 of the thread control block at which the variable is located. */
23216 /* Nothing should follow this operator, so the top of stack would
23217 be returned. */
23218 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23219 address will be bogus in the psymtab. Make it always at least
23220 non-zero to not look as a variable garbage collected by linker
23221 which have DW_OP_addr 0. */
21ae7a4d 23222 if (i < size)
7d79de9a
TT
23223 {
23224 if (computed == nullptr)
23225 dwarf2_complex_location_expr_complaint ();
23226 else
23227 return 0;
23228 }
585861ea 23229 stack[stacki]++;
21ae7a4d
JK
23230 break;
23231
23232 case DW_OP_GNU_uninit:
7d79de9a
TT
23233 if (computed != nullptr)
23234 return 0;
21ae7a4d
JK
23235 break;
23236
336d760d 23237 case DW_OP_addrx:
3019eac3 23238 case DW_OP_GNU_addr_index:
49f6c839 23239 case DW_OP_GNU_const_index:
3019eac3
DE
23240 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23241 &bytes_read);
23242 i += bytes_read;
23243 break;
23244
21ae7a4d 23245 default:
7d79de9a
TT
23246 if (computed == nullptr)
23247 {
23248 const char *name = get_DW_OP_name (op);
21ae7a4d 23249
7d79de9a
TT
23250 if (name)
23251 complaint (_("unsupported stack op: '%s'"),
23252 name);
23253 else
23254 complaint (_("unsupported stack op: '%02x'"),
23255 op);
23256 }
21ae7a4d
JK
23257
23258 return (stack[stacki]);
d53d4ac5 23259 }
3c6e0cb3 23260
21ae7a4d
JK
23261 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23262 outside of the allocated space. Also enforce minimum>0. */
23263 if (stacki >= ARRAY_SIZE (stack) - 1)
23264 {
7d79de9a
TT
23265 if (computed == nullptr)
23266 complaint (_("location description stack overflow"));
21ae7a4d
JK
23267 return 0;
23268 }
23269
23270 if (stacki <= 0)
23271 {
7d79de9a
TT
23272 if (computed == nullptr)
23273 complaint (_("location description stack underflow"));
21ae7a4d
JK
23274 return 0;
23275 }
23276 }
7d79de9a
TT
23277
23278 if (computed != nullptr)
23279 *computed = true;
21ae7a4d 23280 return (stack[stacki]);
c906108c
SS
23281}
23282
23283/* memory allocation interface */
23284
c906108c 23285static struct dwarf_block *
7b5a2f43 23286dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23287{
8d749320 23288 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23289}
23290
c906108c 23291static struct die_info *
b60c80d6 23292dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23293{
23294 struct die_info *die;
b60c80d6
DJ
23295 size_t size = sizeof (struct die_info);
23296
23297 if (num_attrs > 1)
23298 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23299
b60c80d6 23300 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23301 memset (die, 0, sizeof (struct die_info));
23302 return (die);
23303}
2e276125
JB
23304
23305\f
a036ba48 23306
c90ec28a 23307/* Macro support. */
cf2c3c16 23308
9eac9650
TT
23309/* An overload of dwarf_decode_macros that finds the correct section
23310 and ensures it is read in before calling the other overload. */
23311
23312static void
23313dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23314 int section_is_gnu)
23315{
976ca316
SM
23316 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23317 struct objfile *objfile = per_objfile->objfile;
5a0e026f 23318 const struct line_header *lh = cu->line_header;
9eac9650
TT
23319 unsigned int offset_size = cu->header.offset_size;
23320 struct dwarf2_section_info *section;
23321 const char *section_name;
23322
23323 if (cu->dwo_unit != nullptr)
23324 {
23325 if (section_is_gnu)
23326 {
23327 section = &cu->dwo_unit->dwo_file->sections.macro;
23328 section_name = ".debug_macro.dwo";
23329 }
23330 else
23331 {
23332 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23333 section_name = ".debug_macinfo.dwo";
23334 }
23335 }
23336 else
23337 {
23338 if (section_is_gnu)
23339 {
976ca316 23340 section = &per_objfile->per_bfd->macro;
9eac9650
TT
23341 section_name = ".debug_macro";
23342 }
23343 else
23344 {
976ca316 23345 section = &per_objfile->per_bfd->macinfo;
9eac9650
TT
23346 section_name = ".debug_macinfo";
23347 }
23348 }
23349
23350 section->read (objfile);
23351 if (section->buffer == nullptr)
23352 {
23353 complaint (_("missing %s section"), section_name);
23354 return;
23355 }
23356
23357 buildsym_compunit *builder = cu->get_builder ();
23358
976ca316 23359 dwarf_decode_macros (per_objfile, builder, section, lh,
9eac9650
TT
23360 offset_size, offset, section_is_gnu);
23361}
23362
3019eac3
DE
23363/* Return the .debug_loc section to use for CU.
23364 For DWO files use .debug_loc.dwo. */
23365
23366static struct dwarf2_section_info *
23367cu_debug_loc_section (struct dwarf2_cu *cu)
23368{
976ca316 23369 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 23370
3019eac3 23371 if (cu->dwo_unit)
43988095
JK
23372 {
23373 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23374
43988095
JK
23375 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23376 }
976ca316
SM
23377 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
23378 : &per_objfile->per_bfd->loc);
3019eac3
DE
23379}
23380
8cf6f0b1
TT
23381/* A helper function that fills in a dwarf2_loclist_baton. */
23382
23383static void
23384fill_in_loclist_baton (struct dwarf2_cu *cu,
23385 struct dwarf2_loclist_baton *baton,
ff39bb5e 23386 const struct attribute *attr)
8cf6f0b1 23387{
976ca316 23388 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3
DE
23389 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23390
976ca316 23391 section->read (per_objfile->objfile);
8cf6f0b1 23392
976ca316 23393 baton->per_objfile = per_objfile;
8cf6f0b1
TT
23394 baton->per_cu = cu->per_cu;
23395 gdb_assert (baton->per_cu);
23396 /* We don't know how long the location list is, but make sure we
23397 don't run off the edge of the section. */
3019eac3
DE
23398 baton->size = section->size - DW_UNSND (attr);
23399 baton->data = section->buffer + DW_UNSND (attr);
2b24b6e4
TT
23400 if (cu->base_address.has_value ())
23401 baton->base_address = *cu->base_address;
23402 else
23403 baton->base_address = 0;
f664829e 23404 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23405}
23406
4c2df51b 23407static void
ff39bb5e 23408dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23409 struct dwarf2_cu *cu, int is_block)
4c2df51b 23410{
976ca316
SM
23411 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23412 struct objfile *objfile = per_objfile->objfile;
3019eac3 23413 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23414
cd6c91b4 23415 if (attr->form_is_section_offset ()
3019eac3 23416 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23417 the section. If so, fall through to the complaint in the
23418 other branch. */
2c7d5afc 23419 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 23420 {
0d53c4c4 23421 struct dwarf2_loclist_baton *baton;
4c2df51b 23422
8d749320 23423 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23424
8cf6f0b1 23425 fill_in_loclist_baton (cu, baton, attr);
be391dca 23426
2b24b6e4 23427 if (!cu->base_address.has_value ())
b98664d3 23428 complaint (_("Location list used without "
3e43a32a 23429 "specifying the CU base address."));
4c2df51b 23430
f1e6e072
TT
23431 SYMBOL_ACLASS_INDEX (sym) = (is_block
23432 ? dwarf2_loclist_block_index
23433 : dwarf2_loclist_index);
0d53c4c4
DJ
23434 SYMBOL_LOCATION_BATON (sym) = baton;
23435 }
23436 else
23437 {
23438 struct dwarf2_locexpr_baton *baton;
23439
8d749320 23440 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
976ca316 23441 baton->per_objfile = per_objfile;
ae0d2f24
UW
23442 baton->per_cu = cu->per_cu;
23443 gdb_assert (baton->per_cu);
0d53c4c4 23444
4fc6c0d5 23445 if (attr->form_is_block ())
0d53c4c4
DJ
23446 {
23447 /* Note that we're just copying the block's data pointer
23448 here, not the actual data. We're still pointing into the
6502dd73
DJ
23449 info_buffer for SYM's objfile; right now we never release
23450 that buffer, but when we do clean up properly this may
23451 need to change. */
0d53c4c4
DJ
23452 baton->size = DW_BLOCK (attr)->size;
23453 baton->data = DW_BLOCK (attr)->data;
23454 }
23455 else
23456 {
23457 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 23458 sym->natural_name ());
0d53c4c4 23459 baton->size = 0;
0d53c4c4 23460 }
6e70227d 23461
f1e6e072
TT
23462 SYMBOL_ACLASS_INDEX (sym) = (is_block
23463 ? dwarf2_locexpr_block_index
23464 : dwarf2_locexpr_index);
0d53c4c4
DJ
23465 SYMBOL_LOCATION_BATON (sym) = baton;
23466 }
4c2df51b 23467}
6502dd73 23468
2e6a9f79 23469/* See read.h. */
96408a79 23470
2e6a9f79
SM
23471const comp_unit_head *
23472dwarf2_per_cu_data::get_header () const
96408a79 23473{
2e6a9f79
SM
23474 if (!m_header_read_in)
23475 {
23476 const gdb_byte *info_ptr
23477 = this->section->buffer + to_underlying (this->sect_off);
96408a79 23478
2e6a9f79 23479 memset (&m_header, 0, sizeof (m_header));
96408a79 23480
2e6a9f79
SM
23481 read_comp_unit_head (&m_header, info_ptr, this->section,
23482 rcuh_kind::COMPILE);
23483 }
96408a79 23484
2e6a9f79 23485 return &m_header;
96408a79
SA
23486}
23487
09ba997f 23488/* See read.h. */
ae0d2f24 23489
98714339 23490int
09ba997f 23491dwarf2_per_cu_data::addr_size () const
ae0d2f24 23492{
2e6a9f79 23493 return this->get_header ()->addr_size;
ae0d2f24
UW
23494}
23495
09ba997f 23496/* See read.h. */
9eae7c52
TT
23497
23498int
09ba997f 23499dwarf2_per_cu_data::offset_size () const
9eae7c52 23500{
2e6a9f79 23501 return this->get_header ()->offset_size;
96408a79
SA
23502}
23503
09ba997f 23504/* See read.h. */
96408a79
SA
23505
23506int
09ba997f 23507dwarf2_per_cu_data::ref_addr_size () const
96408a79 23508{
2e6a9f79 23509 const comp_unit_head *header = this->get_header ();
96408a79 23510
2e6a9f79
SM
23511 if (header->version == 2)
23512 return header->addr_size;
96408a79 23513 else
2e6a9f79 23514 return header->offset_size;
181cebd4
JK
23515}
23516
09ba997f 23517/* See read.h. */
9aa1f1e3 23518
09ba997f 23519struct type *
293e7e51 23520dwarf2_cu::addr_type () const
9a49df9d 23521{
293e7e51 23522 struct objfile *objfile = this->per_objfile->objfile;
9a49df9d
AB
23523 struct type *void_type = objfile_type (objfile)->builtin_void;
23524 struct type *addr_type = lookup_pointer_type (void_type);
293e7e51 23525 int addr_size = this->per_cu->addr_size ();
9a49df9d
AB
23526
23527 if (TYPE_LENGTH (addr_type) == addr_size)
23528 return addr_type;
23529
09ba997f 23530 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
23531 return addr_type;
23532}
23533
22b6cd70
TT
23534/* A helper function for dwarf2_find_containing_comp_unit that returns
23535 the index of the result, and that searches a vector. It will
23536 return a result even if the offset in question does not actually
23537 occur in any CU. This is separate so that it can be unit
23538 tested. */
ae038cb0 23539
22b6cd70
TT
23540static int
23541dwarf2_find_containing_comp_unit
23542 (sect_offset sect_off,
23543 unsigned int offset_in_dwz,
23544 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 23545{
ae038cb0
DJ
23546 int low, high;
23547
ae038cb0 23548 low = 0;
22b6cd70 23549 high = all_comp_units.size () - 1;
ae038cb0
DJ
23550 while (high > low)
23551 {
36586728 23552 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 23553 int mid = low + (high - low) / 2;
9a619af0 23554
22b6cd70 23555 mid_cu = all_comp_units[mid];
36586728 23556 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 23557 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 23558 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
23559 high = mid;
23560 else
23561 low = mid + 1;
23562 }
23563 gdb_assert (low == high);
22b6cd70
TT
23564 return low;
23565}
23566
23567/* Locate the .debug_info compilation unit from CU's objfile which contains
23568 the DIE at OFFSET. Raises an error on failure. */
23569
23570static struct dwarf2_per_cu_data *
23571dwarf2_find_containing_comp_unit (sect_offset sect_off,
23572 unsigned int offset_in_dwz,
976ca316 23573 dwarf2_per_objfile *per_objfile)
22b6cd70 23574{
976ca316
SM
23575 int low = dwarf2_find_containing_comp_unit
23576 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
23577 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
22b6cd70 23578
45b8ae0c 23579 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 23580 {
36586728 23581 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 23582 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
23583 "offset %s [in module %s]"),
23584 sect_offset_str (sect_off),
976ca316 23585 bfd_get_filename (per_objfile->objfile->obfd));
10b3939b 23586
976ca316 23587 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 23588 <= sect_off);
976ca316 23589 return per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
23590 }
23591 else
23592 {
976ca316 23593 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 23594 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 23595 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 23596 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
23597 return this_cu;
23598 }
23599}
23600
22b6cd70
TT
23601#if GDB_SELF_TEST
23602
23603namespace selftests {
23604namespace find_containing_comp_unit {
23605
23606static void
23607run_test ()
23608{
23609 struct dwarf2_per_cu_data one {};
23610 struct dwarf2_per_cu_data two {};
23611 struct dwarf2_per_cu_data three {};
23612 struct dwarf2_per_cu_data four {};
23613
23614 one.length = 5;
23615 two.sect_off = sect_offset (one.length);
23616 two.length = 7;
23617
23618 three.length = 5;
23619 three.is_dwz = 1;
23620 four.sect_off = sect_offset (three.length);
23621 four.length = 7;
23622 four.is_dwz = 1;
23623
23624 std::vector<dwarf2_per_cu_data *> units;
23625 units.push_back (&one);
23626 units.push_back (&two);
23627 units.push_back (&three);
23628 units.push_back (&four);
23629
23630 int result;
23631
23632 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23633 SELF_CHECK (units[result] == &one);
23634 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23635 SELF_CHECK (units[result] == &one);
23636 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23637 SELF_CHECK (units[result] == &two);
23638
23639 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23640 SELF_CHECK (units[result] == &three);
23641 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23642 SELF_CHECK (units[result] == &three);
23643 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23644 SELF_CHECK (units[result] == &four);
23645}
23646
23647}
23648}
23649
23650#endif /* GDB_SELF_TEST */
23651
9e021579 23652/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 23653
9e021579
SM
23654dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23655 dwarf2_per_objfile *per_objfile)
23656 : per_cu (per_cu),
23657 per_objfile (per_objfile),
9068261f
AB
23658 mark (false),
23659 has_loclist (false),
23660 checked_producer (false),
23661 producer_is_gxx_lt_4_6 (false),
23662 producer_is_gcc_lt_4_3 (false),
eb77c9df 23663 producer_is_icc (false),
9068261f 23664 producer_is_icc_lt_14 (false),
c258c396 23665 producer_is_codewarrior (false),
9068261f 23666 processing_has_namespace_info (false)
93311388 23667{
9816fde3
JK
23668}
23669
23670/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23671
23672static void
95554aad
TT
23673prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23674 enum language pretend_language)
9816fde3
JK
23675{
23676 struct attribute *attr;
23677
23678 /* Set the language we're debugging. */
23679 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 23680 if (attr != nullptr)
9816fde3
JK
23681 set_cu_language (DW_UNSND (attr), cu);
23682 else
9cded63f 23683 {
95554aad 23684 cu->language = pretend_language;
9cded63f
TT
23685 cu->language_defn = language_def (cu->language);
23686 }
dee91e82 23687
7d45c7c3 23688 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
23689}
23690
7188ed02 23691/* See read.h. */
ae038cb0 23692
7188ed02
SM
23693dwarf2_cu *
23694dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 23695{
7188ed02
SM
23696 auto it = m_dwarf2_cus.find (per_cu);
23697 if (it == m_dwarf2_cus.end ())
23698 return nullptr;
ae038cb0 23699
7188ed02
SM
23700 return it->second;
23701}
23702
23703/* See read.h. */
23704
23705void
23706dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
23707{
23708 gdb_assert (this->get_cu (per_cu) == nullptr);
23709
23710 m_dwarf2_cus[per_cu] = cu;
23711}
23712
23713/* See read.h. */
23714
23715void
23716dwarf2_per_objfile::age_comp_units ()
23717{
23718 /* Start by clearing all marks. */
23719 for (auto pair : m_dwarf2_cus)
23720 pair.second->mark = false;
23721
23722 /* Traverse all CUs, mark them and their dependencies if used recently
23723 enough. */
23724 for (auto pair : m_dwarf2_cus)
ae038cb0 23725 {
7188ed02
SM
23726 dwarf2_cu *cu = pair.second;
23727
23728 cu->last_used++;
23729 if (cu->last_used <= dwarf_max_cache_age)
23730 dwarf2_mark (cu);
ae038cb0
DJ
23731 }
23732
7188ed02
SM
23733 /* Delete all CUs still not marked. */
23734 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
ae038cb0 23735 {
7188ed02 23736 dwarf2_cu *cu = it->second;
ae038cb0 23737
7188ed02 23738 if (!cu->mark)
ae038cb0 23739 {
7188ed02
SM
23740 delete cu;
23741 it = m_dwarf2_cus.erase (it);
ae038cb0
DJ
23742 }
23743 else
7188ed02 23744 it++;
ae038cb0
DJ
23745 }
23746}
23747
7188ed02 23748/* See read.h. */
ae038cb0 23749
7188ed02
SM
23750void
23751dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 23752{
7188ed02
SM
23753 auto it = m_dwarf2_cus.find (per_cu);
23754 if (it == m_dwarf2_cus.end ())
23755 return;
ae038cb0 23756
7188ed02 23757 delete it->second;
ae038cb0 23758
7188ed02
SM
23759 m_dwarf2_cus.erase (it);
23760}
ae038cb0 23761
7188ed02
SM
23762dwarf2_per_objfile::~dwarf2_per_objfile ()
23763{
23764 remove_all_cus ();
ae038cb0
DJ
23765}
23766
dee91e82
DE
23767/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23768 We store these in a hash table separate from the DIEs, and preserve them
23769 when the DIEs are flushed out of cache.
23770
23771 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23772 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23773 or the type may come from a DWO file. Furthermore, while it's more logical
23774 to use per_cu->section+offset, with Fission the section with the data is in
23775 the DWO file but we don't know that section at the point we need it.
23776 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23777 because we can enter the lookup routine, get_die_type_at_offset, from
23778 outside this file, and thus won't necessarily have PER_CU->cu.
23779 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23780
dee91e82 23781struct dwarf2_per_cu_offset_and_type
1c379e20 23782{
dee91e82 23783 const struct dwarf2_per_cu_data *per_cu;
9c541725 23784 sect_offset sect_off;
1c379e20
DJ
23785 struct type *type;
23786};
23787
dee91e82 23788/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23789
23790static hashval_t
dee91e82 23791per_cu_offset_and_type_hash (const void *item)
1c379e20 23792{
9a3c8263
SM
23793 const struct dwarf2_per_cu_offset_and_type *ofs
23794 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23795
9c541725 23796 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23797}
23798
dee91e82 23799/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23800
23801static int
dee91e82 23802per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23803{
9a3c8263
SM
23804 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23805 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23806 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23807 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23808
dee91e82 23809 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23810 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23811}
23812
23813/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23814 table if necessary. For convenience, return TYPE.
23815
23816 The DIEs reading must have careful ordering to:
85102364 23817 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
23818 reading current DIE.
23819 * Not trying to dereference contents of still incompletely read in types
23820 while reading in other DIEs.
23821 * Enable referencing still incompletely read in types just by a pointer to
23822 the type without accessing its fields.
23823
23824 Therefore caller should follow these rules:
23825 * Try to fetch any prerequisite types we may need to build this DIE type
23826 before building the type and calling set_die_type.
e71ec853 23827 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23828 possible before fetching more types to complete the current type.
23829 * Make the type as complete as possible before fetching more types. */
1c379e20 23830
f792889a 23831static struct type *
1c379e20
DJ
23832set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23833{
976ca316 23834 dwarf2_per_objfile *per_objfile = cu->per_objfile;
dee91e82 23835 struct dwarf2_per_cu_offset_and_type **slot, ofs;
976ca316 23836 struct objfile *objfile = per_objfile->objfile;
3cdcd0ce
JB
23837 struct attribute *attr;
23838 struct dynamic_prop prop;
1c379e20 23839
b4ba55a1
JB
23840 /* For Ada types, make sure that the gnat-specific data is always
23841 initialized (if not already set). There are a few types where
23842 we should not be doing so, because the type-specific area is
23843 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23844 where the type-specific area is used to store the floatformat).
23845 But this is not a problem, because the gnat-specific information
23846 is actually not needed for these types. */
23847 if (need_gnat_info (cu)
78134374
SM
23848 && type->code () != TYPE_CODE_FUNC
23849 && type->code () != TYPE_CODE_FLT
23850 && type->code () != TYPE_CODE_METHODPTR
23851 && type->code () != TYPE_CODE_MEMBERPTR
23852 && type->code () != TYPE_CODE_METHOD
b4ba55a1
JB
23853 && !HAVE_GNAT_AUX_INFO (type))
23854 INIT_GNAT_SPECIFIC (type);
23855
3f2f83dd
KB
23856 /* Read DW_AT_allocated and set in type. */
23857 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 23858 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23859 {
293e7e51 23860 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 23861 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23862 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd
KB
23863 }
23864 else if (attr != NULL)
23865 {
b98664d3 23866 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 23867 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23868 sect_offset_str (die->sect_off));
3f2f83dd
KB
23869 }
23870
23871 /* Read DW_AT_associated and set in type. */
23872 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 23873 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23874 {
293e7e51 23875 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 23876 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23877 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd
KB
23878 }
23879 else if (attr != NULL)
23880 {
b98664d3 23881 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 23882 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23883 sect_offset_str (die->sect_off));
3f2f83dd
KB
23884 }
23885
3cdcd0ce
JB
23886 /* Read DW_AT_data_location and set in type. */
23887 attr = dwarf2_attr (die, DW_AT_data_location, cu);
293e7e51 23888 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 23889 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
3cdcd0ce 23890
976ca316
SM
23891 if (per_objfile->die_type_hash == NULL)
23892 per_objfile->die_type_hash
0335378b
TT
23893 = htab_up (htab_create_alloc (127,
23894 per_cu_offset_and_type_hash,
23895 per_cu_offset_and_type_eq,
23896 NULL, xcalloc, xfree));
1c379e20 23897
dee91e82 23898 ofs.per_cu = cu->per_cu;
9c541725 23899 ofs.sect_off = die->sect_off;
1c379e20 23900 ofs.type = type;
dee91e82 23901 slot = (struct dwarf2_per_cu_offset_and_type **)
976ca316 23902 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 23903 if (*slot)
b98664d3 23904 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 23905 sect_offset_str (die->sect_off));
8d749320
SM
23906 *slot = XOBNEW (&objfile->objfile_obstack,
23907 struct dwarf2_per_cu_offset_and_type);
1c379e20 23908 **slot = ofs;
f792889a 23909 return type;
1c379e20
DJ
23910}
23911
9c541725 23912/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23913 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23914
23915static struct type *
9c541725 23916get_die_type_at_offset (sect_offset sect_off,
aa66c379 23917 dwarf2_per_cu_data *per_cu,
976ca316 23918 dwarf2_per_objfile *per_objfile)
1c379e20 23919{
dee91e82 23920 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 23921
976ca316 23922 if (per_objfile->die_type_hash == NULL)
f792889a 23923 return NULL;
1c379e20 23924
dee91e82 23925 ofs.per_cu = per_cu;
9c541725 23926 ofs.sect_off = sect_off;
9a3c8263 23927 slot = ((struct dwarf2_per_cu_offset_and_type *)
976ca316 23928 htab_find (per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
23929 if (slot)
23930 return slot->type;
23931 else
23932 return NULL;
23933}
23934
02142a6c 23935/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23936 or return NULL if DIE does not have a saved type. */
23937
23938static struct type *
23939get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23940{
aa66c379 23941 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
23942}
23943
10b3939b
DJ
23944/* Add a dependence relationship from CU to REF_PER_CU. */
23945
23946static void
23947dwarf2_add_dependence (struct dwarf2_cu *cu,
23948 struct dwarf2_per_cu_data *ref_per_cu)
23949{
23950 void **slot;
23951
23952 if (cu->dependencies == NULL)
23953 cu->dependencies
23954 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23955 NULL, &cu->comp_unit_obstack,
23956 hashtab_obstack_allocate,
23957 dummy_obstack_deallocate);
23958
23959 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23960 if (*slot == NULL)
23961 *slot = ref_per_cu;
23962}
1c379e20 23963
f504f079
DE
23964/* Subroutine of dwarf2_mark to pass to htab_traverse.
23965 Set the mark field in every compilation unit in the
7188ed02
SM
23966 cache that we must keep because we are keeping CU.
23967
23968 DATA is the dwarf2_per_objfile object in which to look up CUs. */
ae038cb0 23969
10b3939b
DJ
23970static int
23971dwarf2_mark_helper (void **slot, void *data)
23972{
7188ed02
SM
23973 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
23974 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
23975 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
d07ed419
JK
23976
23977 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23978 reading of the chain. As such dependencies remain valid it is not much
23979 useful to track and undo them during QUIT cleanups. */
7188ed02 23980 if (cu == nullptr)
d07ed419
JK
23981 return 1;
23982
7188ed02 23983 if (cu->mark)
10b3939b 23984 return 1;
10b3939b 23985
7188ed02
SM
23986 cu->mark = true;
23987
23988 if (cu->dependencies != nullptr)
23989 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
10b3939b
DJ
23990
23991 return 1;
23992}
23993
f504f079
DE
23994/* Set the mark field in CU and in every other compilation unit in the
23995 cache that we must keep because we are keeping CU. */
23996
ae038cb0
DJ
23997static void
23998dwarf2_mark (struct dwarf2_cu *cu)
23999{
24000 if (cu->mark)
24001 return;
7188ed02 24002
9068261f 24003 cu->mark = true;
ae038cb0 24004
7188ed02
SM
24005 if (cu->dependencies != nullptr)
24006 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
72bf9492
DJ
24007}
24008
72bf9492
DJ
24009/* Trivial hash function for partial_die_info: the hash value of a DIE
24010 is its offset in .debug_info for this objfile. */
24011
24012static hashval_t
24013partial_die_hash (const void *item)
24014{
9a3c8263
SM
24015 const struct partial_die_info *part_die
24016 = (const struct partial_die_info *) item;
9a619af0 24017
9c541725 24018 return to_underlying (part_die->sect_off);
72bf9492
DJ
24019}
24020
24021/* Trivial comparison function for partial_die_info structures: two DIEs
24022 are equal if they have the same offset. */
24023
24024static int
24025partial_die_eq (const void *item_lhs, const void *item_rhs)
24026{
9a3c8263
SM
24027 const struct partial_die_info *part_die_lhs
24028 = (const struct partial_die_info *) item_lhs;
24029 const struct partial_die_info *part_die_rhs
24030 = (const struct partial_die_info *) item_rhs;
9a619af0 24031
9c541725 24032 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
24033}
24034
3c3bb058
AB
24035struct cmd_list_element *set_dwarf_cmdlist;
24036struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 24037
9291a0cd 24038static void
cd4fb1b2
SM
24039show_check_physname (struct ui_file *file, int from_tty,
24040 struct cmd_list_element *c, const char *value)
9291a0cd 24041{
cd4fb1b2
SM
24042 fprintf_filtered (file,
24043 _("Whether to check \"physname\" is %s.\n"),
24044 value);
9291a0cd
TT
24045}
24046
6c265988 24047void _initialize_dwarf2_read ();
cd4fb1b2 24048void
6c265988 24049_initialize_dwarf2_read ()
9291a0cd 24050{
0743fc83 24051 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 24052Set DWARF specific variables.\n\
590042fc 24053Configure DWARF variables such as the cache size."),
0743fc83
TT
24054 &set_dwarf_cmdlist, "maintenance set dwarf ",
24055 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 24056
0743fc83 24057 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
24058Show DWARF specific variables.\n\
24059Show DWARF variables such as the cache size."),
0743fc83
TT
24060 &show_dwarf_cmdlist, "maintenance show dwarf ",
24061 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 24062
cd4fb1b2
SM
24063 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24064 &dwarf_max_cache_age, _("\
24065Set the upper bound on the age of cached DWARF compilation units."), _("\
24066Show the upper bound on the age of cached DWARF compilation units."), _("\
24067A higher limit means that cached compilation units will be stored\n\
24068in memory longer, and more total memory will be used. Zero disables\n\
24069caching, which can slow down startup."),
24070 NULL,
24071 show_dwarf_max_cache_age,
24072 &set_dwarf_cmdlist,
24073 &show_dwarf_cmdlist);
156942c7 24074
cd4fb1b2
SM
24075 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24076Set debugging of the DWARF reader."), _("\
24077Show debugging of the DWARF reader."), _("\
24078When enabled (non-zero), debugging messages are printed during DWARF\n\
24079reading and symtab expansion. A value of 1 (one) provides basic\n\
24080information. A value greater than 1 provides more verbose information."),
24081 NULL,
24082 NULL,
24083 &setdebuglist, &showdebuglist);
9291a0cd 24084
cd4fb1b2
SM
24085 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24086Set debugging of the DWARF DIE reader."), _("\
24087Show debugging of the DWARF DIE reader."), _("\
24088When enabled (non-zero), DIEs are dumped after they are read in.\n\
24089The value is the maximum depth to print."),
24090 NULL,
24091 NULL,
24092 &setdebuglist, &showdebuglist);
9291a0cd 24093
cd4fb1b2
SM
24094 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24095Set debugging of the dwarf line reader."), _("\
24096Show debugging of the dwarf line reader."), _("\
24097When enabled (non-zero), line number entries are dumped as they are read in.\n\
24098A value of 1 (one) provides basic information.\n\
24099A value greater than 1 provides more verbose information."),
24100 NULL,
24101 NULL,
24102 &setdebuglist, &showdebuglist);
437afbb8 24103
cd4fb1b2
SM
24104 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24105Set cross-checking of \"physname\" code against demangler."), _("\
24106Show cross-checking of \"physname\" code against demangler."), _("\
24107When enabled, GDB's internal \"physname\" code is checked against\n\
24108the demangler."),
24109 NULL, show_check_physname,
24110 &setdebuglist, &showdebuglist);
900e11f9 24111
e615022a
DE
24112 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24113 no_class, &use_deprecated_index_sections, _("\
24114Set whether to use deprecated gdb_index sections."), _("\
24115Show whether to use deprecated gdb_index sections."), _("\
24116When enabled, deprecated .gdb_index sections are used anyway.\n\
24117Normally they are ignored either because of a missing feature or\n\
24118performance issue.\n\
24119Warning: This option must be enabled before gdb reads the file."),
24120 NULL,
24121 NULL,
24122 &setlist, &showlist);
24123
f1e6e072
TT
24124 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24125 &dwarf2_locexpr_funcs);
24126 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24127 &dwarf2_loclist_funcs);
24128
24129 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24130 &dwarf2_block_frame_base_locexpr_funcs);
24131 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24132 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24133
24134#if GDB_SELF_TEST
24135 selftests::register_test ("dw2_expand_symtabs_matching",
24136 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
24137 selftests::register_test ("dwarf2_find_containing_comp_unit",
24138 selftests::find_containing_comp_unit::run_test);
c62446b1 24139#endif
6502dd73 24140}
This page took 5.358805 seconds and 4 git commands to generate.